forked from zaclys/searxng
[enh] fix pep8, improve syntax highlighting
This commit is contained in:
parent
d810763107
commit
af8dac93a8
|
@ -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
|
||||
|
@ -58,7 +50,9 @@ def response(resp):
|
|||
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,
|
||||
|
|
|
@ -37,8 +37,11 @@ def response(resp):
|
|||
# parse results
|
||||
for result in search_results['results']:
|
||||
href = result['url']
|
||||
title = "[" + result['type'] + "] " + result['namespace'] + " " + result['name']
|
||||
content = '<span class="highlight">[' + result['type'] + "] " + result['name'] + " " + result['synopsis'] + "</span><br />" + result['description']
|
||||
title = "[" + result['type'] + "] " +\
|
||||
result['namespace'] + " " + result['name']
|
||||
content = '<span class="highlight">[' + result['type'] + "] " +\
|
||||
result['name'] + " " + result['synopsis'] +\
|
||||
"</span><br />" + result['description']
|
||||
|
||||
# append result
|
||||
results.append({'url': href,
|
||||
|
|
|
@ -99,8 +99,12 @@ 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 = ''
|
||||
|
@ -112,11 +116,12 @@ 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
|
||||
|
|
Loading…
Reference in New Issue