-- makedown : makefile -> HTML -- -- usage: makedown [] [-o ] -- ------------------------------------------------------------------------ -- Makefile markup features: -- -- * Lines beginning with "#" are formatted as paragraphs (

). Blank lines -- indicate paragraph boundaries (empty comment lines count as blank -- lines). -- -- * Within a paragraph, the following markup is supported: -- -- _word_ : italics -- *word* : boldface -- ``...text...`` : code (monospaced) -- -- * Lines beginning with multiple "#" become headers ("##" -> H3, "###" -> -- H2, "####" -> H1). Yes this is backwards from atx format, but THIS is -- the right way. :-) -- -- * Other lines are grouped into lit blocks:

. -- -- Within these, "expect" lines -- lince containing just $(call -- expect,result,exp) -- converted into tables. Consecutive expect -- lines are grouped into a single table. -- -- Blank lines within lit blocks (that is, following and preceding -- non-blank lit lines) become
elements. -- -- * A comment beginning "#-" causes the entire line contents to be ignored -- (equivalent to a blank line, actually). -- ------------------------------------------------------------------------ local lpeg = require "lpeg" local re = require "re" local function expect(a,b) if a ~= b then error(string.format("Failed\nGot: %s\nExpected: %s\n", b, a), 2) end end local fileRE = [[ Top <- ( / /

/ )* -> {} Hx <- "#" ( {"#"+} {~ [^%nl]* ~} -> mark %nl ) -> hx P <- {~ + ~} -> mark -> parg PLine <- "#"->"" %ls+ [^%nl]* %nl->" " Lit <- {~ ( ->br* () )* ~} -> lit LLine <- + -> {} -> expect / ![#%nl] + {%nl}->br ELine <- %ls* "$(call expect" %ls* "," ( "," ) -> {} ")" %ls* %nl Blank <- ( %ls* "#"? %ls* ) %nl / [^#%nl]* "#-" [^%nl]* %nl -- make function arguments Arg <- {~ * ~} ArgC <- "(" ( + / "," )* ")" / ![,)] LC <- "&" -> "&" / "<" -> "<" / [^%nl] ]] -- Encode text with markup as HTML CDATA: "&" -> "&", etc. -- local markRE = [[ Txt <- {~ * ~} C <- ( "[" {~ (!"]".)* ~} "](" {~ [^)]* ~} ")" ) -> lnk / CS <- [%ls]+ -> " " / "*"->"" [^*]* "*"->"" &[^%w] / "_"->"" [^_]* "_"->"" &[^%w] / + WChar <- "``"->"" (!"``" .)* "``"->"" / QC <- "&" -> "&" / "<" -> "<" / [^%ls] ]] local ls = lpeg.S" \t\f\r" local mark function lnk(txt,uri) return '' .. mark(txt) .. '' end local markExp = re.compile(markRE, {ls=ls, lnk=lnk}) function mark(txt) return markExp:match(txt) end expect("b c x & d", markExp:match("_b_ *c* ``x`` & d")) expect('txt', markExp:match("[txt](x)")) local markDefs = { ls = ls, parg = function (c) return "

"..c.."

" end, br = function (c) return "
" end, lit = function (c) return "
"..c.."
" end, hx = function (h,s) local tag = "h" .. tostring(3 - h:sub(-2):len()) return "<"..tag..">"..s.."" end, expect = function(t) local s = "\n" for _,e in ipairs(t) do local eo = e[1]:gsub("%$%$","$") s = s .. "\n" s = s .. "" s = s .. "\n" end return s.."
"..e[2].."
\""..eo.."\"
" end, mark = mark } local fileExp = re.compile(fileRE, markDefs) local template = [[ ]] local function makedown(txt) return template:gsub("<(%w+)>", { MARK = table.concat(fileExp:match(txt), "\n\n"), }) end ------------------------------------------------------------------------ local narg = 0 local function nextArg() narg = narg + 1 return arg[narg] end local files = {} local nameOut for a in nextArg do if a == "--templ" then ftempl = nextArg() elseif a == "-o" then nameOut = nextArg() else table.insert(files,a) end end if files[2] or not files[1] then print("usage: "..arg[0].." []") os.exit(0) end local fin = io.stdin if files[1] then fin = assert(io.open(files[1], "rb")) end local txt = fin:read("*a") fin:close() local htmlOut = makedown(txt) local fout = nameOut and assert(io.open(nameOut, "wb")) or io.stdout fout:write(htmlOut) fout:close()