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/engines/searchcode_code.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'searx/engines/searchcode_code.py') diff --git a/searx/engines/searchcode_code.py b/searx/engines/searchcode_code.py index 2ba0e52f1..ac9647fcd 100644 --- a/searx/engines/searchcode_code.py +++ b/searx/engines/searchcode_code.py @@ -21,6 +21,18 @@ paging = True url = 'https://searchcode.com/' search_url = url+'api/codesearch_I/?{query}&p={pageno}' +code_endings = {'c': 'c', + 'css': 'css', + 'cpp': 'cpp', + 'c++': 'cpp', + 'h': 'c', + 'html': 'html', + 'hpp': 'cpp', + 'js': 'js', + 'lua': 'lua', + 'php': 'php', + 'py': 'python'} + # do search-request def request(query, params): @@ -40,26 +52,22 @@ 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 + '
'
-        for line, code in sorted(lines.items()):
-            content = content + '"
-            
-        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 + "
" - + code_language = code_endings.get(result['filename'].split('.')[-1].lower(), None) + # append result results.append({'url': href, 'title': title, - 'content': content}) + 'content': '', + 'repository': repo, + 'codelines': sorted(lines.items()), + 'code_language': code_language, + 'template': 'code.html'}) # return results return results -- 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/engines/searchcode_code.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) (limited to 'searx/engines/searchcode_code.py') diff --git a/searx/engines/searchcode_code.py b/searx/engines/searchcode_code.py index ac9647fcd..c71832f46 100644 --- a/searx/engines/searchcode_code.py +++ b/searx/engines/searchcode_code.py @@ -10,8 +10,7 @@ from urllib import urlencode from json import loads -import cgi -import re + # engine dependent config categories = ['it'] @@ -21,17 +20,10 @@ paging = True url = 'https://searchcode.com/' search_url = url+'api/codesearch_I/?{query}&p={pageno}' -code_endings = {'c': 'c', - 'css': 'css', - 'cpp': 'cpp', - 'c++': 'cpp', +# special code-endings which are not recognised by the file ending +code_endings = {'cs': 'c#', 'h': 'c', - 'html': 'html', - 'hpp': 'cpp', - 'js': 'js', - 'lua': 'lua', - 'php': 'php', - 'py': 'python'} + 'hpp': 'cpp'} # do search-request @@ -45,7 +37,7 @@ def request(query, params): # get response from search-request def response(resp): results = [] - + search_results = loads(resp.text) # parse results @@ -53,12 +45,14 @@ def response(resp): href = result['url'] title = "" + result['name'] + " - " + result['filename'] repo = result['repo'] - + lines = dict() for line, code in result['lines'].items(): lines[int(line)] = code - code_language = code_endings.get(result['filename'].split('.')[-1].lower(), None) + code_language = code_endings.get( + result['filename'].split('.')[-1].lower(), + result['filename'].split('.')[-1].lower()) # append result results.append({'url': href, -- cgit v1.2.3