From 8d40fbb4fb8d57890cbf02013aa2344b9894c9f2 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <gesang@stud.uni-heidelberg.de>
Date: Wed, 27 Mar 2013 00:24:28 +0100
Subject: cleanup; rewrite directives; update manual

---
 mod/tex/context/third/rst/rst_context.lua    | 152 +++++++++++--------
 mod/tex/context/third/rst/rst_directives.lua | 212 +++++++++++++--------------
 mod/tex/context/third/rst/rst_helpers.lua    |   2 +-
 mod/tex/context/third/rst/rst_parser.lua     |  71 +++++----
 mod/tex/context/third/rst/rst_setups.lua     |  74 +++++-----
 5 files changed, 269 insertions(+), 242 deletions(-)

(limited to 'mod/tex')

diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua
index f880f35..7d0f2b8 100644
--- a/mod/tex/context/third/rst/rst_context.lua
+++ b/mod/tex/context/third/rst/rst_context.lua
@@ -4,7 +4,7 @@
 --        USAGE:  called by rst_parser.lua
 --  DESCRIPTION:  Complement to the reStructuredText parser
 --       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
---      CHANGED:  2012-06-05 22:18:11+0200
+--      CHANGED:  2013-03-26 22:46:17+0100
 --------------------------------------------------------------------------------
 --
 --- TODO
@@ -25,10 +25,14 @@ local utfupper    = utf.upper
 local iowrite     = io.write
 local tableconcat = table.concat
 
+local stringmatch  = string.match
+local stringgmatch = string.gmatch
+local stringgsub   = string.gsub
+
 local dbg_write = helpers.dbg_writef
 
 local C,  Cb, Cc, Cg, Cmt, Cp,
-      Cs, Ct, P,  R,  S,   V,  match
+      Cs, Ct, P,  R,  S,   V,  lpegmatch
       = lpeg.C,  lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp,
         lpeg.Cs, lpeg.Ct, lpeg.P,  lpeg.R,  lpeg.S,   lpeg.V,  lpeg.match
 
@@ -42,7 +46,7 @@ do
         escaped  = P"\\" * V"space"
     }
     function string.strip(str)
-        return stripper:match(str) or ""
+        return lpegmatch(stripper, str) or ""
     end
 end
 local stringstrip  = string.strip
@@ -80,7 +84,7 @@ end
 
 function rst_context.footnote_reference (label)
     local tf = thirddata.rst.state.footnotes
-    if label:match("^%d+$") then -- all digits
+    if stringmatch(label, "^%d+$") then -- all digits
         local c = tonumber(label)
         return [[\\footnote{\\getbuffer[__footnote_number_]].. c .."]}"
     elseif label == "#" then --autonumber
@@ -88,8 +92,8 @@ function rst_context.footnote_reference (label)
         rc = rc + 1
         rst_context.current_footnote_number = rc
         return [[\\footnote{\\getbuffer[__footnote_number_]].. rc .."]}"
-    elseif label:match("^#.+$") then
-        local thelabel = label:match("^#(.+)$")
+    elseif stringmatch(label, "^#.+$") then
+        local thelabel = stringmatch(label, "^#(.+)$")
         return [[\\footnote{\\getbuffer[__footnote_label_]].. thelabel .."]}"
     elseif label == "*" then
         local rc = rst_context.current_symbolnote_number
@@ -106,7 +110,7 @@ do
     local w = S" \v\t\n" / "_"
     local wp = Cs((w + 1)^1)
     function rst_context.whitespace_to_underscore(str)
-        return  str and wp:match(str) or ""
+        return  str and lpegmatch(wp, str) or ""
     end
 end
 
@@ -165,9 +169,9 @@ end
 
 rst_context.roles.color = function(color, str)
     local p = helpers.patterns
-    local definition = color:match("^color_(.+)$")
-    if definition:match("^rgb_") then -- assume rgb
-        local rgb = p.rgbvalues:match(definition)
+    local definition = stringmatch(color, "^color_(.+)$")
+    if stringmatch(definition, "^rgb_") then -- assume rgb
+        local rgb = lpegmatch(p.rgbvalues, definition)
         definition = stringformat([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3])
     end
     return stringformat([[\\colored[%s]{%s}]], definition, str)
@@ -256,14 +260,14 @@ end
 function rst_context.interpreted_text (...)
     local tab = { ... }
     local role, str
-    role = tab[1]:match("^:(.*):$") or tab[3]:match("^:(.*):$")
+    role = stringmatch(tab[1], "^:(.*):$") or stringmatch(tab[3], "^:(.*):$")
     str  = tab[2]
 
     if not role then -- implicit role
         role = "emphasis"
     end
 
-    if role:match("^color_") then
+    if stringmatch(role, "^color_") then
         return rst_context.roles.color(role, str)
     end
 
@@ -277,14 +281,14 @@ end
 
 function rst_context.reference (str)
     rst_context.addsetups("references")
-    str = str:match("^`?([^`]+)`?_$") -- LPEG could render this gibberish legible but not time
+    str = stringmatch(str, "^`?([^`]+)`?_$")
     return [[\\RSTchoosegoto{__target_]] .. rst_context.whitespace_to_underscore(str) .. "}{"
             .. str .. "}"
 end
 
 function rst_context.anon_reference (str)
     rst_context.addsetups("references")
-    str = str:match("^`?([^`]+)`?__$")
+    str = stringmatch(str, "^`?([^`]+)`?__$")
     rst_context.anonymous_links[#rst_context.anonymous_links+1] = str
     link = "__target_anon_" .. #rst_context.anonymous_links
     return stringformat([[\\RSTchoosegoto{%s}{%s}]], link, str)
@@ -320,7 +324,11 @@ function rst_context.target (tab)
                 local anon = create_anonymous()
                 id, arefs[anon[1]] = anon[1], anon[2]
             else
-                id = tab[i]:gsub("\\:",":"):match("`?([^`]+)`?") -- deescaping
+                local tmp = tab[i]
+                tmp = stringgsub(tmp, "\\:",":")
+                tmp = stringmatch(tmp, "`?([^`]+)`?")
+                id = tmp
+                --id = tab[i]:gsub("\\:",":"):match("`?([^`]+)`?") -- deescaping
             end
             if id then
                 refs[id] = refs[id] or target
@@ -376,8 +384,8 @@ do
     }
 
     function rst_context.escape (str)
-        str = str:gsub("\\(.)", "%1")
-        return p_escape:match(str)
+        str = stringgsub(str, "\\(.)", "%1")
+        return lpegmatch(p_escape, str)
     end
 end
 
@@ -641,8 +649,15 @@ function rst_context.paragraph (data)
     if not data then
         return ""
     elseif type(data) == "table" then
-        str = #data > 1 and  helpers.string.wrapat(inline_parser:match(tableconcat(data, " ")), 65) 
-                        or   inline_parser:match(data[1])
+--        str = #data > 1 and  helpers.string.wrapat(lpegmatch(inline_parser, tableconcat(data, " ")), 65) 
+--                        or   inline_parser:match(data[1])
+        if #data > 1 then
+            str = helpers.string.wrapat(
+                lpegmatch(inline_parser, tableconcat(data, " "))
+                , 65)
+        else
+            str = lpegmatch(inline_parser, data[1])
+        end
     else
         str = data
     end
@@ -674,12 +689,10 @@ function rst_context.section (...)  -- TODO general cleanup; move validity
     if #tab == 3 then -- TODO use unicode length with ConTeXt
         adornchar = tab[1]:sub(1,1)
         section = ulen(tab[1]) >= ulen(tab[2])
-        --section = get_line_pattern(adornchar):match(tab[1]) ~= nil and section
         str = stringstrip(tab[2])
     else -- no overline
         adornchar = tab[2]:sub(1,1)
         section = ulen(tab[1]) <= ulen(tab[2])
-        --section = get_line_pattern(adornchar):match(tab[2]) ~= nil and section
         str = tab[1]
     end
 
@@ -725,7 +738,7 @@ do
         nospace  = V"escaped" + (1 - V"space"),
     }
     function stringstrip(str)
-        return stripper:match(str) or ""
+        return lpegmatch(stripper, str) or ""
     end 
 end
 
@@ -753,26 +766,25 @@ local itemstripper = stripme^0 * C(dontstrip^1) * stripme^0
 local function parse_itemstring(str)
     local offset = nil
     local setup = ",fit][itemalign=flushright,"
-    -- string.match is slightly faster than string.find
-    if str:match("^%(") then
+    if stringmatch(str, "^%(") then
         setup = setup .. [[left=(,]]
     end
-    if str:match("%)$") then
+    if stringmatch(str, "%)$") then
         setup = setup .. [[right=)]]
     end
-    if str:match("%.$") then
+    if stringmatch(str, "%.$") then
         setup = setup .. [[stopper={.\\space}]]
     end
-    local num = str:match("^%d")
+    local num = stringmatch(str, "^%d")
     if num then
         -- http://thread.gmane.org/gmane.comp.tex.context/61728/focus=61729
         setup = setup .. ",start=" .. num
         str = "n"
     end
 
-    str = itemstripper:match(str)
+    str = lpegmatch(itemstripper, str)
     str = enumeration_types[str] or str
-    return {setup = setup, str = str}
+    return { setup = setup, str = str }
 end
 
 function rst_context.startitemize(str)
@@ -828,7 +840,7 @@ function rst_context.bullet_item (tab)
 
     return result .. [[
 
-\\item ]] .. inline_parser:match(content) .. [[
+\\item ]] .. lpegmatch(inline_parser, content) .. [[
 
 ]]
 end
@@ -868,7 +880,7 @@ function rst_context.deflist (list)
             local par = final[np]
             tmp = tmp .. [[
     \\RSTdeflistparagraph{%
-]] .. inline_parser:match(par) .. "}\n"
+]] .. lpegmatch(inline_parser, par) .. "}\n"
         end
         tmp = tmp .. "  }"
         deflist = deflist .. tmp
@@ -900,7 +912,7 @@ function rst_context.field_name (str)
 end
 
 function rst_context.field_body (str)
-    return [[\\fieldbody{]] .. inline_parser:match(str) .. [[}]]
+    return [[\\fieldbody{]] .. lpegmatch(inline_parser, str) .. [[}]]
 end
 
 function rst_context.field (tab)
@@ -909,7 +921,7 @@ function rst_context.field (tab)
 
     \\RSTfieldname{%s}
     \\RSTfieldbody{%s}
-]], name, inline_parser:match(body))
+]], name, lpegmatch(inline_parser, body))
 end
 
 function rst_context.line_comment (str)
@@ -937,7 +949,7 @@ function rst_context.option_list (str)
 \\eTR
 \\eTABLEhead
 \\bTABLEbody
-]] .. inline_parser:match(str) .. [[
+]] .. lpegmatch(inline_parser, str) .. [[
 
 \\eTABLEbody
 \\eTABLE
@@ -955,11 +967,10 @@ end
 
 function rst_context.literal_block (str, included)
     local indent = P" "^1
-    --local stripme = indent:match(str) or 0
     local stripme = #str
-    for line in str:gmatch("[^\n]+") do
+    for line in stringgmatch(str, "[^\n]+") do
         -- setting to the lowest indend of all lines
-        local idt = indent:match(line)
+        local idt = lpegmatch(indent, line)
         if line and idt then
             stripme = idt < stripme and idt or stripme
         end
@@ -976,7 +987,7 @@ function rst_context.literal_block (str, included)
         end,
     }
 
-    str = strip:match(str)
+    str = lpegmatch(strip, str)
     str = [[
 
 \starttyping[lines=hyphenated]
@@ -999,7 +1010,7 @@ function rst_context.line_block (str)
     return [[
 
 \\startlines
-]] .. inline_parser:match(str) .. [[\\stoplines
+]] .. lpegmatch(inline_parser, str) .. [[\\stoplines
 ]]
 end
 
@@ -1018,7 +1029,7 @@ function rst_context.block_quote (tab)
 \\startlinecorrection
 \\blank[small]
 \\startblockquote
-]] .. inline_parser:match(tab[1]) .. [[
+]] .. lpegmatch(inline_parser, tab[1]) .. [[
 
 \\stopblockquote
 ]]
@@ -1026,7 +1037,7 @@ function rst_context.block_quote (tab)
     return tab[2] and str .. [[
 \\blank[small]
 \\startattribution
-]] .. inline_parser:match(tab[2]) .. [[
+]] .. lpegmatch(inline_parser, tab[2]) .. [[
 \\stopattribution
 \\blank[small]
 \\stoplinecorrection
@@ -1074,7 +1085,7 @@ function rst_context.grid_table (tab)
                     local c = r[n]
                     if not (c.parent or
                             c.variant == "separator") then
-                        local celltext = inline_parser:match(c.stripped)
+                        local celltext = lpegmatch(inline_parser, c.stripped)
                         if c.span.x or c.span.y then
                             local span_exp = "["
                             if c.span.x then
@@ -1124,7 +1135,7 @@ function rst_context.grid_table (tab)
                 local c = r[n]
                 if not (c.parent or
                         c.variant == "separator") then
-                    local celltext = inline_parser:match(c.stripped)
+                    local celltext = lpegmatch(inline_parser, c.stripped)
                     if c.span.x or c.span.y then
                         local span_exp = "["
                         if c.span.x then
@@ -1172,7 +1183,7 @@ function rst_context.simple_table(tab)
                 for nc=1, #row do
                     local cell = row[nc]
                     dbg_write("%7s | ", cell.content)
-                    local celltext = inline_parser:match(cell.content)
+                    local celltext = lpegmatch(inline_parser, cell.content)
                     if cell.span then
                         head = head .. stringformat([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext or "")
                     else
@@ -1212,7 +1223,7 @@ function rst_context.simple_table(tab)
             for nc=1, #row do
                 local cell = row[nc]
                 dbg_write("%7s | ", cell.content)
-                local celltext = inline_parser:match(cell.content)
+                local celltext = lpegmatch(inline_parser, cell.content)
                 if cell.span then
                     body = body .. stringformat([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext or "")
                 else
@@ -1230,44 +1241,63 @@ end
 function rst_context.footnote (label, content)
     local tf = thirddata.rst.state.footnotes
     rst_context.addsetups("footnotes")
-    if label:match("^%d+$") then -- all digits
-        tf.numbered[tonumber(label)] = rst_context.escape(inline_parser:match(content))
+    if stringmatch(label, "^%d+$") then -- all digits
+        tf.numbered[tonumber(label)] =
+            rst_context.escape(lpegmatch(inline_parser, content))
     elseif label == "#" then --autonumber
         repeat -- until next unrequested number
             tf.autonumber = tf.autonumber + 1
         until tf.numbered[tf.autonumber] == nil
-        tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content))
-    elseif label:match("^#.+$") then
-        local thelabel = label:match("^#(.+)$")
-        tf.autolabel[thelabel] = rst_context.escape(inline_parser:match(content))
+        tf.numbered[tf.autonumber] =
+            rst_context.escape(lpegmatch(inline_parser, content))
+    elseif stringmatch(label, "^#.+$") then
+        local thelabel = stringmatch(label, "^#(.+)$")
+        tf.autolabel[thelabel] =
+            rst_context.escape(lpegmatch(inline_parser, content))
     elseif label == "*" then
         rst_context.addsetups("footnote_symbol")
-        tf.symbol[#tf.symbol+1] = rst_context.escape(inline_parser:match(content))
+        tf.symbol[#tf.symbol+1] =
+            rst_context.escape(lpegmatch(inline_parser, content))
     else -- “citation reference” treated like ordinary footnote
         repeat -- until next unrequested number
             tf.autonumber = tf.autonumber + 1
         until tf.numbered[tf.autonumber] == nil
-        tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content))
+        tf.numbered[tf.autonumber] =
+            rst_context.escape(lpegmatch(inline_parser, content))
     end
     return ""
 end
 
 function rst_context.substitution_definition (subtext, directive, data)
-    data = tableconcat(data, "\n")
-    local rs = rst_context.substitutions
-    rs[subtext] = { directive = directive, data = data }
+    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")
+    rst_context.substitutions[subtext] = { directive = directive,
+                                           data      = data }
     return ""
 end
 
 -- not to be confused with the directive definition table rst_directives
-function rst_context.directive(directive, ...)
-    local d = rst_directives[directive]
-    if d then
+function rst_context.directive(directive, data)
+    local fun  = rst_directives[directive]
+    if fun then
         rst_context.addsetups("directive")
-        local data = {...}
         local result = ""
-        result = d(data)
+        result = fun(data)
         return result
     end
     return ""
 end
+
+-- vim:ft=lua:sw=4:ts=4:expandtab
diff --git a/mod/tex/context/third/rst/rst_directives.lua b/mod/tex/context/third/rst/rst_directives.lua
index f143c56..374f942 100644
--- a/mod/tex/context/third/rst/rst_directives.lua
+++ b/mod/tex/context/third/rst/rst_directives.lua
@@ -4,7 +4,7 @@
 --        USAGE:  called by rst_parser.lua
 --  DESCRIPTION:  Complement to the reStructuredText parser
 --       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
---      CHANGED:  2012-06-05 22:17:51+0200
+--      CHANGED:  2013-03-26 22:45:45+0100
 --------------------------------------------------------------------------------
 --
 
@@ -18,12 +18,14 @@ local rst_directives     = { }
 thirddata.rst_directives = rst_directives
 local rst_context        = thirddata.rst
 
-local stringstrip  = string.strip
-local stringformat = string.format
-local tableconcat  = table.concat
-local lpegmatch    = lpeg.match
+local lpegmatch      = lpeg.match
+local stringformat   = string.format
+local stringstrip    = string.strip
+local tableconcat    = table.concat
+local tableflattened = table.flattened
+local type           = type
 
-rst_directives.anonymous     = 0
+--rst_directives.anonymous     = 0
 rst_directives.images        = {}
 rst_directives.images.done   = {}
 rst_directives.images.values = {}
@@ -52,13 +54,14 @@ rst_directives.images.values.width = {
 }
 
 -- we won't allow passing arbitrary setups to context
-rst_directives.images.permitted_setups = {
-    "width", "scale"
+local permitted_setups = {
+    "width",
+    "scale"
 }
 
 local function img_setup (properties)
     local result = ""
-    for _, prop in next, rst_directives.images.permitted_setups do
+    for _, prop in next, permitted_setups do
         if properties[prop] then
             result = result .. prop .. "=" .. properties[prop] .. ","
         end
@@ -69,124 +72,108 @@ local function img_setup (properties)
     return result
 end
 
-rst_directives.image = function(name, data)
+rst_directives.image = function(data)
     local inline_parser = rst_context.inline_parser
-    local properties = {}
-    local anon = false
-    local rd = rst_directives
-    if not data then -- this makes the “name” argument optional
-        data = name
-        rd.anonymous = rd.anonymous + 1
-        anon = true -- indicates a nameless picture
-        name = "anonymous" .. rd.anonymous
-    end
+    local properties    = {}
+    local anon          = false
+    local rdi           = rst_directives.images
+    local hp            = helpers.patterns
+
+    local name = stringstrip(data.name)
+
+    --rd.anonymous = rd.anonymous + 1
+    --anon = true -- indicates a nameless picture
+    --name = "anonymous" .. rd.anonymous
+
     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 i=1, #data do
-            local str = data[i]
-            local key, val
-            key, val = lpegmatch(p.colon_keyval, str)
-            local rdi = rst_directives.images
-            if key and val then
-                key = rdi.keys[key] -- sanitize key expression
-                if     type(rdi.values[key]) == "table" then
-                    val = rdi.values[key][val]
-                elseif type(rdi.values[key]) == "function" then
-                    val = rdi.values[key](val)
-                end
-                properties[key] = val
-            else
-                processed = processed .. (str and str ~= "" and stringstrip(str))
+    data               = tableflattened(data)
+
+    for i=1, #data do
+        local str = 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.setup = img_setup(properties) or ""
-    data = processed
-    processed = nil
     local img = ""
-    local images_done = rd.images.done
-    if not anon then
-        if not images_done[name] then
-            img = img .. stringformat([[
-
-\useexternalfigure[%s][%s][]
-]], name, data)
-        images_done[name] = true
-        end
-        img = img .. stringformat([[
-\def\RSTsubstitution%s{%%
-  \placefigure[here]{%s}{\externalfigure[%s]%s}
-}
-]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup)
-    else -- image won't be referenced but used instantly
-        img = img .. stringformat([[
+--    local images_done = rdi.done
+--    if not anon then -- TODO: implement?
+--        if not images_done[name] then
+--            img = img .. stringformat([[
+--
+--\useexternalfigure[%s][%s][]%%
+--]], name, data)
+--        images_done[name] = true
+--        end
+--        img = img .. stringformat([[
+--\def\RSTsubstitution%s{%%
+--  \placefigure[here]{%s}{\externalfigure[%s]%s}%%
+--}
+--]], name, rst_context.escape(lpegmatch(inline_parser, properties.caption)), name, properties.setup)
+--    else -- image won't be referenced but used instantly
+    img = stringformat([[
 
 \placefigure[here]{%s}{\externalfigure[%s]%s}
-]], rst_context.escape(lpegmatch(inline_parser, properties.caption)), data, properties.setup)
-    end
+]],     rst_context.escape(lpegmatch(inline_parser, properties.caption)),
+        name,
+        properties.setup)
+--    end
     return img
 end
 
-rst_directives.caution = function(raw)
+rst_directives.caution = function(data)
     local inline_parser = rst_context.inline_parser
     rst_context.addsetups("dbend")
     rst_context.addsetups("caution")
-    local text 
-    local first = true
-    for i=1, #raw do
-        local line = raw[i]
-        if not lpegmatch(helpers.patterns.spacesonly, line) then
-            if first then
-                text =  line
-                first = false
-            else
-                text = text .. " " .. line
-            end
-        end
+    local text = { }
+    for i=1, #data do -- paragraphs
+        local current = tableconcat(data[i], "\n")
+        current = lpegmatch(inline_parser, current)
+        current = rst_context.escape(current)
+        text[i] = current
     end
-    text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) 
     return stringformat([[
 \startRSTcaution
 %s
 \stopRSTcaution
-]], text)
+]], tableconcat(text, "\n\n"))
 end
 
-rst_directives.danger = function(raw)
+rst_directives.danger = function(data)
     local inline_parser = rst_context.inline_parser
     rst_context.addsetups("dbend")
     rst_context.addsetups("danger")
-    local text 
-    local first = true
-    for i=1, #raw do
-        local line = raw[i]
-        if not lpegmatch(helpers.patterns.spacesonly, line) then
-            if first then
-                text =  line
-                first = false
-            else
-                text = text .. " " .. line
-            end
-        end
+    local text = { }
+    for i=1, #data do -- paragraphs
+        local current = tableconcat(data[i], "\n")
+        current = lpegmatch(inline_parser, current)
+        current = rst_context.escape(current)
+        text[i] = current
     end
-    text = rst_context.escape(helpers.string.wrapat(lpegmatch(inline_parser, text))) 
     return stringformat([[
 \startRSTdanger
 %s
 \stopRSTdanger
-]], text)
+]], tableconcat(text, "\n\n"))
 end
 
 -- http://docutils.sourceforge.net/docs/ref/rst/directives.html
-rst_directives.DANGER = function(addendum)
-    local result = ""
-    for i=1, #addendum do
-        local str = addendum[i]
-        result = result .. (stringstrip(str))
+rst_directives.DANGER = function(data)
+    local inline_parser = rst_context.inline_parser
+    local text = { }
+    for i=1, #data do -- paragraphs
+        local current = tableconcat(data[i], "\n")
+        current = lpegmatch(inline_parser, current)
+        current = rst_context.escape(current)
+        text[i] = current
     end
     return stringformat([[
 
@@ -215,7 +202,7 @@ rst_directives.DANGER = function(addendum)
 }
 \blank[force,big]
 \stoplinecorrection
-]], result)
+]], tableconcat(text, "\n\n"))
 end
 
 rst_directives.mp = function(name, data)
@@ -301,24 +288,35 @@ end
 --- Containers.
 --------------------------------------------------------------------------------
 
+--- *data*:
+---     { [1]  -> directive name,
+---       [>1] -> paragraphs }
+
 rst_directives.container = function(data)
     local inline_parser = rst_context.inline_parser
-    local name, content = stringstrip(data[1]), ""
-    if name == "" then
-        name = "framed"
-    end
     local tmp = { }
-    for i=2, #data do
-        tmp[#tmp+1] = data[i]
+    for i=1, #data do -- paragraphs
+        local current = tableconcat(data[i], "\n")
+        current = lpegmatch(inline_parser, current)
+        current = rst_context.escape(current)
+        tmp[i] = current
     end
-    -- content |> concat |> match |> escape
-    content = tableconcat(tmp, " ")
-    content = lpegmatch(inline_parser, content)
-    content = rst_context.escape(content)
-    return stringformat([[
+    local content = tableconcat(tmp, "\n\n")
+    local name = data.name
+    if name and name ~= "" then
+        name = stringstrip(data.name)
+        return stringformat([[
 \start[%s]%%
-%s\stop
+%s%%
+\stop
 ]], name, content)
+    else
+        return stringformat([[
+\begingroup%%
+%s%%
+\endgroup
+]], content)
+    end
 end
 
 -- vim:ft=lua:sw=4:ts=4:expandtab
diff --git a/mod/tex/context/third/rst/rst_helpers.lua b/mod/tex/context/third/rst/rst_helpers.lua
index 936da97..97d4dd7 100644
--- a/mod/tex/context/third/rst/rst_helpers.lua
+++ b/mod/tex/context/third/rst/rst_helpers.lua
@@ -4,7 +4,7 @@
 --        USAGE:  called by rst_parser.lua
 --  DESCRIPTION:  Complement to the reStructuredText parser
 --       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
---      CHANGED:  2012-06-05 22:17:31+0200
+--      CHANGED:  2013-03-26 23:55:04+0100
 --------------------------------------------------------------------------------
 --
 
diff --git a/mod/tex/context/third/rst/rst_parser.lua b/mod/tex/context/third/rst/rst_parser.lua
index 6e7c225..a397f55 100644
--- a/mod/tex/context/third/rst/rst_parser.lua
+++ b/mod/tex/context/third/rst/rst_parser.lua
@@ -4,8 +4,8 @@
 --        USAGE:  refer to doc/documentation.rst
 --  DESCRIPTION:  https://bitbucket.org/phg/context-rst/overview
 --       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
---      VERSION:  0.5
---      CHANGED:  2012-06-05 22:17:18+0200
+--      VERSION:  0.6
+--      CHANGED:  2013-03-26 22:45:59+0100
 --------------------------------------------------------------------------------
 --
 
@@ -30,6 +30,7 @@ rst.crlf          = true
 helpers.rst_debug = false
 
 local iowrite      = io.write
+local ioopen       = io.open
 local stringformat = string.format
 local stringlen    = string.len
 local stringstrip  = string.strip
@@ -184,19 +185,19 @@ local parser = P{
 
     directive = V"explicit_markup_start"
               * C(((V"escaped_colon" + (1 - V"colon" - V"eol"))
-                 - V"substitution_text")^1)
+                 - V"substitution_text")^1) --> directive name
               * V"double_colon"
-              * (V"directive_block_multi" + V"directive_block_single")
+              * Ct(V"directive_block_multi" + V"directive_block_single") --> content
               / rst.directive
               ,
 
-    directive_block_multi = C((1 - V"eol")^0) -- name
+    directive_block_multi = Cg((1 - V"eol")^0, "name") -- name
                           * V"eol"
-                          * V"blank_line"^-1 -- how many empty lines are permitted?
+                          * V"blank_line"^0 -- how many empty lines are permitted?
                           * V"directive_indented_lines"
                           ,
 
-    directive_block_single = C((1 - V"eol")^1) * V"eol",
+    directive_block_single = Ct(C((1 - V"eol")^1)) * V"eol",
 
 --------------------------------------------------------------------------------
 -- Substitution definition block
@@ -217,18 +218,18 @@ local parser = P{
                       * V"bar"
                       ,
 
-    data_directive_block = V"data_directive_block_long"
-                         + V"data_directive_block_short"
+    data_directive_block = V"data_directive_block_multi"
+                         + V"data_directive_block_single"
                          ,
-    data_directive_block_short = C((1 - V"eol")^0) * V"eol",
+    data_directive_block_single = Ct(C((1 - V"eol")^0)) * V"eol",
 
-    data_directive_block_long  = C((1 - V"eol")^0) * V"eol"
-                               * V"directive_indented_lines"
-                               ,
+    data_directive_block_multi  = Cg((1 - V"eol")^0, "first") * V"eol"
+                                * V"directive_indented_lines"
+                                ,
 
-    directive_indented_lines = V"directive_indented_first"
-                             * V"directive_indented_other"^0
-                             * (V"blank_line"^1 * V"directive_indented_other"^1)^1
+    directive_indented_lines = Ct(V"directive_indented_first"
+                                * V"directive_indented_other"^0)
+                             * (V"blank_line"^1 * Ct(V"directive_indented_other"^1))^0
                              ,
 
 
@@ -241,7 +242,11 @@ local parser = P{
                              ,
 
     directive_indented_other = Cmt(V"space"^1, function(s,i,indent)
-                                    warn("sub-m", #state.currentindent <= #indent, #indent, #state.currentindent, i)
+                                    warn("sub-m",
+                                      #state.currentindent <= #indent,
+                                      #indent,
+                                      #state.currentindent,
+                                      i)
                                     return #state.currentindent <= #indent
                                 end)
                              * C((1 - V"eol")^1) * V"eol"
@@ -1384,7 +1389,7 @@ function file_helpers.crlf (raw)
 end
 
 local function load_file (name)
-    f = assert(io.open(name, "r"), "Not a file!")
+    f = assert(ioopen(name, "r"), "Not a file!")
     if not f then return 1 end
     local tmp = f:read("*all")
     f:close()
@@ -1403,7 +1408,7 @@ local function load_file (name)
 end
 
 local function save_file (name, data)
-    f = assert(io.open(name, "w"), "Could not open file "..name.." for writing! Check its permissions")
+    f = assert(ioopen(name, "w"), "Could not open file "..name.." for writing! Check its permissions")
     if not f then return 1 end
     f:write(data)
     f:close()
@@ -1411,17 +1416,15 @@ local function save_file (name, data)
 end
 
 local function get_setups (inline)
-    local optional_setups = optional_setups -- might expect lots of calls
+    local optional_setups = optional_setups
     local setups = ""
     if not inline then
         setups = setups .. [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%{                           Setups                            }%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%+-------------------------------------------------------------+%
+%|                           Setups                            |%
+%+-------------------------------------------------------------+%
 % General                                                       %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 ]]
     end
@@ -1450,11 +1453,9 @@ local function get_setups (inline)
         setups = setups .. [[
 
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%{                           Main                              }%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%+-------------------------------------------------------------+%
+%|                            Main                             |%
+%+-------------------------------------------------------------+%
 
 \starttext
 ]]
@@ -1473,11 +1474,9 @@ function thirddata.rst.standalone (infile, outfile)
 
 \stoptext
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%{                        End of Document                      }%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%+-------------------------------------------------------------+%
+%|                       End of Document                       |%
+%+-------------------------------------------------------------+%
 
 % vim:ft=context:tw=65:shiftwidth=2:tabstop=2:set expandtab
 ]]
diff --git a/mod/tex/context/third/rst/rst_setups.lua b/mod/tex/context/third/rst/rst_setups.lua
index 64e9171..de70d4b 100644
--- a/mod/tex/context/third/rst/rst_setups.lua
+++ b/mod/tex/context/third/rst/rst_setups.lua
@@ -4,7 +4,7 @@
 --        USAGE:  called by rst_parser.lua
 --  DESCRIPTION:  Complement to the reStructuredText parser
 --       AUTHOR:  Philipp Gesang (Phg), <phg42.2a@gmail.com>
---      CHANGED:  2012-06-05 22:17:10+0200
+--      CHANGED:  2013-03-26 23:55:20+0100
 --------------------------------------------------------------------------------
 --
 
@@ -14,14 +14,14 @@ local rst_directives  = thirddata.rst_directives
 local rst_context     = thirddata.rst
 local state           = rst_context.state
 
-local fmt         = string.format
-local stringstrip = string.strip
+local stringformat = string.format
+local stringstrip  = string.strip
 
 function optional_setups.footnote_symbol ()
     local setup = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Footnotes with symbol conversion                              %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \definenote[symbolnote][footnote]
 \setupnote [symbolnote][way=bypage,numberconversion=set 2]
 ]]
@@ -32,9 +32,9 @@ function optional_setups.footnotes ()
     local tf = state.footnotes
     local fn = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Footnotes                                                     %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 ]]
     local buffer = [[
 
@@ -44,13 +44,13 @@ function optional_setups.footnotes ()
 ]]
     
     for nf, note in next, tf.numbered do
-        fn = fn .. fmt(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note)
+        fn = fn .. stringformat(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note)
     end
     for nf, note in next, tf.autolabel do
-        fn = fn .. fmt(buffer, "Labeled footnote", "__footnote_label_"..nf, note)
+        fn = fn .. stringformat(buffer, "Labeled footnote", "__footnote_label_"..nf, note)
     end
     for nf, note in next, tf.symbol do
-        fn = fn .. fmt(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note)
+        fn = fn .. stringformat(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note)
     end
     return fn
 end
@@ -82,9 +82,9 @@ function optional_setups.references ()
 
     local refsection = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % References                                                    %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 ]]
     local references = {}
@@ -97,7 +97,7 @@ function optional_setups.references ()
             if arefs[ref_text] then
                 ref_text = rst_context.anonymous_links[tonumber(arefs[ref_text])]
             end
-            references[#references+1] = fmt([[
+            references[#references+1] = stringformat([[
 \useURL[__target_%s] [%s] []   [%s] ]], rst_context.whitespace_to_underscore(ref), urlescape(target), ref_text)
         end
     end
@@ -120,9 +120,9 @@ function optional_setups.substitutions ()
     local directives = rst_directives
     local substitutions = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Substitutions                                                 %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 ]]
     local rs = rst_context.substitutions
     for name, content in next, rs do
@@ -141,9 +141,9 @@ end
 function optional_setups.directive ()
     --local dirstr = [[
 
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+--%---------------------------------------------------------------%
 --% Directives                                                    %
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+--%---------------------------------------------------------------%
 --]]
     --return dirstr
     return ""
@@ -152,9 +152,9 @@ end
 function optional_setups.blockquote ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Blockquotes                                                   %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \setupdelimitedtext  [blockquote][style={\tfx}] % awful placeholder
 \definedelimitedtext[attribution][blockquote]
 \setupdelimitedtext [attribution][style={\tfx\it}]
@@ -164,9 +164,9 @@ end
 function optional_setups.deflist ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Definitionlist                                                %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \def\startRSTdefinitionlist{
   \bgroup
   \def      \RSTdeflistterm##1{{\bf ##1}}
@@ -189,9 +189,9 @@ end
 function optional_setups.lines ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Lines environment (line blocks)                               %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \setuplines[%
   space=on,%
@@ -204,9 +204,9 @@ end
 function optional_setups.breaks ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Fancy transitions                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 % Get Wolfgang’s module at <https://bitbucket.org/wolfs/fancybreak>.
 \usemodule[fancybreak]
@@ -217,9 +217,9 @@ end
 function optional_setups.fieldlist ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Fieldlists                                                    %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \def\startRSTfieldlist{%
   \bgroup%
@@ -252,9 +252,9 @@ function optional_setups.dbend ()
     -- There's just no reason for not providing this.
     optional_setups.dbend_done = true
     return [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Dangerous bend                                                %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \loadmapfile [manfnt.map]
 \definefontsynonym [bends] [manfnt]
@@ -278,9 +278,9 @@ function optional_setups.caution ()
         --result = result .. optional_setups.dbend()
     --end
     return result .. [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Caution directive                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \usemodule[lettrine]
 
@@ -310,9 +310,9 @@ function optional_setups.danger ()
         --result = result .. optional_setups.dbend()
     --end
     return result .. [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Danger directive                                              %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \usemodule[lettrine]
 
@@ -329,9 +329,9 @@ end
 
 function optional_setups.citations ()
     local cit = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Citations                                                     %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \setupbibtex[database=\jobname]
 ]]
     
@@ -341,9 +341,9 @@ end
 
 function optional_setups.citator ()
     local cit = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Citator Options                                               %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \usemodule[citator]
 \loadbibdb{\jobname.bib}
 \setupcitator[sortmode=authoryear]
-- 
cgit v1.2.3