Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit dcb57cd

Browse files
committed
docs: ext: literalinclude diff: Add diff-files option to literalinclude
Signed-off-by: John Andersen <johnandersenpdx@gmail.com>
1 parent 6ffafad commit dcb57cd

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

docs/_ext/literalinclude_diff.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
Override the literalinclude directive's show_diff method so we can override the
3+
diff'd files paths
4+
"""
5+
import functools
6+
from typing import List, Dict, Any
7+
8+
from docutils.nodes import Node
9+
from docutils.parsers.rst import directives
10+
11+
from sphinx.directives.code import LiteralInclude, LiteralIncludeReader
12+
13+
14+
def LiteralIncludeReader_show_diff(func):
15+
@functools.wraps(func)
16+
def wrapper(self) -> List[Node]:
17+
lines = func(self)
18+
19+
if "diff-files" in self.options:
20+
# Grab the filepath(s) to use from the diff-files option
21+
old_path = self.options["diff-files"].split(" ", maxsplit=1)
22+
# Ensure paths start with ./ if not given
23+
for i, file_path in enumerate(old_path):
24+
if file_path.startswith("/") or file_path.startswith("./"):
25+
continue
26+
old_path[i] = "./" + file_path
27+
# Check if we want to use the same filename for both, or different
28+
if len(old_path) == 2:
29+
new_path = old_path[1]
30+
else:
31+
new_path = old_path[0]
32+
old_path = old_path[0]
33+
# Preform the replacement
34+
lines[0] = f"--- {old_path}\n"
35+
lines[1] = f"+++ {new_path}\n"
36+
37+
return lines
38+
39+
return wrapper
40+
41+
42+
# For fixing up diff paths
43+
LiteralIncludeReader.show_diff = LiteralIncludeReader_show_diff(
44+
LiteralIncludeReader.show_diff
45+
)
46+
LiteralInclude.option_spec.update(
47+
{"diff-files": directives.unchanged_required}
48+
)
49+
50+
51+
def setup(app: "Sphinx") -> Dict[str, Any]:
52+
return {"version": "0.0.1", "parallel_read_safe": True}

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"sphinx_tabs.tabs",
4747
"recommonmark",
4848
"consoletest",
49+
"literalinclude_diff",
4950
]
5051

5152
intersphinx_mapping = {"python": ("https://docs.python.org/3", None)}

tests/docs/test_consoletest.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313
# Root of DFFML source tree
1414
ROOT_DIR = os.path.join(os.path.dirname(__file__), "..", "..")
1515

16-
# Load file by path
17-
spec = importlib.util.spec_from_file_location(
18-
"consoletest", os.path.join(ROOT_DIR, "docs", "_ext", "consoletest.py")
19-
)
20-
consoletest = importlib.util.module_from_spec(spec)
21-
spec.loader.exec_module(consoletest)
16+
# Load files by path. We have to import literalinclude_diff for diff-files
17+
for module_name in ["consoletest", "literalinclude_diff"]:
18+
spec = importlib.util.spec_from_file_location(
19+
module_name,
20+
os.path.join(ROOT_DIR, "docs", "_ext", f"{module_name}.py"),
21+
)
22+
module = importlib.util.module_from_spec(spec)
23+
spec.loader.exec_module(module)
24+
setattr(sys.modules[__name__], module_name, module)
2225

2326

2427
class TestFunctions(AsyncTestCase):

0 commit comments

Comments
 (0)