-
Notifications
You must be signed in to change notification settings - Fork 31
/
readdocs.jl
88 lines (82 loc) · 2.51 KB
/
readdocs.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
using Glob
using Conda
const PANDOC = joinpath(Conda.ROOTENV, "bin", "pandoc")
if !isfile(PANDOC)
Conda.add("pandoc")
end
function convert_text(str; from, to)
f = open(`$PANDOC --from=$from --to=$to`, "r+")
write(f, str)
close(f.in)
read(f, String)
end
DOC_DIR = joinpath(@__DIR__, "gsl-$(GSL_VERSION)", "doc")
function parse_file(docs, filename)
fh = open(filename)
lines = readlines(fh)
close(fh)
lineno = 1
while lineno < length(lines)
l = lines[lineno]
# Walk until we find a section
m = match(r"^\.\. (type|function|const|macro):: (.*)$", l)
if m==nothing
lineno += 1
else
sectype = m.captures[1]
secdecl = replace(m.captures[2], r"(^ +| +$)" => "")
str, lineno = glob_section(lines, lineno+1)
if sectype == "type" || sectype == "const" || sectype == "macro"
# type def
name = secdecl
else
# function
m = match(r"(^| )(\w+) *\(", secdecl)
if m==nothing
error("Failed to match function declaration:\n$secdecl")
end
name = m.captures[2]
end
# convert RST to Markdown
docstr = convert_text(str, from="rst", to="markdown_strict+tex_math_dollars")
docs[name] = "`$secdecl`\n\n" * docstr
end
end
end
function glob_section(lines, lineno)
s = String[]
prevlength = 0 # Length of previous string
while lineno < length(lines)
l = lines[lineno]
reg_adorn = Regex("^(\\W)\\1{$(prevlength-1)}((?!\\1)|\$)")
# Glob lines until we encounter the next section or files ends
if occursin(r"^\.\. (?!math|code)", l)
break
end
if (prevlength>0 && occursin(reg_adorn, l))
# Found a section adornment
s = s[1:end-1]
break
end
push!(s, l)
prevlength = length(l)
lineno += 1
end
return join(s, "\n"), lineno
end
function readdocs()
if !isdir(DOC_DIR)
@info "Downloading GSL docs"
run(`wget http://ftp.gnu.org/gnu/gsl/gsl-$(GSL_VERSION).tar.gz`)
@info "Unpacking"
run(`tar -zxf gsl-$(GSL_VERSION).tar.gz`)
end
@info "Reading the docs and converting to Markdown"
docs = Dict()
docfiles = glob("*.rst", DOC_DIR)
for filename in docfiles
@info("Parsing $filename")
parse_file(docs, filename)
end
return docs
end