Skip to content

Commit a3f556c

Browse files
committed
Preprocess: handle new Ranges placeholder links
The site uses placeholder links that get replaced by JS to point to either Ranges TS or standard Ranges pages, depending on whether they are used on Ranges TS pages or not. This is necessary because the links are generated by the syntax highlighter, which doesn't know about the different targets. This change adds handling of those placeholder links to replace them by proper relative links.
1 parent ceaf61b commit a3f556c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

commands/preprocess.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,28 @@ def transform_loader_link(target, file, root):
173173
abstarget = os.path.join(root, "common/" + convert_loader_name(target))
174174
return os.path.relpath(abstarget, os.path.dirname(file))
175175

176+
def is_ranges_placeholder(target):
177+
if re.match(r'https?://[a-z]+\.cppreference\.com/w/cpp/ranges(-[a-z]+)?-placeholder/.+', target):
178+
return True
179+
return False
180+
181+
def transform_ranges_placeholder(target, file, root):
182+
ranges = 'cpp/experimental/ranges' in file
183+
repl = (r'\1/cpp/experimental/ranges/\2' if ranges else r'\1/cpp/\2')
184+
185+
if 'ranges-placeholder' in target:
186+
match = r'https?://([a-z]+)\.cppreference\.com/w/cpp/ranges-placeholder/(.+)'
187+
else:
188+
match = r'https?://([a-z]+)\.cppreference\.com/w/cpp/ranges-([a-z]+)-placeholder/(.+)'
189+
repl += (r'/\3' if ranges else r'/ranges/\3')
190+
191+
# Turn absolute placeholder link into site-relative link
192+
reltarget = re.sub(match, repl + '.html', target)
193+
194+
# Make site-relative link file-relative
195+
abstarget = os.path.join(root, reltarget)
196+
return os.path.relpath(abstarget, os.path.dirname(file))
197+
176198
def is_external_link(target):
177199
external_link_patterns = [
178200
'http://',
@@ -203,6 +225,9 @@ def transform_link(rename_map, target, file, root):
203225
if is_loader_link(target):
204226
return transform_loader_link(target, file, root)
205227

228+
if is_ranges_placeholder(target):
229+
return transform_ranges_placeholder(target, file, root)
230+
206231
if is_external_link(target):
207232
return target
208233

0 commit comments

Comments
 (0)