Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:11
Show Gist options
  • Save st98/6588562652a22828ae39 to your computer and use it in GitHub Desktop.
Save st98/6588562652a22828ae39 to your computer and use it in GitHub Desktop.
python convert.py -c blog.json x.json とかそういう感じで。https://gist.github.com/st98/846f81d5bd40c22f5079 と組み合わせて使う。アレより適当。
{"title": "ぶろぐー"}
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<title>カテゴリ一覧 | {{ blog.title }}</title>
</head>
<body>
<header>
<h1><a href="index.html" title="{{ blog.title }}">{{ blog.title }}</a></h1>
</header>
<nav>
<h1>カテゴリ一覧</h1>
<ul>
{% for category in categories %}
<li>[<a href="category.html#{{ category }}" id="{{ category }}">{{ category }}</a>]</li>
{% for entry in entries %}
<ul>
{% if entry.status == 'Publish' and entry.category == category %}<li><a href="{{ entry.url }}" title="{{ entry.title }}">{{ entry.title }}</a> (<time>{{ entry.date }}</time>)</li>{% endif %}
</ul>
{% endfor %}
{% endfor %}
</ul>
</nav>
</body>
</html>
import json
import re
import sys
from jinja2 import Environment, FileSystemLoader
def usage(path):
print('usage: python {} [-h] [-c config] json', file=sys.stderr)
# http://stackoverflow.com/questions/4223654/how-to-ensure-that-a-python-dict-keys-are-lowercase
def lower_keys(x):
if isinstance(x, list):
return [lower_keys(v) for v in x]
elif isinstance(x, dict):
return dict((k.lower(), lower_keys(v)) for k, v in x.items())
return x
_d = re.compile(r'(\d{2})/(\d{2})/(\d{4}) (\d{2}:\d{2}:\d{2})')
def convert_date(s):
a = _d.findall(s)[0]
return '{}-{}-{} {}'.format(a[2], a[0], a[1], a[3])
def main(path='', *argv):
import getopt
enc = 'utf-8'
config = None
opts, args = getopt.getopt(argv, 'he:c:')
for opt, arg in opts:
if opt == '-h':
usage(path)
return 1
elif opt == '-e':
enc = arg
elif opt == '-c':
config = arg
if len(args) < 1:
usage(path)
return 1
entries = json.loads(open(args[0], 'rb').read().decode(enc))
entries = list(reversed(lower_keys(entries)))
if config is None:
config = {'title': 'blog'}
else:
config = json.loads(open(config, 'rb').read().decode(enc))
for entry in entries:
if 'date' in entry:
entry['date'] = convert_date(entry['date'])
entry['url'] = '{}-{}.html'.format(entry['date'][:10], entry['date'][11:].replace(':', ''))
if 'body' in entry:
entry['body'] = entry['body'].replace('\n', '<br>')
categories = list(set(entry['category'] for entry in entries if 'category' in entry))
env = Environment(loader=FileSystemLoader('templates', encoding=enc))
index = env.get_template('index.html')
category = env.get_template('category.html')
post = env.get_template('post.html')
open('blog/index.html', 'wb').write(index.render(blog=config, entries=entries).encode(enc))
open('blog/category.html', 'wb').write(category.render(blog=config, entries=entries, categories=categories).encode(enc))
for entry in entries:
open('blog/{}'.format(entry['url']), 'wb').write(post.render(blog=config, entries=entries, entry=entry).encode(enc))
return 0
if __name__ == '__main__':
sys.exit(main(*sys.argv))
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<title>{{ blog.title }}</title>
</head>
<body>
<header>
<h1><a href="index.html" title="{{ blog.title }}">{{ blog.title }}</a></h1>
</header>
<nav>
<h1>記事一覧</h1>
<ul>
{% for entry in entries %}
{% if entry.status == 'Publish' %}<li>[<a href="category.html#{{ entry.category }}" title="{{ entry.category }}">{{ entry.category }}</a>] <a href="{{ entry.url }}" title="{{ entry.title }}">{{ entry.title }}</a> (<time>{{ entry.date }}</time>)</li>{% endif %}
{% endfor %}
</ul>
</nav>
</body>
</html>
<!doctype html>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<title>{{ entry.title }} | {{ blog.title }}</title>
</head>
<body>
<header>
<h1><a href="index.html" title="{{ blog.title }}">{{ blog.title }}</a></h1>
</header>
<main>
<h1><a href="{{ entry.url }}" title="{{ entry.title }}">{{ entry.title }}</a> (<time>{{ entry.date }}</time>)</h1>
{{ entry.body }}
</main>
</body>
</html>
@charset 'utf-8';
@import url('https://fonts.googleapis.com/css?family=Source+Code+Pro:300,400');
body {
width: 90%;
max-width: 40em;
margin: 3em auto;
line-height: 2em;
font-family: 'Source Code Pro', Consolas, 'Hiragino Kaku Gothic ProN', Meiryo, sans-serif;
}
h1, h2, h3, h4 {
margin: 1em 0;
font-weight: 400;
}
main h1 {
font-size: 1.5em;
}
main h2 {
font-size: 1.17em;
}
main h3 {
font-size: 1em;
}
a {
/* padding: 3px 7px; */
text-decoration: none;
color: #2e35e9;
}
a:hover {
text-decoration: underline;
/* background: #eee; */
}
a:visited {
color: #a757a8;
}
p {
text-indent: 1em;
}
time {
color: #555;
}
li {
list-style: square;
}
:target {
font-weight: bolder;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment