mirror of
https://github.com/searx/searx
synced 2024-12-13 09:17:31 +01:00
[enh] fix pep8, improve syntax highlighting
This commit is contained in:
parent
d810763107
commit
af8dac93a8
@ -10,8 +10,7 @@
|
|||||||
|
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
from json import loads
|
from json import loads
|
||||||
import cgi
|
|
||||||
import re
|
|
||||||
|
|
||||||
# engine dependent config
|
# engine dependent config
|
||||||
categories = ['it']
|
categories = ['it']
|
||||||
@ -21,17 +20,10 @@ paging = True
|
|||||||
url = 'https://searchcode.com/'
|
url = 'https://searchcode.com/'
|
||||||
search_url = url+'api/codesearch_I/?{query}&p={pageno}'
|
search_url = url+'api/codesearch_I/?{query}&p={pageno}'
|
||||||
|
|
||||||
code_endings = {'c': 'c',
|
# special code-endings which are not recognised by the file ending
|
||||||
'css': 'css',
|
code_endings = {'cs': 'c#',
|
||||||
'cpp': 'cpp',
|
|
||||||
'c++': 'cpp',
|
|
||||||
'h': 'c',
|
'h': 'c',
|
||||||
'html': 'html',
|
'hpp': 'cpp'}
|
||||||
'hpp': 'cpp',
|
|
||||||
'js': 'js',
|
|
||||||
'lua': 'lua',
|
|
||||||
'php': 'php',
|
|
||||||
'py': 'python'}
|
|
||||||
|
|
||||||
|
|
||||||
# do search-request
|
# do search-request
|
||||||
@ -45,7 +37,7 @@ def request(query, params):
|
|||||||
# get response from search-request
|
# get response from search-request
|
||||||
def response(resp):
|
def response(resp):
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
search_results = loads(resp.text)
|
search_results = loads(resp.text)
|
||||||
|
|
||||||
# parse results
|
# parse results
|
||||||
@ -53,12 +45,14 @@ def response(resp):
|
|||||||
href = result['url']
|
href = result['url']
|
||||||
title = "" + result['name'] + " - " + result['filename']
|
title = "" + result['name'] + " - " + result['filename']
|
||||||
repo = result['repo']
|
repo = result['repo']
|
||||||
|
|
||||||
lines = dict()
|
lines = dict()
|
||||||
for line, code in result['lines'].items():
|
for line, code in result['lines'].items():
|
||||||
lines[int(line)] = code
|
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
|
# append result
|
||||||
results.append({'url': href,
|
results.append({'url': href,
|
||||||
|
@ -31,15 +31,18 @@ def request(query, params):
|
|||||||
# get response from search-request
|
# get response from search-request
|
||||||
def response(resp):
|
def response(resp):
|
||||||
results = []
|
results = []
|
||||||
|
|
||||||
search_results = loads(resp.text)
|
search_results = loads(resp.text)
|
||||||
|
|
||||||
# parse results
|
# parse results
|
||||||
for result in search_results['results']:
|
for result in search_results['results']:
|
||||||
href = result['url']
|
href = result['url']
|
||||||
title = "[" + result['type'] + "] " + result['namespace'] + " " + result['name']
|
title = "[" + result['type'] + "] " +\
|
||||||
content = '<span class="highlight">[' + result['type'] + "] " + result['name'] + " " + result['synopsis'] + "</span><br />" + result['description']
|
result['namespace'] + " " + result['name']
|
||||||
|
content = '<span class="highlight">[' + result['type'] + "] " +\
|
||||||
|
result['name'] + " " + result['synopsis'] +\
|
||||||
|
"</span><br />" + result['description']
|
||||||
|
|
||||||
# append result
|
# append result
|
||||||
results.append({'url': href,
|
results.append({'url': href,
|
||||||
'title': title,
|
'title': title,
|
||||||
|
@ -99,9 +99,13 @@ def code_highlighter(codelines, language=None):
|
|||||||
if not language:
|
if not language:
|
||||||
language = 'text'
|
language = 'text'
|
||||||
|
|
||||||
# find lexer by programing language
|
try:
|
||||||
lexer = get_lexer_by_name(language, stripall=True)
|
# 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 = ''
|
html_code = ''
|
||||||
tmp_code = ''
|
tmp_code = ''
|
||||||
last_line = None
|
last_line = None
|
||||||
@ -112,20 +116,21 @@ def code_highlighter(codelines, language=None):
|
|||||||
line_code_start = line
|
line_code_start = line
|
||||||
|
|
||||||
# new codeblock is detected
|
# new codeblock is detected
|
||||||
if last_line != None and\
|
if last_line is not None and\
|
||||||
last_line +1 != line:
|
last_line + 1 != line:
|
||||||
|
|
||||||
# highlight last codepart
|
# 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)
|
html_code = html_code + highlight(tmp_code, lexer, formatter)
|
||||||
|
|
||||||
# reset conditions for next codepart
|
# reset conditions for next codepart
|
||||||
tmp_code = ''
|
tmp_code = ''
|
||||||
line_code_start = line
|
line_code_start = line
|
||||||
|
|
||||||
# add codepart
|
# add codepart
|
||||||
tmp_code += code + '\n'
|
tmp_code += code + '\n'
|
||||||
|
|
||||||
# update line
|
# update line
|
||||||
last_line = line
|
last_line = line
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user