55# generate_links()
66# Generates a copy of components.adoc with added links to the man pages for the components.
77
8- from os import listdir
9- from os .path import isfile , join
8+ import os
109import re
1110import sys
1211
13- man1_path = '../docs/html/ man/man1'
14- man1_files = {f .replace ('.1.html ' , '' ) for f in listdir (man1_path ) if isfile (join (man1_path , f ))}
15- man9_path = '../docs/html/ man/man9'
16- man9_files = {f .replace ('.9.html ' , '' ) for f in listdir (man9_path ) if isfile (join (man9_path , f ))}
17- man = [ man1_files ,man9_files ]
12+ man1_path = '../docs/man/man1'
13+ man1_files = {f .replace ('.1' , '' ) for f in os . listdir (man1_path ) if os . path . isfile (os . path . join (man1_path , f ))}
14+ man9_path = '../docs/man/man9'
15+ man9_files = {f .replace ('.9' , '' ) for f in os . listdir (man9_path ) if os . path . isfile (os . path . join (man9_path , f ))}
16+ man = { '1' : man1_files , '9' : man9_files }
1817doc1 = set ()
1918doc9 = set ()
20- components = [ doc1 ,doc9 ]
19+ components = { '1' : doc1 , '9' : doc9 }
2120section_switch = '[[sec:realtime-components]]'
22- complist_path = '../docs/src/hal/components.adoc'
21+ # complist_path = '../docs/src/hal/components.adoc'
2322
24- def generate_complist ():
23+ def generate_complist (complist_path ):
2524 file1 = open (complist_path , 'r' )
26- comp_index = 0
25+ manpage = '1'
2726 for line in file1 :
2827 if section_switch in line :
29- comp_index = 1
28+ manpage = '9'
3029
3130 if line [0 ] == '|' and line [1 ] != '=' :
3231 splitted = line .split ('|' )
@@ -35,15 +34,15 @@ def generate_complist():
3534 comp = re .search ('\[.*\]' , splitted [1 ]).group ()
3635 else :
3736 comp = splitted [1 ]
38- components [comp_index ].add (comp .strip ('[] ' ))
37+ components [manpage ].add (comp .strip ('[] ' ))
3938
4039 file1 .close ()
4140 miss1 = man1_files .difference (doc1 )
4241 obs1 = doc1 .difference (man1_files )
4342 miss9 = man9_files .difference (doc9 )
4443 obs9 = doc9 .difference (man9_files )
45-
46- file2 = open ('../docs/src/hal/components_gen1.adoc' , 'w' )
44+ gen1_filename = '../docs/src/hal/components_gen1.adoc'
45+ file2 = open (gen1_filename , 'w' )
4746 if len (miss1 ) > 0 :
4847 file2 .write ('=== Not categorized (auto generated)\n ' )
4948 file2 .write ('[{tab_options}]\n |=======================\n ' )
@@ -57,8 +56,8 @@ def generate_complist():
5756 file2 .write ('| ' + i + ' |||\n ' )
5857 file2 .write ('|=======================\n ' )
5958 file2 .close ()
60-
61- file3 = open ('../docs/src/hal/components_gen9.adoc' , 'w' )
59+ gen9_filename = '../docs/src/hal/components_gen9.adoc'
60+ file3 = open (gen9_filename , 'w' )
6261 if len (miss9 ) > 0 :
6362 file3 .write ('=== Not categorized (auto generated)\n ' )
6463 file3 .write ('[{tab_options}]\n |=======================\n ' )
@@ -73,42 +72,53 @@ def generate_complist():
7372 file3 .write ('|=======================\n ' )
7473 file3 .close ()
7574
75+ generate_links (gen1_filename , '1' , False )
76+ generate_links (gen9_filename , '9' , False )
77+
7678 print ('gen_complist: Added {} uncategorized and {} obsolete entries to hal component list (man1)' .format (len (miss1 ), len (obs1 )))
7779 print ('gen_complist: Added {} uncategorized and {} obsolete entries to hal component list (man9)' .format (len (miss9 ), len (obs9 )))
7880
7981
80- def generate_links ():
81- file1 = open (complist_path , 'r' )
82- file1_links = open ('../docs/src/hal/components_links.adoc' , 'w' )
83- comp_index = 0
84- manpage = '1'
85- for line in file1 :
82+ def generate_links (filename , manpage = '1' , create_backup = True ):
83+ file = open (filename , 'r' )
84+ file_links = []
85+ links_added = 0
86+ for line in file :
8687 if section_switch in line :
87- comp_index = 1
8888 manpage = '9'
8989
9090 if line [0 ] == '|' and line [1 ] != '=' :
9191 splitted = line .split ('|' )
9292
9393 if 'link:' in splitted [1 ]:
9494 link = re .search ('(?<=link:).*(?=\[)' , splitted [1 ]).group ()
95- if not isfile (join ('../docs/html/hal' ,link )):
96- print ('broken link:' , link )
95+ if not os . path . isfile (os . path . join ('../docs/html/hal' ,link )):
96+ print ('gen_complist_link: Broken link:' , link )
9797 else :
9898 comp = splitted [1 ].strip (' ' )
99- if comp in man [comp_index ]:
99+ if comp in man [manpage ]:
100100 line = line .replace (comp , 'link:../man/man' + manpage + '/' + comp + '.' + manpage + '.html[' + comp + ']' , 1 )
101+ links_added += 1
101102
102- file1_links .write (line )
103+ file_links .append (line )
104+
105+ file .close ()
106+
107+ if links_added :
108+ if create_backup :
109+ os .rename (filename , filename + '~' )
110+ file = open (filename , 'w' )
111+ for line in file_links :
112+ file .write (line )
113+ file .close ()
114+ print ('gen_complist_links: Added {} link(s) to {}' .format (links_added , filename ))
103115
104- file1 .close ()
105- file1_links .close ()
106116
107117if __name__ == "__main__" :
108118
109119 if len (sys .argv ) > 1 :
110- if sys .argv [ 1 ] == 'links' :
111- generate_links ()
112-
113- else :
114- generate_complist ()
120+ if len ( sys .argv ) > 2 :
121+ if sys . argv [ 2 ] == 'links' :
122+ generate_links ( sys . argv [ 1 ])
123+ else :
124+ generate_complist (sys . argv [ 1 ] )
0 commit comments