From c971e00c9cd746085a5a9f8605a45f231e6558f0 Mon Sep 17 00:00:00 2001 From: Cristian Date: Wed, 1 Jul 2020 12:23:59 -0500 Subject: [PATCH] feat: Add stdout from process to the template --- archivebox/config/__init__.py | 10 + archivebox/core/views.py | 25 +- archivebox/themes/default/add_links.html | 425 ++++++++++++----------- archivebox/util.py | 24 ++ 4 files changed, 276 insertions(+), 208 deletions(-) diff --git a/archivebox/config/__init__.py b/archivebox/config/__init__.py index 0cb41563..fa979211 100644 --- a/archivebox/config/__init__.py +++ b/archivebox/config/__init__.py @@ -133,6 +133,16 @@ DEFAULT_CLI_COLORS = { } ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()} +COLOR_DICT = { + '00': [(0, 0, 0), (0, 0, 0)], + '31': [(255, 0, 0), (128, 0, 0)], + '32': [(0, 200, 0), (0, 128, 0)], + '33': [(255, 255, 0), (128, 128, 0)], + '34': [(0, 0, 255), (0, 0, 128)], + '35': [(255, 0, 255), (128, 0, 128)], + '36': [(0, 255, 255), (0, 128, 128)], +} + STATICFILE_EXTENSIONS = { # 99.999% of the time, URLs ending in these extensions are static files # that can be downloaded as-is, not html pages that need to be rendered diff --git a/archivebox/core/views.py b/archivebox/core/views.py index 84731e51..b7911674 100644 --- a/archivebox/core/views.py +++ b/archivebox/core/views.py @@ -8,6 +8,9 @@ from django.conf import settings from core.models import Snapshot +from contextlib import redirect_stdout +from io import StringIO + from ..index import load_main_index, load_main_index_meta from ..config import ( OUTPUT_DIR, @@ -16,7 +19,7 @@ from ..config import ( PUBLIC_INDEX, PUBLIC_SNAPSHOTS, ) -from ..util import base_url +from ..util import base_url, ansi_to_html from .. main import add @@ -55,12 +58,20 @@ class AddLinks(View): def post(self, request): url = request.POST['url'] print(f'[+] Adding URL: {url}') - add( - import_str=url, - update_all=False, - out_dir=OUTPUT_DIR, - ) - return redirect('/') + add_stdout = StringIO() + with redirect_stdout(add_stdout): + extracted_links = add( + import_str=url, + update_all=False, + out_dir=OUTPUT_DIR, + ) + print(add_stdout.getvalue()) + + context = { + "stdout": ansi_to_html(add_stdout.getvalue()) + } + + return render(template_name=self.template, request=request, context=context) class LinkDetails(View): diff --git a/archivebox/themes/default/add_links.html b/archivebox/themes/default/add_links.html index dd144834..db09322f 100644 --- a/archivebox/themes/default/add_links.html +++ b/archivebox/themes/default/add_links.html @@ -2,208 +2,231 @@ - - Archived Sites - - - - - - - - - -
-
- -
-
-
-

-
{% csrf_token %} - Add new links...
-
- -
-
- - + tr td a.favicon img { + padding-left: 6px; + padding-right: 12px; + vertical-align: -4px; + } + tr td a.title { + font-size: 1.4em; + text-decoration: none; + color: black; + } + tr td a.title small { + background-color: #efefef; + border-radius: 4px; + float: right; + } + input[type="search"]::-webkit-search-cancel-button { + -webkit-appearance: searchfield-cancel-button; + } + .title-col { + text-align: left; + } + .title-col a { + color: black; + } + + + + + + + + +
+
+ +
+
+
+ {{ stdout | safe }} +

+
+ {% csrf_token %} Add new links...
+
+ +
+ + Go back to Snapshot list +
+ diff --git a/archivebox/util.py b/archivebox/util.py index 87c98263..50511313 100644 --- a/archivebox/util.py +++ b/archivebox/util.py @@ -20,6 +20,7 @@ from .config import ( CHECK_SSL_VALIDITY, WGET_USER_AGENT, CHROME_OPTIONS, + COLOR_DICT ) try: @@ -69,6 +70,8 @@ URL_REGEX = re.compile( re.IGNORECASE, ) +COLOR_REGEX = re.compile(r'\[(?P\d+)(;(?P\d+)(;(?P\d+))?)?m') + def enforce_types(func): """ @@ -195,6 +198,27 @@ def chrome_args(**options) -> List[str]: return cmd_args +def ansi_to_html(text): + """ + Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html + """ + TEMPLATE = '
' + text = text.replace('[m', '
') + + def single_sub(match): + argsdict = match.groupdict() + if argsdict['arg_3'] is None: + if argsdict['arg_2'] is None: + bold, color = 0, argsdict['arg_1'] + else: + bold, color = argsdict['arg_1'], argsdict['arg_2'] + else: + bold, color = argsdict['arg_3'], argsdict['arg_2'] + + return TEMPLATE.format(COLOR_DICT[color][0]) + + return COLOR_REGEX.sub(single_sub, text) + class ExtendedEncoder(pyjson.JSONEncoder): """