6
6
from html .entities import codepoint2name
7
7
import os
8
8
import re
9
- from exnihiloenv .rewriter import ReWriter
10
9
from string import printable
11
10
###############################################################################
12
11
13
12
RE_HEADER = re .compile (r'^<h(\d) id="([^"]+)">(.*)</h(\d)>$' )
14
13
RE_PRE = re .compile (r'^<pre class="([^"]+)">' )
15
14
RE_ENDPRE = re .compile (r'</pre>' )
16
15
RE_LINK = re .compile (r'<a href="#([^"]+)">' )
16
+ RE_TITLE = re .compile (r'^<title>(.*)</title>$' )
17
17
18
18
NEW_HEADER = '<H{lev:d}><a name="{link}">{title}</a></H{lev:d}>\n '
19
19
20
20
SELECTOR = {
21
21
'fortran' : "targetlang" ,
22
+ 'f90' : "targetlang" ,
22
23
'swig' : "code" ,
23
24
'cpp' : "code" ,
24
25
'c++' : "code" ,
@@ -36,49 +37,67 @@ def convert_link(link):
36
37
def repl_link_match (match ):
37
38
return r'<a href="Fortran.html#{}">' .format (convert_link (match .group (1 )))
38
39
39
- def swiggify (path ):
40
- with ReWriter (path ) as rewriter :
41
- (oldf , newf ) = rewriter .files
42
- rewriter .dirty = True
43
-
44
- in_code = False
45
- for line in oldf :
46
- # Convert special characters to HTML &foo; characters
47
- line = line .translate (NONASCII_TO_HTML )
48
-
49
- if not in_code :
50
- match = RE_PRE .match (line )
51
- if match :
52
- code = SELECTOR [match .group (1 )]
53
- line = '\n <div class="{}"><pre>{}' .format (
54
- code , line [match .end ():])
55
- in_code = True
56
-
57
- if not in_code :
58
- match = RE_HEADER .match (line )
59
- if match :
60
- (lev , link , title , lev2 ) = match .groups ()
61
- lev = int (lev ) + 1 # lower the heading level
62
- link = convert_link (link )
63
- line = NEW_HEADER .format (lev = lev , link = link , title = title )
64
-
65
- line = RE_LINK .sub (repl_link_match , line )
66
- line = line .replace ("<p>" , "\n <p>\n " )
67
- line = line .replace ("</p>" , "\n </p>\n " )
68
-
69
- if in_code and line .endswith ("</pre>\n " ):
70
- line = line [:- 1 ] + "</div>\n \n "
71
- in_code = False
72
-
73
- newf .write (line )
74
-
75
- def main ():
76
- extensions = (".html" ,)
77
-
78
- from exnihiloenv .filemodify import _common
79
- _common .run (swiggify , default_extensions = "," .join (extensions ))
40
+ def swiggify (oldf , newf , section ):
41
+ # Replace header and navigation
42
+ title = None
43
+ for line in oldf :
44
+ line = line .strip ()
45
+ match = RE_TITLE .match (line )
46
+ if match is not None :
47
+ title = match .group (1 )
48
+ elif line == "</nav>" :
49
+ break
50
+ newf .write ("""\
51
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
52
+ <html>
53
+ <head>
54
+ <title>{title}</title>
55
+ <link rel="stylesheet" type="text/css" href="style.css">
56
+ <meta http-equiv="content-type" content="text/html; charset=UTF-8">
57
+ </head>
58
+ <body bgcolor="#FFFFFF">
59
+ <H1><a name="{section}">{title}</a></H1>
60
+ """ .format (title = title , section = section ))
61
+
62
+ in_code = False
63
+ for line in oldf :
64
+ # Convert special characters to HTML &foo; characters
65
+ line = line .translate (NONASCII_TO_HTML )
66
+
67
+ if not in_code :
68
+ match = RE_PRE .match (line )
69
+ if match :
70
+ code = SELECTOR [match .group (1 )]
71
+ line = '\n <div class="{}"><pre>{}' .format (
72
+ code , line [match .end ():])
73
+ in_code = True
74
+
75
+ if not in_code :
76
+ match = RE_HEADER .match (line )
77
+ if match :
78
+ (lev , link , title , lev2 ) = match .groups ()
79
+ lev = int (lev ) + 1 # lower the heading level
80
+ link = convert_link (link )
81
+ line = NEW_HEADER .format (lev = lev , link = link , title = title )
82
+
83
+ line = RE_LINK .sub (repl_link_match , line )
84
+ line = line .replace ("<p>" , "\n <p>\n " )
85
+ line = line .replace ("</p>" , "\n </p>\n " )
86
+
87
+ if in_code and line .endswith ("</pre>\n " ):
88
+ line = line [:- 1 ] + "</div>\n \n "
89
+ in_code = False
90
+
91
+ newf .write (line )
92
+
93
+ def run (path ):
94
+ basename = os .path .splitext (os .path .basename (path ))[0 ]
95
+ temp_path = path + ".updated"
96
+ with open (path , 'r' ) as oldf :
97
+ with open (temp_path , 'w' ) as newf :
98
+ swiggify (oldf , newf , basename )
99
+ os .rename (temp_path , path )
80
100
81
101
#-----------------------------------------------------------------------------#
82
102
if __name__ == '__main__' :
83
- #main()
84
- swiggify ("../Fortran.html" )
103
+ run ("../Fortran.html" )
0 commit comments