src/pyams_utils/pygments.py
changeset 284 ad3c56ae5af9
child 318 12e519cc367c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/pygments.py	Fri Nov 30 12:03:59 2018 +0100
@@ -0,0 +1,152 @@
+#
+# Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+from fanstatic import get_library_registry
+from persistent import Persistent
+from pygments import highlight
+from pygments.formatters.html import HtmlFormatter
+from pygments.lexers import get_all_lexers, get_lexer_by_name, guess_lexer
+from pygments.styles import get_all_styles
+from pyramid.response import Response
+from pyramid.view import view_config
+from zope.container.contained import Contained
+from zope.interface import Interface, implementer
+from zope.schema import Bool, Choice
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+from pyams_utils.factory import factory_config
+from pyams_utils.fanstatic import ExternalResource
+from pyams_utils.list import unique_iter
+from pyams_utils.vocabulary import vocabulary_config
+
+from pyams_utils import _
+
+
+#
+# Pygments CSS view
+#
+
+for library in get_library_registry().values():
+    break
+else:
+    try:
+        from pyams_skin import library
+    except ImportError:
+        from pyams_default_theme import library
+
+
+pygments_css = ExternalResource(library, 'get-pygments-style.css', resource_type='css')
+
+
+@view_config(name='get-pygments-style.css')
+def get_pygments_style_view(request):
+    style = request.params.get('style', 'default')
+    styles = HtmlFormatter(linenos='inline',
+                           nowrap=False,
+                           cssclass='source',
+                           style=style).get_style_defs()
+    return Response(styles, content_type='text/css')
+
+
+#
+# Pygments lexers
+#
+
+PYGMENTS_LEXERS_VOCABULARY = 'Pygments lexers vocabulary'
+
+
+@vocabulary_config(name=PYGMENTS_LEXERS_VOCABULARY)
+class PygmentsLexersVocabulary(SimpleVocabulary):
+    """Pygments lexers vocabulary"""
+
+    def __init__(self, context):
+        terms = [SimpleTerm('auto', title=_("Automatic detection"))]
+        for name, aliases, filetypes, mimetypes in sorted(unique_iter(get_all_lexers(),
+                                                                      key=lambda x: x[0].lower()),
+                                                          key=lambda x: x[0].lower()):
+            terms.append(SimpleTerm(aliases[0] if len(aliases) > 0 else name,
+                                    title='{0}{1}'.format(name,
+                                                          ' ({})'.format(', '.join(filetypes)) if filetypes else '')))
+        super(PygmentsLexersVocabulary, self).__init__(terms)
+
+
+PYGMENTS_STYLES_VOCABULARY = 'Pygments styles vocabulary'
+
+
+@vocabulary_config(name=PYGMENTS_STYLES_VOCABULARY)
+class PygmentsStylesVocabulary(SimpleVocabulary):
+    """Pygments styles vocabulary"""
+
+    def __init__(self, context):
+        terms = []
+        for name in sorted(get_all_styles()):
+            terms.append(SimpleTerm(name))
+        super(PygmentsStylesVocabulary, self).__init__(terms)
+
+
+#
+# Pygments configuration
+#
+
+class IPygmentsCodeConfiguration(Interface):
+    """Pygments html formatter options"""
+
+    lexer = Choice(title=_("Selected lexer"),
+                   description=_("Lexer used to format source code"),
+                   required=True,
+                   vocabulary=PYGMENTS_LEXERS_VOCABULARY,
+                   default='auto')
+
+    display_linenos = Bool(title=_("Display line numbers?"),
+                           description=_("If 'no', line numbers will be hidden"),
+                           required=True,
+                           default=True)
+
+    disable_wrap = Bool(title=_("Lines wrap?"),
+                        description=_("If 'yes', lines wraps will be enabled; line numbers will not be "
+                                      "displayed if lines wrap is enabled..."),
+                        required=True,
+                        default=False)
+
+    style = Choice(title=_("Color style"),
+                   description=_("Selected color style"),
+                   required=True,
+                   vocabulary=PYGMENTS_STYLES_VOCABULARY,
+                   default='default')
+
+
+@factory_config(provided=IPygmentsCodeConfiguration)
+@implementer(IPygmentsCodeConfiguration)
+class PygmentsCodeRendererSettings(Persistent, Contained):
+    """Pygments code renderer settings"""
+
+    lexer = FieldProperty(IPygmentsCodeConfiguration['lexer'])
+    display_linenos = FieldProperty(IPygmentsCodeConfiguration['display_linenos'])
+    disable_wrap = FieldProperty(IPygmentsCodeConfiguration['disable_wrap'])
+    style = FieldProperty(IPygmentsCodeConfiguration['style'])
+
+
+def render_source(code: str, settings: IPygmentsCodeConfiguration):
+    """Render source with given settings"""
+    if settings.lexer == 'auto':
+        lexer = guess_lexer(code)
+    else:
+        lexer = get_lexer_by_name(settings.lexer)
+    if lexer is not None:
+        formatter = HtmlFormatter(linenos='inline' if settings.display_linenos else None,
+                                  nowrap=settings.disable_wrap,
+                                  cssclass='source',
+                                  style=settings.style)
+        return highlight(code, lexer, formatter)