diff options
Diffstat (limited to 'mod/tex/context/third/rst/rst_directives.lua')
-rw-r--r-- | mod/tex/context/third/rst/rst_directives.lua | 212 |
1 files changed, 105 insertions, 107 deletions
diff --git a/mod/tex/context/third/rst/rst_directives.lua b/mod/tex/context/third/rst/rst_directives.lua index f143c56..374f942 100644 --- a/mod/tex/context/third/rst/rst_directives.lua +++ b/mod/tex/context/third/rst/rst_directives.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:17:51+0200 +-- CHANGED: 2013-03-26 22:45:45+0100 -------------------------------------------------------------------------------- -- @@ -18,12 +18,14 @@ local rst_directives = { } thirddata.rst_directives = rst_directives local rst_context = thirddata.rst -local stringstrip = string.strip -local stringformat = string.format -local tableconcat = table.concat -local lpegmatch = lpeg.match +local lpegmatch = lpeg.match +local stringformat = string.format +local stringstrip = string.strip +local tableconcat = table.concat +local tableflattened = table.flattened +local type = type -rst_directives.anonymous = 0 +--rst_directives.anonymous = 0 rst_directives.images = {} rst_directives.images.done = {} rst_directives.images.values = {} @@ -52,13 +54,14 @@ rst_directives.images.values.width = { } -- we won't allow passing arbitrary setups to context -rst_directives.images.permitted_setups = { - "width", "scale" +local permitted_setups = { + "width", + "scale" } local function img_setup (properties) local result = "" - for _, prop in next, rst_directives.images.permitted_setups do + for _, prop in next, permitted_setups do if properties[prop] then result = result .. prop .. "=" .. properties[prop] .. "," end @@ -69,124 +72,108 @@ local function img_setup (properties) return result end -rst_directives.image = function(name, data) +rst_directives.image = function(data) local inline_parser = rst_context.inline_parser - local properties = {} - local anon = false - local rd = rst_directives - if not data then -- this makes the “name” argument optional - data = name - rd.anonymous = rd.anonymous + 1 - anon = true -- indicates a nameless picture - name = "anonymous" .. rd.anonymous - end + local properties = {} + local anon = false + local rdi = rst_directives.images + local hp = helpers.patterns + + local name = stringstrip(data.name) + + --rd.anonymous = rd.anonymous + 1 + --anon = true -- indicates a nameless picture + --name = "anonymous" .. rd.anonymous + properties.caption = name - --properties.width = "\\local" - - 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 i=1, #data do - local str = data[i] - local key, val - key, val = lpegmatch(p.colon_keyval, str) - local rdi = rst_directives.images - if key and val then - key = rdi.keys[key] -- sanitize key expression - if type(rdi.values[key]) == "table" then - val = rdi.values[key][val] - elseif type(rdi.values[key]) == "function" then - val = rdi.values[key](val) - end - properties[key] = val - else - processed = processed .. (str and str ~= "" and stringstrip(str)) + data = tableflattened(data) + + for i=1, #data do + local str = data[i] + local key, val = lpegmatch(hp.colon_keyval, str) + if key and val then + key = rdi.keys[key] -- sanitize key expression + local valtype = type(rdi.values[key]) + if valtype == "table" then + val = rdi.values[key][val] + elseif valtype == "function" then + val = rdi.values[key](val) end + properties[key] = val end end properties.setup = img_setup(properties) or "" - data = processed - processed = nil local img = "" - local images_done = rd.images.done - if not anon then - if not images_done[name] then - img = img .. stringformat([[ - -\useexternalfigure[%s][%s][] -]], name, data) - images_done[name] = true - end - img = img .. stringformat([[ -\def\RSTsubstitution%s{%% - \placefigure[here]{%s}{\externalfigure[%s]%s} -} -]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup) - else -- image won't be referenced but used instantly - img = img .. stringformat([[ +-- local images_done = rdi.done +-- if not anon then -- TODO: implement? +-- if not images_done[name] then +-- img = img .. stringformat([[ +-- +--\useexternalfigure[%s][%s][]%% +--]], name, data) +-- images_done[name] = true +-- end +-- img = img .. stringformat([[ +--\def\RSTsubstitution%s{%% +-- \placefigure[here]{%s}{\externalfigure[%s]%s}%% +--} +--]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup) +-- else -- image won't be referenced but used instantly + img = stringformat([[ \placefigure[here]{%s}{\externalfigure[%s]%s} -]], rst_context.escape(lpegmatch(inline_parser, properties.caption)), data, properties.setup) - end +]], rst_context.escape(lpegmatch(inline_parser, properties.caption)), + name, + properties.setup) +-- end return img end -rst_directives.caution = function(raw) +rst_directives.caution = function(data) local inline_parser = rst_context.inline_parser rst_context.addsetups("dbend") rst_context.addsetups("caution") - local text - local first = true - for i=1, #raw do - local line = raw[i] - if not lpegmatch(helpers.patterns.spacesonly, line) then - if first then - text = line - first = false - else - text = text .. " " .. line - end - end + local text = { } + for i=1, #data do -- paragraphs + local current = tableconcat(data[i], "\n") + current = lpegmatch(inline_parser, current) + current = rst_context.escape(current) + text[i] = current end - text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) return stringformat([[ \startRSTcaution %s \stopRSTcaution -]], text) +]], tableconcat(text, "\n\n")) end -rst_directives.danger = function(raw) +rst_directives.danger = function(data) local inline_parser = rst_context.inline_parser rst_context.addsetups("dbend") rst_context.addsetups("danger") - local text - local first = true - for i=1, #raw do - local line = raw[i] - if not lpegmatch(helpers.patterns.spacesonly, line) then - if first then - text = line - first = false - else - text = text .. " " .. line - end - end + local text = { } + for i=1, #data do -- paragraphs + local current = tableconcat(data[i], "\n") + current = lpegmatch(inline_parser, current) + current = rst_context.escape(current) + text[i] = current end - text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) return stringformat([[ \startRSTdanger %s \stopRSTdanger -]], text) +]], tableconcat(text, "\n\n")) end -- http://docutils.sourceforge.net/docs/ref/rst/directives.html -rst_directives.DANGER = function(addendum) - local result = "" - for i=1, #addendum do - local str = addendum[i] - result = result .. (stringstrip(str)) +rst_directives.DANGER = function(data) + local inline_parser = rst_context.inline_parser + local text = { } + for i=1, #data do -- paragraphs + local current = tableconcat(data[i], "\n") + current = lpegmatch(inline_parser, current) + current = rst_context.escape(current) + text[i] = current end return stringformat([[ @@ -215,7 +202,7 @@ rst_directives.DANGER = function(addendum) } \blank[force,big] \stoplinecorrection -]], result) +]], tableconcat(text, "\n\n")) end rst_directives.mp = function(name, data) @@ -301,24 +288,35 @@ end --- Containers. -------------------------------------------------------------------------------- +--- *data*: +--- { [1] -> directive name, +--- [>1] -> paragraphs } + rst_directives.container = function(data) local inline_parser = rst_context.inline_parser - local name, content = stringstrip(data[1]), "" - if name == "" then - name = "framed" - end local tmp = { } - for i=2, #data do - tmp[#tmp+1] = data[i] + for i=1, #data do -- paragraphs + local current = tableconcat(data[i], "\n") + current = lpegmatch(inline_parser, current) + current = rst_context.escape(current) + tmp[i] = current end - -- content |> concat |> match |> escape - content = tableconcat(tmp, " ") - content = lpegmatch(inline_parser, content) - content = rst_context.escape(content) - return stringformat([[ + local content = tableconcat(tmp, "\n\n") + local name = data.name + if name and name ~= "" then + name = stringstrip(data.name) + return stringformat([[ \start[%s]%% -%s\stop +%s%% +\stop ]], name, content) + else + return stringformat([[ +\begingroup%% +%s%% +\endgroup +]], content) + end end -- vim:ft=lua:sw=4:ts=4:expandtab |