diff --git a/requirements.txt b/requirements.txt
index 07b53d2ad..2d434825d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -3,4 +3,5 @@ flask-babel
requests
lxml
pyyaml
+pygments
python-dateutil
diff --git a/searx/engines/searchcode_code.py b/searx/engines/searchcode_code.py
index 0f98352c1..c71832f46 100644
--- a/searx/engines/searchcode_code.py
+++ b/searx/engines/searchcode_code.py
@@ -10,7 +10,7 @@
from urllib import urlencode
from json import loads
-import cgi
+
# engine dependent config
categories = ['it']
@@ -20,6 +20,11 @@ paging = True
url = 'https://searchcode.com/'
search_url = url+'api/codesearch_I/?{query}&p={pageno}'
+# special code-endings which are not recognised by the file ending
+code_endings = {'cs': 'c#',
+ 'h': 'c',
+ 'hpp': 'cpp'}
+
# do search-request
def request(query, params):
@@ -39,27 +44,24 @@ def response(resp):
for result in search_results['results']:
href = result['url']
title = "" + result['name'] + " - " + result['filename']
- content = result['repo'] + "
"
+ repo = result['repo']
lines = dict()
for line, code in result['lines'].items():
lines[int(line)] = code
- content = content + '
' - content = content + str(line) + ' | ' - # Replace every two spaces with ' &nbps;' to keep formatting - # while allowing the browser to break the line if necessary - content = content + cgi.escape(code).replace('\t', ' ').replace(' ', ' ').replace(' ', ' ') - content = content + " |
could not load data!
')})}}$(this).off(a)}),$(".searx_init_map").on("click",function(a){var b=$(this).data("leaflet-target"),c=$(this).data("map-lon"),d=$(this).data("map-lat"),e=$(this).data("map-zoom"),f=$(this).data("map-boundingbox"),g=$(this).data("map-geojson");require(["leaflet-0.7.3.min"],function(){f&&(southWest=L.latLng(f[0],f[2]),northEast=L.latLng(f[1],f[3]),map_bounds=L.latLngBounds(southWest,northEast)),L.Icon.Default.imagePath="./static/themes/oscar/img/map";{var a=L.map(b),h="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",i='Map data © OpenStreetMap contributors',j=new L.TileLayer(h,{minZoom:1,maxZoom:19,attribution:i}),k="http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg",l='Map data © OpenStreetMap contributors | Tiles Courtesy of MapQuest{{ result.pretty_url }} cached
+ {% if result.publishedDate %}{{ result.publishedDate }}
{% endif %} +{% if result.img_src %}{% endif %}{% if result.content %}{{ result.content|safe }}
{% endif %}
{{ result.pretty_url }} cached
+ {% if result.publishedDate %}{{ result.publishedDate }}
{% endif %} +{% if result.img_src %}{% endif %}{% if result.content %}{{ result.content|safe }}
{% endif %}
{{ result.pretty_url }}
+{%- endmacro %} diff --git a/searx/templates/oscar/result_templates/code.html b/searx/templates/oscar/result_templates/code.html new file mode 100644 index 000000000..e608bb04f --- /dev/null +++ b/searx/templates/oscar/result_templates/code.html @@ -0,0 +1,12 @@ +{% from 'oscar/macros.html' import result_header, result_sub_header, result_footer, icon %} + +{{ result_header(result, favicons) }} +{{ result_sub_header(result) }} + +{% if result.content %}{{ result.content|safe }}
{% endif %} + +{% if result.repository %}{{ icon('file') }} {{ result.repository }}
{% endif %} + +{{ result.codelines|code_highlighter(result.code_language)|safe }} + +{{ result_footer(result) }} diff --git a/searx/templates/oscar/result_templates/default.html b/searx/templates/oscar/result_templates/default.html index 23af61f21..2be06642a 100644 --- a/searx/templates/oscar/result_templates/default.html +++ b/searx/templates/oscar/result_templates/default.html @@ -1,9 +1,7 @@ -{% from 'oscar/macros.html' import icon %} +{% from 'oscar/macros.html' import result_header, result_sub_header, result_footer, icon %} -{{ result.content|safe }}
{% endif %} - - -{{ result.engine }} -{{ result.pretty_url }}
+{{ result_footer(result) }} diff --git a/searx/templates/oscar/result_templates/images.html b/searx/templates/oscar/result_templates/images.html index 94627c9b5..7ad4e2435 100644 --- a/searx/templates/oscar/result_templates/images.html +++ b/searx/templates/oscar/result_templates/images.html @@ -1,3 +1,5 @@ +{% from 'oscar/macros.html' import draw_favicon %} +{{ result.pretty_url }}
+{{ result_footer(result) }} 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'] diff --git a/setup.py b/setup.py index d976a31f7..1c1a19ddf 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,7 @@ setup( 'requests', 'lxml', 'pyyaml', + 'pygments', 'setuptools', 'python-dateutil', ], diff --git a/versions.cfg b/versions.cfg index 2f5dae8ee..7f1734908 100644 --- a/versions.cfg +++ b/versions.cfg @@ -4,6 +4,7 @@ Flask = 0.10.1 Flask-Babel = 0.9 Jinja2 = 2.7.2 MarkupSafe = 0.18 +Pygments = 2.0.1 WebOb = 1.3.1 WebTest = 2.0.11 Werkzeug = 0.9.4