From 01d0c2dcca48fde5a99ff1173e80ad3c085cdbb5 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sat, 18 Sep 2010 02:01:06 +0200 Subject: simple directives. --- rst_context.lua | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- rst_parser.lua | 24 +++++++++++++- 2 files changed, 116 insertions(+), 8 deletions(-) diff --git a/rst_context.lua b/rst_context.lua index 39d375b..e64aac4 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -1080,6 +1080,20 @@ function rst_context.substitution_definition (subtext, directive, data) return "" end +-- not to be confused with the directive definition table rst_context.directives +function rst_context.directive(directive, ...) + local rd = rst_context.directives + rst_context.addsetups("directive") + local data = {...} + --print(directive, data, #data) + --for i,j in next,data do print(i,j) end + local result = "" + if rd[directive] then + result = rd[directive](data) + end + return result +end + optional_setups = {} function optional_setups.footnote_symbol () local setup = [[ @@ -1163,26 +1177,87 @@ end -------------------------------------------------------------------------------- rst_context.directives = {} +rst_context.directives.anonymous = 0 rst_context.directives.images_done = {} rst_context.directives.image = function(name, data) + local anon = false + local rd = rst_context.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 processed = "" -- stub; TODO do something useful with optional dimension specifications + if type(data) == "table" then -- should always be true + for _, str in ipairs(data) do + processed = processed .. (str and str ~= "" and string.strip(str)) + end + end + data = processed + processed = nil local img = "" - local images_done = rst_context.directives.images_done - if not images_done[name] then - img = img .. string.format([[ + local images_done = rd.images_done + if not anon then + if not images_done[name] then + img = img .. string.format([[ \useexternalfigure[%s][%s][] ]], name, data) - images_done[name] = true - end - img = img .. string.format([[ -\def\RSTsubstitution%s{ + images_done[name] = true + end + img = img .. string.format([[ +\def\RSTsubstitution%s{% \placefigure[here]{%s}{\externalfigure[%s]} } ]], name, name, name) + else -- image won't be referenced but used instantly + img = img .. string.format([[ + +\placefigure[here]{none}{\externalfigure[%s]} +]], data) + end return img end +-- http://docutils.sourceforge.net/docs/ref/rst/directives.html +rst_context.directives.DANGER = function(addendum) + local result = "" + for _,str in ipairs(addendum) do + result = result .. (string.strip(str)) + end + return string.format([[ + +%% The Rabbit of Caerbannog +\startlinecorrection +\blank[force,big] +\framed[frame=on, + corner=round, + rulethickness=5pt, + align=middle, + width=\hsize, + frameoffset=.5em, + backgroundoffset=1em, + background=color, + backgroundcolor=red, + foreground=color, + foregroundcolor=black]{%% + \language[en-gb]\tfb\bf + Follow only if ye be men of valour, for the entrance to this cave is guarded + by a creature so foul, so cruel that no man yet has fought with it and lived. + Bones of full fifty men lie strewn about its lair. So, brave knights, if you + do doubt your courage or your strength, come no further, for death awaits you + all with nasty, big, pointy teeth.%% + \blank[force,big] + %s%% +} +\blank[force,big] +\stoplinecorrection +]], result) +end + rst_context.directives.ctx = function(name, data) local ctx = string.format([[ @@ -1235,6 +1310,17 @@ function optional_setups.substitutions () return substitutions end +function optional_setups.directive () + local directives = rst_context.directives + local dirstr = [[ + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Directives % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +]] + return dirstr +end + function optional_setups.blockquote () return [[ diff --git a/rst_parser.lua b/rst_parser.lua index cd44f51..7d604aa 100644 --- a/rst_parser.lua +++ b/rst_parser.lua @@ -144,7 +144,7 @@ local parser = P{ explicit_markup_start = V"double_dot" * V"whitespace", explicit_markup = V"footnote_block" - --+ V"directive" + + V"directive_block" + V"substitution_definition" , @@ -152,6 +152,28 @@ local parser = P{ * V"end_block" , +-------------------------------------------------------------------------------- +-- Directives block +-------------------------------------------------------------------------------- + + directive_block = V"directive" + --* (V"blank_line"^-1 * V"directive")^0 + * V"end_block" + , + + directive = V"explicit_markup_start" + * C((V"escaped_colon" + (1 - V"colon" - V"eol"))^1) + * V"double_colon" + * (V"directive_block_multi" + V"directive_block_single") + / rst.directive + , + + directive_block_multi = C((1 - V"eol")^0) * V"eol" + * V"directive_indented_lines" + , + + directive_block_single = C((1 - V"eol")^1) * V"eol", + -------------------------------------------------------------------------------- -- Substitution definition block -------------------------------------------------------------------------------- -- cgit v1.2.3