Skip to content

Commit

Permalink
convertmd.jl converts docstrings to md
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeInnes committed Aug 14, 2015
1 parent ab4f30f commit bd5f551
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions doc/convertmd.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Lazy

function pandoc(rst)
open("doc.rst", "w") do io
print(io, rst)
end
run(`pandoc doc.rst -o doc.md -t markdown_github`)
rm("doc.rst")
result = open(readall, "doc.md")
rm("doc.md")
return result
end

function rmquote(md)
ls = split(md, "\n")
ls = map(ls) do l
l in (">", ">\r") ? "" :
startswith(l, "> ") ? l[3:end] : l
end
join(ls, "\n")
end

escape(md) = @> md replace("\$", "\\\$") replace("&gt;", ">") replace("&lt;", "<")

isvalid(rst) = !ismatch(r":\w+:|doctest|(^|\n)\s+\*|====|\[[\w0-9]+\]_", rst)

function convert(doc)
if isvalid(doc)
pandoc(doc) |> rmquote |> escape
else
"""
```rst
$(chomp(doc))
```
"""
end |> chomp
end

function translate(file)
ls = split(open(readall, file), "\n")
doccing = false
iscode = false
open(file, "w") do io
doc = IOBuffer()
for l in ls
if iscode
l != "" && println(doc)
iscode = false
end
doccing && l == "::" && (iscode = true)
if l == "```rst"
doccing = true
elseif doccing && l == "```"
doccing = false
rst = takebuf_string(doc)
println(io, convert(rst))
elseif doccing
println(doc, l)
else
println(io, l)
end
end
end
end

translate("base/docs/helpdb.jl")

0 comments on commit bd5f551

Please sign in to comment.