Source

muyhomepage2 / muyhomepage2 / app / build.py

Full commit
John Mulligan 3f49883 














John Mulligan b482b52 
John Mulligan 3f49883 

John Mulligan b482b52 


John Mulligan 3f49883 

John Mulligan b482b52 

John Mulligan 3f49883 




John Mulligan 79050d8 
John Mulligan daea4c9 
John Mulligan 3f49883 
John Mulligan 79050d8 
John Mulligan 3f49883 
John Mulligan 79050d8 
John Mulligan 3f49883 





John Mulligan 31a25f5 
John Mulligan 3f49883 



John Mulligan 79050d8 
John Mulligan daea4c9 
John Mulligan 3f49883 











John Mulligan 79050d8 
John Mulligan 3f49883 
John Mulligan daea4c9 

John Mulligan b482b52 

John Mulligan 3f49883 








John Mulligan 31a25f5 
John Mulligan 3f49883 


import jinja2

from muyhomepage2 import site
from muyhomepage2 import page
from muyhomepage2 import util
from muyhomepage2.pagehandlers.handler import handle_action
from muyhomepage2.app import configuration

ENCODING = 'utf-8'

class TemplateConfig(configuration.Config):
    def __init__(self):
        configuration.Config.__init__(self)
        self._jenv = None

    def templateloader(self):
        p = '/home/john/projects/newgenerator/skins/default'
        loader = jinja2.FileSystemLoader(p)
        return loader

    def templateenv(self):
        if not self._jenv:
            self._jenv = jinja2.Environment(loader=self.templateloader())
        return self._jenv


def build(config):
    website = site.Site()
    website.pages = site.read_site_pages(config.page_cache())
    site.update_site_tags(website, config.tag_cache())
    rebuild = set()
    cached = [p.filename for p in website.pages]
    for pagefile in page.walkpages(config.root()):
        if pagefile not in cached:
            npage = page.parse(pagefile)
            rebuild.add(npage)
            website.pages.add(npage)
        else:
            cpage = website.getpagefile(pagefile)
            if cpage.contentcode != util.contentcode(pagefile):
                rebuild.add(page.parse(pagefile))
    for spage in website.pages:
        if spage.action in config.always_rebuild():
            rebuild.add(page.parse(spage.filename))
    compile_pages(config, website, rebuild)
    site.write_site_pages(website, config.page_cache())
    site.write_site_tags(website, config.tag_cache())


def compile_pages(config, website, rebuild):
    for page in rebuild:
        phandler = handle_action(
            config.handlers(),
            config.defaulthandler(),
            page)
        compile_page(config, website, phandler)


def compile_page(config, website, pctx):
    print 'Building:', pctx.page.title
    tname = pctx.template()
    scope = dict(current=pctx, site=website, config=config,
            ext=pctx.extension(website, config))
    env = config.templateenv()
    template = env.get_template(tname)
    fp = open(os.path.join(config.destination(), pctx.finalname()), 'w')
    try:
        fp.write(template.render(scope).encode(ENCODING))
    finally:
        fp.close()
            

if __name__ == '__main__':
    import os
    os.chdir('_sample')
    config = TemplateConfig()
    build(config)