Beispiel #1
0
    def random_page(self,
                    title=None,
                    content=None,
                    creation_date=None,
                    tags=None):
        '''Generate random page, write it and return the corresponding \
object.'''
        if title == None:
            title = util.random_title()
        if content == None:
            content = util.random_md_page()
        if creation_date == None:
            creation_date = util.random_date()
        if tags == None:
            tags = []
        # yes, we pass self as a param. It's a ref to this site, that
        # is needed by the page
        p = s2page.Page(self, title)
        #here, set date and tags??
        # date = util.random_date()
        p.content = content
        p.creation_date = creation_date
        p.tags = tags
        p.write()
        return p
Beispiel #2
0
    def _pages_to_generate(self):
        '''Return list of slugs that correspond to pages to generate.'''
        # right now it gets all the files. In theory, It should only
        # get what's changed... but the program is not doing that yet.

        all_pages = self.get_page_names()
        # keep only those whose status is published
        ptg = []
        for slug in all_pages:
            p = s2page.Page(self, slug, isslug=True)
            if p.published:
                ptg.append({
                    'slug': p.slug,
                    'title': p.title,
                    'date': p.creation_date
                })

        # sort the ptg array in reverse chronological order of its entries.
        sptg = sorted(ptg, key=lambda x: x['date'], reverse=True)
        res = [pinfo['slug'] for pinfo in sptg]
        return res
Beispiel #3
0
 def rename_page(self, old_slug, new_title):
     '''Load the page corresponding to the slug, and rename it.'''
     #load page
     p = s2page.Page(self, old_slug, isslug=True)
     p.rename(new_title)
Beispiel #4
0
 def add_page(self, page_title):
     '''Add a page to the site.'''
     p = s2page.Page(self, page_title)
     return p
Beispiel #5
0
    def generate(self):
        '''Generate the whole static site.

        Iterates through all existing s2 pages, rendering and writing
        them (and copying all common files along). 
        It also generates the toc, a sitemap, and the atom feed
        etc. (in the future it should handle tags and categories)

        '''
        if self._dirs['base'] == None or not self._tree_ready:
            #there's NO base here or up the chain
            raise ValueError  #cannot generate!

        # wipe www dir & recreate
        self._wipe_www_dir()  #copy common files
        #shutil.copytree(self.dirs['common'],
        #                os.path.join(self.dirs['www'],"common"))
        slist = glob.glob(os.path.join(self.dirs['common'], "*"))
        for fo in slist:
            rfn = os.path.split(fo)[1]
            if os.path.isdir(fo):
                shutil.copytree(fo, os.path.join(self.dirs['www'], rfn))
            else:
                shutil.copy(fo, self.dirs['www'])

        # init atom file
        title = self.site_config['site_title']
        if title == '':
            title = "<No title>"
        feed = AtomFeed(title=title,
                        subtitle=self.site_config['site_subtitle'],
                        feed_url=os.path.join(self.site_config['site_url'],
                                              "atom.xml"),
                        url=self.site_config['site_url'],
                        author=self.site_config['default_author'])

        themes_to_copy = []  # full paths!
        generated_page_info = []
        for slug in self._pages_to_generate(
        ):  #this list of pages is in reverse chrono order
            p = s2page.Page(self, slug, isslug=True)
            generated_page_info.append({
                'slug': p.slug,
                'title': p.title,
                'date': p.creation_date,
                'in_toc': p.in_toc
            })
            t = p.theme_path
            if not t in themes_to_copy:
                themes_to_copy.append(t)
            # wipe destination.
            self._wipe_www_page(slug)
            pg_content = p.generate()  #generate page
            # add atom entry
            try:
                cdd = datetime.strptime(
                    p.creation_date,
                    '%Y-%m-%d')  # feed.add needs the dat in datetime format
            except:
                print "Wrong date format in page '%s'. It should be YYYY-MM-DD." % p.slug
                print "Site Generation stopped!!  correct the date and generate again."
                self._wipe_www_dir()
                sys.exit()
            feed.add(title=p.title,
                     content=pg_content,
                     content_type="html",
                     author=p.author,
                     url=os.path.join(self.site_config['site_url'],
                                      "atom.xml"),
                     updated=cdd)

        # copy themes
        wthemesdir = os.path.join(self.dirs['www'], "themes")
        os.mkdir(wthemesdir)
        for d in themes_to_copy:
            dname = os.path.split(d)[1]
            destpath = os.path.join(wthemesdir, dname)
            shutil.copytree(d, destpath)
            # delete tpl files
            ttr = glob.glob(os.path.join(destpath, "*tpl"))
            for f in ttr:
                os.remove(f)

        # write atom file
        atomfile = codecs.open(os.path.join(self.dirs['www'], "atom.xml"),
                               "w",
                               encoding="utf-8",
                               errors="xmlcharrefreplace")
        atomfile.write(feed.to_string())
        atomfile.close()

        # create front page/s
        #print "generated_page_info for gf ",generated_page_info
        ff = self.site_config['fixed_frontpage']
        if ff != None and ff != '':
            self._set_fixed_frontpage(ff)
        else:
            self.generate_front(generated_page_info)
        self._generate_site_map(generated_page_info)