summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-14 01:56:47 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-14 01:56:47 +0200
commit78df2a79804f4461456c3bc258c566b1c92ce4fd (patch)
tree45a90bae231597196fbf902927271c7311206a67
parent1fa5b90dbad108fdd8fa2a80fef4214c3183ec84 (diff)
downloadcontext-rst-78df2a79804f4461456c3bc258c566b1c92ce4fd.tar.gz
reference setups. sanitizing link target id’s
-rw-r--r--rst_context.lua49
1 files changed, 45 insertions, 4 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 35ef40c..5bc1753 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -45,6 +45,7 @@ local rst_context = {}
rst_context.collected_references = {}
rst_context.collected_adornments = {}
rst_context.last_section_level = 0
+rst_context.anonymous_targets = 0
rst_context.anonymous_links = 0
rst_context.context_references = {}
@@ -147,6 +148,7 @@ function rst_context.link_standalone (str)
end
function rst_context.reference (str)
+ rst_context.addsetups("references")
str = str:match("^`?([^`]+)`?_$") -- LPEG could render this gibberish legible but not time
local link = rst_context.collected_references[str] or rst_context.context_references[str]
if not link then -- assume internal
@@ -159,7 +161,20 @@ function rst_context.reference (str)
.. [=[)]]=]
end
+function rst_context.anon_reference (str)
+ rst_context.addsetups("references")
+ str = str:match("^`?([^`]+)`?__$")
+ rst_context.anonymous_links = rst_context.anonymous_links + 1
+ link = "__target_anon_" .. rst_context.anonymous_links
+ return [[\\goto{]]
+ .. str
+ .. [[}[]]
+ .. link
+ .. [=[]]=]
+end
+
function rst_context.target (tab)
+ rst_context.addsetups("references")
--print("GOT ONE!")
--local tab = { ... }
local refs = rst_context.collected_references
@@ -168,14 +183,14 @@ function rst_context.target (tab)
local function resolve_indirect (r)
if r and r:match(".*_$") then -- pointing elsewhere
- return resolve_indirect (refs[r:match("(.*)_$")]) or "need another run!" -- TODO multiple runs && data collection
+ return resolve_indirect (refs[r:match("^`?([^`]*)`?_$")]) or "need another run!" -- TODO multiple runs && data collection
end
return r
end
local function create_anonymous ()
- rst_context.anonymous_links = rst_context.anonymous_links + 1
- return "__target_anon_" .. rst_context.anonymous_links
+ rst_context.anonymous_targets = rst_context.anonymous_targets + 1
+ return "__target_anon_" .. rst_context.anonymous_targets
end
target = resolve_indirect (target)
@@ -188,7 +203,7 @@ function rst_context.target (tab)
end
else
for i=1,#tab do
- local id = tab[i]:gsub("\\:",":") -- deescaping
+ local id = tab[i]:gsub("\\:",":"):match("`?([^`]+)`?") -- deescaping
id = id ~= "" and id or create_anonymous ()
refs[id] = refs[id] or target
end
@@ -213,6 +228,7 @@ local inline_parser = P{
inline_element = Cs((V"strong_emphasis"
+ + V"anon_reference"
+ V"reference"
+ V"emphasis"
+ V"inline_literal"
@@ -336,6 +352,16 @@ local inline_parser = P{
link_standalone = C(V"uri")
/ rst_context.link_standalone,
+ anon_reference = Cs(V"anon_normal_reference" + V"anon_phrase_reference")
+ / rst_context.anon_reference,
+
+ anon_normal_reference = (1 - V"underscore" - V"spacing" - V"eol" - V"punctuation" - V"groupchars")^1 * V"double_underscore",
+
+ anon_phrase_reference = (V"bareia" - V"double_bareia")
+ * C((1 - V"bareia")^1)
+ * V"bareia" * V"double_underscore"
+ ,
+
reference = Cs(V"normal_reference" + V"phrase_reference")
/ rst_context.reference,
@@ -961,4 +987,19 @@ function optional_setups.footnotes ()
return fn
end
+function optional_setups.references ()
+ local refs = [[
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% References %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+]]
+ for ref, target in next, rst_context.collected_references do
+ refs = refs .. string.format([[
+\useURL[__target_%s] [%s] [] [%s]
+]], rst_context.whitespace_to_underscore(ref), target, ref)
+ end
+ return refs
+end
+
return rst_context