forked from TensorNetwork/tensornetwork.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.jl
366 lines (337 loc) · 9.32 KB
/
generate.jl
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
361
362
363
364
365
#
# Static site generator for Tensor Network website
#
include("bibtex.jl")
function pause()
a = readline(STDIN)
(length(a) == 0) && return
(a[1]=='q') && exit(0)
end
#
# Process citations
#
function processCitations(html::String)::Tuple{String,Dict{String,Int}}
cite_re = r"\\(cite|onlinecite){(.+?)}"s
citenums = Dict{String,Int}()
res = String("")
pos = 1
match = false
counter = 1
for m in eachmatch(cite_re,html)
match = true
res *= html[pos:m.offset-1]
names = split(convert(String,m.captures[2]),",")
namenums = Tuple{Int,String,String}[]
for name in names
name = strip(name)
if haskey(citenums,name)
num = citenums[name]
else
citenums[name] = counter
num = counter
counter += 1
end
push!(namenums,(num,name,convert(String,m.captures[1])))
end
sort!(namenums, by = x -> x[1])
for i in 1:length(namenums)
(num,name,cmd) = namenums[i]
if cmd == "cite"
res *= "<a class=\"citation\" href=\"#$(name)_$(num)\">[$num]</a>"
elseif cmd == "onlinecite"
res *= "<a class=\"online_citation\" href=\"#$(name)_$(num)\">$num</a>"
if i < length(namenums) res *= ", " end
end
end
pos = m.offset+length(m.match)
end
if !match
return (html,citenums)
else
res *= html[pos:end]
end
return (res,citenums)
end
#
# Process MathJax
#
function processMathJax(html::String)
mj_re = r"(\@\@.+?\@\@|\$.+?\$|\\begin{equation}.+?\\end{equation}|\\begin{equation\*}.+?\\end{equation\*}|\\begin{align}.+?\\end{align})"s
mjlist = String[]
res = ""
pos = 1
match = false
for m in eachmatch(mj_re,html)
match = true
res *= html[pos:m.offset-1]
#res *= "\n\n<div>"*m.captures[1]*"</div>\n\n"
push!(mjlist,m.captures[1])
res *= "(MathJax$(length(mjlist)))"
pos = m.offset+length(m.match)
end
if match
res *= html[pos:end]
else
res = html
end
return (res,mjlist)
end
function restoreMathJax(html::String,mjlist::Array{String,1})
res = html
for (n,mj) in enumerate(mjlist)
res = replace(res,"(MathJax$n)" => mj)
end
return res
end
#
# Process wiki-style links
#
function processWikiLinks(html::String,ifname::String)
link_re = r"\[\[(.+?)\|(.*?)\]\]"
sub_re = r"(.*?)(#.*)"
missing_links = String[]
res = ""
pos = 1
match = false
for m in eachmatch(link_re,html)
match = true
res *= html[pos:m.offset-1]
target = convert(String,m.captures[2])
#If link has a "#" in it, split into target and sublink
sublink = ""
for sm in eachmatch(sub_re,target)
target = convert(String,sm.captures[1])
sublink = convert(String,sm.captures[2])
end
if isdir("src/"*target)
res *= "["*m.captures[1]*"](/"*target*"/$sublink)"
elseif isfile("src/"*target*".md")
res *= "["*m.captures[1]*"](/"*target*".html$sublink)"
else
res *= "["*m.captures[1]*"](unknown_file)"
push!(missing_links,target)
end
pos = m.offset+length(m.match)
end
if length(missing_links) > 0
println("Missing/Incorrect Wiki Links in File $ifname:")
for link in missing_links
println(" $link")
end
end
if !match
return html
else
res *= html[pos:end]
end
return res
end
#
# Process arxiv preprint links
#
function processArxivLinks(html::String)
link_re = r"(arxiv|cond-mat|quant-ph|math|math-ph|physics)[/:]\W*?([\d\.]+)"i
res = ""
pos = 1
match = false
for m in eachmatch(link_re,html)
match = true
res *= html[pos:m.offset-1]
prefix = m.captures[1]
number = m.captures[2]
if lowercase(prefix) == "arxiv"
res *= "arxiv:[$number](https://arxiv.org/abs/$number)"
else
nbprefix = replace(prefix,"-" => "‑") #non-breaking hypen
res *= "<span>$nbprefix/[$number](https://arxiv.org/abs/$prefix/$number)</span>"
end
pos = m.offset+length(m.match)
end
if !match
return html
else
res *= html[pos:end]
end
return res
end
function processCondMatLinks(html::String)
link_re = r"arxiv:\W*?(\d+?\.\d+)"
res = ""
pos = 1
match = false
for m in eachmatch(link_re,html)
match = true
res *= html[pos:m.offset-1]
res *= "arxiv:["*m.captures[1]*"](https://arxiv.org/abs/"*m.captures[1]*")"
pos = m.offset+length(m.match)
end
if !match
return html
else
res *= html[pos:end]
end
return res
end
function printEditFooter(of::IOStream,fname::String)
template_edit_footer = open("template_edit_footer.html") do file read(file,String) end
link = "https://github.com/TensorNetwork/tensornetwork.org/edit/master/"*fname
out = replace(template_edit_footer,r"{github_link}" => link)
print(of,out)
end
function generateRefs(citenums,btentries)
keys = Array{String,1}(undef,length(citenums))
for (k,v) in citenums
keys[v] = k
end
rhtml = "<a name=\"toc_refs\"></a>\n"
rhtml *= "## References\n"
for (n,k) in enumerate(keys)
if haskey(btentries,k)
bt = btentries[k]
rhtml *= "$n. <a name=\"$(bt.name)_$(n)\"></a>"*convertToMD(bt)
rhtml *= "\n"
end
end
return rhtml
end
#
# Generate a Table of Contents if Requested
#
function generateTOC(input::String,has_refs::Bool)
toc_re = r"<!--TOC-->"is
if occursin(toc_re,input)
output = ""
toc_html = "\n\n\n<div class=\"toc\">\n"
toc_html *= "<b>Table of Contents</b><br/><br/>\n"
lev = 1
sec_re = r"\n(#+)(.*)"
count = 1
pos = 1
for m in eachmatch(sec_re,input)
nlev = length(m.captures[1])
output *= input[pos:m.offset-1]
if nlev > 1
output *= " <a name=\"toc_$count\"></a>\n"
name = strip(convert(String,m.captures[2]))
name = replace(name,r"\\cite{.*?}" => "")
name = replace(name,r"\\onlinecite{.*?}" => "")
for n in 1:nlev toc_html *= " " end
if nlev == lev+1
toc_html *= "<ul>"
elseif nlev == lev-1
toc_html *= "</ul>"
end
toc_html *= "<li><a href=\"#toc_$count\">$name</a></li>\n"
lev = nlev
count += 1
end
output *= m.match
pos = m.offset+length(m.match)
end
while lev != 2
lev -= 1
toc_html *= "</ul>"
end
if has_refs
toc_html *= "<li><a href=\"#toc_refs\">References</a></li>\n"
end
toc_html *= "</ul></div>\n\n\n"
output *= input[pos:end]
#println(toc_html)
return replace(output,toc_re => toc_html)
end
return input
end
header_prenav = open("header_prenav.html") do file read(file,String) end
header_postnav = open("header_postnav.html") do file read(file,String) end
footer = open("footer.html") do file read(file,String) end
idir = "src"
odir = "../tensornetwork.org"
run(`mkdir -p $odir`)
run(`rm -f $odir/\*`)
run(`cp -r css $odir/`)
run(`cp -r images $odir/`)
run(`cp -r htaccess_file $odir/.htaccess`)
run(`chmod 644 $odir/.htaccess`)
for (root,dirs,files) in walkdir(idir)
folderstring = root[4:end]
curri = idir * folderstring
curro = odir * folderstring
folders = split(folderstring,"/")[2:end]
for d in dirs
run(`mkdir -p $curro/$d`)
end
for f in files
(f[1]=='.') && continue
(base,ext) = split(f,".")
ifname = curri*"/"*f
if ext == "md"
ofname = curro*"/"*base*".html"
mdstring = read(ifname,String)
btfile = curri*"/"*base*".bib"
has_refs = isfile(btfile)
#if has_refs
# if mtime(ofname) > mtime(ifname) && mtime(ofname) > mtime(btfile)
# println("Skipping $(ifname), $(btfile), they are older than $(ofname).")
# continue
# end
#else
# if mtime(ofname) > mtime(ifname)
# println("Skipping $(ifname), it is older than $(ofname).")
# continue
# end
#end
mdstring = generateTOC(mdstring,has_refs)
(mdstring,mjlist) = processMathJax(mdstring)
mdstring = processWikiLinks(mdstring,ifname)
(mdstring,citenums) = processCitations(mdstring)
btfile = curri*"/"*base*".bib"
if has_refs
bt = parseBibTex(btfile)
refmd = generateRefs(citenums,bt)
mdstring *= refmd
end
mdstring = processArxivLinks(mdstring)
open("_tmp_file.md","w") do tf
print(tf,mdstring)
end
html = read(`cmark --unsafe --smart _tmp_file.md`,String)
#html = read(`python2.7 -m markdown _tmp_file.md`,String)
html = restoreMathJax(html,mjlist)
#
# Put in backlinks
#
open(ofname,"w") do of
print(of,header_prenav)
if length(folders) > 0 || f!="index.md" #<-- don't show for main page
print(of,"<tr><td></td><td class='backlinks'>")
print(of,"<a href='/'>main</a>/")
tfold = "/"
for fold in folders[1:end-1]
tfold *= fold * "/"
print(of,"<a href=\"$tfold\">$fold</a>/")
end
if f!="index.md"
if length(folders) > 0
fold = folders[end]
tfold *= fold * "/"
print(of,"<a href=\"$tfold\">$fold</a>/")
end
print(of,base)
else
(length(folders) > 0) && print(of,"$(folders[end])/")
end
print(of,"</td></tr>")
end
print(of,header_postnav)
print(of,html)
printEditFooter(of,ifname)
print(of,footer)
end
run(`rm -f _tmp_file.md`)
#elseif ext == "png" || ext == "jpg"
else
run(`cp $ifname $(curro*"/"*f)`)
end
end
end