From c257192d5a22d7ae13e45ff384291ca718fd0f56 Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Wed, 29 Feb 2012 20:07:29 -0800 Subject: [PATCH] adding static page support --- crunch | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/crunch b/crunch index 6602059..415586c 100755 --- a/crunch +++ b/crunch @@ -115,6 +115,7 @@ else: # Define some variables for reuse. build_folder = base_folder + '/' + conf['build_folder'] +pages_folder = base_folder + '/' + conf['pages_folder'] posts_folder = base_folder + '/' + conf['posts_folder'] public_folder = base_folder + '/' + conf['public_folder'] images_folder = base_folder + '/' + conf['images_folder'] @@ -122,6 +123,7 @@ galleries_folder = base_folder + '/' + conf['galleries_folder'] + ### Classes # Define a class for creating a specific page. @@ -335,6 +337,16 @@ def format_post(post): 'isodate':post.date_8601(), 'date':post.date_pretty(), 'short_url':post.url_short(), 'short':post.short} +def format_static(title, content, url): + return """ +
+

%(title)s

+
+
+ %(content)s +
+ """ % {'title':title, 'content':content, 'url':url} + # General purpose formatter for error pages. Takes in an error code string. def format_error(code): return """ @@ -474,6 +486,40 @@ def crunch_errors(): def crunch_pages(): if args.verbose: print 'Building the static pages.' + # Get all the files in the pages folder. + for filename in os.listdir(pages_folder): + # Ensure we're looking at only the files with the right extension per + # conf['extension']. + if filename.endswith(conf['extension']): + if args.verbose: print 'Building ' + filename + + # Fire up a markdown object. + md = markdown.Markdown(extensions = ['meta']) + + # Parse the post and grab the content. + content = md.convert(open(pages_folder + '/' + filename).read()) + + # Pull the title out of the metadata. + title = md.Meta['title'][0] + + # Generate the url + url = '/' + filename.rstrip(conf['extension']) + '.htm' + + # Make the body of the page + body = format_static(title, content, url) + + # Make a new page object and add the body. + page = Page() + page.title = title + ' | ' + page.title + page.body = body + + # Make a new file and write out the page. + n = open(build_folder + url, 'w') + n.writelines(page.formatted()) + n.close + os.chmod(build_folder + url, 0644) + + # Processes all posts. def crunch_posts():