7
7
# collect all link anchors in the file
8
8
# then open each file, find all link, point to the correct anchor
9
9
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)
14
15
with open (adoc_file ) as f :
15
16
adoc_content = f .read ()
16
17
# remove any errant spaces before anchors
17
18
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 :
19
23
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 ))
20
50
return
21
51
22
52
def build_json (sections , output_path ):
@@ -46,22 +76,19 @@ def tag_content(adoc_content):
46
76
adoc_content = re .sub (NONHEADING_RE , f'[.contexttag \\ 2]*\\ 2*\n \n \\ 1\\ 2\\ 3' , adoc_content )
47
77
return adoc_content
48
78
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 )
50
81
sections = [{
51
82
"group_id" : "index_doxygen" ,
52
83
"name" : "Introduction" ,
53
84
"description" : "An introduction to the Pico SDK" ,
54
85
"html" : "index_doxygen.html" ,
55
86
"subitems" : []
56
87
}]
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 )
60
88
with open (adoc_file ) as f :
61
89
adoc_content = f .read ()
62
90
# first, lets add any tags
63
91
adoc_content = tag_content (adoc_content )
64
-
65
92
# now split the file into top-level sections:
66
93
# toolchain expects all headings to be two levels lower
67
94
adoc_content = re .sub (r'(\n==)(=+ \S+)' , "\n \\ 2" , adoc_content )
@@ -72,21 +99,24 @@ def postprocess_doxygen_adoc(adoc_file, output_adoc_path):
72
99
CHAPTER_START_RE = re .compile (r'(\[#)(.*?)(,reftext=".*?"\]= )(.*?$)' )
73
100
# check line by line; if the line matches our chapter break,
74
101
# then pull all following lines into the chapter list until a new match.
102
+ chapter_filename = "all_groups.adoc"
75
103
current_chapter = None
76
104
chapter_dict = {}
77
105
counter = 0
78
106
for line in adoc_content .split ('\n ' ):
107
+ link_targets = collect_link_target (line , chapter_filename )
79
108
m = CHAPTER_START_RE .match (line )
80
109
if m is not None :
81
110
# write the previous chapter
82
111
if current_chapter is not None :
83
- with open (chapter_filename , 'w' ) as f :
112
+ with open (chapter_path , 'w' ) as f :
84
113
f .write ('\n ' .join (current_chapter ))
85
114
# start the new chapter
86
115
current_chapter = []
87
116
# set the data for this chapter
88
117
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 )
90
120
chapter_dict = {
91
121
"group_id" : group_id ,
92
122
"html" : group_id + ".html" ,
@@ -103,16 +133,24 @@ def postprocess_doxygen_adoc(adoc_file, output_adoc_path):
103
133
current_chapter .append (line )
104
134
# write the last chapter
105
135
if current_chapter is not None :
106
- with open (chapter_filename , 'w' ) as f :
136
+ with open (chapter_path , 'w' ) as f :
107
137
f .write ('\n ' .join (current_chapter ))
108
138
build_json (sections , output_path )
109
139
os .remove (adoc_file )
110
- return
140
+ return link_targets
111
141
112
142
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