Skip to content

Commit

Permalink
Merge pull request #26 from polybuildr/jinja
Browse files Browse the repository at this point in the history
Switch to using Jinja2 for templates
  • Loading branch information
polybuildr committed Aug 29, 2015
2 parents 148a3f3 + 29f1104 commit b32d101
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 78 deletions.
94 changes: 44 additions & 50 deletions petroglyph/generator.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import os
import shutil
import re
import copy

import yaml
import htmlmin
from jinja2 import Template
from petroglyph.post import Post
from petroglyph import logger


def process_template(template, args):
placeholders = set(re.findall('{{([a-zA-Z][a-zA-Z0-9_-]*)}}', template))
for placeholder in placeholders:
if placeholder in args:
template = template.replace('{{' + placeholder + '}}', str(args[placeholder]))
template = unicode(template, 'utf-8')
return htmlmin.minify(template)
def process_template(template_text, args):
template = Template(template_text)
html = template.render(args)
if not isinstance(html, unicode):
html = unicode(html, 'utf-8')
return htmlmin.minify(html)


def generate(regenerate=False, dry_run=False):
Expand All @@ -29,8 +28,6 @@ def generate(regenerate=False, dry_run=False):
skin = {}
with open(os.path.join('skin', 'post.html'), 'rb') as f:
skin['post'] = f.read()
with open(os.path.join('skin', 'post-peek.html'), 'rb') as f:
skin['post-peek'] = f.read()
with open(os.path.join('skin', 'home.html')) as f:
skin['home'] = f.read()
with open(os.path.join('skin', 'tag.html')) as f:
Expand All @@ -42,7 +39,6 @@ def generate(regenerate=False, dry_run=False):
posts_meta = yaml.safe_load(f)
else:
posts_meta = {}
post_previews_text = []
os.chdir('blog')
logger.log("Found %d post%s." % (len(posts), '' if len(posts) == 1 else 's'))
stats = {}
Expand All @@ -52,6 +48,25 @@ def generate(regenerate=False, dry_run=False):
stats['generated_posts'] = 0
current_posts_slugs = []

blog = {
'title': config['title'],
'description': config['description'],
'author': config['author'],
'posts': []
}

posts.sort(key=lambda p: (p.get_time(), p.slug), reverse=True)

for post in posts:
item = {
'title': post.title,
'slug': post.slug,
'tags': post.tags,
'date': post.getmtime(),
'peek': post.get_preview()
}
blog['posts'].append(item)

for post in posts:
current_posts_slugs.append(post.slug)
if post.slug in posts_meta:
Expand All @@ -68,14 +83,13 @@ def generate(regenerate=False, dry_run=False):
if regenerate or new or post_hash != posts_meta[post.slug]['hash']:
posts_meta[post.slug] = {'mtime': post.mtime, 'hash': post_hash}
post_args = {
'title': post.title,
'tags': ''.join(
['<a href="../tag/' + tag + '" class="tag">#' + tag + '</a>'
for tag in post.tags]
),
'blog_title': config['title'],
'date': post.getmtime(),
'content': post.get_html()
'post': {
'title': post.title,
'tags': post.tags,
'date': post.getmtime(),
'content': post.get_html()
},
'blog': blog
}
post_data = copy.deepcopy(post.front_matter_data)
post_data.update(post_args)
Expand All @@ -87,43 +101,23 @@ def generate(regenerate=False, dry_run=False):
with open(os.path.join(post.slug, 'index.html'), 'wb') as post_file:
post_file.write(process_template(skin['post'], post_data))

posts.sort(key=lambda p: (p.get_time(), p.slug), reverse=True)
tag_data = {}
for post in posts:
post_peek_args = {
'slug': post.slug,
'title': post.title,
'date': post.getmtime(),
'peek': post.get_preview(),
'tags': ''.join(
['<a href="tag/' + tag + '" class="tag">#' + tag + '</a>'
for tag in post.tags]
)
}
post_peek_data = copy.deepcopy(post.front_matter_data)
post_peek_data.update(post_peek_args)
post_previews_text.append(process_template(skin['post-peek'], post_peek_data))
post_peek_data.update({
'tags': ''.join(
['<a href="../../tag/' + tag + '" class="tag">#' + tag + '</a>'
for tag in post.tags]
),
'slug': '../../' + post.slug
})
for tag in post.tags:
if tag not in tag_data:
tag_data[tag] = []
tag_data[tag].append(process_template(skin['post-peek'], post_peek_data))
tag_data[tag].append({
'title': post.title,
'tags': post.tags,
'date': post.getmtime(),
'peek': post.get_preview(),
'slug': post.slug
})
home_args = {
'title': config['title'],
'author': config['author'],
'description': config['description'],
'posts': "\n".join(post_previews_text)
'blog': blog
}
tag_args = {
'title': config['title'],
'author': config['author'],
'description': config['description'],
'blog': blog
}
if stats['new_posts']:
logger.log(
Expand Down Expand Up @@ -151,8 +145,8 @@ def generate(regenerate=False, dry_run=False):
os.mkdir(os.path.join('tag', tag))
tag_args.update(
{
'tag': '#' + tag,
'posts': "\n".join(tag_data[tag])
'tag': tag,
'posts': tag_data[tag]
}
)
with open(os.path.join('tag', tag, 'index.html'), 'wb') as f:
Expand Down
25 changes: 20 additions & 5 deletions petroglyph/skins/monoblue/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,38 @@
<link rel="stylesheet" href="css/monoblue.css">
<link href="css/prism.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600" rel="stylesheet" type="text/css">
<title>{{title}}</title>
<title>{{ blog.title }}</title>
</head>
<body>
<div class="sidebar-container">
<div class="sidebar">
<div class="title-section">
<h1 id="blog-title">{{title}}</h1>
<p>{{description}}</p>
<h1 id="blog-title">{{ blog.title }}</h1>
<p>{{ blog.description }}</p>
</div>
<hr>
<div class="author-section">
<h2>{{author}}</h2>
<h2>{{ blog.author }}</h2>
</div>
</div>
</div>
<div class="container column">
{{posts}}
{% for post in blog.posts %}
<div class="post">
<div class="post-title-section">
<h2 class="post-title"><a href="{{ post.slug }}">{{ post.title }}</a></h2>
<div class="post-tags">
{% for tag in post.tags %}
<a href="tag/{{ tag }}" class="tag">#{{ tag }}</a>
{% endfor %}
</div>
</div>
<h3 class="post-date">{{ post.date }}</h3>
{{ post.peek }}
<div><strong><a href="{{ post.slug }}">Read more &gt;&gt;</a></strong></div>
</div>
<hr>
{% endfor %}
</div>
<script src="js/prism.js"></script>
</body>
Expand Down
10 changes: 0 additions & 10 deletions petroglyph/skins/monoblue/post-peek.html

This file was deleted.

18 changes: 12 additions & 6 deletions petroglyph/skins/monoblue/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="utf-8">
<title>{{title}} | {{blog_title}}</title>
<title>{{ post.title }} | {{ blog.title }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../css/normalize.css">
<link rel="stylesheet" href="../css/monoblue.css">
Expand All @@ -11,14 +11,18 @@
</head>
<body>
<div class="container single">
<h1 id="blog-title"><a href="../">{{blog_title}}</a></h1>
<h1 id="blog-title"><a href="../">{{ blog.title }}</a></h1>
<div class="post">
<div class="post-title-section">
<h2 class="post-title">{{title}}</h2>
<div class="post-tags">{{tags}}</div>
<h3 class="post-date">{{date}}</h3>
<h2 class="post-title">{{ post.title }}</h2>
<div class="post-tags">
{% for tag in post.tags %}
<a href="../tag/{{ tag }}" class="tag">#{{ tag }}</a>
{% endfor %}
</div>
<h3 class="post-date">{{ post.date }}</h3>
</div>
{{content}}
{{ post.content }}
</div>
<div class="social-plugins">
<div class="fb-like" data-layout="button_count" data-action="like" data-show-faces="false" data-share="false"></div>
Expand All @@ -31,6 +35,7 @@ <h3 class="post-date">{{date}}</h3>
<!-- G +1 -->
<script src="https://apis.google.com/js/platform.js" async defer></script>
<!-- Twitter -->
{% raw %}
<script>!function(d,s,id){{var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){{js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}}}(document, 'script', 'twitter-wjs');</script>
<!-- FB -->
<script>(function(d, s, id) {{
Expand All @@ -40,5 +45,6 @@ <h3 class="post-date">{{date}}</h3>
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}}(document, 'script', 'facebook-jssdk'));</script>
{% endraw %}
</body>
</html>
27 changes: 21 additions & 6 deletions petroglyph/skins/monoblue/tag.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,41 @@
<link rel="stylesheet" href="../../css/monoblue.css">
<link href="../../css/prism.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,400italic,600" rel="stylesheet" type="text/css">
<title>{{title}}</title>
<title>Posts with tag {{ tag }} | {{ blog.title }}</title>
</head>
<body>
<div class="sidebar-container">
<div class="sidebar">
<div class="title-section">
<h1 id="blog-title">{{title}}</h1>
<p>{{description}}</p>
<h1 id="blog-title">{{ blog.title }}</h1>
<p>{{ blog.description }}</p>
</div>
<hr>
<div class="author-section">
<h2>{{author}}</h2>
<h2>{{ blog.author }}</h2>
</div>
</div>
</div>
<div class="container column">
<h2>Posts with tag <span class="tag">{{tag}}</span></h2>
<h2>Posts with tag <span class="tag">#{{ tag }}</span></h2>
<a href="../../">&lt;&lt; Back to home</a>
<hr>
{{posts}}
{% for post in posts %}
<div class="post">
<div class="post-title-section">
<h2 class="post-title"><a href="../../{{ post.slug }}">{{ post.title }}</a></h2>
<div class="post-tags">
{% for tag in post.tags %}
<a href="../../tag/{{ tag }}" class="tag">#{{ tag }}</a>
{% endfor %}
</div>
</div>
<h3 class="post-date">{{ post.date }}</h3>
{{ post.peek }}
<div><strong><a href="{{ post.slug }}">Read more &gt;&gt;</a></strong></div>
</div>
<hr>
{% endfor %}
</div>
<script src="../../js/prism.js"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
license='MIT',
keywords='static blog',
packages=['petroglyph'],
install_requires=['mistune', 'pyyaml', 'docutils', 'htmlmin'],
install_requires=['mistune', 'pyyaml', 'docutils', 'htmlmin', 'jinja2'],
include_package_data=True,
scripts=['petroglyph/petroglyph']
)

0 comments on commit b32d101

Please sign in to comment.