Skip to content

Commit 0673712

Browse files
Documentation: replace relative image paths in Markdown image references with GitHub URLs
1 parent a15672f commit 0673712

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

Doc/md_filter.py

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import re
44

55

6-
def compute_page_id(input_filepath):
7-
# Determine the directory of this script.
8-
script_dir = os.path.dirname(os.path.abspath(__file__))
9-
# The project root is one level up.
10-
root_dir = os.path.dirname(script_dir)
6+
def compute_page_id(root_dir, input_filepath):
117
# Compute the file's absolute path and then its relative path from the root directory.
128
abs_input_path = os.path.abspath(input_filepath)
139
rel_path = os.path.relpath(abs_input_path, root_dir)
@@ -45,8 +41,63 @@ def replace_github_blob_urls(text):
4541
return re.sub(pattern, r'\1raw/\3', text)
4642

4743

44+
def replace_relative_image_paths(text, root_dir, input_filepath):
45+
"""
46+
Replaces relative image paths in Markdown image references with GitHub URLs.
47+
48+
Markdown references of the form:
49+
![Alt Text](relative/image/path)
50+
51+
are replaced as follows:
52+
1. Compute the full relative path of the image with respect to the project root.
53+
The project root is defined as one level up from the directory of this script.
54+
2. If the first-level folder in the computed path starts with "Diligent", treat it as a submodule:
55+
https://github.com/DiligentGraphics/<Submodule>/raw/master/<rest_of_path>
56+
Otherwise, assume it belongs to the root module:
57+
https://github.com/DiligentGraphics/raw/master/<computed_relative_path>
58+
"""
59+
60+
# Regular expression to match Markdown image references.
61+
pattern = r'!\[([^\]]*)\]\(([^)]+)\)'
62+
63+
def repl(match):
64+
alt_text = match.group(1)
65+
image_path = match.group(2)
66+
# If the path is already absolute (starts with "http"), leave it unchanged.
67+
if image_path.startswith("http"):
68+
return match.group(0)
69+
70+
# Compute the full path of the image relative to the project root.
71+
# The image path is relative to the input file location.
72+
input_dir = os.path.dirname(os.path.abspath(input_filepath))
73+
full_image_path = os.path.join(input_dir, image_path)
74+
# Now compute the relative path from the project root.
75+
relative_path = os.path.relpath(full_image_path, root_dir)
76+
# Convert OS-specific path separators to forward slashes.
77+
relative_path_url = relative_path.replace(os.sep, '/')
78+
79+
# Split the path to check the first folder.
80+
parts = relative_path_url.split('/')
81+
if parts and parts[0].startswith("Diligent"):
82+
module = parts[0]
83+
rest = '/'.join(parts[1:])
84+
new_url = f"https://github.com/DiligentGraphics/{module}/raw/master/{rest}"
85+
else:
86+
new_url = f"https://github.com/DiligentGraphics/raw/master/{relative_path_url}"
87+
88+
return f"![{alt_text}]({new_url})"
89+
90+
# Replace all Markdown image references in the text.
91+
return re.sub(pattern, repl, text)
92+
93+
4894
def process_content(input_filepath, lines):
49-
page_id = compute_page_id(input_filepath)
95+
# Determine the directory of this script.
96+
script_dir = os.path.dirname(os.path.abspath(__file__))
97+
# The project root is one level up.
98+
root_dir = os.path.dirname(script_dir)
99+
100+
page_id = compute_page_id(root_dir, input_filepath)
50101
output_lines = []
51102
header_regex = re.compile(r'^\s*(#+)\s*(.+?)\s*$')
52103
header_replaced = False
@@ -58,6 +109,9 @@ def process_content(input_filepath, lines):
58109
# Fix image URLs.
59110
line = replace_github_blob_urls(line)
60111

112+
# Replace relative image paths in Markdown image references with GitHub URLs.
113+
line = replace_relative_image_paths(line, root_dir, input_filepath)
114+
61115
# Look for the first non-empty line that starts with a Markdown header.
62116
if not header_replaced and line.strip():
63117
match = header_regex.match(line)

0 commit comments

Comments
 (0)