Commits

Philipp Gesang committed 9895101

add inline images (non-float) via substitutions

Comments (0)

Files changed (3)

mod/tex/context/third/rst/rst_context.lua

 
 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
     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 = { }
+        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.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")
+    subtext = stringgsub(subtext, "%s", "")
     rst_context.substitutions[subtext] = { directive = directive,
                                            data      = data }
     return ""
 
 -- 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 = ""

mod/tex/context/third/rst/rst_directives.lua

     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
     --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)
     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")

mod/tex/context/third/rst/rst_setups.lua

 --------------------------------------------------------------------------------
 --
 
-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 = [[
     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)
 end
 
 function optional_setups.image ()
-  local image = [[
+    local image = [[
 
 %---------------------------------------------------------------%
 % images                                                        %
 \setupexternalfigure[location={local,global,default}]
 
 ]]
-  return image
+    return image
 end
 
 return optional_setups
 
+-- vim:ft=lua:sw=4:ts=4:expandtab:tw=80