1
0
mirror of https://github.com/pirate/ArchiveBox.git synced 2025-08-24 07:03:03 +02:00

Merge pull request #349 from cdvv7788/ui-enhancements

This commit is contained in:
Nick Sweeting
2020-07-02 02:56:05 -04:00
committed by GitHub
4 changed files with 59 additions and 8 deletions

View File

@@ -133,6 +133,16 @@ DEFAULT_CLI_COLORS = {
} }
ANSI = {k: '' for k in DEFAULT_CLI_COLORS.keys()} 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 = { STATICFILE_EXTENSIONS = {
# 99.999% of the time, URLs ending in these extensions are static files # 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 # that can be downloaded as-is, not html pages that need to be rendered

View File

@@ -8,6 +8,9 @@ from django.conf import settings
from core.models import Snapshot 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 ..index import load_main_index, load_main_index_meta
from ..config import ( from ..config import (
OUTPUT_DIR, OUTPUT_DIR,
@@ -16,7 +19,7 @@ from ..config import (
PUBLIC_INDEX, PUBLIC_INDEX,
PUBLIC_SNAPSHOTS, PUBLIC_SNAPSHOTS,
) )
from ..util import base_url from ..util import base_url, ansi_to_html
from .. main import add from .. main import add
@@ -54,13 +57,24 @@ class AddLinks(View):
def post(self, request): def post(self, request):
url = request.POST['url'] url = request.POST['url']
print(f'[+] Adding URL: {url}') if url:
add( print(f'[+] Adding URL: {url}')
import_str=url, add_stdout = StringIO()
update_all=False, with redirect_stdout(add_stdout):
out_dir=OUTPUT_DIR, extracted_links = add(
) import_str=url,
return redirect('/') 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): class LinkDetails(View):

View File

@@ -197,6 +197,7 @@
</div> </div>
</header> </header>
<center> <center>
{{ stdout | safe }}
<br/><br/> <br/><br/>
<form action="?" method="POST">{% csrf_token %} <form action="?" method="POST">{% csrf_token %}
Add new links...<br/> Add new links...<br/>
@@ -204,6 +205,8 @@
<button role="submit">Add</button> <button role="submit">Add</button>
</form> </form>
</center> </center>
<a href="{% url 'admin:core_snapshot_changelist' %}">Go back to Snapshot list</a>
</body> </body>
</html> </html>

View File

@@ -20,6 +20,7 @@ from .config import (
CHECK_SSL_VALIDITY, CHECK_SSL_VALIDITY,
WGET_USER_AGENT, WGET_USER_AGENT,
CHROME_OPTIONS, CHROME_OPTIONS,
COLOR_DICT
) )
try: try:
@@ -69,6 +70,8 @@ URL_REGEX = re.compile(
re.IGNORECASE, re.IGNORECASE,
) )
COLOR_REGEX = re.compile(r'\[(?P<arg_1>\d+)(;(?P<arg_2>\d+)(;(?P<arg_3>\d+))?)?m')
def enforce_types(func): def enforce_types(func):
""" """
@@ -195,6 +198,27 @@ def chrome_args(**options) -> List[str]:
return cmd_args return cmd_args
def ansi_to_html(text):
"""
Based on: https://stackoverflow.com/questions/19212665/python-converting-ansi-color-codes-to-html
"""
TEMPLATE = '<span style="color: rgb{}"><br>'
text = text.replace('[m', '</span>')
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): class ExtendedEncoder(pyjson.JSONEncoder):
""" """