-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_docs.py
executable file
·360 lines (295 loc) · 12.1 KB
/
gen_docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/opt/homebrew/bin/python3
# !/usr/local/opt/python/libexec/bin/python
# ! /usr/bin/env python
import sys
import re #regular expressions
from cgi import FieldStorage
#import Cookie
import datetime
import os
import mistune #Markdown renderer
from pygments import highlight
from pygments.lexers import CppLexer
#from pygments.lexers import JuliaLexer
from pygments.formatters import HtmlFormatter
#
# Python-Markdown Markdown Renderer
#
import markdown
from functools import partial
# Turn on cgitb to get nice debugging output.
# Remember to turn off when done debugging, otherwise not secure.
#import cgitb; cgitb.enable()
#################################
# version name, display name, associated language
versions = [
#["julia","Julia","Julia"],
["cppv3","C++","C++"],
#["cppv2","C++v2","C++"],
]
default_version = "cppv3"
reldocpath = "docs/"
prenav_header_fname = "docs_header_prenav.html"
postnav_header_fname = "docs_header_postnav.html"
footer_fname = "docs_footer.html"
this_fname = "docs.cgi"
nav_delimiter = " / "
#################################
sys.path.append("/opt/itensor.org/")
named_link_re = re.compile("(.+?)\|(.+)")
#code_block_re = re.compile(r"<code>\n+(.+?)</code>",flags=re.DOTALL)
#paragraph_code_re = re.compile(r"<p><code>(.+?)</code></p>",flags=re.DOTALL)
form = FieldStorage()
def fileExists(fname):
try:
open(fname)
return True
except IOError:
return False
def openFile(fname):
try:
return open(fname)
except IOError:
return None
def printContentType(vers):
print("Content-Type: text/html")
#if vers != None:
#expiration = datetime.datetime.now() + datetime.timedelta(days=30)
#cookie = Cookie.SimpleCookie()
#cookie["vers"] = vers
#cookie["vers"]["expires"] = expiration.strftime("%a, %d-%b-%Y %H:%M:%S PST")
#print cookie.output()
print("\n")
print("\n")
def processMathJax(matchobj,delimit=""):
if delimit=="@@":
math = re.sub(r"\\_","_",matchobj.group(0))
return "<span> " + math + " </span> "
elif delimit=="$$":
white = matchobj.group(1)
math = re.sub(r"\\_","_",matchobj.group(2))
#return "\n{}<div>\n{}{}\n{}</div>\n<!-- -->".format(white,white,math,white)
return "\n{}<div>\n{}{}\n{}</div>\n".format(white,white,math,white)
elif delimit=="align":
white = matchobj.group(1)
math = re.sub(r"\\_","_",matchobj.group(2))
#return "\n{}<div>\n{}{}\n{}</div>\n<!-- -->".format(white,white,math,white)
return "\n{}<div>\n{}{}\n{}</div>\n".format(white,white,math,white)
return None
def includeFile(matchobj):
spacing = len(matchobj.group(1))
fname = matchobj.group(2)
text = ""
try:
f = open(fname,'r')
white = " "*spacing
for line in f:
text += white+line
return text
except:
return "<File {} not found>".format(fname)
def openMDFile(vdocpath,page):
mdfname = vdocpath + page + ".md"
mdfile = openFile(mdfname)
# "page.md" file doesn't exist, reinterpret "page" as a
# directory name and look for a main.md file there
if not mdfile:
mdfname = vdocpath + page + "/main.md"
mdfile = openFile(mdfname)
return mdfile
def convert(string,vers,lang):
#Convert SciPost[Vol,Issue,PageNum] tags
string = re.sub(r"SciPost\[(\d+),(\d+),(\d+)\]",r"<i style='color:#CC0000'>SciPost Phys.</i> <b>\1</b> <a href='https://scipost.org/10.21468/SciPostPhys.\1.\2.\3'>\3</a>",string)
#Convert PhysRev[Letter,Vol,PageNum] tags
string = re.sub(r"PhysRev\[(.+?),(\d+),(\w+)\]",r"<i style='color:#CC0000'>Phys. Rev. \1</i> <b>\2</b> <a href='https://doi.org/10.1103/PhysRev\1.\2.\3'>\3</a>",string)
#Convert Journal[Name,Vol,PageNum,Url] tags
string = re.sub(r"Journal\[(.+?),(\d+),(\d+),(.+?)\]",r"<i style='color:#CC0000'>\1</i> <b>\2</b> <a href='\4'>\3</a>",string)
#Convert arxiv:####.#### links
string = re.sub(r"arxiv:(\d\d\d\d\.\d+)",r"arxiv:<a target='_blank' href='http://arxiv.org/abs/\1'>\1</a>",string)
#Convert cond-mat/####### links
string = re.sub(r"cond-mat/(\d\d\d\d\d\d\d)",r"<a target='_blank' href='http://arxiv.org/abs/cond-mat/\1'>cond-mat/\1</a>",string)
#Convert github:<sha> links
string = re.sub(r"github:(\w{5})\w*",r"<a class='github' target='_blank' href='https://github.com/ITensor/ITensor/commit/\1'>\1</a>",string)
#Convert #nn github issue and PR links
if lang=="Julia":
string = re.sub(r"(Request|request|Pull|pull|PR) #(\d+)",r"\1 <a target='_blank' href='https://github.com/ITensor/ITensors.jl/pull/\2'>#\2</a>",string)
string = re.sub(r"(Issue|issue|Bug|bug) #(\d+)",r"\1 <a target='_blank' href='https://github.com/ITensor/ITensors.jl/issues/\2'>#\2</a>",string)
else:
string = re.sub(r"(Request|request|Pull|pull|PR) #(\d+)",r"\1 <a target='_blank' href='https://github.com/ITensor/ITensor/pull/\2'>#\2</a>",string)
string = re.sub(r"(Issue|issue|Bug|bug) #(\d+)",r"\1 <a target='_blank' href='https://github.com/ITensor/ITensor/issues/\2'>#\2</a>",string)
#Convert VERSION token
string = re.sub(r"VERSION",vers,string)
#Convert MathJax @@...@@ -> <span>@@...@@</span>
#and $$...$$ -> <div>$$..$$</div> to protect
#from Markdown formatter, also replace \_ -> _ in
#Latex string
string = re.sub(r"(@@.+?@@)",partial(processMathJax,delimit="@@"),string)
string = re.sub(r"([ ]*)(\$\$.+?\$\$)",partial(processMathJax,delimit="$$"),string,flags=re.DOTALL|re.MULTILINE)
string = re.sub(r"([ ]*)(\\begin{align}.+?\\end{align})",partial(processMathJax,delimit="align"),string,flags=re.DOTALL|re.MULTILINE)
string = re.sub(r"([ ]*)include:(\S+)",partial(includeFile),string)
#Convert wiki links to markdown link syntax
slist = re.split("\[\[(.+?)\]\]",string)
mdstring = slist[0]
for j in range(1,len(slist)):
chunk = slist[j]
if j%2 == 0:
mdstring += chunk
else:
#Check if a name is provided for this link
nlmatch = named_link_re.match(chunk)
if nlmatch:
name = nlmatch.group(1)
link = nlmatch.group(2)
mdstring += "<a href='%s?vers=%s&page=%s'>%s</a>"%(this_fname,vers,link,name)
else:
#Otherwise use the raw link name (file -> file.md)
mdstring += "<a href='%s?vers=%s&page=%s'>%s</a>"%(this_fname,vers,chunk,chunk)
htmlstring = ""
if sys.version[0] == '2':
class MyRenderer(mistune.Renderer):
mylang = "C++"
def block_code(self, code, lang=None):
lexer = CppLexer()
if self.mylang == "C++":
lexer = CppLexer()
elif self.mylang == "Julia":
lexer = JuliaLexer()
formatter = HtmlFormatter()
return highlight(code, lexer, formatter)
#
# Use Mistune with Python 2 interface:
#
renderer = MyRenderer()
renderer.mylang = lang
md = mistune.Markdown(renderer=renderer)
htmlstring = md.render(mdstring)
else:
#
# Python-Markdown Renderer
#
#htmlstring = markdown.markdown(mdstring, extensions=['codehilite'])
#
# Mistune Renderer
#
md = mistune.create_markdown(escape=False)
htmlstring = md(mdstring)
#
# Do Code Highlighting
#
cpplex = CppLexer()
formatter = HtmlFormatter()
def code_highlight(match):
return "<pre><code>"+highlight(match.group(1),cpplex,formatter)+"</pre></code>"
htmlstring = re.sub(r"<pre><code>(.*?)<\/code><\/pre>",code_highlight,htmlstring,flags=re.DOTALL)
#
# Convert TeX \sub commands to underscores
#
htmlstring = re.sub(r"\\sub",r"_",htmlstring)
return htmlstring
def generate():
page = form.getvalue("page")
vers = "cppv3"
#vers = form.getvalue("vers")
lang = "C++"
#for v in versions:
# if v[0]==vers: lang = v[2]
#Cookie_val = ""
#Try:
# cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
# cookie_val = cookie["vers"].value
#Except (Cookie.CookieError, KeyError):
# cookie_val = default_version
#If vers == None: vers = cookie_val
if page == None: page = "main"
vdocpath = reldocpath + "/" + vers + "/"
mdfile = openMDFile(vdocpath,page)
# If file or folder not found in given
# version, look in 'all' folder as a fallback
inall = False
if not mdfile:
all_docpath = reldocpath + "/all/"
inall = True
mdfile = openMDFile(all_docpath,page)
#
# Start generating the page
#
printContentType(vers)
bodyhtml = ""
if mdfile:
bodyhtml = convert("".join(mdfile.readlines()),vers,lang)
mdfile.close()
else:
bodyhtml = "<p>Page not found</p>"
# Generate directory tree hyperlinks
dirlist = page.split('/')
page_name = dirlist.pop(-1)
# Create navigation line
nav = ""
first = True
for dirname in dirlist:
#nav += "%s<a href=\"%s?page=%s&vers=%s\">%s</a>"%(nav_delimiter,this_fname,dirname,vers,dirname)
if first:
nav += "<li><a href=\"%s?page=%s&vers=%s\"> %s</a></li>\n"%(this_fname,dirname,vers,dirname)
first = False
else:
nav += "<li><a href=\"%s?page=%s&vers=%s\">%s</a></li>\n"%(this_fname,dirname,vers,dirname)
if page_name != "main":
nav += "<li> %s</li>\n"%(page_name)
if page_name != "main":
nav = "<li><a href=\"%s?page=main&vers=%s\">main</a></li>%s\n"%(this_fname,vers,nav)
else:
nav = "<li> main</li>%s\n"%(nav)
nav = "<li><strong>C++ Documentation System</strong> </li>\n" + nav
nav = "<nav class=\"breadcrumb\" aria-label=\"breadcrubs\"><ul>\n" + nav + "\n</ul></nav>"
## Create version information line
#vinfo = "<span class='versions' style='float:right;'>"
#n = 0
#lang = "C++"
#for (v,vname,vlang) in versions:
# if n > 0: vinfo += " | "
# if v == vers:
# vinfo += "<span style='outline:solid 1px;font-weight:bold;'>%s</span>"%(vname)
# lang = vlang
# else:
# vinfo += "<a style='text-decoration:none;' href=\"%s?page=%s&vers=%s\">%s</a>"%(this_fname,page,v,vname)
# n += 1
#vinfo += "</span></br>"
#
# Generate Back-Links
#
backlinks = []
full_dirname = ""
for dirname in dirlist:
nice_dirname = re.sub(r"_",r" ",dirname).title()
text = "Back to " + nice_dirname
full_dirname += dirname
iconfname = "docs/"+vers+"/"+full_dirname+"/icon.png"
iconimg = "<!-- " + iconfname + " -->"
if fileExists(iconfname): iconimg = "<img src=\"%s\" class=\"icon\">"%(iconfname,)
backlinks.append( "<br/>%s<a href=\"%s?page=%s&vers=%s\">%s</a>"%(iconimg,this_fname,full_dirname,vers,text) )
full_dirname += "/"
backlinks.reverse()
#
# Special handling of "codes" docs file
# to make top link for Codes be colored white
#
prenav_fname_to_use = prenav_header_fname
if full_dirname+page_name == "codes":
prenav_fname_to_use = "docs_header_codes_prenav.html"
prenav_header_file = open(prenav_fname_to_use)
postnav_header_file = open(postnav_header_fname)
footer_file = open(footer_fname)
print("".join(prenav_header_file.readlines()))
print(nav)
#print vinfo
print("".join(postnav_header_file.readlines()))
#if vers == "cppv3" and (not inall):
# print "<span style='color:red;'>ITensor version 3 has not been released yet. \
# This documentation is a preview only.</span><br/><br/>"
print(bodyhtml)
for bl in backlinks: print(bl)
if not (len(dirlist)==0 and page_name == "main"):
print("<br/><img src=\"docs/%s/icon.png\" class=\"icon\"><a href=\"%s?vers=%s\">Back to Main</a>"%(vers,this_fname,vers))
print("".join(footer_file.readlines()))
footer_file.close()
#header_file.close()