Browse Source

[enh] fix pep8, improve syntax highlighting

Thomas Pointhuber 10 years ago
parent
commit
af8dac93a8
3 changed files with 29 additions and 27 deletions
  1. 9
    15
      searx/engines/searchcode_code.py
  2. 7
    4
      searx/engines/searchcode_doc.py
  3. 13
    8
      searx/webapp.py

+ 9
- 15
searx/engines/searchcode_code.py View File

@@ -10,8 +10,7 @@
10 10
 
11 11
 from urllib import urlencode
12 12
 from json import loads
13
-import cgi
14
-import re
13
+
15 14
 
16 15
 # engine dependent config
17 16
 categories = ['it']
@@ -21,17 +20,10 @@ paging = True
21 20
 url = 'https://searchcode.com/'
22 21
 search_url = url+'api/codesearch_I/?{query}&p={pageno}'
23 22
 
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#',
28 25
                 'h': 'c',
29
-                'html': 'html',
30
-                'hpp': 'cpp',
31
-                'js': 'js',
32
-                'lua': 'lua',
33
-                'php': 'php',
34
-                'py': 'python'}
26
+                'hpp': 'cpp'}
35 27
 
36 28
 
37 29
 # do search-request
@@ -45,7 +37,7 @@ def request(query, params):
45 37
 # get response from search-request
46 38
 def response(resp):
47 39
     results = []
48
-    
40
+
49 41
     search_results = loads(resp.text)
50 42
 
51 43
     # parse results
@@ -53,12 +45,14 @@ def response(resp):
53 45
         href = result['url']
54 46
         title = "" + result['name'] + " - " + result['filename']
55 47
         repo = result['repo']
56
-        
48
+
57 49
         lines = dict()
58 50
         for line, code in result['lines'].items():
59 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 57
         # append result
64 58
         results.append({'url': href,

+ 7
- 4
searx/engines/searchcode_doc.py View File

@@ -31,15 +31,18 @@ def request(query, params):
31 31
 # get response from search-request
32 32
 def response(resp):
33 33
     results = []
34
-    
34
+
35 35
     search_results = loads(resp.text)
36 36
 
37 37
     # parse results
38 38
     for result in search_results['results']:
39 39
         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
+
43 46
         # append result
44 47
         results.append({'url': href,
45 48
                         'title': title,

+ 13
- 8
searx/webapp.py View File

@@ -99,9 +99,13 @@ def code_highlighter(codelines, language=None):
99 99
     if not language:
100 100
         language = 'text'
101 101
 
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
+
105 109
     html_code = ''
106 110
     tmp_code = ''
107 111
     last_line = None
@@ -112,20 +116,21 @@ def code_highlighter(codelines, language=None):
112 116
             line_code_start = line
113 117
 
114 118
         # 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:
117 121
 
118 122
             # highlight last codepart
119
-            formatter = HtmlFormatter(linenos='inline', linenostart=line_code_start)
123
+            formatter = HtmlFormatter(linenos='inline',
124
+                                      linenostart=line_code_start)
120 125
             html_code = html_code + highlight(tmp_code, lexer, formatter)
121
-            
126
+
122 127
             # reset conditions for next codepart
123 128
             tmp_code = ''
124 129
             line_code_start = line
125 130
 
126 131
         # add codepart
127 132
         tmp_code += code + '\n'
128
-        
133
+
129 134
         # update line
130 135
         last_line = line
131 136