Skip to content

Commit

Permalink
feat: remove mdinclude as the normal include directive supports custo…
Browse files Browse the repository at this point in the history
…m parsers now
  • Loading branch information
vberlier committed May 31, 2022
1 parent 3772151 commit f7da3f0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 92 deletions.
18 changes: 0 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,6 @@ Mudkip enables the following Sphinx extensions:
- [`myst_parser`](https://myst-parser.readthedocs.io/en/latest/) for markdown support
- [`myst_nb`](https://myst-nb.readthedocs.io/en/latest/) for Jupyter notebook support

Additional features are provided through an internal extension.

- `mdinclude` directive

The built-in `include` directive doesn't handle markdown so mudkip provides an `mdinclude` directive to include markdown files.

```reStructuredText
.. mdinclude:: ../README.md
```

It's also possible to only include a specific section of the file with the `start-line` and `end-line` options.

```reStructuredText
.. mdinclude:: ../README.md
:start-line: 7
:end-line: -3
```

## Installation

The package can be installed with `pip`.
Expand Down
7 changes: 3 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ PyPI Package <https://pypi.org/project/mudkip>

---

```{eval-rst}
.. mdinclude:: ../README.md
:start-line: 9
:end-line: -3
```{include} ../README.md
:start-line: 9
:end-line: -3
```
70 changes: 0 additions & 70 deletions mudkip/extension.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import os

from docutils.core import ErrorString
from docutils.io import FileInput
from docutils.parsers import rst
from docutils.utils import new_document, relative_path
from myst_parser.sphinx_parser import MystParser

from . import __version__
from .vitepress import VitePressBuilder

Expand All @@ -21,70 +13,8 @@ def process_doctree(app, doctree, docname):
doctree[name] = value


class MdInclude(rst.Directive):
"""Directive to include a markdown file.
Adapted from https://github.com/miyakogi/m2r/blob/dev/m2r.py
"""

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

def run(self):
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))
file_path = rst.directives.path(self.arguments[0])
absolute_path = os.path.normpath(os.path.join(source_dir, file_path))
path = relative_path(None, absolute_path)

settings = self.state.document.settings

try:
settings.record_dependencies.add(path)
include_file = FileInput(
source_path=path,
encoding=self.options.get("encoding", settings.input_encoding),
error_handler=settings.input_encoding_error_handler,
)
except UnicodeEncodeError as error:
raise self.severe(
f'Problems with "{self.name}" directive path:\n'
f'Cannot encode input file path "{path}" '
"(wrong locale?)."
)
except IOError as error:
raise self.severe(
f'Problems with "{self.name}" directive path:\n{ErrorString(error)}.'
)

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(
f'Problem with "{self.name}" directive:\n{ErrorString(error)}'
)

document = new_document(absolute_path, settings)
MystParser().parse(rawtext, document)

return document.children


def setup(app):
app.connect("doctree-resolved", process_doctree)
app.add_directive("mdinclude", MdInclude)

app.add_builder(VitePressBuilder)

Expand Down

0 comments on commit f7da3f0

Please sign in to comment.