diff --git a/rhodecode/config/conf.py b/rhodecode/config/conf.py --- a/rhodecode/config/conf.py +++ b/rhodecode/config/conf.py @@ -28,7 +28,11 @@ from rhodecode.lib.utils2 import __get_l # language map is also used by whoosh indexer, which for those specified # extensions will index it's content -LANGUAGES_EXTENSIONS_MAP = __get_lem() +# custom extensions to lexers, format is 'ext': 'LexerClass' +extra = { + 'vbs': 'VbNet' +} +LANGUAGES_EXTENSIONS_MAP = __get_lem(extra) DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" diff --git a/rhodecode/lib/annotate.py b/rhodecode/lib/annotate.py --- a/rhodecode/lib/annotate.py +++ b/rhodecode/lib/annotate.py @@ -48,12 +48,12 @@ def annotate_highlight( :param headers: dictionary with headers (keys are whats in ``order`` parameter) """ - from rhodecode.lib.utils import get_custom_lexer + from rhodecode.lib.helpers import get_lexer_for_filenode options['linenos'] = True formatter = AnnotateHtmlFormatter( filenode=filenode, order=order, headers=headers, annotate_from_commit_func=annotate_from_commit_func, **options) - lexer = get_custom_lexer(filenode.extension) or filenode.lexer + lexer = get_lexer_for_filenode(filenode) highlighted = highlight(filenode.content, lexer, formatter) return highlighted diff --git a/rhodecode/lib/helpers.py b/rhodecode/lib/helpers.py --- a/rhodecode/lib/helpers.py +++ b/rhodecode/lib/helpers.py @@ -520,13 +520,18 @@ def get_lexer_safe(mimetype=None, filepa return lexer +def get_lexer_for_filenode(filenode): + lexer = get_custom_lexer(filenode.extension) or filenode.lexer + return lexer + + def pygmentize(filenode, **kwargs): """ pygmentize function using pygments :param filenode: """ - lexer = get_custom_lexer(filenode.extension) or filenode.lexer + lexer = get_lexer_for_filenode(filenode) return literal(code_highlight(filenode.content, lexer, CodeHtmlFormatter(**kwargs))) diff --git a/rhodecode/lib/utils2.py b/rhodecode/lib/utils2.py --- a/rhodecode/lib/utils2.py +++ b/rhodecode/lib/utils2.py @@ -54,7 +54,7 @@ def md5_safe(s): return md5(safe_str(s)) -def __get_lem(): +def __get_lem(extra_mapping=None): """ Get language extension map based on what's inside pygments lexers """ @@ -82,7 +82,16 @@ def __get_lem(): desc = lx.replace('Lexer', '') d[ext].append(desc) - return dict(d) + data = dict(d) + + extra_mapping = extra_mapping or {} + if extra_mapping: + for k, v in extra_mapping.items(): + if k not in data: + # register new mapping2lexer + data[k] = [v] + + return data def str2bool(_str): @@ -110,7 +119,7 @@ def aslist(obj, sep=None, strip=True): :param sep: :param strip: """ - if isinstance(obj, (basestring)): + if isinstance(obj, (basestring,)): lst = obj.split(sep) if strip: lst = [v.strip() for v in lst] diff --git a/rhodecode/lib/vcs/nodes.py b/rhodecode/lib/vcs/nodes.py --- a/rhodecode/lib/vcs/nodes.py +++ b/rhodecode/lib/vcs/nodes.py @@ -27,6 +27,7 @@ import stat from zope.cachedescriptors.property import Lazy as LazyProperty +from rhodecode.config.conf import LANGUAGES_EXTENSIONS_MAP from rhodecode.lib.utils import safe_unicode, safe_str from rhodecode.lib.utils2 import md5 from rhodecode.lib.vcs import path as vcspath @@ -435,11 +436,26 @@ class FileNode(Node): content, name and mimetype. """ from pygments import lexers + + lexer = None try: - lexer = lexers.guess_lexer_for_filename(self.name, self.content, stripnl=False) + lexer = lexers.guess_lexer_for_filename( + self.name, self.content, stripnl=False) except lexers.ClassNotFound: + lexer = None + + # try our EXTENSION_MAP + if not lexer: + try: + lexer_class = LANGUAGES_EXTENSIONS_MAP.get(self.extension) + if lexer_class: + lexer = lexers.get_lexer_by_name(lexer_class[0]) + except lexers.ClassNotFound: + lexer = None + + if not lexer: lexer = lexers.TextLexer(stripnl=False) - # returns first alias + return lexer @LazyProperty diff --git a/rhodecode/templates/files/files_source.html b/rhodecode/templates/files/files_source.html --- a/rhodecode/templates/files/files_source.html +++ b/rhodecode/templates/files/files_source.html @@ -5,7 +5,8 @@ ${c.file} | ${c.file.lines()[0]} ${ungettext('line', 'lines', c.file.lines()[0])} | ${h.format_byte_size_binary(c.file.size)} - | ${c.file.mimetype} + | ${c.file.mimetype} + | ${h.get_lexer_for_filenode(c.file).__class__.__name__}