-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathhooks.py
More file actions
145 lines (119 loc) · 5.36 KB
/
Copy pathhooks.py
File metadata and controls
145 lines (119 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re
import os
def map_spec_filename(filename, version_str):
mapping = {
'a2ui_protocol.md': f'{version_str}-a2ui.md',
'evolution_guide.md': f'{version_str}-evolution-guide.md',
'a2ui_extension_specification.md': (
f'{version_str}-a2ui-extension-specification.md'
),
'basic_catalog_implementation_guide.md': (
f'{version_str}-basic-catalog-implementation-guide.md'
),
}
if version_str == 'v0.8' and filename == 'a2ui_extension_specification.md':
return 'v0.8-a2a-extension.md'
return mapping.get(filename)
def on_page_markdown(markdown, page, config, files):
github_base_url = 'https://github.com/a2ui-project/a2ui/blob/main'
# 1. Pre-expand snippet includes (--8<--)
for _ in range(5):
expanded = False
def snippet_replacer(match):
nonlocal expanded
snippet_path = match.group('snippet_path')
if os.path.exists(snippet_path):
with open(snippet_path, 'r', encoding='utf-8') as f:
content = f.read()
expanded = True
return content
return match.group(0)
regex = r'^\s*--8<--\s+["\'](?P<snippet_path>[^"\']+)["\']\s*$'
new_markdown = re.sub(regex, snippet_replacer, markdown, flags=re.MULTILINE)
if not expanded:
break
markdown = new_markdown
# 2. Extract version prefix from wrapper page filename (e.g. "v0.9.1-")
version_match = re.match(r'^(v\d+(?:\.\d+)*)-', page.file.name)
version_str = None
version_folder = None
if version_match:
version_str = version_match.group(1)
version_folder = version_str.replace('.', '_')
# Calculate file depth relative to docs dir
file_depth = len(page.file.src_path.split('/')) - 1
def replace_path(path):
if path.startswith(('http://', 'https://', 'mailto:', 'tel:')):
return path
parts = path.split('#')
base_path = parts[0]
anchor = f'#{parts[1]}' if len(parts) > 1 else ''
if base_path.startswith('./'):
base_path = base_path[2:]
if version_str:
mapped_name = map_spec_filename(base_path, version_str)
if mapped_name:
return mapped_name + anchor
# Rewrite relative paths to json schemas regardless of depth (e.g. "../json/..." or "../../../json/...")
if '/json/' in path:
rel_prefix = '../' * file_depth
return rel_prefix + version_folder + path[path.index('/json/') :]
# Check if the link points outside the docs folder
up_count = 0
temp_path = path
while temp_path.startswith('../'):
up_count += 1
temp_path = temp_path[3:]
if up_count > file_depth:
strip_count = up_count
# Remove the leading '../' sequences
path_parts = path.split('/')
while strip_count > 0 and path_parts and path_parts[0] == '..':
path_parts.pop(0)
strip_count -= 1
clean_path = '/'.join(path_parts)
# If the link points to another public documentation file, make it an internal link
if clean_path.startswith('docs/public/'):
rel_prefix = '../' * file_depth
return rel_prefix + clean_path[len('docs/public/') :]
# Return the newly formatted absolute GitHub link
return f'{github_base_url}/{clean_path}'
return path
def replace_standard_links(text_content):
def link_replacer(match):
if match.group('code'):
return match.group(0)
path = match.group('path')
new_path = replace_path(path)
matched_str = match.group(0)
start = match.start('path') - match.start(0)
end = match.end('path') - match.start(0)
return matched_str[:start] + new_path + matched_str[end:]
regex = r'(?P<code>`[^`]+`|```[\s\S]*?```)|\[(?P<text>[^\]]+)\]\((?P<path>[^\s)]+)(?:\s+(?P<title>".*?"|\'.*?\'))?\)'
return re.sub(regex, link_replacer, text_content)
def replace_reference_links(text_content):
def ref_replacer(match):
path = match.group('path')
new_path = replace_path(path)
matched_str = match.group(0)
start = match.start('path') - match.start(0)
end = match.end('path') - match.start(0)
return matched_str[:start] + new_path + matched_str[end:]
regex = r'^\s*\[(?P<ref>[^\]]+)\]:\s*(?P<path>[^\s]+)(?:\s+(?P<title>".*?"|\'.*?\'))?\s*$'
return re.sub(regex, ref_replacer, text_content, flags=re.MULTILINE)
markdown = replace_standard_links(markdown)
markdown = replace_reference_links(markdown)
return markdown