euscanwww: Added ansi to html helper
Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
		| @@ -160,7 +160,7 @@ | ||||
|   <dd> | ||||
|     <p>Date: {{ log.datetime }} | ||||
|     <pre class="log"> | ||||
|       {{ log.result }} | ||||
|       {{ log.result|ansi_to_html|safe }} | ||||
|     </pre> | ||||
|   </dd> | ||||
|   {% endif %} | ||||
|   | ||||
| @@ -3,8 +3,11 @@ from django.conf import settings | ||||
|  | ||||
| from euscan.version import is_version_type_stable, get_version_type | ||||
|  | ||||
| from djeuscan.utils import plaintext2html | ||||
|  | ||||
| register = template.Library() | ||||
|  | ||||
|  | ||||
| @register.inclusion_tag('euscan/_packages.html', takes_context=True) | ||||
| def packages(context, pkgs): | ||||
|     context['packages'] = pkgs | ||||
| @@ -66,3 +69,8 @@ def is_stable(version_type): | ||||
| @register.filter | ||||
| def version_type(version): | ||||
|     return get_version_type(version) | ||||
|  | ||||
|  | ||||
| @register.filter | ||||
| def ansi_to_html(text): | ||||
|     return plaintext2html(text) | ||||
|   | ||||
							
								
								
									
										89
									
								
								euscanwww/djeuscan/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								euscanwww/djeuscan/utils.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,89 @@ | ||||
| import re | ||||
| import cgi | ||||
|  | ||||
| colorcodes = { | ||||
|     'bold': ('\033[1m', '\033[22m'), | ||||
|     'cyan': ('\033[36m', '\033[39m'), | ||||
|     'blue': ('\033[34m', '\033[39m'), | ||||
|     'red': ('\033[31m', '\033[39m'), | ||||
|     'magenta': ('\033[35m', '\033[39m'), | ||||
|     'green': ('\033[32m', '\033[39m'), | ||||
|     'underline': ('\033[4m', '\033[24m'), | ||||
| } | ||||
|  | ||||
|  | ||||
| def recolor(color, text): | ||||
|     regexp = "(?:%s)(.*?)(?:%s)" % colorcodes[color] | ||||
|     regexp = regexp.replace('[', r'\[') | ||||
|     return re.sub( | ||||
|         regexp, r'''<span style="color: %s">\1</span>''' % color, text | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def bold(text): | ||||
|     regexp = "(?:%s)(.*?)(?:%s)" % colorcodes['bold'] | ||||
|     regexp = regexp.replace('[', r'\[') | ||||
|     return re.sub(regexp, r'<span style="font-weight:bold">\1</span>', text) | ||||
|  | ||||
|  | ||||
| def underline(text): | ||||
|     regexp = "(?:%s)(.*?)(?:%s)" % colorcodes['underline'] | ||||
|     regexp = regexp.replace('[', r'\[') | ||||
|     return re.sub( | ||||
|         regexp, r'<span style="text-decoration: underline">\1</span>', text | ||||
|     ) | ||||
|  | ||||
|  | ||||
| def removebells(text): | ||||
|     return text.replace('\07', '') | ||||
|  | ||||
|  | ||||
| def removebackspaces(text): | ||||
|     backspace_or_eol = r'(.\010)|(\033\[K)' | ||||
|     n = 1 | ||||
|     while n > 0: | ||||
|         text, n = re.subn(backspace_or_eol, '', text, 1) | ||||
|     return text | ||||
|  | ||||
|  | ||||
| def plaintext2html(text, tabstop=4): | ||||
|     def do_sub(m): | ||||
|         c = m.groupdict() | ||||
|         if c['htmlchars']: | ||||
|             return cgi.escape(c['htmlchars']) | ||||
|         if c['lineend']: | ||||
|             return '<br>' | ||||
|         elif c['space']: | ||||
|             t = m.group().replace('\t', ' ' * tabstop) | ||||
|             t = t.replace(' ', ' ') | ||||
|             return t | ||||
|         elif c['space'] == '\t': | ||||
|             return ' ' * tabstop | ||||
|         else: | ||||
|             url = m.group('protocal') | ||||
|             if url.startswith(' '): | ||||
|                 prefix = ' ' | ||||
|                 url = url[1:] | ||||
|             else: | ||||
|                 prefix = '' | ||||
|             last = m.groups()[-1] | ||||
|             if last in ['\n', '\r', '\r\n']: | ||||
|                 last = '<br>' | ||||
|             return '%s%s' % (prefix, url) | ||||
|     re_string = re.compile( | ||||
|         r'(?P<htmlchars>[<&>])|(?P<space>^[ \t]+)|(?P<lineend>\r\n|\r|\n)|' | ||||
|         r'(?P<protocal>(^|\s)((http|ftp)://.*?))(\s|$)', | ||||
|         re.S | re.M | re.I | ||||
|     ) | ||||
|     result = re.sub(re_string, do_sub, text) | ||||
|     result = recolor('cyan', result) | ||||
|     result = recolor('blue', result) | ||||
|     result = recolor('red', result) | ||||
|     result = recolor('magenta', result) | ||||
|     result = recolor('green', result) | ||||
|     result = bold(result) | ||||
|     result = underline(result) | ||||
|     result = removebells(result) | ||||
|     result = removebackspaces(result) | ||||
|  | ||||
|     return result | ||||
| @@ -9,6 +9,7 @@ import portage | ||||
| from portage.output import EOutput, TermProgressBar | ||||
| from gentoolkit import pprinter as pp | ||||
|  | ||||
|  | ||||
| class ProgressHandler(object): | ||||
|     def __init__(self, progress_bar): | ||||
|         self.curval = 0 | ||||
| @@ -72,6 +73,7 @@ def transform_url(config, cpv, url): | ||||
|         url = to_ebuild_uri(cpv, url) | ||||
|     return url | ||||
|  | ||||
|  | ||||
| def to_ebuild_uri(cpv, url): | ||||
|     cat, pkg, ver, rev = portage.catpkgsplit(cpv) | ||||
|     p = '%s-%s' % (pkg, ver) | ||||
| @@ -90,6 +92,7 @@ def to_ebuild_uri(cpv, url): | ||||
|         url = url.replace(src, '${%s}' % dst) | ||||
|     return url | ||||
|  | ||||
|  | ||||
| def to_mirror(url): | ||||
|     mirrors = portage.settings.thirdpartymirrors() | ||||
|     for mirror_name in mirrors: | ||||
| @@ -113,7 +116,7 @@ class EOutputMem(EOutput): | ||||
|         self.out = StringIO() | ||||
|  | ||||
|     def getvalue(self): | ||||
|         return clean_colors(self.out.getvalue()) | ||||
|         return self.out.getvalue() | ||||
|  | ||||
|     def _write(self, f, msg): | ||||
|         super(EOutputMem, self)._write(self.out, msg) | ||||
| @@ -173,9 +176,11 @@ class EuscanOutput(object): | ||||
|         from euscan.version import get_version_type | ||||
|  | ||||
|         cpv = '%s-%s' % (cp, version) | ||||
|         urls = ' '.join(transform_url(self.config, cpv, url) for url in urls.split()) | ||||
|         urls = ' '.join( | ||||
|             transform_url(self.config, cpv, url) for url in urls.split() | ||||
|         ) | ||||
|  | ||||
|         if self.config['format'] in ['json']: | ||||
|         if self.config['format'] in ['json', 'dict']: | ||||
|             _curr = self.queries[self.current_query] | ||||
|             _curr["result"].append( | ||||
|                 { | ||||
| @@ -195,8 +200,7 @@ class EuscanOutput(object): | ||||
|  | ||||
|     def metadata(self, key, value, show=True): | ||||
|         if self.config["format"]: | ||||
|             self.queries[self.current_query]["metadata"][key] = \ | ||||
|                 clean_colors(value) | ||||
|             self.queries[self.current_query]["metadata"][key] = value | ||||
|         elif show: | ||||
|             print "%s: %s" % (key.capitalize(), value) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user