File: asciidoc.py

package info (click to toggle)
pymarkups 4.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: python: 1,156; sh: 7; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 2,820 bytes parent folder | download | duplicates (2)
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
# This file is part of python-markups module
# License: 3-clause BSD, see LICENSE file
# Copyright: (C) Dave Kuhlman, 2022

import importlib
import warnings
from io import StringIO

import markups.common as common
from markups.abstract import AbstractMarkup, ConvertedMarkup


class AsciiDocMarkup(AbstractMarkup):
    """Markup class for AsciiDoc language.
    Inherits :class:`~markups.abstract.AbstractMarkup`.
    """

    name = "asciidoc"
    attributes = {
        common.LANGUAGE_HOME_PAGE: "https://asciidoc.org",
        common.MODULE_HOME_PAGE: "https://asciidoc-py.github.io",
        common.SYNTAX_DOCUMENTATION: "https://asciidoc-py.github.io/userguide.html",
    }

    file_extensions = (".adoc", ".asciidoc")
    default_extension = ".adoc"

    @staticmethod
    def available() -> bool:
        try:
            importlib.import_module("asciidoc")
            importlib.import_module("lxml")
        except ImportError:
            return False
        return True

    def convert(self, text: str) -> ConvertedMarkup:
        import asciidoc
        from lxml import etree

        outfile = StringIO()
        infile = StringIO(text)
        opts = [
            ("--backend", "html5"),
            ("--attribute", r"newline=\n"),
            ("--attribute", "footer-style=none"),
            ("--out-file", outfile),
        ]
        try:
            asciidoc.execute(None, opts, [infile])
        except SystemExit as ex:
            warnings.warn(str(ex.__context__), SyntaxWarning)
            pass
        result = outfile.getvalue()
        parser = etree.HTMLParser()
        root_element = etree.fromstring(result, parser)
        head_element = root_element.xpath("./head")[0]
        title_element = root_element.xpath("./head/title")[0]
        style_elements = root_element.xpath("./head/style")
        javascript_elements = root_element.xpath("./head/script")
        body_element = root_element.xpath("./body")[0]
        head = ""
        for child in head_element.getchildren():
            head += etree.tostring(
                child,
                encoding="unicode",
                method="html",
            )
        body = ""
        for child in body_element.getchildren():
            body += etree.tostring(
                child,
                encoding="unicode",
                method="html",
            )
        title = title_element.text
        stylesheet = ""
        for style_element in style_elements:
            stylesheet += style_element.text
        javascript = ""
        for javascript_element in javascript_elements:
            javascript += etree.tostring(
                javascript_element,
                encoding="unicode",
                method="html",
            )
        return ConvertedMarkup(body, title, stylesheet, javascript)