From e6911ff5209f54a523b8b874f3b2ea6f1e86a43b Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Thu, 5 Jan 2017 15:55:20 +0100 Subject: [PATCH] add basic css --- generator.py | 108 +++++++++++++++++++++++++++++---------- templates/board.html.j2 | 2 +- templates/boards.html.j2 | 2 +- templates/thread.html.j2 | 2 +- templates/user.html.j2 | 2 +- templates/users.html.j2 | 2 +- 6 files changed, 86 insertions(+), 32 deletions(-) diff --git a/generator.py b/generator.py index 1301957..1b83783 100755 --- a/generator.py +++ b/generator.py @@ -5,42 +5,72 @@ # 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. -# See http://creativecommons.org/publicdomain/zero/1.0/ for a description of CC0. +# See http://creativecommons.org/publicdomain/zero/1.0/ for a description +# of CC0. -import argparse, json, os, re, shutil +import argparse +import json +import os +import re +import shutil from datetime import datetime from jinja2 import Environment, FileSystemLoader +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; +} +""" + + def fromunixtime(value): return datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S') + def url_replacer(value): return re.sub('[^a-zA-Z0-9\n\.]', '_', value) + def user_replacer(match): return '[url=../../user/' + url_replacer(match.group(2)) + '.html]' + match.group(2) + '[/url]' + def tohtml(value): value = value.replace('{{baseurl}}', 'static') value = value.replace('\n', '
') - value = re.sub(r'\[url=\/user\/(.*?)\](.*?)\[\/url\]', user_replacer, value) - value = re.sub(r'\[url=(.*?)\](.*?)\[/url\]', r'\2', value) + value = re.sub(r'\[url=\/user\/(.*?)\](.*?)\[\/url\]', + user_replacer, value) + value = re.sub(r'\[url=(.*?)\](.*?)\[/url\]', + r'\2', value) value = re.sub(r'\[video\](.*?)\[/video\]', r'\1', value) - value = re.sub(r'\[colour=(.*?)\](.*?)\[/colour\]', r'\2', value) + value = re.sub(r'\[colour=(.*?)\](.*?)\[/colour\]', + r'\2', value) value = re.sub(r'\[b\](.*?)\[/b\]', r'\1', value) value = re.sub(r'\[i\](.*?)\[/i\]', r'\1', value) value = re.sub(r'\[u\](.*?)\[/u\]', r'\1', value) value = re.sub(r'\[img\](.*?)\[/img\]', r'', value) - for i in range(25): # ugly hack but works good enough - value = re.sub(r'\[quote=(.+?)\](.+)\[/quote\]', '
' + '' + r'\1\2
', value, count=1) + for i in range(25): # ugly hack but works good enough + value = re.sub(r'\[quote=(.+?)\](.+)\[/quote\]', '
' + '' + r'\1\2
', value, count=1) - for i in range(25): # same here, shut up - value = re.sub(r'\[quote\](.*?)\[/quote\]', r'
\1
', value) + for i in range(25): # same here, shut up + value = re.sub(r'\[quote\](.*?)\[/quote\]', + r'
\1
', value) return value + 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')) @@ -51,6 +81,7 @@ def write_render(rendered, name, outpath): with open(os.path.join(outpath, name), 'w') as f: f.write(rendered) + def find_unregistered_users(data): unregistered_users = [] for board in data['boards']: @@ -68,27 +99,38 @@ def find_unregistered_users(data): return unregistered_users + def find_board_id(board): return board['link'].split('/')[-2] + def render_boards(boards, template_board, template_thread, outpath, title): for board in boards: - 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) + 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) for thread in board['threads']: - 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) + 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) - render_boards(board['boards'], template_board, template_thread, outpath, title) def render(inputfile, staticpath, outpath, title): + write_render(css, 'styles.css', outpath) + with open(inputfile) as data_file: data = json.load(data_file) unregistered_users = find_unregistered_users(data) for unregistered_user in unregistered_users: - data['users'].append({ 'name': unregistered_user, 'registered': None }) + data['users'].append( + {'name': unregistered_user, 'registered': None}) env = Environment(loader=FileSystemLoader('./templates')) env.filters['fromunixtime'] = fromunixtime @@ -102,27 +144,39 @@ def render(inputfile, staticpath, outpath, title): template_board = env.get_template('board.html.j2') template_thread = env.get_template('thread.html.j2') - rendered_users = template_users.render(users=data['users'], title=title + ' - Users') - rendered_boards = template_boards.render(boards=data['boards'], title=title + ' - Boards') + rendered_users = template_users.render( + users=data['users'], title=title + ' - Users') + rendered_boards = template_boards.render( + boards=data['boards'], title=title + ' - Boards') write_render(rendered_users, 'users.html', outpath) write_render(rendered_boards, 'boards.html', outpath) - shutil.rmtree(os.path.join(outpath, 'board', 'thread', 'static'), ignore_errors=True) - shutil.copytree(staticpath, os.path.join(outpath, 'board', 'thread', 'static')) + shutil.rmtree(os.path.join(outpath, 'board', + 'thread', 'static'), ignore_errors=True) + shutil.copytree(staticpath, os.path.join( + outpath, 'board', 'thread', 'static')) for user in data['users']: - 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) + 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) - render_boards(data['boards'], template_board, template_thread, outpath, title) + render_boards(data['boards'], template_board, + template_thread, outpath, title) if __name__ == "__main__": - 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') + 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') args = parser.parse_args() diff --git a/templates/board.html.j2 b/templates/board.html.j2 index f7bc752..8f2bfc9 100644 --- a/templates/board.html.j2 +++ b/templates/board.html.j2 @@ -3,7 +3,7 @@ - + {{ title }} diff --git a/templates/boards.html.j2 b/templates/boards.html.j2 index 0e9d3b9..ebc6fb9 100644 --- a/templates/boards.html.j2 +++ b/templates/boards.html.j2 @@ -3,7 +3,7 @@ - + {{ title }} diff --git a/templates/thread.html.j2 b/templates/thread.html.j2 index e339678..f406e33 100644 --- a/templates/thread.html.j2 +++ b/templates/thread.html.j2 @@ -3,7 +3,7 @@ - + {{ title }} diff --git a/templates/user.html.j2 b/templates/user.html.j2 index 068b3cb..b05fa20 100644 --- a/templates/user.html.j2 +++ b/templates/user.html.j2 @@ -3,7 +3,7 @@ - + {{ title }} diff --git a/templates/users.html.j2 b/templates/users.html.j2 index 55c4b6c..d43689d 100644 --- a/templates/users.html.j2 +++ b/templates/users.html.j2 @@ -3,7 +3,7 @@ - + {{ title }}