diff options
-rw-r--r-- | rst_context.lua | 160 | ||||
-rw-r--r-- | rst_helpers.lua | 1 | ||||
-rw-r--r-- | rst_parser.lua | 16 |
3 files changed, 157 insertions, 20 deletions
diff --git a/rst_context.lua b/rst_context.lua index f17f5a6..34f4594 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -809,10 +809,9 @@ function rst_context.literal_block (str, included) \stoptyping ]] if included then -- escaping can ruin your day - return str:gsub("\\", "\\\\") - else - return str + str = str:gsub("\\", "\\\\") end + return str end function rst_context.included_literal_block (str) @@ -1184,8 +1183,15 @@ rst_context.directives.images.keys = { ["width"] = "width", ["size"] = "width", ["caption"] = "caption", + ["scale"] = "scale", } +rst_context.directives.images.values.scale = function (orig) + -- http://wiki.contextgarden.net/Reference/en/useexternalfigure + -- scale=1000 is original size; to get 72%, use scale=720. + return tonumber(orig) * 1000 +end + rst_context.directives.images.values.width = { ["fit"] = "\\hsize", ["hsize"] = "\\hsize", @@ -1196,7 +1202,7 @@ rst_context.directives.images.values.width = { -- we won't allow passing arbitrary setups to context rst_context.directives.images.permitted_setups = { - "width" + "width", "scale" } local function img_setup (properties) @@ -1211,6 +1217,7 @@ local function img_setup (properties) end return result end + rst_context.directives.image = function(name, data) local properties = {} local anon = false @@ -1222,7 +1229,7 @@ rst_context.directives.image = function(name, data) name = "anonymous" .. rd.anonymous end properties.caption = name - properties.width = "\\local" + --properties.width = "\\local" local processed = "" -- stub; TODO do something useful with optional dimension specifications if type(data) == "table" then -- should always be true @@ -1233,7 +1240,11 @@ rst_context.directives.image = function(name, data) local rdi = rst_context.directives.images if key and val then key = rdi.keys[key] -- sanitize key expression - val = rdi.values[key] and rdi.values[key][val] or val -- e.g. captions have no substitutions + 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 string.strip(str)) @@ -1254,7 +1265,7 @@ rst_context.directives.image = function(name, data) images_done[name] = true end img = img .. string.format([[ -\def\RSTsubstitution%s{% +\def\RSTsubstitution%s{%% \placefigure[here]{%s}{\externalfigure[%s]%s} } ]], name, properties.caption, name, properties.setup) @@ -1267,6 +1278,52 @@ rst_context.directives.image = function(name, data) return img end +rst_context.directives.caution = function(raw) + rst_context.addsetups("dbend") + rst_context.addsetups("caution") + local text + local first = true + for _, line in ipairs(raw) do + if not helpers.patterns.spacesonly:match(line) then + if first then + text = line + first = false + else + text = text .. " " .. line + end + end + end + text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) + return string.format([[ +\startRSTcaution +%s +\stopRSTcaution +]], text) +end + +rst_context.directives.danger = function(raw) + rst_context.addsetups("dbend") + rst_context.addsetups("danger") + local text + local first = true + for _, line in ipairs(raw) do + if not helpers.patterns.spacesonly:match(line) then + if first then + text = line + first = false + else + text = text .. " " .. line + end + end + end + text = rst_context.escape(helpers.string.wrapat(inline_parser:match(text))) + return string.format([[ +\startRSTdanger +%s +\stopRSTdanger +]], text) +end + -- http://docutils.sourceforge.net/docs/ref/rst/directives.html rst_context.directives.DANGER = function(addendum) local result = "" @@ -1356,14 +1413,15 @@ function optional_setups.substitutions () end function optional_setups.directive () - local directives = rst_context.directives - local dirstr = [[ + --local directives = rst_context.directives + --local dirstr = [[ -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% Directives % -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -]] - return dirstr +--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +--% Directives % +--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +--]] + --return dirstr + return "" end function optional_setups.blockquote () @@ -1464,4 +1522,78 @@ function optional_setups.fieldlist () ]] end +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] + +\def\GetSym#1{\getglyph{bends}{\char#1}} + +\startsymbolset [Dangerous Bends] + \definesymbol [dbend] [\GetSym{127}] + \definesymbol [lhdbend] [\GetSym{126}] + \definesymbol [lhdbend] [\GetSym{0}] +\stopsymbolset + +\setupsymbolset [Dangerous Bends] + +]] +end + +function optional_setups.caution () + local result = "" + --if not optional_setups.dbend_done then + --result = result .. optional_setups.dbend() + --end + return result .. [[ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Caution directive % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\usemodule[lettrine] + +\setbox0=\hbox{\symbol[dbend]\hbox to 1em{}} +\newskip\RSTbendskip +\RSTbendskip=\wd0 + +\def\startRSTcaution{% +\startparagraph +\lettrine[Lines=2,Raise=.5,Findent=\RSTbendskip,Nindent=0pt]{\symbol[dbend]}{}% +} + +\let\stopRSTcaution\stopparagraph + +]] + +end + +function optional_setups.danger () + local result = "" + --if not optional_setups.dbend_done then + --result = result .. optional_setups.dbend() + --end + return result .. [[ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Danger directive % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\usemodule[lettrine] + +\def\startRSTdanger{% +\startparagraph +\lettrine[Lines=2,Raise=.5,Findent=1em,Nindent=0pt]{\symbol[dbend]\symbol[dbend]}{}% +} + +\let\stopRSTdanger\stopparagraph + +]] + +end + return rst_context diff --git a/rst_helpers.lua b/rst_helpers.lua index 33c1a80..49014f4 100644 --- a/rst_helpers.lua +++ b/rst_helpers.lua @@ -585,6 +585,7 @@ do -- for repeated calls as well. local ulen = utf.len function helpers.string.wrapat (str, width) + local width = width or 65 local linelength = 0 local wrap = P{ [1] = "wrapper", diff --git a/rst_parser.lua b/rst_parser.lua index 7d604aa..7162597 100644 --- a/rst_parser.lua +++ b/rst_parser.lua @@ -149,7 +149,6 @@ local parser = P{ , explicit_markup_block = V"explicit_markup"^1 - * V"end_block" , -------------------------------------------------------------------------------- @@ -162,7 +161,7 @@ local parser = P{ , directive = V"explicit_markup_start" - * C((V"escaped_colon" + (1 - V"colon" - V"eol"))^1) + * C(((V"escaped_colon" + (1 - V"colon" - V"eol")) - V"substitution_text")^1) * V"double_colon" * (V"directive_block_multi" + V"directive_block_single") / rst.directive @@ -179,16 +178,20 @@ local parser = P{ -------------------------------------------------------------------------------- substitution_definition = V"explicit_markup_start" - * V"bar" - * C((1 - V"bar" - V"eol")^1) -- substitution text - * V"bar" + * V"substitution_text" * V"whitespace" * C((1 - V"colon" - V"space" - V"eol")^1) -- directive * V"double_colon" * Ct(V"data_directive_block") + * V"end_block"^-1 / rst.substitution_definition , + substitution_text = V"bar" + * C((1 - V"bar" - V"eol")^1) + * V"bar" + , + data_directive_block = V"data_directive_block_long" + V"data_directive_block_short" , @@ -1081,7 +1084,8 @@ local parser = P{ par_other = V"par_matchindent" * C((1 - V"literal_block_shorthand" - V"eol")^1) - * (V"included_literal_block" + V"eol"), + * (V"included_literal_block" + V"eol") + , par_setindent = Cmt(V"space"^0, function (s, i, indent) warn("par-i", #indent, "", "", i) |