22import  shutil 
33
44from  html .parser  import  HTMLParser 
5+ from  html  import  escape  as  html_escape 
6+ from  urllib .parse  import  quote_plus 
57
68
7- class  _HTMLParser (HTMLParser ):
9+ class  NavHTMLParser (HTMLParser ):
10+     def  __init__ (self , * _ , ** __ ) ->  None :
11+         super ().__init__ (* _ , * __ )
12+         self .title  =  None 
13+ 
814    def  handle_starttag (self , tag , attrs ):
9-         print ("HTML Tag:" , tag , attrs , sep = "\n   " )
15+         print ("  HTML Tag:" , tag , attrs , sep = "\n     " )
16+         if  tag  ==  "meta" :
17+             for  attr  in  attrs :
18+                 if  attr [0 ] ==  "nav-title" :
19+                     self .title  =  attr [1 ] if  attr [1 ] else  None 
20+ 
1021
22+ html_files  =  {}
1123
1224for  dir , subdirs , files  in  os .walk ("./www" ):
1325    if  (not  subdirs  and  not  files ) or  (
@@ -24,9 +36,23 @@ def handle_starttag(self, tag, attrs):
2436
2537        if  file .endswith (".html" ):
2638            try :
27-                 p  =  _HTMLParser ()
39+                 print (os .path .join (dir , file ), "START" )
40+                 p  =  NavHTMLParser ()
2841                p .feed (open (os .path .join (dir , file ), "rb" ).read ().decode ())
2942                p .close ()
43+                 html_files [os .path .join (dir [5 :], file )] =  p .title 
3044                print (os .path .join (dir , file ), "SUCCESS" )
3145            except  Exception  as  e :
3246                print (os .path .join (dir , file ), e )
47+ 
48+ if  html_files :
49+     with  open ("./www/nav.html" , "wb" ) as  f :
50+         f .write ("<><head><title>Navigation</title></head><>" .encode ())
51+         for  file , title  in  html_files .items ():
52+             f .write (
53+                 f'<div><a href="{ quote_plus (file )} { html_escape (file )}  .encode ()
54+             )
55+             if  title :
56+                 f .write (f": { html_escape (title )}  .encode ())
57+             f .write (f"</div>" .encode ())
58+         f .write ("</body></html>" .encode ())
0 commit comments