changeset 5400:b7ab238032dd

fix/simplify HighlightParser and compatibility wrappers (details see below) building of extensions list: don't use regex if .startswith is enough for compat wrappers, use empty extensions list, because we have all those extensions declared in the HighlightParser itself, so moin will call that directly, without using the wrapper. make sure that the mimetypes support knows all mimetypes/filename extensions known to pygments. when building the extensions -> parser mapping, first process the highlight parser, so that later in processing, other ("better") parsers can overwrite the mapping entry for some extension they support. this fixes that moin did syntax highlighting for .html attachments although it has a safe html parser that could safely render the .html file. usually we want to prefer rendering before highlighting. Thanks for Reimar Bauer for the patch.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Wed, 06 Jan 2010 04:10:21 +0100
parents ffaec355ee84
children 0c424b6dc029
files MoinMoin/parser/highlight.py MoinMoin/parser/text_cplusplus.py MoinMoin/parser/text_diff.py MoinMoin/parser/text_irssi.py MoinMoin/parser/text_java.py MoinMoin/parser/text_pascal.py MoinMoin/parser/text_python.py MoinMoin/wikiutil.py
diffstat 8 files changed, 52 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/parser/highlight.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/highlight.py	Wed Jan 06 04:10:21 2010 +0100
@@ -20,13 +20,21 @@
 from MoinMoin.Page import Page
 
 Dependencies = ['user'] # the "Toggle line numbers link" depends on user's language
-extensions = []
-extension_re = re.compile(r'^\*(\..*$)')
-for name, short, patterns, mime in pygments.lexers.get_all_lexers():
-    for pattern in patterns:
-        m = extension_re.match(pattern)
-        if m and m.groups(0):
-            extensions.extend(m.groups(0))
+
+
+def extensions_from_lexer_filenames(filenames):
+    # pygment's lexer.filenames is like ['*.py', 'Python'], but we only want
+    # the filename extensions list (like ['.py', ]):
+    return [filename[1:] for filename in filenames if filename.startswith('*.')]
+
+def extensions_for_all_lexers():
+    """
+    get supported filename extensions for all pygments lexers
+    """
+    extensions = []
+    for name, short, patterns, mime in pygments.lexers.get_all_lexers():
+        extensions.extend(extensions_from_lexer_filenames(patterns))
+    return extensions
 
 
 class PygmentsFormatter(pygments.formatter.Formatter):
@@ -115,7 +123,7 @@
 class Parser:
     parsername = "highlight"  # compatibility wrappers override this with the pygments lexer name
     Dependencies = Dependencies
-    extensions = extensions
+    extensions = extensions_for_all_lexers()
 
     def __init__(self, raw, request, filename=None, format_args='', **kw):
         self.request = request
--- a/MoinMoin/parser/text_cplusplus.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_cplusplus.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,8 @@
 
 class Parser(HighlightParser):
     parsername = 'cpp'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
+
--- a/MoinMoin/parser/text_diff.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_diff.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,7 @@
 
 class Parser(HighlightParser):
     parsername = 'diff'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
--- a/MoinMoin/parser/text_irssi.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_irssi.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,7 @@
 
 class Parser(HighlightParser):
     parsername = 'irc'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
--- a/MoinMoin/parser/text_java.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_java.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,7 @@
 
 class Parser(HighlightParser):
     parsername = 'java'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
--- a/MoinMoin/parser/text_pascal.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_pascal.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,7 @@
 
 class Parser(HighlightParser):
     parsername = 'pascal'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
--- a/MoinMoin/parser/text_python.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/parser/text_python.py	Wed Jan 06 04:10:21 2010 +0100
@@ -23,4 +23,7 @@
 
 class Parser(HighlightParser):
     parsername = 'python'  # Lexer name pygments recognizes
+    extensions = [] # this is only a compatibility wrapper, we have declared
+                    # support for this extension in the HighlightParser, so
+                    # moin will call that directly
 
--- a/MoinMoin/wikiutil.py	Mon Dec 28 14:53:08 2009 +0000
+++ b/MoinMoin/wikiutil.py	Wed Jan 06 04:10:21 2010 +0100
@@ -868,6 +868,15 @@
  '.wmv': 'video/x-ms-wmv',
  '.swf': 'application/x-shockwave-flash',
 }
+
+# add all mimetype patterns of pygments
+import pygments.lexers
+
+for name, short, patterns, mime in pygments.lexers.get_all_lexers():
+    for pattern in patterns:
+        if pattern.startswith('*.') and mime:
+            MIMETYPES_MORE[pattern[1:]] = mime[0]
+
 [mimetypes.add_type(mimetype, ext, True) for ext, mimetype in MIMETYPES_MORE.items()]
 
 MIMETYPES_sanitize_mapping = {
@@ -1188,7 +1197,13 @@
     """
     if not hasattr(cfg.cache, 'EXT_TO_PARSER'):
         etp, etd = {}, None
-        for pname in getPlugins('parser', cfg):
+        parser_plugins = getPlugins('parser', cfg)
+        # force the 'highlight' parser to be the first entry in the list
+        # this makes it possible to overwrite some mapping entries later, so that
+        # moin will use some "better" parser for some filename extensions
+        parser_plugins.remove('highlight')
+        parser_plugins = ['highlight'] + parser_plugins
+        for pname in parser_plugins:
             try:
                 Parser = importPlugin(cfg, 'parser', pname, 'Parser')
             except PluginMissingError:
@@ -1196,7 +1211,7 @@
             if hasattr(Parser, 'extensions'):
                 exts = Parser.extensions
                 if isinstance(exts, list):
-                    for ext in Parser.extensions:
+                    for ext in exts:
                         etp[ext] = Parser
                 elif str(exts) == '*':
                     etd = Parser