Skip to content

Commit 8979222

Browse files
resolve links
1 parent 900b60e commit 8979222

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ $(ASCIIDOC_DOXYGEN_DIR)/picosdk_index.json $(ASCIIDOC_DOXYGEN_DIR)/index_doxygen
8282
python3 -m doxygentoasciidoc -f $(DOXYGEN_XML_DIR)/index.xml > $(ASCIIDOC_DOXYGEN_DIR)/all_groups.adoc
8383
python3 -m doxygentoasciidoc -f $(DOXYGEN_XML_DIR)/indexpage.xml -c > $(ASCIIDOC_DOXYGEN_DIR)/index_doxygen.adoc
8484
python3 -m doxygentoasciidoc -f $(DOXYGEN_XML_DIR)/examples_page.xml -c > $(ASCIIDOC_DOXYGEN_DIR)/examples_page.adoc
85-
python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)/all_groups.adoc $(ASCIIDOC_DOXYGEN_DIR)
86-
python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)/index_doxygen.adoc $(ASCIIDOC_DOXYGEN_DIR)
87-
python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)/examples_page.adoc $(ASCIIDOC_DOXYGEN_DIR)
85+
# python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)/all_groups.adoc $(ASCIIDOC_DOXYGEN_DIR)
86+
# python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)/index_doxygen.adoc $(ASCIIDOC_DOXYGEN_DIR)
87+
python3 $(SCRIPTS_DIR)/postprocess_doxygen_adoc.py $(ASCIIDOC_DOXYGEN_DIR)
8888
-cp $(DOXYGEN_XML_DIR)/*.png $(ASCIIDOC_DOXYGEN_DIR)
8989

9090
build_doxygen_adoc: $(ASCIIDOC_DOXYGEN_DIR)/index_doxygen.adoc

scripts/postprocess_doxygen_adoc.py

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,46 @@
77
# collect all link anchors in the file
88
# then open each file, find all link, point to the correct anchor
99

10-
def cleanup_text_page(adoc_file, output_adoc_path):
11-
script_path = os.path.realpath(__file__)
12-
top_dir_path = re.sub(r'/scripts/.*$', "", script_path)
13-
output_path = os.path.join(top_dir_path, adoc_file)
10+
def cleanup_text_page(adoc_file, output_adoc_path, link_targets):
11+
filename = os.path.basename(adoc_file)
12+
# script_path = os.path.realpath(__file__)
13+
# top_dir_path = re.sub(r'/scripts/.*$', "", script_path)
14+
# output_path = os.path.join(top_dir_path, adoc_file)
1415
with open(adoc_file) as f:
1516
adoc_content = f.read()
1617
# remove any errant spaces before anchors
1718
adoc_content = re.sub(r'( +)(\[\[[^[]*?\]\])', "\\2", adoc_content)
18-
with open(output_path, 'w') as f:
19+
# collect link targets
20+
for line in adoc_content.split('\n'):
21+
link_targets = collect_link_target(line, filename)
22+
with open(adoc_file, 'w') as f:
1923
f.write(adoc_content)
24+
return link_targets
25+
26+
def collect_link_target(line, chapter_filename):
27+
# collect a list of all link targets, so we can fix internal links
28+
l = re.search(r'(#)([^,\]]+)([,\]])', line)
29+
if l is not None:
30+
link_targets[l.group(2)] = chapter_filename
31+
return link_targets
32+
33+
def resolve_links(adoc_file, link_targets):
34+
filename = os.path.basename(adoc_file)
35+
with open(adoc_file) as f:
36+
adoc_content = f.read()
37+
output_content = []
38+
for line in adoc_content.split('\n'):
39+
# e.g., <<examples_page,here>>
40+
m = re.search("(<<)([^,]+)(,?[^>]*>>)", line)
41+
if m is not None:
42+
target = m.group(2)
43+
# only resolve link if it points to another file
44+
if target in link_targets and link_targets[target] != filename:
45+
new_target = link_targets[target]+"#"+target
46+
line = re.sub("(<<)([^,]+)(,?[^>]*>>)", f"\\1{new_target}\\3", line)
47+
output_content.append(line)
48+
with open(adoc_file, 'w') as f:
49+
f.write('\n'.join(output_content))
2050
return
2151

2252
def build_json(sections, output_path):
@@ -46,22 +76,19 @@ def tag_content(adoc_content):
4676
adoc_content = re.sub(NONHEADING_RE, f'[.contexttag \\2]*\\2*\n\n\\1\\2\\3', adoc_content)
4777
return adoc_content
4878

49-
def postprocess_doxygen_adoc(adoc_file, output_adoc_path):
79+
def postprocess_doxygen_adoc(adoc_file, output_adoc_path, link_targets):
80+
output_path = re.sub(r'[^/]+$', "", adoc_file)
5081
sections = [{
5182
"group_id": "index_doxygen",
5283
"name": "Introduction",
5384
"description": "An introduction to the Pico SDK",
5485
"html": "index_doxygen.html",
5586
"subitems": []
5687
}]
57-
script_path = os.path.realpath(__file__)
58-
top_dir_path = re.sub(r'/scripts/.*$', "", script_path)
59-
output_path = os.path.join(top_dir_path, output_adoc_path)
6088
with open(adoc_file) as f:
6189
adoc_content = f.read()
6290
# first, lets add any tags
6391
adoc_content = tag_content(adoc_content)
64-
6592
# now split the file into top-level sections:
6693
# toolchain expects all headings to be two levels lower
6794
adoc_content = re.sub(r'(\n==)(=+ \S+)', "\n\\2", adoc_content)
@@ -72,21 +99,24 @@ def postprocess_doxygen_adoc(adoc_file, output_adoc_path):
7299
CHAPTER_START_RE = re.compile(r'(\[#)(.*?)(,reftext=".*?"\]= )(.*?$)')
73100
# check line by line; if the line matches our chapter break,
74101
# then pull all following lines into the chapter list until a new match.
102+
chapter_filename = "all_groups.adoc"
75103
current_chapter = None
76104
chapter_dict = {}
77105
counter = 0
78106
for line in adoc_content.split('\n'):
107+
link_targets = collect_link_target(line, chapter_filename)
79108
m = CHAPTER_START_RE.match(line)
80109
if m is not None:
81110
# write the previous chapter
82111
if current_chapter is not None:
83-
with open(chapter_filename, 'w') as f:
112+
with open(chapter_path, 'w') as f:
84113
f.write('\n'.join(current_chapter))
85114
# start the new chapter
86115
current_chapter = []
87116
# set the data for this chapter
88117
group_id = re.sub("^group_+", "", m.group(2))
89-
chapter_filename = os.path.join(output_path, group_id+".adoc")
118+
chapter_filename = group_id+".adoc"
119+
chapter_path = os.path.join(output_path, chapter_filename)
90120
chapter_dict = {
91121
"group_id": group_id,
92122
"html": group_id+".html",
@@ -103,16 +133,24 @@ def postprocess_doxygen_adoc(adoc_file, output_adoc_path):
103133
current_chapter.append(line)
104134
# write the last chapter
105135
if current_chapter is not None:
106-
with open(chapter_filename, 'w') as f:
136+
with open(chapter_path, 'w') as f:
107137
f.write('\n'.join(current_chapter))
108138
build_json(sections, output_path)
109139
os.remove(adoc_file)
110-
return
140+
return link_targets
111141

112142
if __name__ == '__main__':
113-
adoc_file = sys.argv[1]
114-
output_adoc_path = sys.argv[2]
115-
if re.search("all_groups.adoc", adoc_file) is not None:
116-
postprocess_doxygen_adoc(adoc_file, output_adoc_path)
117-
else:
118-
cleanup_text_page(adoc_file, output_adoc_path)
143+
output_adoc_path = sys.argv[1]
144+
adoc_files = [f for f in os.listdir(output_adoc_path) if re.search(".adoc", f) is not None]
145+
link_targets = {}
146+
for adoc_file in adoc_files:
147+
adoc_filepath = os.path.join(output_adoc_path, adoc_file)
148+
if re.search("all_groups.adoc", adoc_file) is not None:
149+
link_targets = postprocess_doxygen_adoc(adoc_filepath, output_adoc_path, link_targets)
150+
else:
151+
link_targets = cleanup_text_page(adoc_filepath, output_adoc_path, link_targets)
152+
# now that we have a complete list of all link targets, resolve all internal links
153+
adoc_files = [f for f in os.listdir(output_adoc_path) if re.search(".adoc", f) is not None]
154+
for adoc_file in adoc_files:
155+
adoc_filepath = os.path.join(output_adoc_path, adoc_file)
156+
resolve_links(adoc_filepath, link_targets)

0 commit comments

Comments
 (0)