Skip to content

Commit 6569cda

Browse files
committed
Add ability to output docs as markdown and pdf
1 parent cfdd0ab commit 6569cda

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

docs/assemble-md.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import os
2+
import sys
3+
import markdown2
4+
5+
def adjust_image_paths(content, current_file_dir, output_base_dir):
6+
"""
7+
Adjust relative image paths to be relative to the output base directory and use forward slashes.
8+
"""
9+
lines = content.split('\n')
10+
for i, line in enumerate(lines):
11+
if '](' in line:
12+
#if '![' in line and '](' in line:
13+
start_link = line.find('](') + 2
14+
end_link = line.find(')', start_link)
15+
relative_path = line[start_link:end_link]
16+
if not os.path.isabs(relative_path):
17+
# Calculate the relative path from the current file directory to the image
18+
image_path = os.path.normpath(os.path.join(current_file_dir, relative_path))
19+
relative_to_output = os.path.relpath(image_path, output_base_dir)
20+
# Convert backslashes to forward slashes
21+
relative_to_output = relative_to_output.replace('\\', '/')
22+
lines[i] = line[:start_link] + relative_to_output + line[end_link:]
23+
return '\n'.join(lines)
24+
25+
def assemble_markdown_files(index_file, output_file_base):
26+
markdown_content = ""
27+
toc_content = ""
28+
base_path = os.path.dirname(os.path.abspath(index_file))
29+
output_base_dir = os.path.dirname(os.path.abspath(output_file_base + '.md'))
30+
31+
with open(index_file, 'r') as file:
32+
lines = file.readlines()
33+
34+
for line in lines:
35+
if ' [' in line:
36+
start_link = line.find('(') + 1
37+
end_link = line.find(')')
38+
file_link = line[start_link:end_link].strip()
39+
file_path = os.path.normpath(os.path.join(base_path, file_link))
40+
41+
# Generate clean anchors for ToC links
42+
anchor_link = f'#{os.path.basename(file_link).replace(".md", "").replace(" ", "-").lower()}'
43+
toc_line = line.replace(file_link, anchor_link)
44+
toc_content += toc_line
45+
46+
# Check file type and skip non-markdown files
47+
if file_link.lower().endswith('.md') and os.path.exists(file_path):
48+
with open(file_path, 'r') as infile:
49+
file_contents = infile.read()
50+
file_contents = adjust_image_paths(file_contents, os.path.dirname(file_path), output_base_dir)
51+
file_contents = file_contents.replace("\n#", "\n##") # Downgrade headers
52+
markdown_content += f"\n<a id='{os.path.basename(file_link).replace('.md', '').replace(' ', '-').lower()}'></a>\n\n----\n\n"
53+
markdown_content += file_contents + '\n'
54+
elif not file_link.lower().endswith('.md'):
55+
toc_content += f" (External file not included in compilation)\n"
56+
else:
57+
markdown_content += f"# File not found: {file_link}\n\n"
58+
else:
59+
toc_content += line
60+
markdown_content += line
61+
62+
complete_markdown = toc_content + markdown_content
63+
with open(output_file_base + '.md', 'w') as outfile:
64+
outfile.write(complete_markdown)
65+
66+
if __name__ == "__main__":
67+
index_filename = 'README.md'
68+
output_filename_base = 'Complete'
69+
70+
if len(sys.argv) > 1:
71+
index_filename = sys.argv[1]
72+
if len(sys.argv) > 2:
73+
output_filename_base = sys.argv[2]
74+
75+
assemble_markdown_files(index_filename, output_filename_base)

docs/conf.py

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"sphinx.ext.napoleon",
1818
"sphinx_rtd_theme",
1919
"sphinxcontrib.yowasp_wavedrom",
20+
"sphinx_markdown_builder",
21+
"rst2pdf.pdfbuilder",
2022
]
2123

2224
with open(".gitignore") as f:
@@ -57,3 +59,7 @@
5759
.. role:: py(code)
5860
:language: python
5961
"""
62+
63+
pdf_documents = [
64+
('index', 'amaranth-soc', project, ' Amaranth project contributors'),
65+
]

pyproject.toml

+15-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ license = {file = "LICENSE.txt"}
1313

1414
requires-python = "~=3.8"
1515
dependencies = [
16-
# this version requirement needs to be synchronized with the one in .github/workflows/main.yml
17-
"amaranth>=0.5,<0.6",
16+
# this version requirement needs to be synchronized with the one in .github/workflows/main.yml
17+
"amaranth>=0.5,<0.6",
18+
"markdown2>=2.5.1",
1819
]
1920

2021
[project.urls]
@@ -40,6 +41,8 @@ docs = [
4041
"sphinxcontrib-yowasp-wavedrom~=1.0",
4142
"sphinx-rtd-theme~=1.2",
4243
"sphinx-autobuild",
44+
"sphinx-markdown-builder>=0.6.7",
45+
"rst2pdf>=0.102",
4346
]
4447

4548
[tool.pdm.scripts]
@@ -50,6 +53,16 @@ test-docs.cmd = "sphinx-build -b doctest docs docs/_build"
5053

5154
document.cmd = "sphinx-build docs docs/_build"
5255
document-live.cmd = "sphinx-autobuild docs docs/_build --watch amaranth_soc"
56+
document-md.composite = [
57+
"sphinx-build -M markdown docs docs/_build",
58+
"docs/assemble-md.py docs/_build/markdown/index.md docs/amaranth-soc.md",
59+
"echo Markdown documentation created at docs/amaranth-soc.md"
60+
]
61+
document-pdf.composite = [
62+
"sphinx-build -b pdf docs docs/_build",
63+
"cp docs/_build/amaranth-soc.pdf docs",
64+
"echo PDF documentation created at docs/amaranth-soc.pdf"
65+
]
5366

5467
coverage-text.cmd = "python -m coverage report"
5568
coverage-html.cmd = "python -m coverage html"

0 commit comments

Comments
 (0)