Markdown to HTML Code
require("lfs")
markdowncommand = nil
function main(cmd)
markdowncommand = cmd
if markdowncommand then
compileInFolder("introduction", ".")
else
print("Please provide a markdown command to use.")
end
end
function compileInFolder(label, name)
local current = lfs.currentdir()
if current then
lfs.chdir(name)
for file in lfs.dir(".") do
if file:sub(1,1) ~= '.' then
local attr = lfs.attributes(file)
if attr.mode == "directory" then
compileInFolder(file, file)
elseif attr.mode == "file" and file:sub(-3) == ".md" then
compileFile(file=="README.md" and label or file=="SUMMARY.md" and "tree" or file:sub(1,-4), file)
end
end
end
lfs.chdir(current)
end
end
function compileFile(label, name)
local outname = name=="README.md" and "index.html" or label..".html"
local pop = io.popen(markdowncommand.." "..name)
if pop then
local body = pop:read('*a')
pop:close()
body = body:gsub("href=\"([%w_/%-]*)README.md\"", "href=\"%1index.html\""):gsub("href=\"([%w_/%-]+).md\"", "href=\"%1.html\"")
local out = io.open(outname,'w')
if out then
out:write("<!DOCTYPE html>\n<html>\n<head>\n<title>"..label.."</title>\n<style>body{margin:1em;line-spacing:1.5em;}h1{margin: 0;}h2{margin:0;margin-top:1em;}h3{margin:0;margin-top:1em;}ul{width:90%;}pre,code{background:#eee}</style>\n</head>\n<body>\n"..body..(label=="tree" and "<p><a href=\"LICENSE\">LICENSE</a></p>\n" or "").."</body>\n</html>\n")
out:close()
end
end
end
main(...)