From 7bf1026be09021209a1f470f956e9d24d802ce86 Mon Sep 17 00:00:00 2001 From: chris Date: Sun, 9 Feb 2014 18:35:41 +0100 Subject: [PATCH] factor our file writes. --- blogtopoid/__init__.py | 2 +- blogtopoid/blogtopoid.py | 90 ++++++++++------------------------------ blogtopoid/commands.py | 25 +++++++---- 3 files changed, 39 insertions(+), 78 deletions(-) diff --git a/blogtopoid/__init__.py b/blogtopoid/__init__.py index 6cb8a80..a39f440 100644 --- a/blogtopoid/__init__.py +++ b/blogtopoid/__init__.py @@ -32,7 +32,7 @@ def main(): commands.quickstart() elif args.post: - commands.post() + commands.new_post() else: commands.generate() diff --git a/blogtopoid/blogtopoid.py b/blogtopoid/blogtopoid.py index 89361d2..5c422f1 100644 --- a/blogtopoid/blogtopoid.py +++ b/blogtopoid/blogtopoid.py @@ -172,18 +172,12 @@ class Post(object): else: return - # write post to disk - codecs.open( - os.path.join(config.outputdir, self.outfile), - 'w', - 'utf-8' - ).write( - post_template.render( - config=config, - post=self, - pages=pages, - add_style=add_style, - ) + # return rendered post + return post_template.render( + config=config, + post=self, + pages=pages, + add_style=add_style, ) @@ -293,7 +287,7 @@ def generate_feed(posts): def generate_index(posts, pages): - """ write index.html to disk + """ render index.html files contents :param posts: post objs to generate index for. :type posts: list of Post @@ -305,82 +299,42 @@ def generate_index(posts, pages): post_template = env.get_template('index.html') # generate index from template - ihtml = post_template.render( + return post_template.render( config=config, pages=pages, posts=posts, ) - codecs.open( - os.path.join(config.outputdir, 'index.html'), - 'w', - 'utf-8', - ).write(ihtml) -def prepare_style(outputdir, blogurl): +def prepare_style(): """ read and process style/ directory """ config = Config() # copy static files - if not os.path.exists(os.path.join(outputdir, 'style')): - os.makedirs(os.path.join(outputdir, 'style')) + if not os.path.exists(os.path.join(config.outputdir, 'style')): + os.makedirs(os.path.join(config.outputdir, 'style')) # copy supplementary files to ouput dir for filename in os.listdir(config.styledir): if os.path.splitext(filename)[1].lower() != '.css': shutil.copy( os.path.join(config.styledir, filename), - os.path.join(outputdir, 'style') + os.path.join(config.outputdir, 'style') ) - # write possible syntax highlights - codecs.open( - os.path.join(config.styledir, 'pygments.css'), - 'w', - 'utf-8', - ).write(HtmlFormatter().get_style_defs('.codehilite')) - # cat all css files together - allcss = "" + # generate syntax highlight css and append all other CSS files + allcss = HtmlFormatter().get_style_defs('.codehilite') for cssfile in glob.iglob(os.path.join(config.styledir, '*.css')): allcss = allcss + codecs.open(cssfile, 'r', 'utf-8').read() - allcss = allcss.replace('{{styleurl}}', "{}style/".format(blogurl)) + allcss = allcss.replace('{{styleurl}}', "{}style/".format(config.blogurl)) - # minimise and write css - codecs.open( - os.path.join(outputdir, 'style', 'style.css'), - 'w', - 'utf-8' - ).write(cssmin(allcss, wrap=1000)) + # minimise css + return cssmin(allcss, wrap=1000) -def generate_tag_indeces(tagobjs, pages): - """ write tags to disk +def write_file(filename, content): + """ write `content` to `filename`, utf-8 encoded. - :param pages: pages objs for sidebar - :type pages: list of Page - :param tagobjs: tag objs to generate indeces for. - :type tagobjs: dict (str, Tag) + :param filename: filename to write to + :param content: content to write """ - config = Config() - - # make output directory for tags - outputdir = config.outputdir - if not os.path.exists(os.path.join(outputdir, 'tags')): - os.makedirs(os.path.join(outputdir, 'tags')) - - # load jinja2 template - env = Environment(loader=FileSystemLoader(config.templatedir)) - post_template = env.get_template('index.html') - - # generate index from index.md - # TODO move markdown to md module - for tag in tagobjs.values(): - ihtml = post_template.render( - config=config, - pages=pages, - posts=tag.posts, - ) - codecs.open( - os.path.join(config.outputdir, 'tags', '{}.html'.format(tag.name)), - 'w', - 'utf-8', - ).write(ihtml) + codecs.open(filename, 'w', 'utf-8').write(content) diff --git a/blogtopoid/commands.py b/blogtopoid/commands.py index c51590c..04c55b0 100644 --- a/blogtopoid/commands.py +++ b/blogtopoid/commands.py @@ -10,7 +10,7 @@ import ConfigParser import pkg_resources from .blogtopoid import (Config, generate_index, generate_feed, - generate_tag_indeces, prepare_style, tags, Post, Page) + prepare_style, tags, Post, Page, write_file) def quickstart(): @@ -78,7 +78,7 @@ def quickstart(): sys.exit(0) -def post(): +def new_post(): """ ask for YAML front-matter options, create empty post and start editor """ @@ -97,7 +97,8 @@ def generate(): page = Page(infile) pages.append(page) for page in pages: - page.render(pages) + write_file(os.path.join(config.outputdir, page.outfile), + page.render(pages)) posts = [] for infile in os.listdir(unicode(config.inputdir)): @@ -115,19 +116,25 @@ def generate(): # render post htmls for post in posts: - post.render(pages) + write_file(os.path.join(config.outputdir, post.outfile), + post.render(pages)) # generate index from index.md - generate_index(posts, pages) + write_file(os.path.join(config.outputdir, 'index.html'), + generate_index(posts, pages)) # generate rss feed generate_feed(posts) # generate tag pages - generate_tag_indeces(tags, pages) + for tag in tags: + write_file( + os.path.join(config.outputdir, 'tags', '{}.html'.format(tag.name)), + generate_index(tag.posts, pages) + ) # copy style dir to disk - prepare_style( - config.outputdir, - config.blogurl, + write_file( + os.path.join(config.outputdir, 'style', 'style.css'), + prepare_style() )