示例#1
0
def atom(cache):
    """Build atom feed."""
    data = _complement(index=cache.index())
    dest = os.path.join(conf.get('build_path'), conf.get('atom_location'))
    logger.info(_to('atom feed', dest))
    helpers.makedirs(os.path.dirname(dest))
    templates.render(data, 'atom.xml', dest)
示例#2
0
def sitemap(cache):
    """Build sitemap.xml."""
    data = _complement(index=cache.full_index())
    dest = os.path.join(conf.get('build_path'), const.SITEMAP)
    logger.info(_to('sitemap', dest))
    helpers.makedirs(os.path.dirname(dest))
    templates.render(data, 'sitemap.xml', dest)
示例#3
0
def theme_update(path=None, safe=False):
    conf.load(path)

    if safe:
        logger.info('saving current theme')
        assets_bak = "%s.bak" % pathes.theme_assets_installed()
        templates_bak = "%s.bak" % pathes.theme_templates_installed()
        try:
            for path in [templates_bak, assets_bak]:
                if os.path.isdir(path):
                    shutil.rmtree(path, ignore_errors=True)
            shutil.move(pathes.theme_assets_installed(), assets_bak)
            shutil.move(pathes.theme_templates_installed(), templates_bak)
        except Exception as e:
            logger.error('error saving existing theme files: ' + str(e))
            return

    logger.info('updating theme files')
    try:
        path = pathes.theme_assets_source()
        helpers.copydir(path, pathes.theme_assets_installed())
        path = pathes.theme_templates_source()
        helpers.copydir(path, pathes.theme_templates_installed())
    except Exception as e:
        logger.error('error updating theme: ' + str(e))
示例#4
0
def post(path=None, name=None, force=False, edit=False):
    """Create new post."""
    conf.load(path)
    path = source.PostSource.create(name, force)
    logger.info('post created: ' + path)
    if edit:
        helpers.execute(conf.get('editor_cmd'), path)
示例#5
0
def static(cache):
    """Copy other assets as is to the build path."""
    for source in cache.assets(processed=False):
        logger.info('copying: ' + source.rel_path())
        helpers.makedirs(source.dest_dir())
        shutil.copyfile(source.path(), source.dest())
        helpers.utime(source.dest(), source.updated())
        source.processed(True)
示例#6
0
def archive(cache):
    """Build blog archive page."""
    dest = os.path.join(conf.get('build_path'), conf.get('archive_location'))
    logger.info('archive: ' + conf.get('archive_location'))
    helpers.makedirs(os.path.dirname(dest))
    page_data = {'title': 'Archive', 'tags': cache.tags()}
    data = _complement(page_data, index=cache.index())
    templates.render(data, 'archive.html', dest)
示例#7
0
def tags(cache):
    """Build blog tag pages."""
    for tag in cache.tags():
        tag = tag['name']
        dest = helpers.tag_path(tag)
        logger.info(_to('tag', tag, dest))
        helpers.makedirs(os.path.dirname(dest))
        data = _complement({'title': tag}, index=cache.index(tag=tag))
        templates.render(data, 'tag.html', dest)
示例#8
0
def env():
    global _env
    if _env is None:
        search_pathes = [pathes.templates(), pathes.theme_templates()]
        logger.info("templates pathes: [%s]" % ', '.join(search_pathes))
        loader = jinja2.FileSystemLoader(searchpath=search_pathes)
        _env = jinja2.Environment(loader=loader, extensions=JINJA_EXTENSIONS)
        _env.filters.update(custom_filters())
        _env.globals.update(custom_globals())
    return _env
示例#9
0
def pages(cache):
    """Build site pages."""
    for source in cache.pages():
        logger.info(_to('page', source.rel_path(), source.rel_dest()))
        helpers.makedirs(source.dest_dir())
        try:
            data = _complement(source.data(), index=cache.index())
            templates.render_page(data, source.dest())
        except Exception as ex:
            logger.error('page building error: ' + str(ex))
            logger.debug(traceback.format_exc())
示例#10
0
def page(path=None, name=None, force=False, edit=False):
    """Create new page."""
    conf.load(path)
    try:
        path = source.PageSource.create(name, force)
    except source.PageExistsException:
        logger.error('page already exists, use -f to overwrite')
        return
    logger.info('page created: ' + path)
    if edit:
        helpers.execute(conf.get('editor_cmd'), path)
示例#11
0
def humans(cache):
    """Build humans.txt."""
    for source in cache.assets(basename='humans.txt'):
        logger.info('processing ' + source.rel_path())
        helpers.makedirs(source.dest_dir())
        try:
            data = _complement({})
            templates.render_file(source.path(), data, source.dest())
        except Exception as ex:
            logger.error('humans.txt processing failed: ' + str(ex))
            logger.debug(traceback.format_exc())
        finally:
            source.processed(True)
示例#12
0
def js(cache):
    """Minify JavaScript files to the build path."""
    for source in cache.assets(ext='.js'):
        helpers.makedirs(source.dest_dir())
        command = conf.get('min_js_cmd')
        if conf.get('min_js') and command:
            logger.info('minifying JavaScript: ' + source.rel_path())
            helpers.execute(command, source.path(), source.dest())
        else:
            logger.info('copying: ' + source.rel_path())
            shutil.copyfile(source.path(), source.dest())
        helpers.utime(source.dest(), source.updated())
        source.processed(True)
示例#13
0
def build(path=None, output=None):
    """Generate web content from source."""
    conf.load(path)
    cache = Cache()
    if cache.processing_errors():
        for file_name, error in cache.processing_errors():
            message = "error processing source file '%s' - %s"
            logger.error(message % (file_name, error))
    if output:
        conf.set('build_path', output)
    logger.info('build directory: ' + conf.get('build_path'))
    for builder in builders.order():
        builder(cache)
示例#14
0
def less(cache):
    """Compile and minify less files."""
    for source in cache.assets(ext='.less'):
        helpers.makedirs(source.dest_dir())
        logger.info('compiling LESS: ' + source.rel_path())
        if conf.get('min_css') and conf.get('min_css_cmd'):
            tmp_file = os.path.join(source.dest_dir(), '_' + source.basename())
            helpers.execute(conf.get('less_cmd'), source.path(), tmp_file)
            logger.info('minifying CSS: ' + source.rel_path())
            helpers.execute(conf.get('min_css_cmd'), tmp_file, source.dest())
            os.remove(tmp_file)
        else:
            helpers.execute(conf.get('less_cmd'), source.path(), source.dest())
        helpers.utime(source.dest(), source.updated())
        source.processed(True)
示例#15
0
def init(path=None, force=False):
    """Create new website."""
    conf.generate(path, force)
    try:
        src = pathes.proto()
        existing = helpers.copydir(src, pathes.site(), force=force)
        if len(existing):
            message = "some existing files were overwritten" if force else \
                "some existing files were NOT overwritten (use --force " \
                "to overwrite)"
            logger.warn("%s\n- %s" % (message, '\n- '.join(existing)))
        logger.info('website created successfully, have fun!')
    except Exception as ex:
        logger.error('initialization failed: ' + str(ex))
        print(str(ex))
示例#16
0
def deploy(path=None):
    """Deploy generated website to the remote web server."""
    conf.load(path)
    helpers.check_build(conf.get('build_path'))
    logger.info('deploying website...')
    if not conf.get('deploy_cmd'):
        raise Exception('deploy command is not defined')
    cmd = conf.get('deploy_cmd').format(build_path=conf.get('build_path'))
    try:
        output = subprocess.check_output(cmd, shell=True)
        logger.debug("Command output:\n%s" % output.decode('utf-8'))
        logger.info('done')
    except subprocess.CalledProcessError as e:
        logger.error(e)
        logger.debug("Command output:\n%s" % e.output.decode('utf-8'))
示例#17
0
def posts(cache):
    """Build blog posts and copy the latest post to the site root."""
    for source in cache.posts():
        logger.info(_to('post', source.rel_path(), source.rel_dest()))
        helpers.makedirs(source.dest_dir())
        try:
            data = _complement(source.data())
            templates.render_page(data, source.dest())
        except Exception as ex:
            logger.error('post building error: ' + str(ex))
            logger.debug(traceback.format_exc())

    if conf.get('post_at_root_url'):  # put the latest post at site root url
        last = cache.posts()[0]
        path = os.path.join(conf.get('build_path'), conf.get('index_page'))
        logger.info(_to('root', last.rel_dest(), conf.get('index_page')))
        if any(cache.pages(dest=conf.get('index_page'))):
            logger.warn('root page will be overwritten by the latest post')
        try:
            shutil.copyfile(last.dest(), path)
        except FileNotFoundError:
            logger.error("latest post was not generated and can't be copied")
示例#18
0
def clean(path=None):
    """Delete all generated content."""
    conf.load(path)
    logger.info('cleaning output...')
    helpers.rmdir(conf.get('build_path'))
    logger.info('done')