File: sphinx.py

package info (click to toggle)
sphinx-mdinclude 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: python: 1,242; makefile: 53; sh: 8
file content (142 lines) | stat: -rw-r--r-- 5,144 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
Sphinx extension
"""

import os
import os.path
from typing import Any, Dict, List, Union

from docutils import io, statemachine, utils
from docutils.io import error_string as ErrorString
from docutils.nodes import document as Document
from docutils.parsers import rst
from docutils.parsers.rst import directives as rst_directives
from sphinx.application import Sphinx

from . import RestMarkdown
from .__version__ import __version__


class MdIncludeParser(rst.Parser, object):
    # Explicitly tell supported formats to sphinx
    supported = ("markdown", "md", "mkd")

    def parse(
        self,
        inputstrings: Union[str, statemachine.StringList],
        document: Document,
    ) -> None:
        if isinstance(inputstrings, statemachine.StringList):
            inputstring = "\n".join(inputstrings)
        else:
            inputstring = inputstrings
        config = document.settings.env.config
        converter = RestMarkdown(
            no_underscore_emphasis=config.no_underscore_emphasis,
            parse_relative_links=config.md_parse_relative_links,
            anonymous_references=config.md_anonymous_references,
            disable_inline_math=config.md_disable_inline_math,
        )
        super().parse(converter(inputstring), document)


class MdInclude(rst.Directive):
    """Directive class to include markdown in sphinx.

    Load a file and convert it to rst and insert as a node. Currently
    directive-specific options are not implemented.
    """

    required_arguments = 1
    optional_arguments = 0
    option_spec = {
        "start-line": int,
        "end-line": int,
    }

    def run(self) -> List[Any]:
        """Most of this method is from ``docutils.parser.rst.Directive``.

        docutils version: 0.12
        """
        if not self.state.document.settings.file_insertion_enabled:
            raise self.warning('"%s" directive disabled.' % self.name)
        source = self.state_machine.input_lines.source(
            self.lineno - self.state_machine.input_offset - 1
        )
        source_dir = os.path.dirname(os.path.abspath(source))
        path = rst_directives.path(self.arguments[0])
        path = os.path.normpath(os.path.join(source_dir, path))
        path = utils.relative_path(None, path)
        path = str(path)

        # get options (currently not use directive-specific options)
        encoding = self.options.get(
            "encoding", self.state.document.settings.input_encoding
        )
        e_handler = self.state.document.settings.input_encoding_error_handler
        tab_width = self.options.get(
            "tab-width", self.state.document.settings.tab_width
        )

        # open the including file
        try:
            self.state.document.settings.record_dependencies.add(path)
            include_file = io.FileInput(
                source_path=path, encoding=encoding, error_handler=e_handler
            )
        except UnicodeEncodeError:
            raise self.severe(
                'Problems with "%s" directive path:\n'
                'Cannot encode input file path "%s" '
                "(wrong locale?)." % (self.name, str(path))
            )
        except IOError as error:
            raise self.severe(
                'Problems with "%s" directive path:\n%s.'
                % (self.name, ErrorString(error))
            )

        # read from the file
        startline = self.options.get("start-line", None)
        endline = self.options.get("end-line", None)
        try:
            if startline or (endline is not None):
                lines = include_file.readlines()
                rawtext = "".join(lines[startline:endline])
            else:
                rawtext = include_file.read()
        except UnicodeError as error:
            raise self.severe(
                'Problem with "%s" directive:\n%s' % (self.name, ErrorString(error))
            )

        config = self.state.document.settings.env.config
        converter = RestMarkdown(
            no_underscore_emphasis=config.no_underscore_emphasis,
            parse_relative_links=config.md_parse_relative_links,
            anonymous_references=config.md_anonymous_references,
            disable_inline_math=config.md_disable_inline_math,
        )
        include_lines = statemachine.string2lines(
            converter(rawtext), tab_width, convert_whitespace=True
        )
        self.state_machine.insert_input(include_lines, path)
        return []


def setup(app: Sphinx) -> Dict[str, Union[str, bool]]:
    """When used for sphinx extension."""
    app.add_config_value("no_underscore_emphasis", False, "env")
    app.add_config_value("md_parse_relative_links", False, "env")
    app.add_config_value("md_anonymous_references", False, "env")
    app.add_config_value("md_disable_inline_math", False, "env")
    app.add_source_suffix(".md", "markdown")
    app.add_source_parser(MdIncludeParser)
    app.add_directive("mdinclude", MdInclude)
    metadata: Dict[str, Union[str, bool]] = {
        "version": __version__,
        "parallel_read_safe": True,
        "parallel_write_safe": True,
    }
    return metadata