summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-15 15:34:30 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-15 15:34:30 +0200
commit35b9bd9ecc9fc606f8652f20b1126e6019b96006 (patch)
treef8fc2e4f8fafcb89f579c28ec4f38a2ad4084b0f
parent18f1a4107f48beb33fced6c7c4a770a718c15aa1 (diff)
downloadcontext-rst-35b9bd9ecc9fc606f8652f20b1126e6019b96006.tar.gz
image substitution directive
-rw-r--r--rst_context.lua63
-rw-r--r--rst_parser.lua41
2 files changed, 78 insertions, 26 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 1ac5f96..36dbdfc 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -48,6 +48,7 @@ rst_context.last_section_level = 0
rst_context.anonymous_targets = 0
rst_context.anonymous_links = 0
rst_context.context_references = {}
+rst_context.substitutions = {}
rst_context.current_footnote_number = 0
rst_context.current_symbolnote_number = 0
@@ -207,6 +208,11 @@ function rst_context.inline_internal_target (str)
return "\\\\reference[__target_" .. rst_context.whitespace_to_underscore(str) .."]{}"
end
+function rst_context.substitution_reference (str)
+ rst_context.addsetups("substitutions")
+ return [[{\\RSTsubstitution]] .. string.strip(str) .. "}"
+end
+
function rst_context.escape (str)
str = str:gsub("\\(.)", "%1")
return str
@@ -228,9 +234,9 @@ local inline_parser = P{
+ V"emphasis"
+ V"inline_literal"
+ V"interpreted_text"
- + V"inline_internal_target" -- TODO
+ + V"inline_internal_target"
+ V"footnote_reference"
--- + V"substitution_reference" -- TODO
+ + V"substitution_reference"
+ V"link_standalone")
* V"succede_inline"),
@@ -389,6 +395,13 @@ local inline_parser = P{
/ rst_context.inline_internal_target
,
+ substitution_reference = V"bar"
+ * C((1 - V"bar")^1)
+ * V"bar"
+ * (V"underscore" + V"double_underscore")^-1
+ / rst_context.substitution_reference
+ ,
+
--------------------------------------------------------------------------------
-- Urls
--------------------------------------------------------------------------------
@@ -920,7 +933,7 @@ function rst_context.simple_table(tab)
return head .. body .. tail
end
-function rst_context.footnote(label, content)
+function rst_context.footnote (label, content)
local tf = state.footnotes
rst_context.addsetups("footnotes")
if label:match("^%d+$") then -- all digits
@@ -945,6 +958,13 @@ function rst_context.footnote(label, content)
return ""
end
+function rst_context.substitution_definition (subtext, directive, data)
+ print(subtext,directive,data)
+ local rs = rst_context.substitutions
+ rs[subtext] = { directive = directive, data = data }
+ return "\n% substitution definition block\n"
+end
+
optional_setups = {}
function optional_setups.footnote_symbol ()
local setup = [[
@@ -1024,4 +1044,41 @@ function optional_setups.references ()
return refsection
end
+function optional_setups.substitutions ()
+ local images_done = {}
+
+ local image = function(name, data)
+ local img = ""
+ if not images_done[name] then
+ img = img .. string.format([[
+
+\useexternalfigure[%s][%s][]
+]], name, data)
+ end
+ img = img .. string.format([[
+\def\RSTsubstitution%s{
+ \placefigure[here]{%s}{\externalfigure[%s]}
+}
+]], name, name, name)
+ return img
+ end
+
+ local substitutions = [[
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Substitutions %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+]]
+ local rs = rst_context.substitutions
+ for name, content in next, rs do
+ local directive, data = content.directive, content.data
+ name, data = string.strip(name), string.strip(data)
+ if string.strip(directive) == "image" then
+ substitutions = substitutions .. image(name, data)
+ end
+ end
+ return substitutions
+end
+
return rst_context
diff --git a/rst_parser.lua b/rst_parser.lua
index b065bea..dd20354 100644
--- a/rst_parser.lua
+++ b/rst_parser.lua
@@ -100,7 +100,6 @@ local parser = P{
block = V"target_block"
+ Cs(V"list") / rst.escape
- + V"comment"
+ V"line_block"
+ Cs(V"table_block") / rst.escape
+ Cs(V"section") / rst.escape
@@ -108,6 +107,7 @@ local parser = P{
+ V"literal_block"
+ Cs(V"block_quote") / rst.escape
+ V"explicit_markup"
+ + V"comment"
+ Cs(V"paragraph") / rst.escape
,
@@ -123,35 +123,30 @@ local parser = P{
explicit_markup_start = V"double_dot" * V"whitespace",
explicit_markup = V"footnote_block"
- --+ V"hyperlink_target"
--+ V"directive"
- --+ V"substitution_definition"
+ + V"substitution_definition"
--+ V"comment block"
,
--------------------------------------------------------------------------------
--- Explicit markup hyperlink target
+-- Substitution definition block
--------------------------------------------------------------------------------
- --hyperlink_target_block = V"hyperlink_target"
- --* (V"blank_line"^-1 * V"hyperlink_target")^0
- --* V"end_block"
- --,
-
- --hyperlink_target = V"hl_anonymous"
- ----+ V"hl_named"
- --,
-
- --hl_anonymous = V"hl_anonymous_start"
- --* C(V"link_block")
- --,
-
- --hl_anonymous_start = (V"double_underscore"
- --+ V"double_dot" * V"space" * V"double_underscore" * V"colon")
- --* V"whitespace"
- --,
+ substitution_definition = V"explicit_markup_start"
+ * V"bar"
+ * C((1 - V"bar" - V"eol")^1) -- substitution text
+ * V"bar"
+ * V"whitespace"
+ * C((1 - V"colon" - V"space" - V"eol")^1) -- directive
+ * V"double_colon"
+ * C(V"data_directive_block")
+ * V"end_block"
+ / rst.substitution_definition
+ ,
- --link_block =
+ data_directive_block = V"rest_of_line" * V"eol"
+ * V"indented_lines"^-1 -- nothing said about definitions
+ , -- spanning multiple paragraphs
--------------------------------------------------------------------------------
-- Explicit markup footnote block
@@ -1004,7 +999,7 @@ local parser = P{
--------------------------------------------------------------------------------
comment = V"double_dot" * V"whitespace"^0
- * ((V"block_comment" + V"line_comment") - V"footnote_marker")
+ * ((V"block_comment" + V"line_comment") - V"footnote_marker" - V"bar")
,
block_comment = V"eol"