summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-18 10:48:15 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-18 10:48:15 +0200
commit7358c6bd092cf39fee1e73420aa7f9687c8833dd (patch)
treeee3111ff4ced63e225355a8305b4540e5addc0fb
parent01d0c2dcca48fde5a99ff1173e80ad3c085cdbb5 (diff)
downloadcontext-rst-7358c6bd092cf39fee1e73420aa7f9687c8833dd.tar.gz
support for key-val extended image directives (only “width” for now).
-rw-r--r--rst_context.lua63
-rw-r--r--rst_helpers.lua7
2 files changed, 61 insertions, 9 deletions
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
diff --git a/rst_helpers.lua b/rst_helpers.lua
index b8902ca..33c1a80 100644
--- a/rst_helpers.lua
+++ b/rst_helpers.lua
@@ -90,6 +90,13 @@ do
p.whitespace = S" \t\v\r\n"^1
p.strip = p.whitespace^0 * C((1 - (p.whitespace * p.last))^1) * p.whitespace^0 * p.last
+
+
+ local colon = P":"
+ local escaped_colon = P"\\:"
+ local nocolon = (escaped_colon + (1 - colon))^1
+ p.colon_right = nocolon * colon
+ p.colon_keyval = C(nocolon) * colon * p.space^1 * C((1 - (p.space^0 * P(-1)))^1)
end
function helpers.cell.create(raw, n_row, n_col, parent, variant)