From 2cf6ef7c6fbc62113df963c36bbe212b8ae122a6 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Tue, 12 May 2015 20:32:08 +0200 Subject: [PATCH] refactor all the replace stuff into a single method --- generator.py | 12 ++++++++---- templates/board.html.j2 | 4 ++-- templates/thread.html.j2 | 2 +- templates/users.html.j2 | 2 +- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/generator.py b/generator.py index c919fac..87dd68d 100755 --- a/generator.py +++ b/generator.py @@ -15,8 +15,11 @@ from jinja2 import Environment, FileSystemLoader def fromunixtime(value): return datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S') +def url_replacer(value): + return value.replace('/', '_').replace(' ', '_').replace('?', '').replace('<', '').replace('>', '') + def user_replacer(match): - return '[url=../../user/' + match.group(2).replace('/', '_').replace(' ', '_').replace('?', '') + '.html]' + match.group(2) + '[/url]' + return '[url=../../user/' +url_replacer(match.group(2)) + '.html]' + match.group(2) + '[/url]' def tohtml(value): value = value.replace('{{baseurl}}', 'static') @@ -68,11 +71,11 @@ def find_unregistered_users(data): 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', board['title'].replace('/', '_').replace(' ', '_').replace('?', '') + '.html'), outpath) + write_render(rendered_board, os.path.join('board', url_replacer(board['title']) + '.html'), outpath) for thread in board['threads']: rendered_thread = template_thread.render(thread=thread, title=title + ' - ' + thread['title']) - write_render(rendered_thread, os.path.join('board', 'thread', thread['title'].replace('/', '_').replace(' ', '_').replace('?', '') + '.html'), outpath) + write_render(rendered_thread, os.path.join('board', 'thread', url_replacer(thread['title']) + '.html'), outpath) render_boards(board['boards'], template_board, template_thread, outpath, title) @@ -87,6 +90,7 @@ def render(inputfile, staticpath, outpath, title): env = Environment(loader=FileSystemLoader('./templates')) env.filters['fromunixtime'] = fromunixtime env.filters['tohtml'] = tohtml + env.filters['url_replacer'] = url_replacer template_users = env.get_template('users.html.j2') template_user = env.get_template('user.html.j2') @@ -105,7 +109,7 @@ def render(inputfile, staticpath, outpath, title): for user in data['users']: rendered_user = template_user.render(user=user, title=title + ' - ' + user['name']) - write_render(rendered_user, os.path.join('user', user['name'].replace('/', '_').replace(' ', '_').replace('?', '') + '.html'), outpath) + write_render(rendered_user, os.path.join('user', url_replacer(user['name']) + '.html'), outpath) render_boards(data['boards'], template_board, template_thread, outpath, title) diff --git a/templates/board.html.j2 b/templates/board.html.j2 index d9f105d..7a93291 100644 --- a/templates/board.html.j2 +++ b/templates/board.html.j2 @@ -10,14 +10,14 @@ {% if board.boards |length > 0 %}
{% endif %} diff --git a/templates/thread.html.j2 b/templates/thread.html.j2 index 6e3fd3f..096bf75 100644 --- a/templates/thread.html.j2 +++ b/templates/thread.html.j2 @@ -9,7 +9,7 @@

{{ thread.title }}

{% for post in thread.posts %} -

{{ post.user.name }} - {{ post.timestamp | fromunixtime }}

+

{{ post.user.name }} - {{ post.timestamp | fromunixtime }}

{{ post.message | tohtml }}

{% for attachment in post.attachments %} --
diff --git a/templates/users.html.j2 b/templates/users.html.j2 index 30a0e85..5218c73 100644 --- a/templates/users.html.j2 +++ b/templates/users.html.j2 @@ -13,7 +13,7 @@