Skip to content

Commit 6e3e0df

Browse files
committed
Improve document regeneration script
Now it makes a document that's completely compatible with the SWIG makedoc.py
1 parent c02baaf commit 6e3e0df

File tree

2 files changed

+64
-45
lines changed

2 files changed

+64
-45
lines changed

Doc/Manual/src/pandoc2swigman.py

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
from html.entities import codepoint2name
77
import os
88
import re
9-
from exnihiloenv.rewriter import ReWriter
109
from string import printable
1110
###############################################################################
1211

1312
RE_HEADER = re.compile(r'^<h(\d) id="([^"]+)">(.*)</h(\d)>$')
1413
RE_PRE = re.compile(r'^<pre class="([^"]+)">')
1514
RE_ENDPRE = re.compile(r'</pre>')
1615
RE_LINK = re.compile(r'<a href="#([^"]+)">')
16+
RE_TITLE = re.compile(r'^<title>(.*)</title>$')
1717

1818
NEW_HEADER = '<H{lev:d}><a name="{link}">{title}</a></H{lev:d}>\n'
1919

2020
SELECTOR = {
2121
'fortran': "targetlang",
22+
'f90': "targetlang",
2223
'swig': "code",
2324
'cpp': "code",
2425
'c++': "code",
@@ -36,49 +37,67 @@ def convert_link(link):
3637
def repl_link_match(match):
3738
return r'<a href="Fortran.html#{}">'.format(convert_link(match.group(1)))
3839

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)
80100

81101
#-----------------------------------------------------------------------------#
82102
if __name__ == '__main__':
83-
#main()
84-
swiggify("../Fortran.html")
103+
run("../Fortran.html")

Doc/Manual/src/regenerate.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ pandoc --from=gfm+smart --no-highlight \
44
-M title:"SWIG and Fortran" -M author:"Seth R Johnson" \
55
--toc --standalone -H style.css -o ../Fortran.html Fortran.md
66
python pandoc2swigman.py
7-
git add -p ../Fortran.html
7+
(cd .. && python maketoc.py)

0 commit comments

Comments
 (0)