From 31ec3203c5222d0ccc10ca14580c18786ac7a52e Mon Sep 17 00:00:00 2001 From: Brian Hardisty Date: Wed, 5 Jul 2017 02:59:09 -0700 Subject: [PATCH] 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 --- index.py | 5 ++-- templates/index.html | 52 ++++++++++++++++++++-------------------- templates/index_row.html | 20 ++++++++-------- 3 files changed, 39 insertions(+), 38 deletions(-) diff --git a/index.py b/index.py index d8a5910a..ab80a70b 100644 --- a/index.py +++ b/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)) diff --git a/templates/index.html b/templates/index.html index f6287e5d..1f358aea 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,7 +3,7 @@ Archived Sites
-

+

Archived Sites
- Archived with: Bookmark Archiver on {date_updated} + Archived with: Bookmark Archiver on ${date_updated}

@@ -76,7 +76,7 @@ Starred - Saved Articles ({num_links}) + Saved Articles (${num_links}) Files PDF Screenshot @@ -84,7 +84,7 @@ Original URL - {rows} + ${rows} diff --git a/templates/index_row.html b/templates/index_row.html index ffe4bb9e..a89769f8 100644 --- a/templates/index_row.html +++ b/templates/index_row.html @@ -1,12 +1,12 @@ - {time} - - - {title} {tags} + ${time} + + + ${title} ${tags} - 📂 - 📄 - 🖼 - 🏛 - 🔗 {url} -📂 + 📄 + 🖼 + 🏛 + 🔗 ${url} +