summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_context.lua
diff options
context:
space:
mode:
Diffstat (limited to 'mod/tex/context/third/rst/rst_context.lua')
-rw-r--r--mod/tex/context/third/rst/rst_context.lua57
1 files changed, 32 insertions, 25 deletions
diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua
index 3ea50af..1a86da1 100644
--- a/mod/tex/context/third/rst/rst_context.lua
+++ b/mod/tex/context/third/rst/rst_context.lua
@@ -18,11 +18,16 @@
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 utf = unicode.utf8
+local utflen = utf.len
+local utflower = utf.lower
+local utfupper = utf.upper
+local iowrite = io.write
local dbg_write = helpers.dbg_writef
-local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match = 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
+local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match =
+ 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
-- This one should ignore escaped spaces.
do
@@ -37,10 +42,12 @@ do
return stripper:match(str) or ""
end
end
+local stringstrip = string.strip
+local fmt = string.format
local err = function(str)
if str then
- io.write("\n*[rstctx] Error: " .. str .. "\n\n")
+ iowrite("\n*[rstctx] Error: " .. str .. "\n\n")
end
end
@@ -145,11 +152,11 @@ rst_context.roles.sans_serif = rst_context.roles.sans
rst_context.roles.ss = rst_context.roles.sans
rst_context.roles.uppercase = function(str)
- return utf.upper(str)
+ return utfupper(str)
end
rst_context.roles.lowercase = function(str)
- return utf.lower(str)
+ return utflower(str)
end
rst_context.roles.color = function(color, str)
@@ -157,9 +164,9 @@ rst_context.roles.color = function(color, str)
local definition = color:match("^color_(.+)$")
if definition:match("^rgb_") then -- assume rgb
local rgb = p.rgbvalues:match(definition)
- definition = string.format([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3])
+ definition = fmt([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3])
end
- return string.format([[\\colored[%s]{%s}]], definition, str)
+ return fmt([[\\colored[%s]{%s}]], definition, str)
end
--- Inofficial text roles for my private bib
@@ -259,7 +266,7 @@ function rst_context.anon_reference (str)
str = str:match("^`?([^`]+)`?__$")
rst_context.anonymous_links[#rst_context.anonymous_links+1] = str
link = "__target_anon_" .. #rst_context.anonymous_links
- return string.format([[\\RSTchoosegoto{%s}{%s}]], link, str)
+ return fmt([[\\RSTchoosegoto{%s}{%s}]], link, str)
end
local whitespace = S" \n\t\v"
@@ -311,7 +318,7 @@ function rst_context.substitution_reference (str, underscores)
local sub = ""
rst_context.addsetups("substitutions")
if underscores == "_" then -- normal reference
- sub = sub .. [[\\reference[__target_]] .. rst_context.whitespace_to_underscore(string.strip(str)) .. "]{}"
+ sub = sub .. [[\\reference[__target_]] .. rst_context.whitespace_to_underscore(stringstrip(str)) .. "]{}"
elseif underscores == "__" then -- normal reference
rst_context.anonymous_targets = rst_context.anonymous_targets + 1
sub = sub .. [[\\reference[__target_anon_]] .. rst_context.anonymous_targets .. "]{}"
@@ -368,7 +375,7 @@ local corresponding = {
local inline_parser = P{
[1] = "block",
- block = Cs(V"inline_as_first"^-1 * (V"except" + V"inline_element" + 1)^1),
+ block = Cs(V"inline_as_first"^-1 * (V"except" + V"inline_element" + 1)^0),
inline_element = V"precede_inline"
* Cs(V"inline_do_elements")
@@ -612,7 +619,7 @@ function rst_context.paragraph (data)
else
str = data
end
- return string.format([[
+ return fmt([[
\\startparagraph
%s
@@ -636,12 +643,12 @@ function rst_context.section (...) -- TODO general cleanup; move validity
local tab = { ... } -- checking to parser.
local section, str = true, ""
local adornchar
- local ulen = unicode.utf8.len
+ local ulen = utflen
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 = string.strip(tab[2])
+ str = stringstrip(tab[2])
else -- no overline
adornchar = tab[2]:sub(1,1)
section = ulen(tab[1]) <= ulen(tab[2])
@@ -662,7 +669,7 @@ function rst_context.section (...) -- TODO general cleanup; move validity
ref = get_context_reference (str)
- str = string.format("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str)
+ str = fmt("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str)
else
return [[{\\bf fix your sectioning!}\\endgraf}]]
end
@@ -690,7 +697,7 @@ do
escaped = P"\\" * V"space",
nospace = V"escaped" + (1 - V"space"),
}
- function string.strip(str)
+ function stringstrip(str)
return stripper:match(str) or ""
end
end
@@ -744,7 +751,7 @@ end
function rst_context.startitemize(str)
local setup = ""
local result = ""
- str = string.strip(str)
+ str = stringstrip(str)
local listtype = enumeration_types[str] or parse_itemstring(str)
@@ -784,8 +791,8 @@ function rst_context.bullet_item (tab)
if helpers.list.successor(itemtype, li[current_itemdepth]) then
-- just leave it alone
elseif helpers.list.greater(itemtype, li[current_itemdepth]) then
- local itemnum = tonumber(string.strip(itemtype)) or helpers.list.get_decimal(itemtype)
- result = result .. string.format([[
+ local itemnum = tonumber(stringstrip(itemtype)) or helpers.list.get_decimal(itemtype)
+ result = result .. fmt([[
\\setnumber[itemgroup:itemize]{%s}
]], itemnum)
end
@@ -815,12 +822,12 @@ function rst_context.deflist (list)
local nc = 2
local tmp = [[
- \\RSTdeflistterm{]] .. string.strip(term) .. "}"
+ \\RSTdeflistterm{]] .. stringstrip(term) .. "}"
if #item > 2 then
while nc < #item do
tmp = tmp .. [[
- \\RSTdeflistclassifier{]] .. string.strip(item[nc]) .. "}"
+ \\RSTdeflistclassifier{]] .. stringstrip(item[nc]) .. "}"
nc = nc + 1
end
end
@@ -868,7 +875,7 @@ end
function rst_context.field (tab)
local name, body = tab[1], tab[2]
- return string.format([[
+ return fmt([[
\\RSTfieldname{%s}
\\RSTfieldbody{%s}
@@ -880,7 +887,7 @@ function rst_context.line_comment (str)
end
function rst_context.block_comment (str)
- return string.format([[
+ return fmt([[
\iffalse
%s\fi
@@ -908,7 +915,7 @@ function rst_context.option_list (str)
end
function rst_context.option_item (tab)
- return string.format([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR
+ return fmt([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR
]], tab[1], tab[2])
end
@@ -1134,7 +1141,7 @@ function rst_context.simple_table(tab)
dbg_write("%7s | ", cell.content)
local celltext = inline_parser:match(cell.content)
if cell.span then
- head = head .. string.format([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "")
+ head = head .. fmt([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "")
else
head = head .. [[\\bTH ]] .. celltext .. [[\\eTH]]
end
@@ -1173,7 +1180,7 @@ function rst_context.simple_table(tab)
dbg_write("%7s | ", cell.content)
local celltext = inline_parser:match(cell.content)
if cell.span then
- body = body .. string.format([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "")
+ body = body .. fmt([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "")
else
body = body .. [[\\bTC ]] .. celltext .. [[\\eTC]]
end