From a3c01af8bbd581000e276cca076023c308bd688c Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 4 Jun 2013 15:44:44 +0200 Subject: add inline images (non-float) via substitutions --- mod/tex/context/third/rst/rst_context.lua | 43 +++++++++------ mod/tex/context/third/rst/rst_directives.lua | 80 +++++++++++++++++++++------- mod/tex/context/third/rst/rst_setups.lua | 19 +++---- 3 files changed, 100 insertions(+), 42 deletions(-) diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua index b9b9e58..c7e21fe 100644 --- a/mod/tex/context/third/rst/rst_context.lua +++ b/mod/tex/context/third/rst/rst_context.lua @@ -345,14 +345,14 @@ end function rst_context.substitution_reference (str, underscores) local sub = "" - rst_context.addsetups("substitutions") + rst_context.addsetups "substitutions" if underscores == "_" then -- normal reference 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 .. "]{}" end - return sub .. [[{\\RSTsubstitution]] .. str:gsub("%s", "") .. "}" + return sub .. [[{\\RSTsubstitution]] .. stringgsub(str, "%s", "") .. "}" end do @@ -1268,21 +1268,34 @@ function rst_context.footnote (label, content) return "" end +--- hack to differentiate inline images +local special_substitutions = { + image = "inline_image", +} + function rst_context.substitution_definition (subtext, directive, data) - local tmp - if data.first ~= "" then - tmp = { data.first } + local special = special_substitutions[directive] + if special then + --- override; pass data directly + directive = special 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 + 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") + data = stringstrip(data) end - data = tableconcat(tmp, "\n\n") + subtext = stringgsub(subtext, "%s", "") rst_context.substitutions[subtext] = { directive = directive, data = data } return "" @@ -1290,7 +1303,7 @@ end -- not to be confused with the directive definition table rst_directives function rst_context.directive(directive, data) - local fun = rst_directives[directive] + local fun = rst_directives[directive] if fun then rst_context.addsetups("directive") local result = "" diff --git a/mod/tex/context/third/rst/rst_directives.lua b/mod/tex/context/third/rst/rst_directives.lua index b64bafc..f5572b7 100644 --- a/mod/tex/context/third/rst/rst_directives.lua +++ b/mod/tex/context/third/rst/rst_directives.lua @@ -72,10 +72,35 @@ local function img_setup (properties) return result end -rst_directives.image = function(data) +local collect_image_properties = function (data) + local image_directives = rst_directives.images + local p_keyval = helpers.patterns.colon_keyval + local properties = { } + + data = tableflattened(data) + for i=1, #data do + local str = stringstrip(data[i]) + local key, val = lpegmatch(p_keyval, str) + if key and val then + key = image_directives.keys[key] -- sanitize key expression + local valtype = type(image_directives.values[key]) + if valtype == "table" then + val = image_directives.values[key][val] + elseif valtype == "function" then + val = image_directives.values[key](val) + end + properties[key] = val + end + end + return properties +end + +--- ordinary image directives are converted to floats + +local float_image = function (data) rst_context.addsetups "image" local inline_parser = rst_context.inline_parser - local properties = {} + local properties local anon = false local rdi = rst_directives.images local hp = helpers.patterns @@ -95,22 +120,7 @@ rst_directives.image = function(data) --anon = true -- indicates a nameless picture --name = "anonymous" .. rd.anonymous - data = tableflattened(data) - - for i=1, #data do - local str = stringstrip(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 = collect_image_properties(data) if properties.caption then caption = lpegmatch(inline_parser, properties.caption) @@ -143,6 +153,40 @@ rst_directives.image = function(data) return img end +--- inline substitutions are converted to bare external figures +local inline_image = function (name, data) + rst_context.addsetups "image" + local filename = data.first + local p_keyval = helpers.patterns.colon_keyval + local properties + + if not filename then --- garbage, ignore + return "" + end + data.first = nil + filename = stringstrip(filename) + properties = collect_image_properties(data) + + local scheme = "\n\\def\\RSTsubstitution%s{\n \\externalfigure[%s]%s%%\n}\n" + local options = "" + if next(properties) then + local tmp = { } + tmp[#tmp+1] = "[" + for key, value in next, properties do + tmp[#tmp+1] = key + tmp[#tmp+1] = "={" + tmp[#tmp+1] = rst_context.escape(value) + tmp[#tmp+1] = "}," + end + tmp[#tmp+1] = "]" + options = tableconcat(tmp) + end + return stringformat(scheme, name, filename, options) +end + +rst_directives.image = float_image +rst_directives.inline_image = inline_image + rst_directives.caution = function(data) local inline_parser = rst_context.inline_parser rst_context.addsetups("dbend") diff --git a/mod/tex/context/third/rst/rst_setups.lua b/mod/tex/context/third/rst/rst_setups.lua index 21f0fe9..31f314e 100644 --- a/mod/tex/context/third/rst/rst_setups.lua +++ b/mod/tex/context/third/rst/rst_setups.lua @@ -8,13 +8,14 @@ -------------------------------------------------------------------------------- -- -local optional_setups = { } -thirddata.rst_setups = optional_setups -local rst_directives = thirddata.rst_directives -local rst_context = thirddata.rst +local optional_setups = { } +thirddata.rst_setups = optional_setups +local rst_directives = thirddata.rst_directives +local rst_context = thirddata.rst -local stringformat = string.format -local stringstrip = string.strip +local stringformat = string.format +local stringstrip = string.strip +local stringgsub = string.gsub function optional_setups.footnote_symbol () local setup = [[ @@ -126,7 +127,6 @@ function optional_setups.substitutions () local rs = rst_context.substitutions for name, content in next, rs do local id, data = content.directive, content.data - name, data = name:gsub("%s", ""), stringstrip(data) local directive = directives[id] if directive then substitutions = substitutions .. directive(name, data) @@ -361,7 +361,7 @@ function optional_setups.citator () end function optional_setups.image () - local image = [[ + local image = [[ %---------------------------------------------------------------% % images % @@ -369,8 +369,9 @@ function optional_setups.image () \setupexternalfigure[location={local,global,default}] ]] - return image + return image end return optional_setups +-- vim:ft=lua:sw=4:ts=4:expandtab:tw=80 -- cgit v1.2.3