From 31ec3203c5222d0ccc10ca14580c18786ac7a52e Mon Sep 17 00:00:00 2001 From: Brian Hardisty Date: Wed, 5 Jul 2017 02:59:09 -0700 Subject: [PATCH 1/3] 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} + From 28445d26e86187569432e4e294083effc5020552 Mon Sep 17 00:00:00 2001 From: Brian Hardisty Date: Wed, 5 Jul 2017 03:07:21 -0700 Subject: [PATCH 2/3] Remove obsolete note on double-bracketed CSS in templates --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 727dc053..008be4cf 100644 --- a/README.md +++ b/README.md @@ -96,8 +96,6 @@ env CHROME_BINARY=google-chrome-stable RESOLUTION=1440,900 FETCH_PDF=False ./arc To tweak the outputted html index file's look and feel, just copy the files in `templates/` somewhere else and edit away. Use the two index config variables above to point the script to your new custom template files. -The templates use format strings (not a proper templating engine like jinja2), which is why the CSS is double-bracketed `{{...}}`. - ## Publishing Your Archive The archive produced by `./archive.py` is suitable for serving on any provider that From 26b5e4aa3c23078ae77c8541099dfbf211bbe130 Mon Sep 17 00:00:00 2001 From: Brian Hardisty Date: Wed, 5 Jul 2017 03:22:11 -0700 Subject: [PATCH 3/3] Use `$identifier` over `${identifier}` `${identifier}` has the potential to conflict with JavaScript template literals. --- templates/index.html | 8 ++++---- templates/index_row.html | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/templates/index.html b/templates/index.html index 1f358aea..c33dc5ca 100644 --- a/templates/index.html +++ b/templates/index.html @@ -64,11 +64,11 @@
-

+

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 a89769f8..2708463e 100644 --- a/templates/index_row.html +++ b/templates/index_row.html @@ -1,12 +1,12 @@ - ${time} - - - ${title} ${tags} + $time + + + $title $tags - 📂 - 📄 - 🖼 - 🏛 - 🔗 ${url} + 📂 + 📄 + 🖼 + 🏛 + 🔗 $url