proboards-website-generator/generator.py

184 lines
6.5 KiB
Python
Raw Normal View History

2015-05-12 14:47:18 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2015-05-12 14:50:39 +00:00
# 2015 by Sebastian Hugentobler <shugentobler@vanwa.ch>
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
2017-01-05 14:55:20 +00:00
# See http://creativecommons.org/publicdomain/zero/1.0/ for a description
# of CC0.
2015-05-12 14:50:39 +00:00
2017-01-05 14:55:20 +00:00
import argparse
import json
import os
import re
import shutil
2015-05-12 14:47:18 +00:00
from datetime import datetime
from jinja2 import Environment, FileSystemLoader
2017-01-05 14:55:20 +00:00
css = """
body {
margin: 2em auto;
max-width: 50em;
line-height: 1.6;
font-size: 1.1em;
color: #444;
padding: 0 1em;
}
h1,h2,h3 {
line-height: 1.2;
}
"""
2015-05-12 14:47:18 +00:00
def fromunixtime(value):
return datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S')
2017-01-05 14:55:20 +00:00
def url_replacer(value):
2015-05-18 14:55:35 +00:00
return re.sub('[^a-zA-Z0-9\n\.]', '_', value)
2017-01-05 14:55:20 +00:00
2015-05-12 14:47:18 +00:00
def user_replacer(match):
return '[url=../../user/' + url_replacer(match.group(2)) + '.html]' + match.group(2) + '[/url]'
2015-05-12 14:47:18 +00:00
2017-01-05 14:55:20 +00:00
2015-05-12 14:47:18 +00:00
def tohtml(value):
value = value.replace('{{baseurl}}', 'static')
value = value.replace('\n', '<br />')
2017-01-05 14:55:20 +00:00
value = re.sub(r'\[url=\/user\/(.*?)\](.*?)\[\/url\]',
user_replacer, value)
value = re.sub(r'\[url=(.*?)\](.*?)\[/url\]',
r'<a href="\1">\2</a>', value)
2015-05-12 14:47:18 +00:00
value = re.sub(r'\[video\](.*?)\[/video\]', r'<a href="\1">\1</a>', value)
2017-01-05 14:55:20 +00:00
value = re.sub(r'\[colour=(.*?)\](.*?)\[/colour\]',
r'<font color="\1">\2</font>', value)
2015-05-12 14:47:18 +00:00
value = re.sub(r'\[b\](.*?)\[/b\]', r'<b>\1</b>', value)
value = re.sub(r'\[i\](.*?)\[/i\]', r'<i>\1</i>', value)
value = re.sub(r'\[u\](.*?)\[/u\]', r'<u>\1</u>', value)
value = re.sub(r'\[img\](.*?)\[/img\]', r'<img src="\1">', value)
2017-01-05 14:55:20 +00:00
for i in range(25): # ugly hack but works good enough
value = re.sub(r'\[quote=(.+?)\](.+)\[/quote\]', '<fieldset><legend>' + '<a href="../../user/' +
url_replacer(r'\1') + '.html">' + r'\1</a></legend>\2</fieldset>', value, count=1)
2015-05-12 14:47:18 +00:00
2017-01-05 14:55:20 +00:00
for i in range(25): # same here, shut up
value = re.sub(r'\[quote\](.*?)\[/quote\]',
r'<fieldset>\1</fieldset>', value)
2015-05-12 14:47:18 +00:00
return value
2017-01-05 14:55:20 +00:00
2015-05-12 14:47:18 +00:00
def write_render(rendered, name, outpath):
if not os.path.exists(os.path.join(outpath, 'board', 'thread')):
os.makedirs(os.path.join(outpath, 'board', 'thread'))
if not os.path.exists(os.path.join(outpath, 'user')):
os.makedirs(os.path.join(outpath, 'user'))
with open(os.path.join(outpath, name), 'w') as f:
f.write(rendered)
2017-01-05 14:55:20 +00:00
2015-05-12 14:47:18 +00:00
def find_unregistered_users(data):
unregistered_users = []
for board in data['boards']:
for thread in board['threads']:
for post in thread['posts']:
if post['user']['name'] not in unregistered_users:
found = False
for user in data['users']:
if post['user']['name'] == user['name']:
found = True
break
if not found:
unregistered_users.append(post['user']['name'])
return unregistered_users
2017-01-05 14:55:20 +00:00
2015-05-18 15:55:38 +00:00
def find_board_id(board):
return board['link'].split('/')[-2]
2017-01-05 14:55:20 +00:00
2015-05-12 18:18:31 +00:00
def render_boards(boards, template_board, template_thread, outpath, title):
2015-05-12 14:47:18 +00:00
for board in boards:
2017-01-05 14:55:20 +00:00
rendered_board = template_board.render(
board=board, title=title + ' - ' + board['title'])
write_render(rendered_board, os.path.join('board', find_board_id(
board) + '_' + url_replacer(board['title']) + '.html'), outpath)
2015-05-12 14:47:18 +00:00
for thread in board['threads']:
2017-01-05 14:55:20 +00:00
rendered_thread = template_thread.render(
board=board, thread=thread, title=title + ' - ' + thread['title'])
write_render(rendered_thread, os.path.join('board', 'thread', thread[
'id'] + '_' + url_replacer(thread['title']) + '.html'), outpath)
render_boards(board['boards'], template_board,
template_thread, outpath, title)
2015-05-12 14:47:18 +00:00
2015-05-12 18:18:31 +00:00
def render(inputfile, staticpath, outpath, title):
2017-01-05 14:55:20 +00:00
write_render(css, 'styles.css', outpath)
2015-05-12 14:47:18 +00:00
with open(inputfile) as data_file:
data = json.load(data_file)
unregistered_users = find_unregistered_users(data)
for unregistered_user in unregistered_users:
2017-01-05 14:55:20 +00:00
data['users'].append(
{'name': unregistered_user, 'registered': None})
2015-05-12 14:47:18 +00:00
env = Environment(loader=FileSystemLoader('./templates'))
env.filters['fromunixtime'] = fromunixtime
env.filters['tohtml'] = tohtml
env.filters['url_replacer'] = url_replacer
2015-05-18 15:55:38 +00:00
env.filters['find_board_id'] = find_board_id
2015-05-12 14:47:18 +00:00
template_users = env.get_template('users.html.j2')
template_user = env.get_template('user.html.j2')
template_boards = env.get_template('boards.html.j2')
template_board = env.get_template('board.html.j2')
template_thread = env.get_template('thread.html.j2')
2017-01-05 14:55:20 +00:00
rendered_users = template_users.render(
users=data['users'], title=title + ' - Users')
rendered_boards = template_boards.render(
boards=data['boards'], title=title + ' - Boards')
2015-05-12 14:47:18 +00:00
write_render(rendered_users, 'users.html', outpath)
write_render(rendered_boards, 'boards.html', outpath)
2017-01-05 14:55:20 +00:00
shutil.rmtree(os.path.join(outpath, 'board',
'thread', 'static'), ignore_errors=True)
shutil.copytree(staticpath, os.path.join(
outpath, 'board', 'thread', 'static'))
2015-05-12 14:47:18 +00:00
for user in data['users']:
2017-01-05 14:55:20 +00:00
rendered_user = template_user.render(
user=user, title=title + ' - ' + user['name'])
write_render(rendered_user, os.path.join(
'user', url_replacer(user['name']) + '.html'), outpath)
2015-05-12 14:47:18 +00:00
2017-01-05 14:55:20 +00:00
render_boards(data['boards'], template_board,
template_thread, outpath, title)
2015-05-12 14:47:18 +00:00
if __name__ == "__main__":
2017-01-05 14:55:20 +00:00
parser = argparse.ArgumentParser(
description='build a static website out of a proboard json dump')
parser.add_argument('--data', default='board.json',
help='board data (json file)')
parser.add_argument('--static', default='static',
help='path to the static files (images, attachments)')
parser.add_argument('--out', default='rendered',
help='path where the website gets rendered to')
parser.add_argument('--title', default='Proboard',
help='title for your pages')
2015-05-12 14:47:18 +00:00
args = parser.parse_args()
2015-05-12 18:18:31 +00:00
render(args.data, args.static, args.out, args.title)