summaryrefslogtreecommitdiff
path: root/searx/webapp.py
diff options
context:
space:
mode:
authorAdam Tauber <asciimoo@gmail.com>2015-01-15 18:26:00 +0100
committerAdam Tauber <asciimoo@gmail.com>2015-01-15 18:26:00 +0100
commita04fafd419bd465c533cc223e4036e1ca0997d5f (patch)
treef739d7d4567fcc8b1691bbef24deb9cb13ce7e14 /searx/webapp.py
parentc5599e3c7c790ac2674b68f99919c0ec284327a3 (diff)
parent400b54191c590663f0cfe91045f70a5d9223aa19 (diff)
downloadsearxng-a04fafd419bd465c533cc223e4036e1ca0997d5f.tar.gz
searxng-a04fafd419bd465c533cc223e4036e1ca0997d5f.zip
Merge branch 'code_results' of https://github.com/pointhi/searx into pointhi-code_results
Conflicts: searx/static/themes/default/css/style.css searx/static/themes/oscar/css/oscar.min.css searx/templates/oscar/result_templates/torrent.html
Diffstat (limited to 'searx/webapp.py')
-rw-r--r--searx/webapp.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/searx/webapp.py b/searx/webapp.py
index 8ed4cc7c1..57551a655 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
@@ -101,6 +104,54 @@ def get_locale():
return locale
+# code-highlighter
+@app.template_filter('code_highlighter')
+def code_highlighter(codelines, language=None):
+ if not language:
+ language = 'text'
+
+ 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
+
+ # parse lines
+ for line, code in codelines:
+ if not last_line:
+ line_code_start = line
+
+ # new codeblock is detected
+ if last_line is not 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']