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): def fromunixtime(value):
return datetime.fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S') 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): 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): def tohtml(value):
value = value.replace('{{baseurl}}', 'static') value = value.replace('{{baseurl}}', 'static')
@ -68,11 +71,11 @@ def find_unregistered_users(data):
def render_boards(boards, template_board, template_thread, outpath, title): def render_boards(boards, template_board, template_thread, outpath, title):
for board in boards: for board in boards:
rendered_board = template_board.render(board=board, title=title + ' - ' + board['title']) 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']: for thread in board['threads']:
rendered_thread = template_thread.render(thread=thread, title=title + ' - ' + thread['title']) 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) 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 = Environment(loader=FileSystemLoader('./templates'))
env.filters['fromunixtime'] = fromunixtime env.filters['fromunixtime'] = fromunixtime
env.filters['tohtml'] = tohtml env.filters['tohtml'] = tohtml
env.filters['url_replacer'] = url_replacer
template_users = env.get_template('users.html.j2') template_users = env.get_template('users.html.j2')
template_user = env.get_template('user.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']: for user in data['users']:
rendered_user = template_user.render(user=user, title=title + ' - ' + user['name']) 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) render_boards(data['boards'], template_board, template_thread, outpath, title)

View File

@ -10,14 +10,14 @@
{% if board.boards |length > 0 %} {% if board.boards |length > 0 %}
<ul> <ul>
{% for board in board.boards %} {% 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 %} {% endfor %}
</ul> </ul>
<hr> <hr>
{% endif %} {% endif %}
<ul> <ul>
{% for thread in board.threads %} {% 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 %} {% endfor %}
</ul> </ul>
</body> </body>

View File

@ -9,7 +9,7 @@
<body> <body>
<h1>{{ thread.title }}</h1> <h1>{{ thread.title }}</h1>
{% for post in thread.posts %} {% 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> <p>{{ post.message | tohtml }}</p>
{% for attachment in post.attachments %} {% for attachment in post.attachments %}
--<br> --<br>

View File

@ -13,7 +13,7 @@
<hr> <hr>
<ul> <ul>
{% for user in users %} {% 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 %} {% endfor %}
</ul> </ul>
</body> </body>