1
0
Fork 0
archivebox/index.py
Brian Hardisty 31ec3203c5
Use template strings for substitution in HTML output
`str.format()` can only use substitutions identified by braces (`{` and
`}`). This has the potential to conflict with other code in the HTML
template, such as CSS or JavaScript.

Template strings can use substitutions identified by `$` or `${}`, e.g.:
`$identifier` or `${identifier}`. These substitutions won't conflict
with CSS or JavaScript, allowing users to write HTML templates that
don't require double braces anywhere there's a substitution conflict.
This is especially useful when one is using a build tool to generate the
final CSS/JavaScript/HTML.

https://docs.python.org/3/library/string.html#template-strings
2017-07-05 02:59:09 -07:00

31 lines
988 B
Python

import os
from datetime import datetime
from string import Template
from config import INDEX_TEMPLATE, INDEX_ROW_TEMPLATE
from parse import derived_link_info
def dump_index(links, service):
"""create index.html file for a given list of links and service"""
with open(INDEX_TEMPLATE, 'r', encoding='utf-8') as f:
index_html = f.read()
# TODO: refactor this out into index_template.html
with open(INDEX_ROW_TEMPLATE, 'r', encoding='utf-8') as f:
link_html = f.read()
article_rows = '\n'.join(
Template(link_html).substitute(**derived_link_info(link)) for link in links
)
template_vars = {
'num_links': len(links),
'date_updated': datetime.now().strftime('%Y-%m-%d'),
'time_updated': datetime.now().strftime('%Y-%m-%d %H:%M'),
'rows': article_rows,
}
with open(os.path.join(service, 'index.html'), 'w', encoding='utf-8') as f:
f.write(Template(index_html).substitute(template_vars))