Philipp Gesang avatar Philipp Gesang committed 83b5bb8

prototype for a “container” directive

Comments (0)

Files changed (3)

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

 local helpers        = helpers        or thirddata and thirddata.rst_helpers
 local rst_directives = rst_directives or thirddata and thirddata.rst_directives
 
-local utf      = unicode.utf8
-local utflen   = utf.len
-local utflower = utf.lower
-local utfupper = utf.upper
-local iowrite  = io.write
+local utf         = unicode.utf8
+local utflen      = utf.len
+local utflower    = utf.lower
+local utfupper    = utf.upper
+local iowrite     = io.write
+local tableconcat = table.concat
 
 local dbg_write = helpers.dbg_writef
 
 end
 
 function rst_context.joinindented (tab)
-    return table.concat (tab, "")
+    return tableconcat (tab, "")
 end
 
 local corresponding = {
     if not data then
         return ""
     elseif type(data) == "table" then
-        str = #data > 1 and  helpers.string.wrapat(inline_parser:match(table.concat(data, " ")), 65) 
+        str = #data > 1 and  helpers.string.wrapat(inline_parser:match(tableconcat(data, " ")), 65) 
                         or   inline_parser:match(data[1])
     else
         str = data
 end
 
 function rst_context.substitution_definition (subtext, directive, data)
-    data = table.concat(data, "\n")
+    data = tableconcat(data, "\n")
     local rs = rst_context.substitutions
     rs[subtext] = { directive = directive, data = data }
     return ""
 
 -- not to be confused with the directive definition table rst_directives
 function rst_context.directive(directive, ...)
-    local rd = rst_directives
-    rst_context.addsetups("directive")
-    local data = {...}
-    local result = ""
-    if rd[directive] then
-        result = rd[directive](data)
+    local d = rst_directives[directive]
+    if d then
+        rst_context.addsetups("directive")
+        local data = {...}
+        local result = ""
+        result = d(data)
+        return result
     end
-    return result
+    return ""
 end

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

 
 local stringstrip  = string.strip
 local stringformat = string.format
+local tableconcat  = table.concat
+local lpegmatch    = lpeg.match
 
 rst_directives.anonymous     = 0
 rst_directives.images        = {}
     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
+        for i=1, #data do
+            local str = data[i]
             local key, val
-            key, val = p.colon_keyval:match(str)
+            key, val = lpegmatch(p.colon_keyval, str)
             local rdi = rst_directives.images
             if key and val then
                 key = rdi.keys[key] -- sanitize key expression
 \def\RSTsubstitution%s{%%
   \placefigure[here]{%s}{\externalfigure[%s]%s}
 }
-]], name, rst_context.escape(inline_parser:match(properties.caption)), name, properties.setup)
+]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup)
     else -- image won't be referenced but used instantly
         img = img .. stringformat([[
 
 \placefigure[here]{%s}{\externalfigure[%s]%s}
-]], rst_context.escape(inline_parser:match(properties.caption)), data, properties.setup)
+]], rst_context.escape(lpegmatch(inline_parser, properties.caption)), data, properties.setup)
     end
     return img
 end
     rst_context.addsetups("caution")
     local text 
     local first = true
-    for _, line in ipairs(raw) do
-        if not helpers.patterns.spacesonly:match(line) then
+    for i=1, #raw do
+        local line = raw[i]
+        if not lpegmatch(helpers.patterns.spacesonly, line) then
             if first then
                 text =  line
                 first = false
             end
         end
     end
-    text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) 
+    text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) 
     return stringformat([[
 \startRSTcaution
 %s
     rst_context.addsetups("danger")
     local text 
     local first = true
-    for _, line in ipairs(raw) do
-        if not helpers.patterns.spacesonly:match(line) then
+    for i=1, #raw do
+        local line = raw[i]
+        if not lpegmatch(helpers.patterns.spacesonly, line) then
             if first then
                 text =  line
                 first = false
             end
         end
     end
-    text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) 
+    text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) 
     return stringformat([[
 \startRSTdanger
 %s
 -- http://docutils.sourceforge.net/docs/ref/rst/directives.html
 rst_directives.DANGER = function(addendum)
     local result = ""
-    for _,str in ipairs(addendum) do
+    for i=1, #addendum do
+        local str = addendum[i]
         result = result .. (stringstrip(str))
     end
     return stringformat([[
     data = data or name
     local formula
     if type(data) == "table" then
-        local last, i = table.maxn(data), 1
+        local last, i = #data, 1
         while i <= last do
             local line = stringstrip(data[i])
             if line and line ~= "" then
 ]], name, data)
 end
 
+--------------------------------------------------------------------------------
+--- Containers.
+--------------------------------------------------------------------------------
+
+rst_directives.container = function(data)
+    local inline_parser = rst_context.inline_parser
+    local name, content = stringstrip(data[1]), ""
+    local tmp = { }
+    for i=2, #data do
+        tmp[#tmp+1] = data[i]
+    end
+    content = tableconcat(tmp, " ")
+    content = lpegmatch(inline_parser, content)
+    return stringformat([[
+\ifcsname %s\endcsname%%
+  \csname %s\endcsname%%
+\else
+  \relax
+\fi%%
+{%s}%%
+]], name, name, content)
+end
+
+-- vim:ft=lua:sw=4:ts=4:expandtab
+

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

 ]]
     local rs = rst_context.substitutions
     for name, content in next, rs do
-        local directive, data = content.directive, content.data
+        local id, data = content.directive, content.data
         name, data = name:gsub("%s", ""), stringstrip(data)
-        if directives[directive] then
-            substitutions = substitutions .. directives[directive](name, data)
+        local directive = directives[id]
+        if directive then
+            substitutions = substitutions .. directive(name, data)
         else
-            err(directive .. " does not exist.")
+            err(id .. " does not exist.")
         end
     end
     return substitutions
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.