#!/usr/bin/env lua5.3 -- special marks: -- \1 - paragraph (empty line) -- \4 - remove spaces around it -- \3 - ref (followed by label|) --------------------------------------------------------------- header = [[
Copyright © 2024 Lua.org, PUC-Rio. All rights reserved.
]] footer = "\n\n\n\n" local seefmt = '(see %s)' if arg[1] == 'port' then seefmt = '(ver %s)' header = string.gsub(header, "by (.-)\n", "%1\n
Tradução: Sérgio Queiroz de Medeiros", 1) header = string.gsub(header, "Lua (%d+.%d+) Reference Manual", "Manual de Referência de Lua %1") header = string.gsub(header, "All rights reserved", "Todos os direitos reservados") end --------------------------------------------------------------- local function compose (f,g) assert(f and g) return function (s) return g(f(s)) end end local function concat (f, g) assert(f and g) return function (s) return f(s) .. g(s) end end local Tag = {} setmetatable(Tag, { __index = function (t, tag) local v = function (n, att) local e = "" if type(att) == "table" then for k,v in pairs(att) do e = string.format('%s %s="%s"', e, k, v) end end if n then return string.format("<%s%s>%s%s>", tag, e, n, tag) else return string.format("<%s%s>", tag, e) end end t[tag] = v return v end }) --------------------------------------------------------------- local labels = {} local function anchor (text, label, link, textlink) if labels[label] then error("label " .. label .. " already defined") end labels[label] = {text = textlink, link = link} return Tag.a(text, {name=link}) end local function makeref (label) assert(not string.find(label, "|")) return string.format("\3%s\3", label) end local function ref (label) local l = labels[label] if not l then io.stderr:write("label ", label, " undefined\n") return "@@@@@@@" else return Tag.a(l.text, {href="#"..l.link}) end end --------------------------------------------------------------- local function nopara (t) t = string.gsub(t, "\1", "\n\n") t = string.gsub(t, "
%s*
", "") return t end local function fixpara (t) t = string.gsub(t, "\1", "\n\n\n\n") t = string.gsub(t, "
%s*
", "") return t end local function antipara (t) return "\n" .. t .. "" end Tag.pre = compose(Tag.pre, antipara) Tag.ul = compose(Tag.ul, antipara) --------------------------------------------------------------- local Gfoots = 0 local footnotes = {} local line = Tag.hr(nil) local function dischargefoots () if #footnotes == 0 then return "" end local fn = table.concat(footnotes) footnotes = {} return line .. Tag.h3"footnotes:" .. fn .. line end local Glists = 0 local listings = {} local function dischargelist () if #listings == 0 then return "" end local l = listings listings = {} return line .. table.concat(l, line..line) .. line end --------------------------------------------------------------- local counters = { h1 = {val = 1}, h2 = {father = "h1", val = 1}, h3 = {father = "h2", val = 1}, listing = {father = "h1", val = 1}, } local function inccounter (count) counters[count].val = counters[count].val + 1 for c, v in pairs(counters) do if v.father == count then v.val = 1 end end end local function getcounter (count) local c = counters[count] if c.father then return getcounter(c.father) .. "." .. c.val else return c.val .. "" end end --------------------------------------------------------------- local function fixed (x) return function () return x end end local function id (x) return x end local function prepos (x, y) assert(x and y) return function (s) return string.format("%s%s%s", x, s, y) end end local rw = Tag.b local function LuaName (name) return Tag.code(name) end local function getparam (s) local i, e = string.find(s, "^[^%s@|]+|") if not i then return nil, s else return string.sub(s, i, e - 1), string.sub(s, e + 1) end end local function gettitle (h) local title, p = assert(string.match(h, "
" end end local function verbatim (s) s = nopara(s) s = string.gsub(s, "\n", "\n ") s = string.gsub(s, "\n%s*$", "\n") return Tag.pre(s) end local function verb (s) return Tag.code(s) end local function lua2link (e) return string.find(e, "luaL?_") and e or "pdf-"..e end local verbfixed = verb local Tex = { ANSI = function (func) return "ISO C function " .. Tag.code(func) end, At = fixed"@", B = Tag.b, bigskip = fixed"", bignum = id, C = fixed"", Ci = prepos(""), CId = function (func) return "C function " .. Tag.code(func) end, chapter = section"h1", Char = compose(verbfixed, prepos("'", "'")), Cdots = fixed"···", Close = fixed"}", col = Tag.td, defid = function (name) local l = lua2link(name) local c = Tag.code(name) return anchor(c, l, l, c) end, def = Tag.em, description = compose(nopara, Tag.ul), Em = fixed("\4" .. "—" .. "\4"), emph = Tag.em, emphx = Tag.em, -- emphasis plus index (if there was an index) En = fixed("–"), format = fixed"", ["false"] = fixed(Tag.b"false"), id = Tag.code, idx = Tag.code, index = fixed"", Lidx = fixed"", -- Tag.code, ldots = fixed"...", x = id, itemize = compose(nopara, Tag.ul), leq = fixed"≤", Lid = function (s) return makeref(lua2link(s)) end, M = Tag.em, N = function (s) return (string.gsub(s, " ", " ")) end, NE = id, -- tag"foreignphrase", num = id, ["nil"] = fixed(Tag.b"nil"), fail = fixed(Tag.b"fail"), Open = fixed"{", part = section("h1", true), Pat = compose(verbfixed, prepos("'", "'")), preface = section("h1", true), psect = section("h2", true), Q = prepos('"', '"'), refchp = makeref, refcode = makeref, refsec = makeref, pi = fixed"π", rep = Tag.em, -- compose(prepos("<", ">"), Tag.em), Rw = rw, rw = rw, sb = Tag.sub, sp = Tag.sup, St = compose(verbfixed, prepos('"', '"')), sect1 = section"h1", sect2 = section"h2", sect3 = section"h3", sect4 = section("h4", true), simplesect = id, Tab2 = function (s) return Tag.table(s, {border=1}) end, row = Tag.tr, title = Tag.title, todo = Tag.todo, ["true"] = fixed(Tag.b"true"), T = verb, item = function (s) local t, p = string.match(s, "^([^\n|]+)|()") if t then s = string.sub(s, p) s = Tag.b(t) ..": " .. s end return Tag.li(fixpara(s)) end, verbatim = verbatim, manual = id, -- for the manual link =function (s) local l, t = getparam(s) assert(l) return string.format("%s (%s)", t, makeref(l)) end, see = function (s) return string.format(seefmt, makeref(s)) end, See = makeref, seeC = function (s) return string.format(seefmt, makeref(s)) end, seeF = function (s) return string.format(seefmt, makeref(lua2link(s))) end, APIEntry = function (e) local h, name h, e = string.match(e, "^%s*(.-)%s*|(.*)$") name = string.match(h, "(luaL?_[%w_]+)%)? +%(") or string.match(h, "luaL?_[%w_]+") local a = anchor(Tag.code(name), name, name, Tag.code(name)) local apiicmd, ne = string.match(e, "^(.-)(.*)") --io.stderr:write(e) if not apiicmd then return antipara(Tag.hr() .. Tag.h3(a)) .. Tag.pre(h) .. e else return antipara(Tag.hr() .. Tag.h3(a)) .. apiicmd .. Tag.pre(h) .. ne end end, LibEntry = function (e) local h, name h, e = string.match(e, "^(.-)|(.*)$") name = string.gsub(h, " (.+", "") local l = lua2link(name) local a = anchor(Tag.code(h), l, l, Tag.code(name)) return Tag.hr() .. Tag.h3(a) .. e end, Produc = compose(nopara, Tag.pre), producname = prepos("\t", " ::= "), Or = fixed" | ", VerBar = fixed"|", -- vertical bar OrNL = fixed" | \4", bnfNter = prepos("", ""), bnfopt = prepos("[", "]"), bnfrep = prepos("{", "}"), bnfter = compose(Tag.b, prepos("‘", "’")), producbody = function (s) s = string.gsub(s, "%s+", " ") s = string.gsub(s, "\4", "\n\t\t") return s end, apii = function (s) local pop,push,err = string.match(s, "^(.-),(.-),(.*)$") if pop ~= "?" and string.find(pop, "%W") then pop = "(" .. pop .. ")" end if push ~= "?" and string.find(push, "%W") then push = "(" .. push .. ")" end err = (err == "-") and "–" or Tag.em(err) return Tag.span( string.format("[-%s, +%s, %s]", pop, push, err), {class="apii"} ) end, } local others = prepos("?? "," ??") local function trata (t) t = string.gsub(t, "@(%w+)(%b{})", function (w, f) f = trata(string.sub(f, 2, -2)) if type(Tex[w]) ~= "function" then io.stderr:write(w .. "\n") return others(f) else return Tex[w](f, w) end end) return t end --------------------------------------------------------------------- --------------------------------------------------------------------- -- read whole book t = io.read"*a" t = string.gsub(t, "[<>&\128-\255]", {["<"] = "<", [">"] = ">", ["&"] = "&", ["\170"] = "ª", ["\186"] = "º", ["\192"] = "À", ["\193"] = "Á", ["\194"] = "Â", ["\195"] = "Ã", ["\199"] = "Ç", ["\201"] = "É", ["\202"] = "Ê", ["\205"] = "Í", ["\211"] = "Ó", ["\212"] = "Ô", ["\218"] = "Ú", ["\224"] = "à", ["\225"] = "á", ["\226"] = "â", ["\227"] = "ã", ["\231"] = "ç", ["\233"] = "é", ["\234"] = "ê", ["\237"] = "í", ["\243"] = "ó", ["\244"] = "ô", ["\245"] = "õ", ["\250"] = "ú", ["\252"] = "ü" }) t = string.gsub(t, "\n\n+", "\1") -- complete macros with no arguments t = string.gsub(t, "(@%w+)([^{%w])", "%1{}%2") t = trata(t) -- correct references t = string.gsub(t, "\3(.-)\3", ref) -- remove extra space (??) t = string.gsub(t, "%s*\4%s*", "") t = nopara(t) -- HTML 3.2 does not need
(but complains when it is in wrong places :) t = string.gsub(t, "", "") io.write(header, t, footer)