summaryrefslogtreecommitdiff
path: root/mod/tex/context
diff options
context:
space:
mode:
authorPhilipp Gesang <gesang@stud.uni-heidelberg.de>2013-03-12 18:11:37 +0100
committerPhilipp Gesang <gesang@stud.uni-heidelberg.de>2013-03-12 18:11:37 +0100
commit942daebfbd9ce27aa5208411d58226100a1b2935 (patch)
treee6fc5900aba33189b9108c045360adf51c7f6056 /mod/tex/context
parent4c8419ab61e06bfcc639cb731d9837d1159b5800 (diff)
downloadcontext-rst-942daebfbd9ce27aa5208411d58226100a1b2935.tar.gz
prototype for a “container” directive
Diffstat (limited to 'mod/tex/context')
-rw-r--r--mod/tex/context/third/rst/rst_context.lua32
-rw-r--r--mod/tex/context/third/rst/rst_directives.lua55
-rw-r--r--mod/tex/context/third/rst/rst_setups.lua9
3 files changed, 65 insertions, 31 deletions
diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua
index b079730..a5fb603 100644
--- a/mod/tex/context/third/rst/rst_context.lua
+++ b/mod/tex/context/third/rst/rst_context.lua
@@ -18,11 +18,12 @@
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 dbg_write = helpers.dbg_writef
@@ -381,7 +382,7 @@ do
end
function rst_context.joinindented (tab)
- return table.concat (tab, "")
+ return tableconcat (tab, "")
end
local corresponding = {
@@ -640,7 +641,7 @@ 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)
+ str = #data > 1 and helpers.string.wrapat(inline_parser:match(tableconcat(data, " ")), 65)
or inline_parser:match(data[1])
else
str = data
@@ -1245,7 +1246,7 @@ function rst_context.footnote (label, content)
end
function rst_context.substitution_definition (subtext, directive, data)
- data = table.concat(data, "\n")
+ data = tableconcat(data, "\n")
local rs = rst_context.substitutions
rs[subtext] = { directive = directive, data = data }
return ""
@@ -1253,12 +1254,13 @@ 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)
+ local d = rst_directives[directive]
+ if d then
+ rst_context.addsetups("directive")
+ local data = {...}
+ local result = ""
+ result = d(data)
+ return result
end
- return result
+ return ""
end
diff --git a/mod/tex/context/third/rst/rst_directives.lua b/mod/tex/context/third/rst/rst_directives.lua
index 9ece1db..c5f0b68 100644
--- a/mod/tex/context/third/rst/rst_directives.lua
+++ b/mod/tex/context/third/rst/rst_directives.lua
@@ -20,6 +20,8 @@ local rst_context = thirddata.rst
local stringstrip = string.strip
local stringformat = string.format
+local tableconcat = table.concat
+local lpegmatch = lpeg.match
rst_directives.anonymous = 0
rst_directives.images = {}
@@ -84,9 +86,10 @@ rst_directives.image = function(name, data)
local processed = "" -- stub; TODO do something useful with optional dimension specifications
if type(data) == "table" then -- should always be true
local p = helpers.patterns
- for _, str in ipairs(data) do
+ for i=1, #data do
+ local str = data[i]
local key, val
- key, val = p.colon_keyval:match(str)
+ key, val = lpegmatch(p.colon_keyval, str)
local rdi = rst_directives.images
if key and val then
key = rdi.keys[key] -- sanitize key expression
@@ -118,12 +121,12 @@ rst_directives.image = function(name, data)
\def\RSTsubstitution%s{%%
\placefigure[here]{%s}{\externalfigure[%s]%s}
}
-]], name, rst_context.escape(inline_parser:match(properties.caption)), name, properties.setup)
+]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup)
else -- image won't be referenced but used instantly
img = img .. stringformat([[
\placefigure[here]{%s}{\externalfigure[%s]%s}
-]], rst_context.escape(inline_parser:match(properties.caption)), data, properties.setup)
+]], rst_context.escape(lpegmatch(inline_parser, properties.caption)), data, properties.setup)
end
return img
end
@@ -134,8 +137,9 @@ rst_directives.caution = function(raw)
rst_context.addsetups("caution")
local text
local first = true
- for _, line in ipairs(raw) do
- if not helpers.patterns.spacesonly:match(line) then
+ for i=1, #raw do
+ local line = raw[i]
+ if not lpegmatch(helpers.patterns.spacesonly, line) then
if first then
text = line
first = false
@@ -144,7 +148,7 @@ rst_directives.caution = function(raw)
end
end
end
- text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text)))
+ text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text)))
return stringformat([[
\startRSTcaution
%s
@@ -158,8 +162,9 @@ rst_directives.danger = function(raw)
rst_context.addsetups("danger")
local text
local first = true
- for _, line in ipairs(raw) do
- if not helpers.patterns.spacesonly:match(line) then
+ for i=1, #raw do
+ local line = raw[i]
+ if not lpegmatch(helpers.patterns.spacesonly, line) then
if first then
text = line
first = false
@@ -168,7 +173,7 @@ rst_directives.danger = function(raw)
end
end
end
- text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text)))
+ text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text)))
return stringformat([[
\startRSTdanger
%s
@@ -179,7 +184,8 @@ end
-- http://docutils.sourceforge.net/docs/ref/rst/directives.html
rst_directives.DANGER = function(addendum)
local result = ""
- for _,str in ipairs(addendum) do
+ for i=1, #addendum do
+ local str = addendum[i]
result = result .. (stringstrip(str))
end
return stringformat([[
@@ -264,7 +270,7 @@ rst_directives.math = function (name, data)
data = data or name
local formula
if type(data) == "table" then
- local last, i = table.maxn(data), 1
+ local last, i = #data, 1
while i <= last do
local line = stringstrip(data[i])
if line and line ~= "" then
@@ -291,3 +297,28 @@ rst_directives.replace = function(name, data)
]], name, data)
end
+--------------------------------------------------------------------------------
+--- Containers.
+--------------------------------------------------------------------------------
+
+rst_directives.container = function(data)
+ local inline_parser = rst_context.inline_parser
+ local name, content = stringstrip(data[1]), ""
+ local tmp = { }
+ for i=2, #data do
+ tmp[#tmp+1] = data[i]
+ end
+ content = tableconcat(tmp, " ")
+ content = lpegmatch(inline_parser, content)
+ return stringformat([[
+\ifcsname %s\endcsname%%
+ \csname %s\endcsname%%
+\else
+ \relax
+\fi%%
+{%s}%%
+]], name, name, content)
+end
+
+-- vim:ft=lua:sw=4:ts=4:expandtab
+
diff --git a/mod/tex/context/third/rst/rst_setups.lua b/mod/tex/context/third/rst/rst_setups.lua
index 93084d9..64e9171 100644
--- a/mod/tex/context/third/rst/rst_setups.lua
+++ b/mod/tex/context/third/rst/rst_setups.lua
@@ -126,12 +126,13 @@ function optional_setups.substitutions ()
]]
local rs = rst_context.substitutions
for name, content in next, rs do
- local directive, data = content.directive, content.data
+ local id, data = content.directive, content.data
name, data = name:gsub("%s", ""), stringstrip(data)
- if directives[directive] then
- substitutions = substitutions .. directives[directive](name, data)
+ local directive = directives[id]
+ if directive then
+ substitutions = substitutions .. directive(name, data)
else
- err(directive .. " does not exist.")
+ err(id .. " does not exist.")
end
end
return substitutions