mirror of
https://github.com/ArchiveBox/ArchiveBox.git
synced 2025-05-14 15:14:31 -04:00
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
This commit is contained in:
parent
bca2dceec0
commit
31ec3203c5
3 changed files with 39 additions and 38 deletions
5
index.py
5
index.py
|
@ -1,5 +1,6 @@
|
|||
import os
|
||||
from datetime import datetime
|
||||
from string import Template
|
||||
|
||||
from config import INDEX_TEMPLATE, INDEX_ROW_TEMPLATE
|
||||
from parse import derived_link_info
|
||||
|
@ -16,7 +17,7 @@ def dump_index(links, service):
|
|||
link_html = f.read()
|
||||
|
||||
article_rows = '\n'.join(
|
||||
link_html.format(**derived_link_info(link)) for link in links
|
||||
Template(link_html).substitute(**derived_link_info(link)) for link in links
|
||||
)
|
||||
|
||||
template_vars = {
|
||||
|
@ -27,4 +28,4 @@ def dump_index(links, service):
|
|||
}
|
||||
|
||||
with open(os.path.join(service, 'index.html'), 'w', encoding='utf-8') as f:
|
||||
f.write(index_html.format(**template_vars))
|
||||
f.write(Template(index_html).substitute(template_vars))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue