summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/context/third/rst/rst_context.lua
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/context/third/rst/rst_context.lua')
-rw-r--r--Master/texmf-dist/tex/context/third/rst/rst_context.lua204
1 files changed, 122 insertions, 82 deletions
diff --git a/Master/texmf-dist/tex/context/third/rst/rst_context.lua b/Master/texmf-dist/tex/context/third/rst/rst_context.lua
index e7637b3bf52..7d0f2b86ace 100644
--- a/Master/texmf-dist/tex/context/third/rst/rst_context.lua
+++ b/Master/texmf-dist/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), <phg42.2a@gmail.com>
--- CHANGED: 2012-06-05 22:18:11+0200
+-- CHANGED: 2013-03-26 22:46:17+0100
--------------------------------------------------------------------------------
--
--- TODO
@@ -18,16 +18,21 @@
local helpers = helpers or thirddata and thirddata.rst_helpers
local rst_directives = rst_directives or thirddata and thirddata.rst_directives
-local utf = unicode.utf8
-local utflen = utf.len
-local utflower = utf.lower
-local utfupper = utf.upper
-local iowrite = io.write
+local utf = unicode.utf8
+local utflen = utf.len
+local utflower = utf.lower
+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
@@ -41,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
@@ -72,13 +77,14 @@ rst_context.current_footnote_number = 0
rst_context.current_symbolnote_number = 0
function rst_context.addsetups(item)
+ local state = thirddata.rst.state
state.addme[item] = state.addme[item] or true
return 0
end
function rst_context.footnote_reference (label)
- local tf = state.footnotes
- if label:match("^%d+$") then -- all digits
+ local tf = thirddata.rst.state.footnotes
+ if stringmatch(label, "^%d+$") then -- all digits
local c = tonumber(label)
return [[\\footnote{\\getbuffer[__footnote_number_]].. c .."]}"
elseif label == "#" then --autonumber
@@ -86,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
@@ -104,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
@@ -163,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)
@@ -254,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
@@ -275,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)
@@ -318,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
@@ -374,13 +384,13 @@ 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
function rst_context.joinindented (tab)
- return table.concat (tab, "")
+ return tableconcat (tab, "")
end
local corresponding = {
@@ -639,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(table.concat(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
@@ -672,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
@@ -723,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
@@ -751,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)
@@ -826,7 +840,7 @@ function rst_context.bullet_item (tab)
return result .. [[
-\\item ]] .. inline_parser:match(content) .. [[
+\\item ]] .. lpegmatch(inline_parser, content) .. [[
]]
end
@@ -842,7 +856,8 @@ function rst_context.deflist (list)
local deflist = [[
\\startRSTdefinitionlist
]]
- for nd, item in ipairs(list) do
+ for nd=1, #list do
+ local item = list[nd]
local term = item[1]
local nc = 2
local tmp = [[
@@ -860,10 +875,12 @@ function rst_context.deflist (list)
\\RSTdeflistdefinition{%
]]
- for np, par in ipairs(item[#item]) do -- definitions, multi-paragraph
+ local final = item[#item]
+ for np=1, #final do
+ local par = final[np]
tmp = tmp .. [[
\\RSTdeflistparagraph{%
-]] .. inline_parser:match(par) .. "}\n"
+]] .. lpegmatch(inline_parser, par) .. "}\n"
end
tmp = tmp .. " }"
deflist = deflist .. tmp
@@ -895,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)
@@ -904,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)
@@ -932,7 +949,7 @@ function rst_context.option_list (str)
\\eTR
\\eTABLEhead
\\bTABLEbody
-]] .. inline_parser:match(str) .. [[
+]] .. lpegmatch(inline_parser, str) .. [[
\\eTABLEbody
\\eTABLE
@@ -950,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
@@ -971,7 +987,7 @@ function rst_context.literal_block (str, included)
end,
}
- str = strip:match(str)
+ str = lpegmatch(strip, str)
str = [[
\starttyping[lines=hyphenated]
@@ -994,7 +1010,7 @@ function rst_context.line_block (str)
return [[
\\startlines
-]] .. inline_parser:match(str) .. [[\\stoplines
+]] .. lpegmatch(inline_parser, str) .. [[\\stoplines
]]
end
@@ -1013,7 +1029,7 @@ function rst_context.block_quote (tab)
\\startlinecorrection
\\blank[small]
\\startblockquote
-]] .. inline_parser:match(tab[1]) .. [[
+]] .. lpegmatch(inline_parser, tab[1]) .. [[
\\stopblockquote
]]
@@ -1021,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
@@ -1054,9 +1070,9 @@ function rst_context.grid_table (tab)
]]
while nr <= tab.head_end do
local r = tab.rows[nr]
- --for i,r in ipairs(tab.rows) do
local isempty = true
- for n, cell in ipairs(r) do
+ for n=1, #r do
+ local cell = r[n]
if cell.variant == "normal" then
isempty = false
break
@@ -1065,10 +1081,11 @@ function rst_context.grid_table (tab)
if not isempty then
local row = [[\\bTR]]
- for n,c in ipairs(r) do
+ for n=1, #r do
+ 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
@@ -1103,9 +1120,9 @@ function rst_context.grid_table (tab)
end
while nr <= #tab.rows do
local r = tab.rows[nr]
- --for i,r in ipairs(tab.rows) do
local isempty = true
- for n, cell in ipairs(r) do
+ for n=1, #r do
+ local cell = r[n]
if cell.variant == "normal" then
isempty = false
break
@@ -1114,10 +1131,11 @@ function rst_context.grid_table (tab)
if not isempty then
local row = [[\\bTR]]
- for n,c in ipairs(r) do
+ for n=1, #r do
+ 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
@@ -1162,9 +1180,10 @@ function rst_context.simple_table(tab)
if not row.ignore then
dbg_write(">hr>" .. #row)
head = head .. [[\\bTR]]
- for nc,cell in ipairs(row) do
+ 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
@@ -1201,9 +1220,10 @@ function rst_context.simple_table(tab)
if not row.ignore then
dbg_write(">tr>" .. #row)
body = body .. [[\\bTR]]
- for nc,cell in ipairs(row) do
+ 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
@@ -1219,45 +1239,65 @@ function rst_context.simple_table(tab)
end
function rst_context.footnote (label, content)
- local tf = state.footnotes
+ 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 = table.concat(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 rd = rst_directives
- rst_context.addsetups("directive")
- local data = {...}
- local result = ""
- if rd[directive] then
- result = rd[directive](data)
+function rst_context.directive(directive, data)
+ local fun = rst_directives[directive]
+ if fun then
+ rst_context.addsetups("directive")
+ local result = ""
+ result = fun(data)
+ return result
end
- return result
+ return ""
end
+
+-- vim:ft=lua:sw=4:ts=4:expandtab