summaryrefslogtreecommitdiff
path: root/rst_context.lua
diff options
context:
space:
mode:
Diffstat (limited to 'rst_context.lua')
-rw-r--r--rst_context.lua549
1 files changed, 6 insertions, 543 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 5f700f7..77e0966 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -2,9 +2,6 @@
--------------------------------------------------------------------------------
-- FILE: rst_context.lua
-- USAGE: ./rst_context.lua
--- DESCRIPTION:
--- OPTIONS: ---
--- REQUIREMENTS: ---
-- AUTHOR: Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
-- VERSION: 1.0
-- CREATED: 31/08/10 19:35:15 CEST
@@ -20,6 +17,7 @@
require "lpeg"
help = require "rst_helpers"
+rst_directives = require "rst_directives"
local dbg_write = help.dbg_writef
@@ -45,7 +43,7 @@ local err = function(str)
end
end
-local rst_context = {}
+rst_context = {}
rst_context.collected_adornments = {}
rst_context.last_section_level = 0
@@ -474,6 +472,8 @@ local inline_parser = P{
url_path = V"slash" * (V"url_path_char"^1 * V"slash"^-1)^1,
}
+rst_context.inline_parser = inline_parser
+
function rst_context.paragraph (data)
local str
if not data then
@@ -1091,9 +1091,9 @@ function rst_context.substitution_definition (subtext, directive, data)
return ""
end
--- not to be confused with the directive definition table rst_context.directives
+-- not to be confused with the directive definition table rst_directives
function rst_context.directive(directive, ...)
- local rd = rst_context.directives
+ local rd = rst_directives
rst_context.addsetups("directive")
local data = {...}
local result = ""
@@ -1103,541 +1103,4 @@ function rst_context.directive(directive, ...)
return result
end
-optional_setups = {}
-function optional_setups.footnote_symbol ()
- local setup = [[
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Footnotes with symbol conversion %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\definenote[symbolnote][footnote]
-\setupnote [symbolnote][way=bypage,numberconversion=set 2]
-]]
- return setup
-end
-
-function optional_setups.footnotes ()
- local tf = state.footnotes
- local fn = [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Footnotes %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-]]
- local buffer = [[
-
-%% %s
-\startbuffer[%s]
-%s\stopbuffer
-]]
-
- for nf, note in next, tf.numbered do
- fn = fn .. string.format(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note)
- end
- for nf, note in next, tf.autolabel do
- fn = fn .. string.format(buffer, "Labeled footnote", "__footnote_label_"..nf, note)
- end
- for nf, note in next, tf.symbol do
- fn = fn .. string.format(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note)
- end
- return fn
-end
-
-function optional_setups.references ()
- local refs = rst_context.collected_references
- local crefs = rst_context.context_references
- local arefs = rst_context.anonymous_set
-
- local function urlescape (str)
- return str:gsub("#", "\\#")
- end
-
- local function resolve_indirect (r)
- if r and r:match(".*_$") then -- pointing elsewhere
- local look_me_up = r:match("^`?([^`]*)`?_$")
- local result = resolve_indirect (refs[look_me_up])
- if result then
- return result
- else
- if rst_context.structure_references[look_me_up] then
- -- Internal link, no useURL etc.
- return false
- end
- end
- end
- return r
- end
-
- local refsection = [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% References %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-]]
- local references = {}
- local ref_keys = {}
- for ref, target in next, refs do
- ref_keys[#ref_keys+1] = [[__target_]] .. rst_context.whitespace_to_underscore(ref)
- target = resolve_indirect(target)
- if target ~= false then
- ref_text = ref
- if arefs[ref_text] then
- ref_text = rst_context.anonymous_links[tonumber(arefs[ref_text])]
- end
- references[#references+1] = string.format([[
-\useURL[__target_%s] [%s] [] [%s] ]], rst_context.whitespace_to_underscore(ref), urlescape(target), ref_text)
- end
- end
- refsection = refsection .. table.concat(references, "\n")
- -- this is needed in order to select the right reference command later
- refsection = refsection .. "\n\n" .. [[\def \RSTexternalreferences{]] .. table.concat(ref_keys, ",") .. [[}
-
-% #1 target name, #2 link text
-\def\RSTchoosegoto#1#2{%
- \rawdoifinsetelse{#1}{\RSTexternalreferences}%
- {\from[#1]}%
- {\goto{#2}[#1]}%
-}
-]]
-
- return refsection
-end
-
---------------------------------------------------------------------------------
--- Directives for use with |substitutions|
---------------------------------------------------------------------------------
-
-rst_context.directives = {}
-rst_context.directives.anonymous = 0
-rst_context.directives.images = {}
-rst_context.directives.images.done = {}
-rst_context.directives.images.values = {}
-
-rst_context.directives.images.keys = {
- ["width"] = "width",
- ["size"] = "width",
- ["caption"] = "caption",
- ["alt"] = "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",
- ["broad"] = "\\hsize",
- ["normal"] = "local",
- ["normal"] = "local",
-}
-
--- we won't allow passing arbitrary setups to context
-rst_context.directives.images.permitted_setups = {
- "width", "scale"
-}
-
-local function img_setup (properties)
- local result = ""
- for _, prop in next, rst_context.directives.images.permitted_setups do
- if properties[prop] then
- result = result .. prop .. "=" .. properties[prop] .. ","
- end
- end
- if result ~= "" then
- result = "[" .. result .. "]"
- end
- return result
-end
-
-rst_context.directives.image = function(name, data)
- local properties = {}
- 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
- 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 _, str in ipairs(data) do
- local key, val
- key, val = p.colon_keyval:match(str)
- local rdi = rst_context.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 string.strip(str))
- end
- 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 .. string.format([[
-
-\useexternalfigure[%s][%s][]
-]], name, data)
- images_done[name] = true
- end
- img = img .. string.format([[
-\def\RSTsubstitution%s{%%
- \placefigure[here]{%s}{\externalfigure[%s]%s}
-}
-]], name, rst_context.escape(inline_parser:match(properties.caption)), name, properties.setup)
- else -- image won't be referenced but used instantly
- img = img .. string.format([[
-
-\placefigure[here]{%s}{\externalfigure[%s]%s}
-]], rst_context.escape(inline_parser:match(properties.caption)), data, properties.setup)
- end
- 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 = ""
- 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([[
-
-\startbuffer[%s]
-%s
-\stopbuffer
-\def\RSTsubstitution%s{%%
- \getbuffer[%s]
-}
-]], name, data, name, name)
- return ctx
-end
-
-rst_context.directives.lua = function(name, data)
- local luacode = string.format([[
-
-\startbuffer[%s]
-\startluacode
-%s
-\stopluacode
-\stopbuffer
-\def\RSTsubstitution%s{%%
- \getbuffer[%s]%%
-}
-]], name, data, name, name)
- return luacode
-end
-
-rst_context.directives.replace = function(name, data)
- return string.format([[
-
-\def\RSTsubstitution%s{%s}
-]], name, data)
-end
-
-function optional_setups.substitutions ()
- local directives = rst_context.directives
- local substitutions = [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Substitutions %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-]]
- local rs = rst_context.substitutions
- for name, content in next, rs do
- local directive, data = content.directive, content.data
- name, data = name:gsub("%s", ""), string.strip(data)
- if directives[directive] then
- substitutions = substitutions .. directives[directive](name, data)
- else
- err(directive .. " does not exist.")
- end
- end
- return substitutions
-end
-
-function optional_setups.directive ()
- --local directives = rst_context.directives
- --local dirstr = [[
-
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
---% Directives %
---%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
---]]
- --return dirstr
- return ""
-end
-
-function optional_setups.blockquote ()
- return [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Blockquotes %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\setupdelimitedtext [blockquote][style={\tfx}] % awful placeholder
-\definedelimitedtext[attribution][blockquote]
-\setupdelimitedtext [attribution][style={\tfx\it}]
-]]
-end
-
-function optional_setups.deflist ()
- return [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Definitionlist %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-\def\startRSTdefinitionlist{
- \bgroup
- \def \RSTdeflistterm##1{{\bf ##1}}
- \def\RSTdeflistclassifier##1{\hbox to 1em{\it ##1}}
- \def\RSTdeflistdefinition##1{%
- \startnarrower[left]
- ##1%
- \stopnarrower}
- \def\RSTdeflistparagraph ##1{%
- \startparagraph{%
- \noindentation ##1
- \stopparagraph}
- }
-}
-
-\let\stopRSTdefinitionlist\egroup
-]]
-end
-
-function optional_setups.lines ()
- return [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Lines environment (line blocks) %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-\setuplines[%
- space=on,%
- before={\startlinecorrection\blank[small]},%
- after={\blank[small]\stoplinecorrection},%
-]
-]]
-end
-
-function optional_setups.breaks ()
- return [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Fancy transitions %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-\usemodule[fancybreak]
-\setupfancybreak[symbol=star]
-]]
-end
-
-function optional_setups.fieldlist ()
- return [[
-
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Fieldlists %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-
-\def\startRSTfieldlist{%
- \bgroup%
- \unexpanded\def\RSTfieldname##1{\bTR\bTC ##1\eTC}
- \unexpanded\def\RSTfieldbody##1{\bTC ##1\eTC\eTR}
-%
- \setupTABLE[c][first] [background=color, backgroundcolor=grey, style=\bf]
- \setupTABLE[c][2] [align=right]
- \setupTABLE[c][each] [frame=off]
- \setupTABLE[r][each] [frame=off]
- \bTABLE[split=yes,option=stretch]
- \bTABLEhead
- \bTR
- \bTH Field \eTH
- \bTH Body \eTH
- \eTR
- \eTABLEhead
- \bTABLEbody
-}
-
-\def\stopRSTfieldlist{%
- %\eTABLEbody % doesn't work, temporarily moved to rst_context.field_list()
- \eTABLE
- \egroup%
-}
-]]
-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]}
-\newskip\RSTbendskip
-\RSTbendskip=\wd0
-\advance\RSTbendskip by 1em % These two lines should add
-\advance\RSTbendskip by 1pt % 13.4pt in mkiv and 13.14983pt in mkii
- % to make the indent equal to the indent
- % of the “danger” directive.
- % (2*(width)dbend + (kern)1pt + 1em
-
-\def\startRSTcaution{%
-\startparagraph
-\dontleavehmode\lettrine[Lines=2,Raise=.6,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=.6,Findent=1em,Nindent=0pt]{\symbol[dbend]\kern 1pt\symbol[dbend]}{}%
-}
-
-\let\stopRSTdanger\stopparagraph
-
-]]
-
-end
-
return rst_context