refactor all the replace stuff into a single method

This commit is contained in:
Sebastian Hugentobler 2015-05-12 20:32:08 +02:00
parent 8e3dccd637
commit 2cf6ef7c6f
4 changed files with 12 additions and 8 deletions

View File

@ -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)

View File

@ -10,14 +10,14 @@
{% if board.boards |length > 0 %}
<ul>
{% for board in board.boards %}
<li><a href={{ "../board/" + board.title.replace('/', '_').replace(' ', '_').replace('?', '') + ".html" }}>{{ board.title }}</a></li>
<li><a href={{ "../board/" + board.title |url_replacer + ".html" }}>{{ board.title }}</a></li>
{% endfor %}
</ul>
<hr>
{% endif %}
<ul>
{% for thread in board.threads %}
<li><a href={{ "thread/" + thread.title.replace('/', '_').replace(' ', '_').replace('?', '') + ".html" }}>{{ thread.title }}</a></li>
<li><a href={{ "thread/" + thread.title |url_replacer + ".html" }}>{{ thread.title }}</a></li>
{% endfor %}
</ul>
</body>

View File

@ -9,7 +9,7 @@
<body>
<h1>{{ thread.title }}</h1>
{% for post in thread.posts %}
<h3><a href={{ "../../user/" + post.user.name.replace('/', '_').replace(' ', '_').replace('?', '') + ".html" }}>{{ post.user.name }}</a> - {{ post.timestamp | fromunixtime }}</h3>
<h3><a href={{ "../../user/" + post.user.name |url_replacer + ".html" }}>{{ post.user.name }}</a> - {{ post.timestamp | fromunixtime }}</h3>
<p>{{ post.message | tohtml }}</p>
{% for attachment in post.attachments %}
--<br>

View File

@ -13,7 +13,7 @@
<hr>
<ul>
{% for user in users %}
<li><a href={{ "user/" + user.name.replace('/', '_').replace(' ', '_').replace('?', '') + ".html" }}>{{ user.name }}</a></li>
<li><a href={{ "user/" + user.name |url_replacer + ".html" }}>{{ user.name }}</a></li>
{% endfor %}
</ul>
</body>