on master help sign in
[enh] fix pep8, improve syntax highlighting
af8dac9Thomas Pointhuber
searx/engines/searchcode_code.py+9−15
@@ -10,8 +10,7 @@
10
10
11from urllib import urlencode
11from urllib import urlencode
12from json import loads
12from json import loads
13−import cgi
13+
14−import re
15
14
16# engine dependent config
15# engine dependent config
17categories = ['it']
16categories = ['it']
@@ -21,17 +20,10 @@ paging = True
21url = 'https://searchcode.com/'
20url = 'https://searchcode.com/'
22search_url = url+'api/codesearch_I/?{query}&p={pageno}'
21search_url = url+'api/codesearch_I/?{query}&p={pageno}'
23
22
23+# special code-endings which are not recognised by the file ending
24−code_endings = {'c': 'c',
24+code_endings = {'cs': 'c#',
25− 'css': 'css',
26− 'cpp': 'cpp',
27− 'c++': 'cpp',
28 'h': 'c',
25 'h': 'c',
29− 'html': 'html',
30− 'hpp': 'cpp',
26+ 'hpp': 'cpp'}
31− 'js': 'js',
32− 'lua': 'lua',
33− 'php': 'php',
34− 'py': 'python'}
35
27
36
28
37# do search-request
29# do search-request
@@ -45,7 +37,7 @@ def request(query, params):
45# get response from search-request
37# get response from search-request
46def response(resp):
38def response(resp):
47 results = []
39 results = []
48−
40+
49 search_results = loads(resp.text)
41 search_results = loads(resp.text)
50
42
51 # parse results
43 # parse results
@@ -53,12 +45,14 @@ def response(resp):
53 href = result['url']
45 href = result['url']
54 title = "" + result['name'] + " - " + result['filename']
46 title = "" + result['name'] + " - " + result['filename']
55 repo = result['repo']
47 repo = result['repo']
56−
48+
57 lines = dict()
49 lines = dict()
58 for line, code in result['lines'].items():
50 for line, code in result['lines'].items():
59 lines[int(line)] = code
51 lines[int(line)] = code
60
52
61− code_language = code_endings.get(result['filename'].split('.')[-1].lower(), None)
53+ code_language = code_endings.get(
54+ result['filename'].split('.')[-1].lower(),
55+ result['filename'].split('.')[-1].lower())
62
56
63 # append result
57 # append result
64 results.append({'url': href,
58 results.append({'url': href,
searx/engines/searchcode_doc.py+7−4
@@ -31,15 +31,18 @@ def request(query, params):
31# get response from search-request
31# get response from search-request
32def response(resp):
32def response(resp):
33 results = []
33 results = []
34−
34+
35 search_results = loads(resp.text)
35 search_results = loads(resp.text)
36
36
37 # parse results
37 # parse results
38 for result in search_results['results']:
38 for result in search_results['results']:
39 href = result['url']
39 href = result['url']
40+ title = "[" + result['type'] + "] " +\
40− title = "[" + result['type'] + "] " + result['namespace'] + " " + result['name']
41+ result['namespace'] + " " + result['name']
41− content = '<span class="highlight">[' + result['type'] + "] " + result['name'] + " " + result['synopsis'] + "</span><br />" + result['description']
42+ content = '<span class="highlight">[' + result['type'] + "] " +\
42−
43+ result['name'] + " " + result['synopsis'] +\
44+ "</span><br />" + result['description']
45+
43 # append result
46 # append result
44 results.append({'url': href,
47 results.append({'url': href,
45 'title': title,
48 'title': title,
searx/webapp.py+13−8
@@ -99,9 +99,13 @@ def code_highlighter(codelines, language=None):
99 if not language:
99 if not language:
100 language = 'text'
100 language = 'text'
101
101
102+ try:
102− # find lexer by programing language
103+ # find lexer by programing language
103− lexer = get_lexer_by_name(language, stripall=True)
104+ lexer = get_lexer_by_name(language, stripall=True)
104−
105+ except:
106+ # if lexer is not found, using default one
107+ lexer = get_lexer_by_name('text', stripall=True)
108+
105 html_code = ''
109 html_code = ''
106 tmp_code = ''
110 tmp_code = ''
107 last_line = None
111 last_line = None
@@ -112,20 +116,21 @@ def code_highlighter(codelines, language=None):
112 line_code_start = line
116 line_code_start = line
113
117
114 # new codeblock is detected
118 # new codeblock is detected
115− if last_line != None and\
119+ if last_line is not None and\
116− last_line +1 != line:
120+ last_line + 1 != line:
117
121
118 # highlight last codepart
122 # highlight last codepart
119− formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start)
123+ formatter = HtmlFormatter(linenos='inline',
124+ linenostart=line_code_start)
120 html_code = html_code + highlight(tmp_code, lexer, formatter)
125 html_code = html_code + highlight(tmp_code, lexer, formatter)
121−
126+
122 # reset conditions for next codepart
127 # reset conditions for next codepart
123 tmp_code = ''
128 tmp_code = ''
124 line_code_start = line
129 line_code_start = line
125
130
126 # add codepart
131 # add codepart
127 tmp_code += code + '\n'
132 tmp_code += code + '\n'
128−
133+
129 # update line
134 # update line
130 last_line = line
135 last_line = line
131
136