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..5efa79cd 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 @@ -54,13 +57,24 @@ 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('/') + if url: + print(f'[+] Adding URL: {url}') + 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()) + } + else: + context = {"stdout": "Please enter a URL"} + + 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..6c625594 100644 --- a/archivebox/themes/default/add_links.html +++ b/archivebox/themes/default/add_links.html @@ -197,6 +197,7 @@
+ {{ stdout | safe }}

{% csrf_token %} Add new links...
@@ -204,6 +205,8 @@
+ + 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): """