Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions .github/workflows/docutils_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Script to convert package setup to myst-docutils."""

import sys
from pathlib import Path

import tomlkit

Expand Down Expand Up @@ -43,15 +44,12 @@ def modify_readme(content: str) -> str:


if __name__ == "__main__":
project_path = sys.argv[1]
readme_path = sys.argv[2]
with open(project_path) as f:
content = f.read()
project_path = Path(sys.argv[1])
content = project_path.read_text()
content = modify_toml(content)
with open(project_path, "w") as f:
f.write(content)
with open(readme_path) as f:
content = f.read()
project_path.write_text(content)

readme_path = Path(sys.argv[2])
content = readme_path.read_text()
content = modify_readme(content)
with open(readme_path, "w") as f:
f.write(content)
readme_path.write_text(content)
5 changes: 3 additions & 2 deletions myst_parser/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import zlib
from collections.abc import Iterator
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import IO, TYPE_CHECKING, TypedDict
from urllib.request import urlopen

Expand Down Expand Up @@ -417,7 +418,7 @@ def fetch_inventory(
if uri.startswith(("http://", "https://")):
with urlopen(uri, timeout=timeout) as stream:
return load(stream, base_url=base_url)
with open(uri, "rb") as stream:
with Path(uri).open("rb") as stream:
return load(stream, base_url=base_url)


Expand Down Expand Up @@ -478,7 +479,7 @@ def inventory_cli(inputs: None | list[str] = None):
invdata = load(stream)
base_url = args.uri
else:
with open(args.uri, "rb") as stream:
with Path(args.uri).open("rb") as stream:
invdata = load(stream)

filtered: InventoryType = {
Expand Down
7 changes: 3 additions & 4 deletions myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from collections.abc import Callable, Iterable, Iterator, MutableMapping, Sequence
from contextlib import contextmanager, suppress
from datetime import date, datetime
from pathlib import Path
from types import ModuleType
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -1225,10 +1226,8 @@ def render_image(self, token: SyntaxTreeNode) -> None:
# make the path relative to an "including" document
# this is set when using the `relative-images` option of the MyST `include` directive
destination = os.path.normpath(
os.path.join(
self.md_env.get("relative-images", ""),
os.path.normpath(destination),
)
Path(self.md_env.get("relative-images", ""))
/ os.path.normpath(destination)
)

img_node["uri"] = destination
Expand Down
2 changes: 1 addition & 1 deletion myst_parser/mdit_to_docutils/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _handle_relative_docs(self, destination: str) -> str:
if relative_include is not None and destination.startswith(relative_include[0]):
source_dir, include_dir = relative_include[1:]
destination = os.path.relpath(
os.path.join(include_dir, os.path.normpath(destination)), source_dir
Path(include_dir) / os.path.normpath(destination), source_dir
)
return destination

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ exclude = [
]

[tool.ruff.lint]
extend-select = ["B", "C4", "FA", "FURB", "I", "ICN", "ISC", "N", "PERF", "PGH", "PIE", "RUF", "SIM", "UP"]
extend-select = ["B", "C4", "FA", "FURB", "I", "ICN", "ISC", "N", "PERF", "PGH", "PIE", "PTH", "RUF", "SIM", "UP"]
extend-ignore = ["ISC001", "RUF005", "RUF012"]

[tool.ruff.lint.per-file-ignores]
Expand Down
6 changes: 2 additions & 4 deletions tests/test_commonmark/test_commonmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
"""

import json
import os
from pathlib import Path

import pytest
from markdown_it.renderer import RendererHTML

from myst_parser.config.main import MdParserConfig
from myst_parser.parsers.mdit import create_md_parser

with open(
os.path.join(os.path.dirname(__file__), "commonmark.json"), encoding="utf8"
) as fin:
with (Path(__file__).parent / "commonmark.json").open(encoding="utf8") as fin:
tests = json.load(fin)


Expand Down
12 changes: 5 additions & 7 deletions tests/test_sphinx/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,23 @@ def test_basic(app, status, warning, get_sphinx_app_output):

"""

import os
import pathlib
import shutil
from pathlib import Path

import pytest
from bs4 import BeautifulSoup
from docutils import nodes

from myst_parser._compat import findall

SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs"))
SOURCE_DIR = (Path(__file__).parent / "sourcedirs").resolve()


@pytest.fixture(scope="session", autouse=True)
def remove_sphinx_builds():
"""remove all build directories from the test folder"""
yield
srcdirs = pathlib.Path(SOURCE_DIR)
for entry in srcdirs.iterdir(): # type: pathlib.Path
for entry in SOURCE_DIR.iterdir():
if entry.is_dir() and entry.joinpath("_build").exists():
shutil.rmtree(str(entry.joinpath("_build")))

Expand All @@ -66,7 +64,7 @@ def read(
regress_ext=".html",
replace=None,
):
outpath = pathlib.Path(str(app.srcdir), "_build", buildername, filename)
outpath = Path(app.srcdir) / "_build" / buildername / filename
if not outpath.exists():
raise OSError(f"no output file exists: {outpath}")

Expand Down Expand Up @@ -115,7 +113,7 @@ def read(
for node in findall(doctree)(
lambda n: "source" in n and not isinstance(n, str)
):
node["source"] = pathlib.Path(node["source"]).name
node["source"] = Path(node["source"]).name

doctree = doctree.deepcopy()

Expand Down
58 changes: 26 additions & 32 deletions tests/test_sphinx/test_sphinx_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import pytest
from sphinx.util.console import strip_colors

SOURCE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "sourcedirs"))
SOURCE_DIR = (Path(__file__).parent / "sourcedirs").resolve()


@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "basic"),
srcdir=SOURCE_DIR / "basic",
freshenv=True,
confoverrides={"myst_enable_extensions": ["dollarmath"]},
)
Expand Down Expand Up @@ -77,7 +77,7 @@ def test_basic(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "references"),
srcdir=SOURCE_DIR / "references",
freshenv=True,
confoverrides={
"myst_enable_extensions": ["dollarmath"],
Expand Down Expand Up @@ -118,7 +118,7 @@ def test_references(

@pytest.mark.sphinx(
buildername="singlehtml",
srcdir=os.path.join(SOURCE_DIR, "references_singlehtml"),
srcdir=SOURCE_DIR / "references_singlehtml",
freshenv=True,
confoverrides={"nitpicky": True},
)
Expand Down Expand Up @@ -177,7 +177,7 @@ def test_references_singlehtml(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "heading_slug_func"),
srcdir=SOURCE_DIR / "heading_slug_func",
freshenv=True,
)
def test_heading_slug_func(
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_heading_slug_func(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "extended_syntaxes"),
srcdir=SOURCE_DIR / "extended_syntaxes",
freshenv=True,
)
def test_extended_syntaxes(
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_extended_syntaxes(

@pytest.mark.sphinx(
buildername="text",
srcdir=os.path.join(SOURCE_DIR, "extended_syntaxes"),
srcdir=SOURCE_DIR / "extended_syntaxes",
freshenv=True,
)
def test_extended_syntaxes_text(
Expand Down Expand Up @@ -276,9 +276,7 @@ def test_extended_syntaxes_text(
sys.platform == "win32",
reason="original_uri attribute handling differs on Windows",
)
@pytest.mark.sphinx(
buildername="html", srcdir=os.path.join(SOURCE_DIR, "includes"), freshenv=True
)
@pytest.mark.sphinx(buildername="html", srcdir=SOURCE_DIR / "includes", freshenv=True)
def test_includes(
app,
status,
Expand Down Expand Up @@ -324,7 +322,7 @@ def test_includes(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "include_from_rst"),
srcdir=SOURCE_DIR / "include_from_rst",
freshenv=True,
)
def test_include_from_rst(
Expand All @@ -348,9 +346,7 @@ def test_include_from_rst(
)


@pytest.mark.sphinx(
buildername="html", srcdir=os.path.join(SOURCE_DIR, "footnotes"), freshenv=True
)
@pytest.mark.sphinx(buildername="html", srcdir=SOURCE_DIR / "footnotes", freshenv=True)
def test_footnotes(
app,
status,
Expand Down Expand Up @@ -392,7 +388,7 @@ def test_footnotes(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "commonmark_only"),
srcdir=SOURCE_DIR / "commonmark_only",
freshenv=True,
)
def test_commonmark_only(
Expand Down Expand Up @@ -421,7 +417,7 @@ def test_commonmark_only(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "substitutions"),
srcdir=SOURCE_DIR / "substitutions",
freshenv=True,
)
def test_substitutions(
Expand Down Expand Up @@ -453,7 +449,7 @@ def test_substitutions(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "substitutions_missing"),
srcdir=SOURCE_DIR / "substitutions_missing",
freshenv=True,
)
def test_substitutions_missing(
Expand All @@ -472,9 +468,7 @@ def test_substitutions_missing(
)


@pytest.mark.sphinx(
buildername="gettext", srcdir=os.path.join(SOURCE_DIR, "gettext"), freshenv=True
)
@pytest.mark.sphinx(buildername="gettext", srcdir=SOURCE_DIR / "gettext", freshenv=True)
def test_gettext(
app,
status,
Expand All @@ -501,7 +495,7 @@ def test_gettext(
)
@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "gettext"),
srcdir=SOURCE_DIR / "gettext",
freshenv=True,
confoverrides={"language": "fr", "gettext_compact": False, "locale_dirs": ["."]},
)
Expand Down Expand Up @@ -542,7 +536,7 @@ def test_gettext_html(

@pytest.mark.sphinx(
buildername="gettext",
srcdir=os.path.join(SOURCE_DIR, "gettext"),
srcdir=SOURCE_DIR / "gettext",
freshenv=True,
confoverrides={
"gettext_additional_targets": [
Expand Down Expand Up @@ -576,7 +570,7 @@ def test_gettext_additional_targets(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "mathjax"),
srcdir=SOURCE_DIR / "mathjax",
freshenv=True,
confoverrides={"myst_enable_extensions": ["dollarmath"]},
)
Expand All @@ -597,7 +591,7 @@ def test_mathjax_warning(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "mathjax_default"),
srcdir=SOURCE_DIR / "mathjax_default",
freshenv=True,
confoverrides={"myst_enable_extensions": ["dollarmath"]},
)
Expand All @@ -623,7 +617,7 @@ def test_mathjax_default(

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "mathjax_default"),
srcdir=SOURCE_DIR / "mathjax_default",
freshenv=True,
confoverrides={
"myst_enable_extensions": ["dollarmath"],
Expand Down Expand Up @@ -651,7 +645,7 @@ def test_mathjax_no_override(
)
@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "mathjax4"),
srcdir=SOURCE_DIR / "mathjax4",
freshenv=True,
confoverrides={"myst_enable_extensions": ["dollarmath"]},
)
Expand All @@ -676,7 +670,7 @@ def test_mathjax4_warning(
)
@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "mathjax"),
srcdir=SOURCE_DIR / "mathjax",
freshenv=True,
confoverrides={"myst_enable_extensions": ["dollarmath"]},
)
Expand All @@ -702,7 +696,7 @@ def test_mathjax3_config_on_sphinx9(
)
@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "fieldlist"),
srcdir=SOURCE_DIR / "fieldlist",
freshenv=True,
)
def test_fieldlist_extension(
Expand Down Expand Up @@ -736,7 +730,7 @@ def test_fieldlist_extension(

@pytest.mark.sphinx(
buildername="texinfo",
srcdir=os.path.join(SOURCE_DIR, "texinfo"),
srcdir=SOURCE_DIR / "texinfo",
freshenv=True,
)
def test_texinfo(app, status, warning):
Expand All @@ -749,7 +743,7 @@ def test_texinfo(app, status, warning):

@pytest.mark.sphinx(
buildername="html",
srcdir=os.path.join(SOURCE_DIR, "includes"),
srcdir=SOURCE_DIR / "includes",
freshenv=True,
)
def test_include_read_event(app, status, warning):
Expand All @@ -770,15 +764,15 @@ def handle_include_read(
expected = [
("../include_from_rst/include.md", "index"),
("include1.inc.md", "index"),
(os.path.join("subfolder", "include2.inc.md"), "include1.inc"),
(str(Path("subfolder", "include2.inc.md")), "include1.inc"),
("include_code.py", "index"),
("include_code.py", "index"),
("include_literal.txt", "index"),
("include_literal.txt", "index"),
]
expected_events = []
for include_file_name, parent_docname in expected:
with open(os.path.join(SOURCE_DIR, "includes", include_file_name)) as file:
with (SOURCE_DIR / "includes" / include_file_name).open() as file:
content = file.read()
expected_events.append((Path(include_file_name), parent_docname, [content]))
assert len(include_read_events) == len(expected_events), "Wrong number of events"
Expand Down
Loading