File: utils.py

package info (click to toggle)
flask-flatpages 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 300 kB
  • sloc: python: 1,054; makefile: 150
file content (94 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download
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
"""Utility functions to render Markdown text to HTML."""

import markdown
from markdown.extensions import codehilite

from .imports import PygmentsHtmlFormatter

from io import StringIO


class NamedStringIO(StringIO, object):
    """Subclass adding a Name to :class:`StringIO` objects."""

    def __init__(self, content, name):
        """
        Initialise the NamedStringIO.

        :param content: The string to be treated as a stream
        :param name: The name to attach to the stream. Will
            be consumed in e.g. ReaderErrors raised by pyyaml.
        """
        super().__init__(content)
        self.name = name


def force_unicode(value, encoding="utf-8", errors="strict"):
    """Convert bytes or any other Python instance to string."""
    if isinstance(value, str):
        return value
    return value.decode(encoding, errors)


def pygmented_markdown(text, flatpages=None):
    """Render Markdown text to HTML.

    Uses the `CodeHilite`_ extension only if `Pygments`_ is available. If
    `Pygments`_ is not available, "codehilite" is removed from list of
    extensions.

    If you need other extensions, set them up using the
    ``FLATPAGES_MARKDOWN_EXTENSIONS`` setting, which should be a sequence
    of strings or Markdown Extension objects.
    Extensions specified with entrypoint strings should be configured using
    ``FLATPAGES_EXTENSION_CONFIGS``.

    .. _CodeHilite:
       http://www.freewisdom.org/projects/python-markdown/CodeHilite
    .. _Pygments: http://pygments.org/
    """
    if flatpages:
        extensions = flatpages.config("markdown_extensions")
        extension_configs = flatpages.config("extension_configs")
    else:
        extensions = []
        extension_configs = {}
    if PygmentsHtmlFormatter is None:
        original_extensions = extensions
        original_config = extension_configs
        extensions = []
        extension_configs = {}

        for extension in original_extensions:
            if (
                isinstance(extension, str)
                and "codehilite" in extension
            ):
                continue
            elif isinstance(extension, codehilite.CodeHiliteExtension):
                continue
            extensions.append(extension)
            if isinstance(extension, str):
                if extension in original_config:
                    extension_configs[extension] = original_config[extension]
    elif not extensions:
        extensions = ["codehilite"]
    return markdown.markdown(
        text, extensions=extensions, extension_configs=extension_configs
    )


def pygments_style_defs(style="default"):
    """:return: the CSS definitions for the `CodeHilite`_ Markdown plugin.

    :param style: The Pygments `style`_ to use.

    Only available if `Pygments`_ is.

    .. _CodeHilite:
       http://www.freewisdom.org/projects/python-markdown/CodeHilite
    .. _Pygments: http://pygments.org/
    .. _style: http://pygments.org/docs/styles/
    """
    formatter = PygmentsHtmlFormatter(style=style)
    return formatter.get_style_defs(".codehilite")