Mercurial > kallithea
changeset 3375:7000fc4aa569 beta
Add option to define custom lexers for custom extensions for code highlight in rcextension module
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 17 Feb 2013 21:21:45 +0100 |
parents | e971b511e0f4 |
children | e67b2ef07a8e |
files | rhodecode/config/rcextensions/__init__.py rhodecode/lib/annotate.py rhodecode/lib/helpers.py rhodecode/lib/utils.py |
diffstat | 4 files changed, 37 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/rhodecode/config/rcextensions/__init__.py Tue Jan 29 17:56:11 2013 +0900 +++ b/rhodecode/config/rcextensions/__init__.py Sun Feb 17 21:21:45 2013 +0100 @@ -6,6 +6,14 @@ # build by pygments EXTRA_MAPPINGS = {} +# additional lexer definitions for custom files +# it's overrides pygments lexers, and uses defined name of lexer to colorize the +# files. Format is {'ext': 'lexer_name'} +# List of lexers can be printed running: +# python -c "import pprint;from pygments import lexers;pprint.pprint([(x[0], x[1]) for x in lexers.get_all_lexers()]);" + +EXTRA_LEXERS = {} + #============================================================================== # WHOOSH INDEX EXTENSIONS #==============================================================================
--- a/rhodecode/lib/annotate.py Tue Jan 29 17:56:11 2013 +0900 +++ b/rhodecode/lib/annotate.py Sun Feb 17 21:21:45 2013 +0100 @@ -10,14 +10,13 @@ :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com> :license: GPLv3, see COPYING for more details. """ +import StringIO from rhodecode.lib.vcs.exceptions import VCSError from rhodecode.lib.vcs.nodes import FileNode from pygments.formatters import HtmlFormatter from pygments import highlight -import StringIO - def annotate_highlight(filenode, annotate_from_changeset_func=None, order=None, headers=None, **options): @@ -34,11 +33,12 @@ :param headers: dictionary with headers (keys are whats in ``order`` parameter) """ + from rhodecode.lib.utils import get_custom_lexer options['linenos'] = True formatter = AnnotateHtmlFormatter(filenode=filenode, order=order, headers=headers, annotate_from_changeset_func=annotate_from_changeset_func, **options) - lexer = filenode.lexer + lexer = get_custom_lexer(filenode.extension) or filenode.lexer highlighted = highlight(filenode.content, lexer, formatter) return highlighted
--- a/rhodecode/lib/helpers.py Tue Jan 29 17:56:11 2013 +0900 +++ b/rhodecode/lib/helpers.py Sun Feb 17 21:21:45 2013 +0100 @@ -41,7 +41,7 @@ convert_boolean_attrs, NotGiven, _make_safe_id_component from rhodecode.lib.annotate import annotate_highlight -from rhodecode.lib.utils import repo_name_slug +from rhodecode.lib.utils import repo_name_slug, get_custom_lexer from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \ get_changeset_safe, datetime_to_time, time_to_datetime, AttributeDict from rhodecode.lib.markup_renderer import MarkupRenderer @@ -253,13 +253,14 @@ def pygmentize(filenode, **kwargs): - """pygmentize function using pygments + """ + pygmentize function using pygments :param filenode: """ - - return literal(code_highlight(filenode.content, - filenode.lexer, CodeHtmlFormatter(**kwargs))) + lexer = get_custom_lexer(filenode.extension) or filenode.lexer + return literal(code_highlight(filenode.content, lexer, + CodeHtmlFormatter(**kwargs))) def pygmentize_annotation(repo_name, filenode, **kwargs):
--- a/rhodecode/lib/utils.py Tue Jan 29 17:56:11 2013 +0900 +++ b/rhodecode/lib/utils.py Sun Feb 17 21:21:45 2013 +0100 @@ -550,6 +550,26 @@ log.debug('adding extra into INDEX_EXTENSIONS') conf.INDEX_EXTENSIONS.extend(getattr(EXT, 'EXTRA_INDEX_EXTENSIONS', [])) + # auto check if the module is not missing any data, set to default if is + # this will help autoupdate new feature of rcext module + from rhodecode.config import rcextensions + for k in dir(rcextensions): + if not k.startswith('_') and not hasattr(EXT, k): + setattr(EXT, k, getattr(rcextensions, k)) + + +def get_custom_lexer(extension): + """ + returns a custom lexer if it's defined in rcextensions module, or None + if there's no custom lexer defined + """ + import rhodecode + from pygments import lexers + #check if we didn't define this extension as other lexer + if rhodecode.EXTENSIONS and extension in rhodecode.EXTENSIONS.EXTRA_LEXERS: + _lexer_name = rhodecode.EXTENSIONS.EXTRA_LEXERS[extension] + return lexers.get_lexer_by_name(_lexer_name) + #============================================================================== # TEST FUNCTIONS AND CREATORS