##// END OF EJS Templates
lexers: added small extensions table to extend syntaxt hl for file sources....
marcink -
r796:76d12e4e default
parent child Browse files
Show More
@@ -28,7 +28,11 b' from rhodecode.lib.utils2 import __get_l'
28
28
29 # language map is also used by whoosh indexer, which for those specified
29 # language map is also used by whoosh indexer, which for those specified
30 # extensions will index it's content
30 # extensions will index it's content
31 LANGUAGES_EXTENSIONS_MAP = __get_lem()
31 # custom extensions to lexers, format is 'ext': 'LexerClass'
32 extra = {
33 'vbs': 'VbNet'
34 }
35 LANGUAGES_EXTENSIONS_MAP = __get_lem(extra)
32
36
33 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
37 DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"
34
38
@@ -48,12 +48,12 b' def annotate_highlight('
48 :param headers: dictionary with headers (keys are whats in ``order``
48 :param headers: dictionary with headers (keys are whats in ``order``
49 parameter)
49 parameter)
50 """
50 """
51 from rhodecode.lib.utils import get_custom_lexer
51 from rhodecode.lib.helpers import get_lexer_for_filenode
52 options['linenos'] = True
52 options['linenos'] = True
53 formatter = AnnotateHtmlFormatter(
53 formatter = AnnotateHtmlFormatter(
54 filenode=filenode, order=order, headers=headers,
54 filenode=filenode, order=order, headers=headers,
55 annotate_from_commit_func=annotate_from_commit_func, **options)
55 annotate_from_commit_func=annotate_from_commit_func, **options)
56 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
56 lexer = get_lexer_for_filenode(filenode)
57 highlighted = highlight(filenode.content, lexer, formatter)
57 highlighted = highlight(filenode.content, lexer, formatter)
58 return highlighted
58 return highlighted
59
59
@@ -520,13 +520,18 b' def get_lexer_safe(mimetype=None, filepa'
520 return lexer
520 return lexer
521
521
522
522
523 def get_lexer_for_filenode(filenode):
524 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
525 return lexer
526
527
523 def pygmentize(filenode, **kwargs):
528 def pygmentize(filenode, **kwargs):
524 """
529 """
525 pygmentize function using pygments
530 pygmentize function using pygments
526
531
527 :param filenode:
532 :param filenode:
528 """
533 """
529 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
534 lexer = get_lexer_for_filenode(filenode)
530 return literal(code_highlight(filenode.content, lexer,
535 return literal(code_highlight(filenode.content, lexer,
531 CodeHtmlFormatter(**kwargs)))
536 CodeHtmlFormatter(**kwargs)))
532
537
@@ -54,7 +54,7 b' def md5_safe(s):'
54 return md5(safe_str(s))
54 return md5(safe_str(s))
55
55
56
56
57 def __get_lem():
57 def __get_lem(extra_mapping=None):
58 """
58 """
59 Get language extension map based on what's inside pygments lexers
59 Get language extension map based on what's inside pygments lexers
60 """
60 """
@@ -82,7 +82,16 b' def __get_lem():'
82 desc = lx.replace('Lexer', '')
82 desc = lx.replace('Lexer', '')
83 d[ext].append(desc)
83 d[ext].append(desc)
84
84
85 return dict(d)
85 data = dict(d)
86
87 extra_mapping = extra_mapping or {}
88 if extra_mapping:
89 for k, v in extra_mapping.items():
90 if k not in data:
91 # register new mapping2lexer
92 data[k] = [v]
93
94 return data
86
95
87
96
88 def str2bool(_str):
97 def str2bool(_str):
@@ -110,7 +119,7 b' def aslist(obj, sep=None, strip=True):'
110 :param sep:
119 :param sep:
111 :param strip:
120 :param strip:
112 """
121 """
113 if isinstance(obj, (basestring)):
122 if isinstance(obj, (basestring,)):
114 lst = obj.split(sep)
123 lst = obj.split(sep)
115 if strip:
124 if strip:
116 lst = [v.strip() for v in lst]
125 lst = [v.strip() for v in lst]
@@ -27,6 +27,7 b' import stat'
27
27
28 from zope.cachedescriptors.property import Lazy as LazyProperty
28 from zope.cachedescriptors.property import Lazy as LazyProperty
29
29
30 from rhodecode.config.conf import LANGUAGES_EXTENSIONS_MAP
30 from rhodecode.lib.utils import safe_unicode, safe_str
31 from rhodecode.lib.utils import safe_unicode, safe_str
31 from rhodecode.lib.utils2 import md5
32 from rhodecode.lib.utils2 import md5
32 from rhodecode.lib.vcs import path as vcspath
33 from rhodecode.lib.vcs import path as vcspath
@@ -435,11 +436,26 b' class FileNode(Node):'
435 content, name and mimetype.
436 content, name and mimetype.
436 """
437 """
437 from pygments import lexers
438 from pygments import lexers
439
440 lexer = None
438 try:
441 try:
439 lexer = lexers.guess_lexer_for_filename(self.name, self.content, stripnl=False)
442 lexer = lexers.guess_lexer_for_filename(
443 self.name, self.content, stripnl=False)
440 except lexers.ClassNotFound:
444 except lexers.ClassNotFound:
445 lexer = None
446
447 # try our EXTENSION_MAP
448 if not lexer:
449 try:
450 lexer_class = LANGUAGES_EXTENSIONS_MAP.get(self.extension)
451 if lexer_class:
452 lexer = lexers.get_lexer_by_name(lexer_class[0])
453 except lexers.ClassNotFound:
454 lexer = None
455
456 if not lexer:
441 lexer = lexers.TextLexer(stripnl=False)
457 lexer = lexers.TextLexer(stripnl=False)
442 # returns first alias
458
443 return lexer
459 return lexer
444
460
445 @LazyProperty
461 @LazyProperty
@@ -5,7 +5,8 b''
5 <span> <strong>${c.file}</strong></span>
5 <span> <strong>${c.file}</strong></span>
6 <span> | ${c.file.lines()[0]} ${ungettext('line', 'lines', c.file.lines()[0])}</span>
6 <span> | ${c.file.lines()[0]} ${ungettext('line', 'lines', c.file.lines()[0])}</span>
7 <span> | ${h.format_byte_size_binary(c.file.size)}</span>
7 <span> | ${h.format_byte_size_binary(c.file.size)}</span>
8 <span class="item last"> | ${c.file.mimetype}</span>
8 <span> | ${c.file.mimetype} </span>
9 <span class="item last"> | ${h.get_lexer_for_filenode(c.file).__class__.__name__}</span>
9 </div>
10 </div>
10 <div class="buttons">
11 <div class="buttons">
11 <a id="file_history_overview" href="#">
12 <a id="file_history_overview" href="#">
General Comments 0
You need to be logged in to leave comments. Login now