From 8d40fbb4fb8d57890cbf02013aa2344b9894c9f2 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 27 Mar 2013 00:24:28 +0100 Subject: cleanup; rewrite directives; update manual --- mod/tex/context/third/rst/rst_context.lua | 152 ++++++++++++++++++------------ 1 file changed, 91 insertions(+), 61 deletions(-) (limited to 'mod/tex/context/third/rst/rst_context.lua') diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua index f880f35..7d0f2b8 100644 --- a/mod/tex/context/third/rst/rst_context.lua +++ b/mod/tex/context/third/rst/rst_context.lua @@ -4,7 +4,7 @@ -- USAGE: called by rst_parser.lua -- DESCRIPTION: Complement to the reStructuredText parser -- AUTHOR: Philipp Gesang (Phg), --- CHANGED: 2012-06-05 22:18:11+0200 +-- CHANGED: 2013-03-26 22:46:17+0100 -------------------------------------------------------------------------------- -- --- TODO @@ -25,10 +25,14 @@ local utfupper = utf.upper local iowrite = io.write local tableconcat = table.concat +local stringmatch = string.match +local stringgmatch = string.gmatch +local stringgsub = string.gsub + local dbg_write = helpers.dbg_writef local C, Cb, Cc, Cg, Cmt, Cp, - Cs, Ct, P, R, S, V, match + Cs, Ct, P, R, S, V, lpegmatch = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match @@ -42,7 +46,7 @@ do escaped = P"\\" * V"space" } function string.strip(str) - return stripper:match(str) or "" + return lpegmatch(stripper, str) or "" end end local stringstrip = string.strip @@ -80,7 +84,7 @@ end function rst_context.footnote_reference (label) local tf = thirddata.rst.state.footnotes - if label:match("^%d+$") then -- all digits + if stringmatch(label, "^%d+$") then -- all digits local c = tonumber(label) return [[\\footnote{\\getbuffer[__footnote_number_]].. c .."]}" elseif label == "#" then --autonumber @@ -88,8 +92,8 @@ function rst_context.footnote_reference (label) rc = rc + 1 rst_context.current_footnote_number = rc return [[\\footnote{\\getbuffer[__footnote_number_]].. rc .."]}" - elseif label:match("^#.+$") then - local thelabel = label:match("^#(.+)$") + elseif stringmatch(label, "^#.+$") then + local thelabel = stringmatch(label, "^#(.+)$") return [[\\footnote{\\getbuffer[__footnote_label_]].. thelabel .."]}" elseif label == "*" then local rc = rst_context.current_symbolnote_number @@ -106,7 +110,7 @@ do local w = S" \v\t\n" / "_" local wp = Cs((w + 1)^1) function rst_context.whitespace_to_underscore(str) - return str and wp:match(str) or "" + return str and lpegmatch(wp, str) or "" end end @@ -165,9 +169,9 @@ end rst_context.roles.color = function(color, str) local p = helpers.patterns - local definition = color:match("^color_(.+)$") - if definition:match("^rgb_") then -- assume rgb - local rgb = p.rgbvalues:match(definition) + local definition = stringmatch(color, "^color_(.+)$") + if stringmatch(definition, "^rgb_") then -- assume rgb + local rgb = lpegmatch(p.rgbvalues, definition) definition = stringformat([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3]) end return stringformat([[\\colored[%s]{%s}]], definition, str) @@ -256,14 +260,14 @@ end function rst_context.interpreted_text (...) local tab = { ... } local role, str - role = tab[1]:match("^:(.*):$") or tab[3]:match("^:(.*):$") + role = stringmatch(tab[1], "^:(.*):$") or stringmatch(tab[3], "^:(.*):$") str = tab[2] if not role then -- implicit role role = "emphasis" end - if role:match("^color_") then + if stringmatch(role, "^color_") then return rst_context.roles.color(role, str) end @@ -277,14 +281,14 @@ end function rst_context.reference (str) rst_context.addsetups("references") - str = str:match("^`?([^`]+)`?_$") -- LPEG could render this gibberish legible but not time + str = stringmatch(str, "^`?([^`]+)`?_$") return [[\\RSTchoosegoto{__target_]] .. rst_context.whitespace_to_underscore(str) .. "}{" .. str .. "}" end function rst_context.anon_reference (str) rst_context.addsetups("references") - str = str:match("^`?([^`]+)`?__$") + str = stringmatch(str, "^`?([^`]+)`?__$") rst_context.anonymous_links[#rst_context.anonymous_links+1] = str link = "__target_anon_" .. #rst_context.anonymous_links return stringformat([[\\RSTchoosegoto{%s}{%s}]], link, str) @@ -320,7 +324,11 @@ function rst_context.target (tab) local anon = create_anonymous() id, arefs[anon[1]] = anon[1], anon[2] else - id = tab[i]:gsub("\\:",":"):match("`?([^`]+)`?") -- deescaping + local tmp = tab[i] + tmp = stringgsub(tmp, "\\:",":") + tmp = stringmatch(tmp, "`?([^`]+)`?") + id = tmp + --id = tab[i]:gsub("\\:",":"):match("`?([^`]+)`?") -- deescaping end if id then refs[id] = refs[id] or target @@ -376,8 +384,8 @@ do } function rst_context.escape (str) - str = str:gsub("\\(.)", "%1") - return p_escape:match(str) + str = stringgsub(str, "\\(.)", "%1") + return lpegmatch(p_escape, str) end end @@ -641,8 +649,15 @@ function rst_context.paragraph (data) if not data then return "" elseif type(data) == "table" then - str = #data > 1 and helpers.string.wrapat(inline_parser:match(tableconcat(data, " ")), 65) - or inline_parser:match(data[1]) +-- str = #data > 1 and helpers.string.wrapat(lpegmatch(inline_parser, tableconcat(data, " ")), 65) +-- or inline_parser:match(data[1]) + if #data > 1 then + str = helpers.string.wrapat( + lpegmatch(inline_parser, tableconcat(data, " ")) + , 65) + else + str = lpegmatch(inline_parser, data[1]) + end else str = data end @@ -674,12 +689,10 @@ function rst_context.section (...) -- TODO general cleanup; move validity if #tab == 3 then -- TODO use unicode length with ConTeXt adornchar = tab[1]:sub(1,1) section = ulen(tab[1]) >= ulen(tab[2]) - --section = get_line_pattern(adornchar):match(tab[1]) ~= nil and section str = stringstrip(tab[2]) else -- no overline adornchar = tab[2]:sub(1,1) section = ulen(tab[1]) <= ulen(tab[2]) - --section = get_line_pattern(adornchar):match(tab[2]) ~= nil and section str = tab[1] end @@ -725,7 +738,7 @@ do nospace = V"escaped" + (1 - V"space"), } function stringstrip(str) - return stripper:match(str) or "" + return lpegmatch(stripper, str) or "" end end @@ -753,26 +766,25 @@ local itemstripper = stripme^0 * C(dontstrip^1) * stripme^0 local function parse_itemstring(str) local offset = nil local setup = ",fit][itemalign=flushright," - -- string.match is slightly faster than string.find - if str:match("^%(") then + if stringmatch(str, "^%(") then setup = setup .. [[left=(,]] end - if str:match("%)$") then + if stringmatch(str, "%)$") then setup = setup .. [[right=)]] end - if str:match("%.$") then + if stringmatch(str, "%.$") then setup = setup .. [[stopper={.\\space}]] end - local num = str:match("^%d") + local num = stringmatch(str, "^%d") if num then -- http://thread.gmane.org/gmane.comp.tex.context/61728/focus=61729 setup = setup .. ",start=" .. num str = "n" end - str = itemstripper:match(str) + str = lpegmatch(itemstripper, str) str = enumeration_types[str] or str - return {setup = setup, str = str} + return { setup = setup, str = str } end function rst_context.startitemize(str) @@ -828,7 +840,7 @@ function rst_context.bullet_item (tab) return result .. [[ -\\item ]] .. inline_parser:match(content) .. [[ +\\item ]] .. lpegmatch(inline_parser, content) .. [[ ]] end @@ -868,7 +880,7 @@ function rst_context.deflist (list) local par = final[np] tmp = tmp .. [[ \\RSTdeflistparagraph{% -]] .. inline_parser:match(par) .. "}\n" +]] .. lpegmatch(inline_parser, par) .. "}\n" end tmp = tmp .. " }" deflist = deflist .. tmp @@ -900,7 +912,7 @@ function rst_context.field_name (str) end function rst_context.field_body (str) - return [[\\fieldbody{]] .. inline_parser:match(str) .. [[}]] + return [[\\fieldbody{]] .. lpegmatch(inline_parser, str) .. [[}]] end function rst_context.field (tab) @@ -909,7 +921,7 @@ function rst_context.field (tab) \\RSTfieldname{%s} \\RSTfieldbody{%s} -]], name, inline_parser:match(body)) +]], name, lpegmatch(inline_parser, body)) end function rst_context.line_comment (str) @@ -937,7 +949,7 @@ function rst_context.option_list (str) \\eTR \\eTABLEhead \\bTABLEbody -]] .. inline_parser:match(str) .. [[ +]] .. lpegmatch(inline_parser, str) .. [[ \\eTABLEbody \\eTABLE @@ -955,11 +967,10 @@ end function rst_context.literal_block (str, included) local indent = P" "^1 - --local stripme = indent:match(str) or 0 local stripme = #str - for line in str:gmatch("[^\n]+") do + for line in stringgmatch(str, "[^\n]+") do -- setting to the lowest indend of all lines - local idt = indent:match(line) + local idt = lpegmatch(indent, line) if line and idt then stripme = idt < stripme and idt or stripme end @@ -976,7 +987,7 @@ function rst_context.literal_block (str, included) end, } - str = strip:match(str) + str = lpegmatch(strip, str) str = [[ \starttyping[lines=hyphenated] @@ -999,7 +1010,7 @@ function rst_context.line_block (str) return [[ \\startlines -]] .. inline_parser:match(str) .. [[\\stoplines +]] .. lpegmatch(inline_parser, str) .. [[\\stoplines ]] end @@ -1018,7 +1029,7 @@ function rst_context.block_quote (tab) \\startlinecorrection \\blank[small] \\startblockquote -]] .. inline_parser:match(tab[1]) .. [[ +]] .. lpegmatch(inline_parser, tab[1]) .. [[ \\stopblockquote ]] @@ -1026,7 +1037,7 @@ function rst_context.block_quote (tab) return tab[2] and str .. [[ \\blank[small] \\startattribution -]] .. inline_parser:match(tab[2]) .. [[ +]] .. lpegmatch(inline_parser, tab[2]) .. [[ \\stopattribution \\blank[small] \\stoplinecorrection @@ -1074,7 +1085,7 @@ function rst_context.grid_table (tab) local c = r[n] if not (c.parent or c.variant == "separator") then - local celltext = inline_parser:match(c.stripped) + local celltext = lpegmatch(inline_parser, c.stripped) if c.span.x or c.span.y then local span_exp = "[" if c.span.x then @@ -1124,7 +1135,7 @@ function rst_context.grid_table (tab) local c = r[n] if not (c.parent or c.variant == "separator") then - local celltext = inline_parser:match(c.stripped) + local celltext = lpegmatch(inline_parser, c.stripped) if c.span.x or c.span.y then local span_exp = "[" if c.span.x then @@ -1172,7 +1183,7 @@ function rst_context.simple_table(tab) for nc=1, #row do local cell = row[nc] dbg_write("%7s | ", cell.content) - local celltext = inline_parser:match(cell.content) + local celltext = lpegmatch(inline_parser, cell.content) if cell.span then head = head .. stringformat([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "") else @@ -1212,7 +1223,7 @@ function rst_context.simple_table(tab) for nc=1, #row do local cell = row[nc] dbg_write("%7s | ", cell.content) - local celltext = inline_parser:match(cell.content) + local celltext = lpegmatch(inline_parser, cell.content) if cell.span then body = body .. stringformat([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "") else @@ -1230,44 +1241,63 @@ end function rst_context.footnote (label, content) local tf = thirddata.rst.state.footnotes rst_context.addsetups("footnotes") - if label:match("^%d+$") then -- all digits - tf.numbered[tonumber(label)] = rst_context.escape(inline_parser:match(content)) + if stringmatch(label, "^%d+$") then -- all digits + tf.numbered[tonumber(label)] = + rst_context.escape(lpegmatch(inline_parser, content)) elseif label == "#" then --autonumber repeat -- until next unrequested number tf.autonumber = tf.autonumber + 1 until tf.numbered[tf.autonumber] == nil - tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content)) - elseif label:match("^#.+$") then - local thelabel = label:match("^#(.+)$") - tf.autolabel[thelabel] = rst_context.escape(inline_parser:match(content)) + tf.numbered[tf.autonumber] = + rst_context.escape(lpegmatch(inline_parser, content)) + elseif stringmatch(label, "^#.+$") then + local thelabel = stringmatch(label, "^#(.+)$") + tf.autolabel[thelabel] = + rst_context.escape(lpegmatch(inline_parser, content)) elseif label == "*" then rst_context.addsetups("footnote_symbol") - tf.symbol[#tf.symbol+1] = rst_context.escape(inline_parser:match(content)) + tf.symbol[#tf.symbol+1] = + rst_context.escape(lpegmatch(inline_parser, content)) else -- “citation reference” treated like ordinary footnote repeat -- until next unrequested number tf.autonumber = tf.autonumber + 1 until tf.numbered[tf.autonumber] == nil - tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content)) + tf.numbered[tf.autonumber] = + rst_context.escape(lpegmatch(inline_parser, content)) end return "" end function rst_context.substitution_definition (subtext, directive, data) - data = tableconcat(data, "\n") - local rs = rst_context.substitutions - rs[subtext] = { directive = directive, data = data } + local tmp + if data.first ~= "" then + tmp = { data.first } + else + tmp = { } + end + data.first = nil + for i=1, #data do -- paragraphs + local current = tableconcat(data[i], "\n") + --current = lpegmatch(inline_parser, current) + --current = rst_context.escape(current) + tmp[#tmp+1] = current + end + data = tableconcat(tmp, "\n\n") + rst_context.substitutions[subtext] = { directive = directive, + data = data } return "" end -- not to be confused with the directive definition table rst_directives -function rst_context.directive(directive, ...) - local d = rst_directives[directive] - if d then +function rst_context.directive(directive, data) + local fun = rst_directives[directive] + if fun then rst_context.addsetups("directive") - local data = {...} local result = "" - result = d(data) + result = fun(data) return result end return "" end + +-- vim:ft=lua:sw=4:ts=4:expandtab -- cgit v1.2.3