From 367b12ba402d8079d3577abe815cde7fa2f48607 Mon Sep 17 00:00:00 2001 From: jdcaballerov Date: Wed, 2 Dec 2020 16:56:16 -0500 Subject: [PATCH] Replace legacy templates for django templates --- archivebox/index/html.py | 74 +-- .../themes/default/core/snapshot_list.html | 55 +- archivebox/themes/default/link_details.html | 488 ++++++++++++++++++ archivebox/themes/default/main_index.html | 23 +- .../themes/default/main_index_minimal.html | 24 + archivebox/themes/default/main_index_row.html | 22 + 6 files changed, 582 insertions(+), 104 deletions(-) create mode 100644 archivebox/themes/default/link_details.html create mode 100644 archivebox/themes/default/main_index_minimal.html create mode 100644 archivebox/themes/default/main_index_row.html diff --git a/archivebox/index/html.py b/archivebox/index/html.py index b793cea3..b6e8f985 100644 --- a/archivebox/index/html.py +++ b/archivebox/index/html.py @@ -1,6 +1,5 @@ __package__ = 'archivebox.index' -from string import Template from datetime import datetime from typing import List, Optional, Iterator, Mapping from pathlib import Path @@ -24,14 +23,16 @@ from ..config import ( VERSION, GIT_SHA, FOOTER_INFO, - ARCHIVE_DIR_NAME, HTML_INDEX_FILENAME, + STATIC_DIR_NAME, + ROBOTS_TXT_FILENAME, + FAVICON_FILENAME, + setup_django, ) -MAIN_INDEX_TEMPLATE = str(Path(TEMPLATES_DIR) / 'main_index.html') -MINIMAL_INDEX_TEMPLATE = str(Path(TEMPLATES_DIR) / 'main_index_minimal.html') -MAIN_INDEX_ROW_TEMPLATE = str(Path(TEMPLATES_DIR) / 'main_index_row.html') -LINK_DETAILS_TEMPLATE = str(Path(TEMPLATES_DIR) / 'link_details.html') +MAIN_INDEX_TEMPLATE = 'main_index.html' +MINIMAL_INDEX_TEMPLATE = 'main_index_minimal.html' +LINK_DETAILS_TEMPLATE = 'link_details.html' TITLE_LOADING_MSG = 'Not yet archived...' @@ -49,54 +50,33 @@ def parse_html_main_index(out_dir: Path=OUTPUT_DIR) -> Iterator[str]: yield line.split('"')[1] return () +@enforce_types +def write_html_main_index(links: List[Link], out_dir: Path=OUTPUT_DIR, finished: bool=False) -> None: + """write the html link index to a given path""" + + copy_and_overwrite(str(Path(TEMPLATES_DIR) / FAVICON_FILENAME), str(out_dir / FAVICON_FILENAME)) + copy_and_overwrite(str(Path(TEMPLATES_DIR) / ROBOTS_TXT_FILENAME), str(out_dir / ROBOTS_TXT_FILENAME)) + copy_and_overwrite(str(Path(TEMPLATES_DIR) / STATIC_DIR_NAME), str(out_dir / STATIC_DIR_NAME)) + + rendered_html = main_index_template(links, finished=finished) + atomic_write(str(out_dir / HTML_INDEX_FILENAME), rendered_html) + @enforce_types def main_index_template(links: List[Link], template: str=MAIN_INDEX_TEMPLATE) -> str: """render the template for the entire main index""" - return render_legacy_template(template, { + return render_django_template(template, { 'version': VERSION, 'git_sha': GIT_SHA, 'num_links': str(len(links)), 'date_updated': datetime.now().strftime('%Y-%m-%d'), 'time_updated': datetime.now().strftime('%Y-%m-%d %H:%M'), - 'rows': '\n'.join( - main_index_row_template(link) - for link in links - ), + 'links': [link._asdict(extended=True) for link in links], 'footer_info': FOOTER_INFO, }) -@enforce_types -def main_index_row_template(link: Link) -> str: - """render the template for an individual link row of the main index""" - - from ..extractors.wget import wget_output_path - - return render_legacy_template(MAIN_INDEX_ROW_TEMPLATE, { - **link._asdict(extended=True), - - # before pages are finished archiving, show loading msg instead of title - 'title': htmlencode( - link.title - or (link.base_url if link.is_archived else TITLE_LOADING_MSG) - ), - - # before pages are finished archiving, show fallback loading favicon - 'favicon_url': ( - str(Path(ARCHIVE_DIR_NAME) / link.timestamp / 'favicon.ico') - # if link['is_archived'] else 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=' - ), - - # before pages are finished archiving, show the details page instead - 'wget_url': urlencode(wget_output_path(link) or 'index.html'), - - # replace commas in tags with spaces, or file extension if it's static - 'tags': (link.tags or '') + (' {}'.format(link.extension) if link.is_static else ''), - }) - - ### Link Details Index @enforce_types @@ -114,7 +94,7 @@ def link_details_template(link: Link) -> str: link_info = link._asdict(extended=True) - return render_legacy_template(LINK_DETAILS_TEMPLATE, { + return render_django_template(LINK_DETAILS_TEMPLATE, { **link_info, **link_info['canonical'], 'title': htmlencode( @@ -134,17 +114,13 @@ def link_details_template(link: Link) -> str: 'oldest_archive_date': ts_to_date(link.oldest_archive_date), }) - @enforce_types -def render_legacy_template(template_path: str, context: Mapping[str, str]) -> str: +def render_django_template(template: str, context: Mapping[str, str]) -> str: """render a given html template string with the given template content""" + from django.template.loader import render_to_string - # will be replaced by django templates in the future - with open(template_path, 'r', encoding='utf-8') as template: - template_str = template.read() - return Template(template_str).substitute(**context) - - + setup_django(check_db=False) + return render_to_string(template, context) def snapshot_icons(snapshot) -> str: diff --git a/archivebox/themes/default/core/snapshot_list.html b/archivebox/themes/default/core/snapshot_list.html index 73761372..ce2b2faa 100644 --- a/archivebox/themes/default/core/snapshot_list.html +++ b/archivebox/themes/default/core/snapshot_list.html @@ -18,40 +18,27 @@ Original URL - - {% for link in object_list %} - - {{link.added}} - - {% if link.is_archived %} - - {% else %} - - {% endif %} - - {{link.title|default:'Loading...'}} - {{link.tags_str}} - - - - {{link.icons}} - - - {{link.url}} - - - {% endfor %} - - -
- - {% if page_obj.has_previous %} - « first - previous - {% endif %} - - - Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}. + + {% for link in object_list %} + {% include 'main_index_row.html' with link=link %} + {% endfor %} + + +
+ + {% if page_obj.has_previous %} + « first + previous + {% endif %} + + + Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}. + + + {% if page_obj.has_next %} + next + last » + {% endif %} {% if page_obj.has_next %} diff --git a/archivebox/themes/default/link_details.html b/archivebox/themes/default/link_details.html new file mode 100644 index 00000000..b1edcfe0 --- /dev/null +++ b/archivebox/themes/default/link_details.html @@ -0,0 +1,488 @@ + + + + {{title}} + + + + + +
+
+ +
+
+
+
+
+
Added
+ {{bookmarked_date}} +
+
+
First Archived
+ {{oldest_archive_date}} +
+
+
Last Checked
+ {{updated_date}} +
+
+
+
+
Type
+
{{extension}}
+
+
+
Tags
+
{{tags}}
+
+
+
Status
+
{{status}}
+
+
+
Saved
+ ✅ {{num_outputs}} +
+
+
Errors
+ ❌ {{num_failures}} +
+
+
Size
+ {{size}} +
+
+
+
+
🗃 Files
+ JSON | + WARC | + Media | + Git | + Favicon | + See all... +
+
+
+
+
+
+ +
+ + + +

Wget > WARC

+

archive/{{domain}}

+
+
+
+
+
+ +
+ + + +

Chrome > SingleFile

+

archive/singlefile.html

+
+
+
+
+
+ +
+ + + +

Archive.Org

+

web.archive.org/web/...

+
+
+
+
+
+ +
+ + + +

Original

+

{{domain}}

+
+
+
+
+
+
+ +
+ + + +

Chrome > PDF

+

archive/output.pdf

+
+
+
+
+
+ +
+ + + +

Chrome > Screenshot

+

archive/screenshot.png

+
+
+
+
+
+ +
+ + + +

Chrome > HTML

+

archive/output.html

+
+
+
+
+
+ +
+ + + +

Readability

+

archive/readability/...

+
+
+
+
+
+
+ +
+ + + +

mercury

+

archive/mercury/...

+
+
+
+
+
+
+ + + + + + + + diff --git a/archivebox/themes/default/main_index.html b/archivebox/themes/default/main_index.html index 11c6a9a8..3df77ff3 100644 --- a/archivebox/themes/default/main_index.html +++ b/archivebox/themes/default/main_index.html @@ -233,26 +233,7 @@ {% for link in links %} - - {{link.bookmarked_date}} - - {% if link.is_archived %} - - {% else %} - - {% endif %} - - {{link.title|default:'Loading...'}} - {{link.tags|default:''}} - - - - 📄 - {{link.num_outputs}} - - - {{link.url}} - + {% include 'main_index_row.html' with link=link %} {% endfor %} @@ -261,7 +242,7 @@
Archive created using ArchiveBox - version v{{VERSION}}   |   + version v{{version}}   |   Download index as JSON

{{FOOTER_INFO}} diff --git a/archivebox/themes/default/main_index_minimal.html b/archivebox/themes/default/main_index_minimal.html new file mode 100644 index 00000000..dcfaa23f --- /dev/null +++ b/archivebox/themes/default/main_index_minimal.html @@ -0,0 +1,24 @@ + + + + Archived Sites + + + + + + + + + + + + + + {% for link in links %} + {% include "main_index_row.html" with link=link %} + {% endfor %} + +
BookmarkedSaved Link ({{num_links}})FilesOriginal URL
+ + \ No newline at end of file diff --git a/archivebox/themes/default/main_index_row.html b/archivebox/themes/default/main_index_row.html new file mode 100644 index 00000000..5e21a8c1 --- /dev/null +++ b/archivebox/themes/default/main_index_row.html @@ -0,0 +1,22 @@ +{% load static %} + + + {% if link.bookmarked_date %} {{ link.bookmarked_date }} {% else %} {{ link.added }} {% endif %} + + {% if link.is_archived %} + + {% else %} + + {% endif %} + + {{link.title|default:'Loading...'}} + {% if link.tags_str != None %} {{link.tags_str|default:''}} {% else %} {{ link.tags|default:'' }} {% endif %} + + + + 📄 + {% if link.icons %} {{link.icons}} {% else %} {{ link.num_outputs}} {% endif %} + + + {{link.url}} + \ No newline at end of file