##// END OF EJS Templates
Add option to define custom lexers for custom extensions for code highlight in rcextension module
marcink -
r3375:7000fc4a beta
parent child Browse files
Show More
@@ -6,6 +6,14 b''
6 6 # build by pygments
7 7 EXTRA_MAPPINGS = {}
8 8
9 # additional lexer definitions for custom files
10 # it's overrides pygments lexers, and uses defined name of lexer to colorize the
11 # files. Format is {'ext': 'lexer_name'}
12 # List of lexers can be printed running:
13 # python -c "import pprint;from pygments import lexers;pprint.pprint([(x[0], x[1]) for x in lexers.get_all_lexers()]);"
14
15 EXTRA_LEXERS = {}
16
9 17 #==============================================================================
10 18 # WHOOSH INDEX EXTENSIONS
11 19 #==============================================================================
@@ -10,14 +10,13 b''
10 10 :copyright: (C) 2011-2012 Marcin Kuzminski <marcin@python-works.com>
11 11 :license: GPLv3, see COPYING for more details.
12 12 """
13 import StringIO
13 14
14 15 from rhodecode.lib.vcs.exceptions import VCSError
15 16 from rhodecode.lib.vcs.nodes import FileNode
16 17 from pygments.formatters import HtmlFormatter
17 18 from pygments import highlight
18 19
19 import StringIO
20
21 20
22 21 def annotate_highlight(filenode, annotate_from_changeset_func=None,
23 22 order=None, headers=None, **options):
@@ -34,11 +33,12 b' def annotate_highlight(filenode, annotat'
34 33 :param headers: dictionary with headers (keys are whats in ``order``
35 34 parameter)
36 35 """
36 from rhodecode.lib.utils import get_custom_lexer
37 37 options['linenos'] = True
38 38 formatter = AnnotateHtmlFormatter(filenode=filenode, order=order,
39 39 headers=headers,
40 40 annotate_from_changeset_func=annotate_from_changeset_func, **options)
41 lexer = filenode.lexer
41 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
42 42 highlighted = highlight(filenode.content, lexer, formatter)
43 43 return highlighted
44 44
@@ -41,7 +41,7 b' from webhelpers.html.tags import _set_in'
41 41 convert_boolean_attrs, NotGiven, _make_safe_id_component
42 42
43 43 from rhodecode.lib.annotate import annotate_highlight
44 from rhodecode.lib.utils import repo_name_slug
44 from rhodecode.lib.utils import repo_name_slug, get_custom_lexer
45 45 from rhodecode.lib.utils2 import str2bool, safe_unicode, safe_str, \
46 46 get_changeset_safe, datetime_to_time, time_to_datetime, AttributeDict
47 47 from rhodecode.lib.markup_renderer import MarkupRenderer
@@ -253,13 +253,14 b' class CodeHtmlFormatter(HtmlFormatter):'
253 253
254 254
255 255 def pygmentize(filenode, **kwargs):
256 """pygmentize function using pygments
256 """
257 pygmentize function using pygments
257 258
258 259 :param filenode:
259 260 """
260
261 return literal(code_highlight(filenode.content,
262 filenode.lexer, CodeHtmlFormatter(**kwargs)))
261 lexer = get_custom_lexer(filenode.extension) or filenode.lexer
262 return literal(code_highlight(filenode.content, lexer,
263 CodeHtmlFormatter(**kwargs)))
263 264
264 265
265 266 def pygmentize_annotation(repo_name, filenode, **kwargs):
@@ -550,6 +550,26 b' def load_rcextensions(root_path):'
550 550 log.debug('adding extra into INDEX_EXTENSIONS')
551 551 conf.INDEX_EXTENSIONS.extend(getattr(EXT, 'EXTRA_INDEX_EXTENSIONS', []))
552 552
553 # auto check if the module is not missing any data, set to default if is
554 # this will help autoupdate new feature of rcext module
555 from rhodecode.config import rcextensions
556 for k in dir(rcextensions):
557 if not k.startswith('_') and not hasattr(EXT, k):
558 setattr(EXT, k, getattr(rcextensions, k))
559
560
561 def get_custom_lexer(extension):
562 """
563 returns a custom lexer if it's defined in rcextensions module, or None
564 if there's no custom lexer defined
565 """
566 import rhodecode
567 from pygments import lexers
568 #check if we didn't define this extension as other lexer
569 if rhodecode.EXTENSIONS and extension in rhodecode.EXTENSIONS.EXTRA_LEXERS:
570 _lexer_name = rhodecode.EXTENSIONS.EXTRA_LEXERS[extension]
571 return lexers.get_lexer_by_name(_lexer_name)
572
553 573
554 574 #==============================================================================
555 575 # TEST FUNCTIONS AND CREATORS
General Comments 0
You need to be logged in to leave comments. Login now