From 7358c6bd092cf39fee1e73420aa7f9687c8833dd Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 18 Sep 2010 10:48:15 +0200 Subject: =?UTF-8?q?support=20for=20key-val=20extended=20image=20directives?= =?UTF-8?q?=20(only=20=E2=80=9Cwidth=E2=80=9D=20for=20now).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rst_context.lua | 63 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 9 deletions(-) (limited to 'rst_context.lua') diff --git a/rst_context.lua b/rst_context.lua index e64aac4..f17f5a6 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -1085,8 +1085,6 @@ function rst_context.directive(directive, ...) local rd = rst_context.directives rst_context.addsetups("directive") local data = {...} - --print(directive, data, #data) - --for i,j in next,data do print(i,j) end local result = "" if rd[directive] then result = rd[directive](data) @@ -1178,9 +1176,43 @@ end rst_context.directives = {} rst_context.directives.anonymous = 0 -rst_context.directives.images_done = {} +rst_context.directives.images = {} +rst_context.directives.images.done = {} +rst_context.directives.images.values = {} + +rst_context.directives.images.keys = { + ["width"] = "width", + ["size"] = "width", + ["caption"] = "caption", +} + +rst_context.directives.images.values.width = { + ["fit"] = "\\hsize", + ["hsize"] = "\\hsize", + ["broad"] = "\\hsize", + ["normal"] = "local", + ["normal"] = "local", +} +-- we won't allow passing arbitrary setups to context +rst_context.directives.images.permitted_setups = { + "width" +} + +local function img_setup (properties) + local result = "" + for _, prop in next, rst_context.directives.images.permitted_setups do + if properties[prop] then + result = result .. prop .. "=" .. properties[prop] .. "," + end + end + if result ~= "" then + result = "[" .. result .. "]" + end + return result +end rst_context.directives.image = function(name, data) + local properties = {} local anon = false local rd = rst_context.directives if not data then -- this makes the “name” argument optional @@ -1189,17 +1221,30 @@ rst_context.directives.image = function(name, data) anon = true -- indicates a nameless picture name = "anonymous" .. rd.anonymous end + 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 _, str in ipairs(data) do - processed = processed .. (str and str ~= "" and string.strip(str)) + local key, val + key, val = p.colon_keyval:match(str) + local rdi = rst_context.directives.images + if key and val then + key = rdi.keys[key] -- sanitize key expression + val = rdi.values[key] and rdi.values[key][val] or val -- e.g. captions have no substitutions + properties[key] = val + else + processed = processed .. (str and str ~= "" and string.strip(str)) + end end end + properties.setup = img_setup(properties) or "" data = processed processed = nil local img = "" - local images_done = rd.images_done + local images_done = rd.images.done if not anon then if not images_done[name] then img = img .. string.format([[ @@ -1210,14 +1255,14 @@ rst_context.directives.image = function(name, data) end img = img .. string.format([[ \def\RSTsubstitution%s{% - \placefigure[here]{%s}{\externalfigure[%s]} + \placefigure[here]{%s}{\externalfigure[%s]%s} } -]], name, name, name) +]], name, properties.caption, name, properties.setup) else -- image won't be referenced but used instantly img = img .. string.format([[ -\placefigure[here]{none}{\externalfigure[%s]} -]], data) +\placefigure[here]{%s}{\externalfigure[%s]%s} +]], properties.caption, data, properties.setup) end return img end -- cgit v1.2.3