File: markup.py

package info (click to toggle)
django-markupfield 2.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 236 kB
  • sloc: python: 724; makefile: 16; sh: 5
file content (103 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from functools import partial
from django.utils.html import escape, linebreaks, urlize
from django.utils.translation import pgettext_lazy as _
from django.conf import settings

# build DEFAULT_MARKUP_TYPES
DEFAULT_MARKUP_TYPES = [
    ("html", lambda markup: markup, _("django-markupfield", "HTML")),
    (
        "plain",
        lambda markup: linebreaks(urlize(escape(markup))),
        _("django-markupfield", "Plain"),
    ),
]

try:
    import pygments  # noqa

    PYGMENTS_INSTALLED = True

    def _register_pygments_rst_directive():
        from docutils import nodes
        from docutils.parsers.rst import directives
        from pygments import highlight
        from pygments.lexers import get_lexer_by_name, TextLexer
        from pygments.formatters import HtmlFormatter

        DEFAULT = HtmlFormatter()
        VARIANTS = {
            "linenos": HtmlFormatter(linenos=True),
        }

        def pygments_directive(
            name,
            arguments,
            options,
            content,
            lineno,
            content_offset,
            block_text,
            state,
            state_machine,
        ):
            try:
                lexer = get_lexer_by_name(arguments[0])
            except ValueError:
                # no lexer found - use the text one instead of an exception
                lexer = TextLexer()
            formatter = options and VARIANTS[options.keys()[0]] or DEFAULT
            parsed = highlight(u"\n".join(content), lexer, formatter)
            return [nodes.raw("", parsed, format="html")]

        pygments_directive.arguments = (1, 0, 1)
        pygments_directive.content = 1
        directives.register_directive("code", pygments_directive)


except ImportError:
    PYGMENTS_INSTALLED = False

try:
    import markdown

    md_filter = markdown.markdown

    # try and replace if pygments & codehilite are available
    if PYGMENTS_INSTALLED:
        try:
            from markdown.extensions.codehilite import makeExtension  # noqa

            md_filter = partial(
                markdown.markdown, extensions=[makeExtension(css_class="highlight")]
            )
        except ImportError:
            pass

    # whichever markdown_filter was available
    DEFAULT_MARKUP_TYPES.append(
        ("markdown", md_filter, _("django-markupfield", "Markdown"))
    )

except ImportError:
    pass

try:
    from docutils.core import publish_parts

    if PYGMENTS_INSTALLED:
        _register_pygments_rst_directive()

    def render_rest(markup):
        overrides = getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {})
        overrides.update({"raw_enabled": False, "file_insertion_enabled": False})
        parts = publish_parts(
            source=markup, writer_name="html4css1", settings_overrides=overrides
        )
        return parts["fragment"]

    DEFAULT_MARKUP_TYPES.append(
        ("restructuredtext", render_rest, _("django-markupfield", "Restructured Text"))
    )
except ImportError:
    pass