From 7adb17452d8c845f46858b4cebe8198988edfdbc Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Sat, 20 Dec 2014 23:33:03 +0100 Subject: [enh] add result_templates/code.html --- searx/webapp.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 915fb3564..11db1bf2e 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -33,6 +33,9 @@ from flask import ( redirect, send_from_directory ) from flask.ext.babel import Babel, gettext, format_date +from pygments import highlight +from pygments.lexers import get_lexer_by_name +from pygments.formatters import HtmlFormatter from searx import settings, searx_dir from searx.engines import ( categories, engines, get_engines_stats, engine_shortcuts @@ -90,6 +93,49 @@ def get_locale(): return locale +# code-highlighter +@app.template_filter('code_highlighter') +def code_highlighter(codelines, language=None): + if not language: + language = 'text' + + # find lexer by programing language + lexer = get_lexer_by_name(language, stripall=True) + + html_code = '' + tmp_code = '' + last_line = None + + # parse lines + for line, code in codelines: + if not last_line: + line_code_start = line + + # new codeblock is detected + if last_line != None and\ + last_line +1 != line: + + # highlight last codepart + formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start) + html_code = html_code + highlight(tmp_code, lexer, formatter) + + # reset conditions for next codepart + tmp_code = '' + line_code_start = line + + # add codepart + tmp_code += code + '\n' + + # update line + last_line = line + + # highlight last codepart + formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start) + html_code = html_code + highlight(tmp_code, lexer, formatter) + + return html_code + + def get_base_url(): if settings['server']['base_url']: hostname = settings['server']['base_url'] -- cgit v1.2.3 From af8dac93a8acff5042b7b399c38e348f0bdc32ad Mon Sep 17 00:00:00 2001 From: Thomas Pointhuber Date: Mon, 22 Dec 2014 16:26:45 +0100 Subject: [enh] fix pep8, improve syntax highlighting --- searx/webapp.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'searx/webapp.py') diff --git a/searx/webapp.py b/searx/webapp.py index 11db1bf2e..ab1e2747f 100644 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -99,9 +99,13 @@ def code_highlighter(codelines, language=None): if not language: language = 'text' - # find lexer by programing language - lexer = get_lexer_by_name(language, stripall=True) - + try: + # find lexer by programing language + lexer = get_lexer_by_name(language, stripall=True) + except: + # if lexer is not found, using default one + lexer = get_lexer_by_name('text', stripall=True) + html_code = '' tmp_code = '' last_line = None @@ -112,20 +116,21 @@ def code_highlighter(codelines, language=None): line_code_start = line # new codeblock is detected - if last_line != None and\ - last_line +1 != line: + if last_line is not None and\ + last_line + 1 != line: # highlight last codepart - formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start) + formatter = HtmlFormatter(linenos='inline', + linenostart=line_code_start) html_code = html_code + highlight(tmp_code, lexer, formatter) - + # reset conditions for next codepart tmp_code = '' line_code_start = line # add codepart tmp_code += code + '\n' - + # update line last_line = line -- cgit v1.2.3