Commits

Philipp Gesang  committed c6feaed

cleanup; rewrite directives; update manual

  • Participants
  • Parent commits 3f1ce9e

Comments (0)

Files changed (7)

File mod/doc/context/third/rst/documentation.rst

 diverted me from this path.
 
 -   *Typography*. It’s all about the details. No matter how good your
-    converter is, it still won’t reach |TEX|’s omnipotence and
-    flexibility. |rstcontext| is a tool to generate raw material
-    for your typesetting job, not a typesetting system in itself.
+    converter is, auto-generated code will not reach |TEX|’s
+    omnipotence and flexibility. |rstcontext| is a tool to
+    generate raw material for your typesetting job, not a
+    typesetting system in itself.
 
 -   *Testing*. Never underestimate the insights gained from reading
     the resulting |CONTEXT| file. Quite some effort has been
     undertaken to make it human-readable, especially the setups.
 
--   *MkII*. I’m not an MkII user at all save for rapid testing and
-    the occasional check for the sanity of |CONTEXT|’s behaviour.
-    Slow hardware forces me to run |PDFTEX| instead of |LUATEX|
-    whenvever I need some result as quick as possible, so I wanted
-    to keep the code MkII clean. Do not expect Unicode (as  in
-    this document) to work without precautions.
+-   *MkII*. I’m not an MkII user at all, and compatibility with
+    it is not a primary objective for |rstcontext|.
+    However, an effort has been made to keep the output essentially
+    MkII clean. Do not expect Unicode to work without
+    precautions.
 
 During the development readability of the generated code was
 alway one of the main goals of |rstcontext|. Quite some computing
 set to ``here`` by default.)
 
 .. image:: cow
+    cow.pdf
     width: hsize
     alt: A generic Dutch cow (*bos primigenius taurus*).
 
 .. _docutils: http://docutils.sourceforge.net/
 .. _Pandoc: http://johnmacfarlane.net/pandoc/
 
+Containers
+**********
+
+Upon request |rstcontext| now supports another kind of
+directive, namely containers_.
+Due to their being defined explicitly in terms of HTML,
+*containers* lack a corresponding construct in |CONTEXT| (or
+|TEX| for that matter).
+Some parts of |CONTEXT| (e. g. ``\framed``) come quite close with
+respect to functionality as well as generality.
+However, none of the candidates alone covers the entire spectrum
+of functionality that containers_ are supposed to.
+For that reason the implementation leaves them essentially
+undefined.
+
+If an explicit name is specified, then the ``container``
+directive maps to the environment of that name.
+Anonymous containers are interpreted as a |TEX| group.
+Any text block inside the element is treated as ordinary
+paragraph.
+In below example the content will be handled as if between
+``\startxyzzy`` and ``\stopxyzzy``, where it is up to the user to
+define the *xyzzy* environment::
+
+    This is a paragraph.
+
+    .. container:: xyzzy
+
+        whatever
+
+        foo **bar** baz
+
+    This is another paragraph.
+
+The middle part translates to |CONTEXT| as follows::
+
+    \start[xyzzy]%
+    whatever
+
+    foo {\sc bar} baz
+    \stop
+
+Note that the ``\start[foo]``/``\stop``-environment is equivalent
+to ``\startfoo``/``\stopfoo``, except that the environment
+doesn’t actually need to be defined.
+
+.. caution::
+    Support for the *container* directive is considered
+    experimental.
+    Suggestions for improving or extending the current
+    implementation are always welcome.
+
+.. _containers: http://docutils.sourceforge.net/docs/ref/rst/directives.html#container
+
 =======================
 Substitution Directives
 =======================
 
 ::
 
-    Copyright 2010-2011 Philipp Gesang. All rights reserved.
+    Copyright 2010-2013 Philipp Gesang. All rights reserved.
 
     Redistribution and use in source and binary forms, with or
     without modification, are permitted provided that the

File mod/doc/context/third/rst/manual.tex

 \vfill
 \framed [align=right,frame=off,topframe=on] {%
 \tfxx\ss\setupinterlinespace[small]\noindentation
-Copyright 2010--2012 by Philipp Gesang, Heidelberg.\par
+Copyright 2010--2013 Philipp Gesang, Radebeul.\par
 Mail any bug reports, patches or suggestions to\par
 \type{string.format("%s@%s.com", "phg42.2a", "gmail")}\par
 or pay a visit to \goto{my BitBucket home}[url(http://bitbucket.org/phg/)].\par

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

 --        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
 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
 
         escaped  = P"\\" * V"space"
     }
     function string.strip(str)
-        return stripper:match(str) or ""
+        return lpegmatch(stripper, str) or ""
     end
 end
 local stringstrip  = string.strip
 
 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
         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
     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
 
 
 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)
 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
 
 
 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)
                 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
     }
 
     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
 
     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
     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
 
         nospace  = V"escaped" + (1 - V"space"),
     }
     function stringstrip(str)
-        return stripper:match(str) or ""
+        return lpegmatch(stripper, str) or ""
     end 
 end
 
 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)
 
     return result .. [[
 
-\\item ]] .. inline_parser:match(content) .. [[
+\\item ]] .. lpegmatch(inline_parser, content) .. [[
 
 ]]
 end
             local par = final[np]
             tmp = tmp .. [[
     \\RSTdeflistparagraph{%
-]] .. inline_parser:match(par) .. "}\n"
+]] .. lpegmatch(inline_parser, par) .. "}\n"
         end
         tmp = tmp .. "  }"
         deflist = deflist .. tmp
 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)
 
     \\RSTfieldname{%s}
     \\RSTfieldbody{%s}
-]], name, inline_parser:match(body))
+]], name, lpegmatch(inline_parser, body))
 end
 
 function rst_context.line_comment (str)
 \\eTR
 \\eTABLEhead
 \\bTABLEbody
-]] .. inline_parser:match(str) .. [[
+]] .. lpegmatch(inline_parser, str) .. [[
 
 \\eTABLEbody
 \\eTABLE
 
 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
         end,
     }
 
-    str = strip:match(str)
+    str = lpegmatch(strip, str)
     str = [[
 
 \starttyping[lines=hyphenated]
     return [[
 
 \\startlines
-]] .. inline_parser:match(str) .. [[\\stoplines
+]] .. lpegmatch(inline_parser, str) .. [[\\stoplines
 ]]
 end
 
 \\startlinecorrection
 \\blank[small]
 \\startblockquote
-]] .. inline_parser:match(tab[1]) .. [[
+]] .. lpegmatch(inline_parser, tab[1]) .. [[
 
 \\stopblockquote
 ]]
     return tab[2] and str .. [[
 \\blank[small]
 \\startattribution
-]] .. inline_parser:match(tab[2]) .. [[
+]] .. lpegmatch(inline_parser, tab[2]) .. [[
 \\stopattribution
 \\blank[small]
 \\stoplinecorrection
                     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
                 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
                 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
             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
 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

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

 --        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
 --------------------------------------------------------------------------------
 --
 
 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 = {}
 }
 
 -- 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
     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"
+    data               = tableflattened(data)
 
-    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))
+    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([[
 
 }
 \blank[force,big]
 \stoplinecorrection
-]], result)
+]], tableconcat(text, "\n\n"))
 end
 
 rst_directives.mp = function(name, data)
 --- 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"
+    local tmp = { }
+    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
-    local tmp = { }
-    for i=2, #data do
-        tmp[#tmp+1] = data[i]
+    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
+]], name, content)
+    else
+        return stringformat([[
+\begingroup%%
+%s%%
+\endgroup
+]], content)
     end
-    -- content |> concat |> match |> escape
-    content = tableconcat(tmp, " ")
-    content = lpegmatch(inline_parser, content)
-    content = rst_context.escape(content)
-    return stringformat([[
-\start[%s]%%
-%s\stop
-]], name, content)
 end
 
 -- vim:ft=lua:sw=4:ts=4:expandtab

File mod/tex/context/third/rst/rst_helpers.lua

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

File mod/tex/context/third/rst/rst_parser.lua

 --        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
 --------------------------------------------------------------------------------
 --
 
 helpers.rst_debug = false
 
 local iowrite      = io.write
+local ioopen       = io.open
 local stringformat = string.format
 local stringlen    = string.len
 local stringstrip  = string.strip
 
     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
                       * 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
                              ,
 
 
                              ,
 
     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"
 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()
 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()
 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
         setups = setups .. [[
 
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%{                           Main                              }%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%+-------------------------------------------------------------+%
+%|                            Main                             |%
+%+-------------------------------------------------------------+%
 
 \starttext
 ]]
 
 \stoptext
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%{                        End of Document                      }%
-%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%+-------------------------------------------------------------+%
+%|                       End of Document                       |%
+%+-------------------------------------------------------------+%
 
 % vim:ft=context:tw=65:shiftwidth=2:tabstop=2:set expandtab
 ]]

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

 --        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
 --------------------------------------------------------------------------------
 --
 
 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]
 ]]
     local tf = state.footnotes
     local fn = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Footnotes                                                     %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 ]]
     local buffer = [[
 
 ]]
     
     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
 
     local refsection = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % References                                                    %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 ]]
     local 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
     local directives = rst_directives
     local substitutions = [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Substitutions                                                 %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 ]]
     local rs = rst_context.substitutions
     for name, content in next, rs do
 function optional_setups.directive ()
     --local dirstr = [[
 
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+--%---------------------------------------------------------------%
 --% Directives                                                    %
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+--%---------------------------------------------------------------%
 --]]
     --return dirstr
     return ""
 function optional_setups.blockquote ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Blockquotes                                                   %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \setupdelimitedtext  [blockquote][style={\tfx}] % awful placeholder
 \definedelimitedtext[attribution][blockquote]
 \setupdelimitedtext [attribution][style={\tfx\it}]
 function optional_setups.deflist ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Definitionlist                                                %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \def\startRSTdefinitionlist{
   \bgroup
   \def      \RSTdeflistterm##1{{\bf ##1}}
 function optional_setups.lines ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Lines environment (line blocks)                               %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \setuplines[%
   space=on,%
 function optional_setups.breaks ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Fancy transitions                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 % Get Wolfgang’s module at <https://bitbucket.org/wolfs/fancybreak>.
 \usemodule[fancybreak]
 function optional_setups.fieldlist ()
     return [[
 
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Fieldlists                                                    %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \def\startRSTfieldlist{%
   \bgroup%
     -- There's just no reason for not providing this.
     optional_setups.dbend_done = true
     return [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Dangerous bend                                                %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \loadmapfile [manfnt.map]
 \definefontsynonym [bends] [manfnt]
         --result = result .. optional_setups.dbend()
     --end
     return result .. [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Caution directive                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \usemodule[lettrine]
 
         --result = result .. optional_setups.dbend()
     --end
     return result .. [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Danger directive                                              %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 
 \usemodule[lettrine]
 
 
 function optional_setups.citations ()
     local cit = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Citations                                                     %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \setupbibtex[database=\jobname]
 ]]
     
 
 function optional_setups.citator ()
     local cit = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 % Citator Options                                               %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%---------------------------------------------------------------%
 \usemodule[citator]
 \loadbibdb{\jobname.bib}
 \setupcitator[sortmode=authoryear]