on master help sign in
[enh] fix pep8, improve syntax highlighting
commitaf8dac93a8acff5042b7b399c38e348f0bdc32ad authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateMon 22 Dec 2014, 16:26 (12 years ago) parentd810763
3 files changed+29−27
searx/engines/searchcode_code.py+9−15 searx/engines/searchcode_doc.py+7−4 searx/webapp.py+13−8
searx/engines/searchcode_code.py+9−15
@@ -10,8 +10,7 @@
1010
1111from urllib import urlencode
1212from json import loads
13−import cgi
14−import re
13+
1514
1615# engine dependent config
1716categories = ['it']
@@ -21,17 +20,10 @@ paging = True
2120url = 'https://searchcode.com/'
2221search_url = url+'api/codesearch_I/?{query}&p={pageno}'
2322
24−code_endings = {'c': 'c',
25− 'css': 'css',
26− 'cpp': 'cpp',
27− 'c++': 'cpp',
23+# special code-endings which are not recognised by the file ending
24+code_endings = {'cs': 'c#',
2825 'h': 'c',
29− 'html': 'html',
30− 'hpp': 'cpp',
31− 'js': 'js',
32− 'lua': 'lua',
33− 'php': 'php',
34− 'py': 'python'}
26+ 'hpp': 'cpp'}
3527
3628
3729# do search-request
@@ -45,7 +37,7 @@ def request(query, params):
4537# get response from search-request
4638def response(resp):
4739 results = []
48−
40+
4941 search_results = loads(resp.text)
5042
5143 # parse results
@@ -53,12 +45,14 @@ def response(resp):
5345 href = result['url']
5446 title = "" + result['name'] + " - " + result['filename']
5547 repo = result['repo']
56−
48+
5749 lines = dict()
5850 for line, code in result['lines'].items():
5951 lines[int(line)] = code
6052
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())
6256
6357 # append result
6458 results.append({'url': href,
searx/engines/searchcode_doc.py+7−4
@@ -31,15 +31,18 @@ def request(query, params):
3131# get response from search-request
3232def response(resp):
3333 results = []
34−
34+
3535 search_results = loads(resp.text)
3636
3737 # parse results
3838 for result in search_results['results']:
3939 href = result['url']
40− title = "[" + result['type'] + "] " + result['namespace'] + " " + result['name']
41− content = '<span class="highlight">[' + result['type'] + "] " + result['name'] + " " + result['synopsis'] + "</span><br />" + result['description']
42−
40+ title = "[" + result['type'] + "] " +\
41+ result['namespace'] + " " + result['name']
42+ content = '<span class="highlight">[' + result['type'] + "] " +\
43+ result['name'] + " " + result['synopsis'] +\
44+ "</span><br />" + result['description']
45+
4346 # append result
4447 results.append({'url': href,
4548 'title': title,
searx/webapp.py+13−8
@@ -99,9 +99,13 @@ def code_highlighter(codelines, language=None):
9999 if not language:
100100 language = 'text'
101101
102− # find lexer by programing language
103− lexer = get_lexer_by_name(language, stripall=True)
104−
102+ try:
103+ # find lexer by programing language
104+ lexer = get_lexer_by_name(language, stripall=True)
105+ except:
106+ # if lexer is not found, using default one
107+ lexer = get_lexer_by_name('text', stripall=True)
108+
105109 html_code = ''
106110 tmp_code = ''
107111 last_line = None
@@ -112,20 +116,21 @@ def code_highlighter(codelines, language=None):
112116 line_code_start = line
113117
114118 # new codeblock is detected
115− if last_line != None and\
116− last_line +1 != line:
119+ if last_line is not None and\
120+ last_line + 1 != line:
117121
118122 # highlight last codepart
119− formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start)
123+ formatter = HtmlFormatter(linenos='inline',
124+ linenostart=line_code_start)
120125 html_code = html_code + highlight(tmp_code, lexer, formatter)
121−
126+
122127 # reset conditions for next codepart
123128 tmp_code = ''
124129 line_code_start = line
125130
126131 # add codepart
127132 tmp_code += code + '\n'
128−
133+
129134 # update line
130135 last_line = line
131136