summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rst_context.lua89
-rw-r--r--rst_parser.lua237
2 files changed, 211 insertions, 115 deletions
diff --git a/rst_context.lua b/rst_context.lua
index bc96a9a..5c36c54 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -48,23 +48,40 @@ rst_context.last_section_level = 0
rst_context.anonymous_links = 0
rst_context.context_references = {}
-rst_context.current_footnote_number = 0
+rst_context.current_footnote_number = 0
+rst_context.current_symbolnote_number = 0
function rst_context.footnote_reference (label)
- local tf = tracklists.footnotes
+ local tf = state.footnotes
if label:match("^%d+$") then -- all digits
- -- TODO
+ local c = tonumber(label)
+ print("creating footnote nr " .. c)
+ return [[\\footnote{\\getbuffer[__footnote_number_]].. c .."]}"
elseif label == "#" then --autonumber
- rst_context.current_footnote_number = rst_context.current_footnote_number + 1
- print("creating footnote nr " .. rst_context.current_footnote_number)
- return [[\\footnote{\\getbuffer[__autonumber]]..rst_context.current_footnote_number.."]}"
+ local rc = rst_context.current_footnote_number
+ rc = rc + 1
+ --rst_context.current_footnote_number = rst_context.current_footnote_number + 1
+ print("creating footnote nr " .. rc)
+ rst_context.current_footnote_number = rc
+ return [[\\footnote{\\getbuffer[__footnote_number_]].. rc .."]}"
elseif label:match("^#.+$") then
- -- TODO
+ local thelabel = label:match("^#(.+)$")
+ print("creating labeled footnote " .. thelabel)
+ return [[\\footnote{\\getbuffer[__footnote_label_]].. thelabel .."]}"
elseif label == "*" then
- -- TODO
+ local rc = rst_context.current_symbolnote_number
+ rc = rc + 1
+ print("creating symbolnote nr " .. rc)
+ rst_context.current_symbolnote_number = rc
+ return [[\\symbolnote{\\getbuffer[__footnote_symbol_]].. rc .."]}"
end
end
+function rst_context.addsetups(item)
+ state.addme[item] = state.addme[item] or true
+ return 0
+end
+
-- So we can use crefs[n][2] to refer to the place where the reference was
-- created.
local function get_context_reference (str)
@@ -304,8 +321,8 @@ local inline_parser = P{
,
footnote_label = V"digit"^1
- + V"gartenzaun"
+ V"gartenzaun" * V"letter"^1
+ + V"gartenzaun"
+ V"asterisk"
,
@@ -321,9 +338,15 @@ local inline_parser = P{
url_path = V"slash" * (V"url_path_char"^1 * V"slash"^-1)^1,
}
-function rst_context.paragraph (tab)
- local str = inline_parser:match(table.concat(tab, " "))
- --print(inline_parser:match(table.concat(tab, " ")))
+function rst_context.paragraph (data)
+ local str
+ if not data then
+ return ""
+ elseif type(data) == "table" then
+ str = inline_parser:match(table.concat(data, " "))
+ else
+ str = data
+ end
return string.format([[
\\startparagraph
@@ -840,7 +863,8 @@ function rst_context.simple_table(tab)
end
function rst_context.footnote(label, content)
- local tf = tracklists.footnotes
+ local tf = state.footnotes
+ rst_context.addsetups("footnotes")
if label:match("^%d+$") then -- all digits
tf.numbered[tonumber(label)] = content
elseif label == "#" then --autonumber
@@ -852,10 +876,49 @@ function rst_context.footnote(label, content)
local thelabel = label:match("^#(.+)$")
tf.autolabel[thelabel] = content
elseif label == "*" then
+ rst_context.addsetups("footnote_symbol")
tf.symbol[#tf.symbol+1] = content
end
return ""
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
return rst_context
diff --git a/rst_parser.lua b/rst_parser.lua
index 6430692..d4ad90b 100644
--- a/rst_parser.lua
+++ b/rst_parser.lua
@@ -48,22 +48,24 @@ local utf = unicode.utf8
local eol = P"\n"
-tracklists = {}
-tracklists.depth = 0
-tracklists.bullets = {} -- mapping bullet forms to depth
-tracklists.bullets.max = 0
-tracklists.lastbullet = ""
-tracklists.roman_cache = {} -- storing roman numerals that were already converted
-tracklists.currentindent = "" -- used in definition lists and elsewhere
-tracklists.currentwidth = 0 -- table layout
-tracklists.currentlayout = {} -- table layout
-
-tracklists.footnotes = {}
-tracklists.footnotes.autonumber = 0
-tracklists.footnotes.numbered = {}
-tracklists.footnotes.labeled = {}
-tracklists.footnotes.autolabel = {}
-tracklists.footnotes.symbol = {}
+state = {}
+state.depth = 0
+state.bullets = {} -- mapping bullet forms to depth
+state.bullets.max = 0
+state.lastbullet = ""
+state.roman_cache = {} -- storing roman numerals that were already converted
+state.currentindent = "" -- used in definition lists and elsewhere
+state.currentwidth = 0 -- table layout
+state.currentlayout = {} -- table layout
+
+state.footnotes = {}
+state.footnotes.autonumber = 0
+state.footnotes.numbered = {}
+state.footnotes.labeled = {}
+state.footnotes.autolabel = {}
+state.footnotes.symbol = {}
+
+state.addme = {}
local enclosed_mapping = {
["'"] = "'",
@@ -108,7 +110,7 @@ do
end
return false
end
- tracklists.conversion = con
+ state.conversion = con
local rnums = {
i = 1,
@@ -156,7 +158,7 @@ do
end
return value
end
- tracklists.roman_to_arab = roman_to_arab
+ state.roman_to_arab = roman_to_arab
local suc = function (str, old)
str, old = itemstripper:match(str), itemstripper:match(old)
@@ -176,7 +178,7 @@ do
end
- local trc = tracklists.roman_cache
+ local trc = state.roman_cache
n_str = trc[str] or nil
n_old = trc[old] or nil
if not n_str then
@@ -192,7 +194,7 @@ do
end
end
- tracklists.successor = suc
+ state.successor = suc
end
local parser = P{
@@ -212,8 +214,8 @@ local parser = P{
+ Cs(V"transition") --/ rst.escape
+ V"literal_block"
+ Cs(V"block_quote") / rst.escape
- + Cs(V"list") / rst.escape
+ V"explicit_markup"
+ + Cs(V"list") / rst.escape
+ Cs(V"paragraph") / rst.escape
,
@@ -245,6 +247,7 @@ local parser = P{
footnote = V"explicit_markup_start"
* V"footnote_marker"
* C(V"footnote_content")
+ * (V"blank_line" - V"end_block")^-1
--* C(V"block")
/ rst.footnote
,
@@ -253,30 +256,64 @@ local parser = P{
,
footnote_label = V"digit"^1
+ + (V"gartenzaun" * V"letter"^1)
+ V"gartenzaun"
- + V"gartenzaun" * V"letter"^1
+ V"asterisk"
,
- footnote_content = V"footnote_simple" -- single line
- + V"footnote_long"
+ --footnote_content = V"footnote_simple" -- single line
+ --+ V"footnote_long"
+
+ footnote_content = V"footnote_long" -- single line
+ + V"footnote_simple"
,
- footnote_simple = (1 - V"eol")^1 * V"eol",
+ footnote_simple = (1 - V"eol")^1 * V"eol"
+ ,
- footnote_long = V"eol"
+ footnote_long = (1 - V"eol")^1 * V"eol"
* V"footnote_body"
,
- footnote_body = V"comment"
- + V"line_block"
- + Cs(V"table_block") / rst.escape
- + Cs(V"transition") --/ rst.escape
- + Cs(V"block_quote") / rst.escape
- + Cs(V"list") / rst.escape
- + Cs(V"paragraph") / rst.escape
+ --footnote_body = V"fn_body_first"
+ --* (V"fn_body_other" * (V"blank_line" * V"fn_body_other")^0)^0
+ --,
+
+ footnote_body = V"fn_body_first"
+ * (V"fn_body_other" + V"fn_body_other_block")^0
,
+ fn_body_first = Cmt(V"space"^1, function(s, i, indent)
+ warn("fn-in", true, #indent)
+ state.currentindent = indent
+ return true
+ end)
+ * (1 - V"eol")^1 * V"eol"
+ ,
+
+ fn_matchindent = Cmt(V"space"^1, function(s, i, indent)
+ local tc = state.currentindent
+ warn("fn-ma", tc == indent, #tc, #indent, i)
+ return tc == indent
+ end)
+ ,
+
+ fn_body_other = V"fn_body_other_regular"
+ * (V"blank_line" * V"fn_body_other_regular")^0
+ ,
+
+ fn_body_other_regular = V"fn_matchindent"
+ * (1 - V"eol")^1 * V"eol"
+ ,
+
+ -- TODO find a way to get those to work in footnotes!
+ fn_body_other_block = V"line_block"
+ + V"table_block"
+ + V"transition"
+ + V"block_quote"
+ + V"list"
+ ,
+
--------------------------------------------------------------------------------
-- Table block
--------------------------------------------------------------------------------
@@ -306,19 +343,19 @@ local parser = P{
st_setindent = Cmt(V"space"^0, function(s, i, indent)
warn("sta-i", "true", #indent, "set", i)
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end)
,
st_matchindent = Cmt(V"space"^0, function(s, i, indent)
- warn("sta-m", tracklists.currentindent == indent, #indent, #tracklists.currentindent, i)
- return tracklists.currentindent == indent
+ warn("sta-m", state.currentindent == indent, #indent, #state.currentindent, i)
+ return state.currentindent == indent
end)
,
st_setlayout = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
- local tc = tracklists.currentlayout
+ local tc = state.currentlayout
warn("sta-l", #layout, "set", "", i)
tc.raw = layout
tc.bounds = help.get_st_boundaries(layout)
@@ -335,8 +372,8 @@ local parser = P{
-- Don't check for matching indent but if the rest is
-- fine then the line should be sane. This allows
-- cells starting with spaces.
- content = content:sub(#tracklists.currentindent)
- local tcb = tracklists.currentlayout.bounds
+ content = content:sub(#state.currentindent)
+ local tcb = state.currentlayout.bounds
local n = 1
local spaces_only = P" "^1
while n < #tcb.slices do
@@ -365,13 +402,13 @@ local parser = P{
,
st_normal_sep = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
- warn("sta-s", tracklists.currentlayout.raw == layout, #layout, #tracklists.currentlayout.raw, i)
- return tracklists.currentlayout.raw == layout
+ warn("sta-s", state.currentlayout.raw == layout, #layout, #state.currentlayout.raw, i)
+ return state.currentlayout.raw == layout
end)
,
st_colspan_sep = Cmt(V"dash"^1 * (V"spaces" * V"dash"^1)^0, function(s, i, layout)
- local tcb = tracklists.currentlayout.bounds
+ local tcb = state.currentlayout.bounds
local this = help.get_st_boundaries (layout)
local start_valid = false
for start, _ in next, this.starts do
@@ -384,12 +421,12 @@ local parser = P{
end
end
if not stop_valid then
- warn("sta-x", stop_valid, #layout, #tracklists.currentlayout.raw, i)
+ warn("sta-x", stop_valid, #layout, #state.currentlayout.raw, i)
return false
end
end
end
- warn("sta-x", start_valid, #layout, #tracklists.currentlayout.raw, i)
+ warn("sta-x", start_valid, #layout, #state.currentlayout.raw, i)
return start_valid
end)
,
@@ -414,7 +451,7 @@ local parser = P{
gt_setindent = Cmt(V"space"^0, function(s, i, indent)
warn("tab-i", true, #indent, "set", i)
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end)
,
@@ -423,7 +460,7 @@ local parser = P{
gt_sethorizontal = Cmt(V"gt_layoutmarkers"^3, function (s, i, width)
warn("tab-h", "width", "true", #width, "set", i)
- tracklists.currentwidth = #width
+ state.currentwidth = #width
return true
end)
,
@@ -433,7 +470,7 @@ local parser = P{
,
gt_matchindent = Cmt(V"space"^0, function (s, i, this)
- local matchme = tracklists.currentindent
+ local matchme = state.currentindent
warn("tab-m", "indent", #this == #matchme, #this, #matchme, i)
return #this == #matchme
end)
@@ -459,7 +496,7 @@ local parser = P{
gt_bodysep = V"gt_matchindent"
* C(Cmt(V"table_intersection"
* (V"table_hline"^1 * V"table_intersection")^1, function(s, i, separator)
- local matchme = tracklists.currentwidth
+ local matchme = state.currentwidth
warn("tab-m", "body", #separator == matchme, #separator, matchme, i)
return #separator == matchme
end))
@@ -473,7 +510,7 @@ local parser = P{
gt_headsep = V"gt_matchindent"
* C(Cmt(V"table_intersection"
* (V"table_header_hline"^1 * V"table_intersection")^1, function(s, i, separator)
- local matchme = tracklists.currentwidth
+ local matchme = state.currentwidth
warn("tab-s", "head", #separator == matchme, #separator, matchme, i)
return #separator == matchme
end))
@@ -496,7 +533,7 @@ local parser = P{
block_quote_first = Cmt(V"space"^1, function (s, i, indent)
warn("bkq-i", #indent, "", indent, "", i)
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end) / ""
* -V"attrib_dash"
@@ -505,9 +542,9 @@ local parser = P{
,
block_quote_other = Cmt(V"space"^1, function (s, i, indent)
- warn("bkq-m", #indent, #tracklists.currentindent,
- indent, tracklists.currentindent, i)
- return tracklists.currentindent == indent
+ warn("bkq-m", #indent, #state.currentindent,
+ indent, state.currentindent, i)
+ return state.currentindent == indent
end) / ""
* -V"attrib_dash"
* (1 - V"eol")^1
@@ -530,9 +567,9 @@ local parser = P{
,
block_quote_attri_other = Cmt(V"space"^1, function (s, i, indent)
- warn("bqa-m", #indent, utf.len(tracklists.currentindent),
- indent, tracklists.currentindent, i)
- return utf.len(tracklists.currentindent) == #indent
+ warn("bqa-m", #indent, utf.len(state.currentindent),
+ indent, state.currentindent, i)
+ return utf.len(state.currentindent) == #indent
end) / ""
* (1 - V"eol")^1
* V"eol"
@@ -556,23 +593,23 @@ local parser = P{
line_block_first = Cmt(V"line_block_marker", function(s, i, marker)
warn("lbk-i", #marker, "", marker, "", i)
- tracklists.currentindent = marker
+ state.currentindent = marker
return true
end) / ""
* V"line_block_line"
,
line_block_empty = Cmt(V"line_block_empty_marker", function(s, i, marker)
- warn("lbk-e", #marker, #tracklists.currentindent, marker, tracklists.currentindent, i)
+ warn("lbk-e", #marker, #state.currentindent, marker, state.currentindent, i)
marker = marker:gsub("|.*", "| ")
- return tracklists.currentindent == marker
+ return state.currentindent == marker
end) / ""
/ rst.line_block_empty
,
line_block_other = Cmt(V"line_block_marker", function(s, i, marker)
- warn("lbk-m", #marker, #tracklists.currentindent, marker, tracklists.currentindent, i)
- return tracklists.currentindent == marker
+ warn("lbk-m", #marker, #state.currentindent, marker, state.currentindent, i)
+ return state.currentindent == marker
end) / ""
* V"line_block_line"
,
@@ -585,8 +622,8 @@ local parser = P{
line_block_cont = (V"eol" - V"line_block_marker")
* Cmt(V"space"^1, function(s, i, spaces)
- warn("lbk-c", #spaces, #tracklists.currentindent, spaces, tracklists.currentindent, i)
- return #spaces >= #tracklists.currentindent
+ warn("lbk-c", #spaces, #state.currentindent, spaces, state.currentindent, i)
+ return #spaces >= #state.currentindent
end) / ""
* (1 - V"eol")^1
,
@@ -623,7 +660,7 @@ local parser = P{
indent == "" then
return false
end
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end)
* V"rest_of_line"
@@ -632,10 +669,10 @@ local parser = P{
literal_block_other = Cmt(V"space"^1, function (s, i, indent)
warn("lbk-m",
#indent,
- #tracklists.currentindent,
- #indent >= #tracklists.currentindent,
+ #state.currentindent,
+ #indent >= #state.currentindent,
i)
- return #indent >= #tracklists.currentindent
+ return #indent >= #state.currentindent
end)
* V"rest_of_line"
* V"eol",
@@ -646,7 +683,7 @@ local parser = P{
indent == "" then
return false
end
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end)
* V"rest_of_line"
@@ -655,10 +692,10 @@ local parser = P{
quoted_literal_block_other = Cmt(V"adornment_char", function (s, i, indent)
warn("lbk-m",
#indent,
- #tracklists.currentindent,
- #indent >= #tracklists.currentindent,
+ #state.currentindent,
+ #indent >= #state.currentindent,
i)
- return #indent >= #tracklists.currentindent
+ return #indent >= #state.currentindent
end)
* V"rest_of_line"
* V"eol",
@@ -753,7 +790,7 @@ local parser = P{
definition_list = Cs(V"definition_item"
* (V"blank_line" * V"definition_item")^0)
- * V"blank_line"
+ * V"end_block"
/ rst.deflist
,
@@ -779,8 +816,8 @@ local parser = P{
/ rst.deflist_def,
definition_indent = Cmt(V"space"^1, function(s, i, indent)
- warn("def-i", #indent, #tracklists.currentindent, indent == tracklists.currentindent, i)
- tracklists.currentindent = indent
+ warn("def-i", #indent, #state.currentindent, indent == state.currentindent, i)
+ state.currentindent = indent
return true
end),
@@ -803,8 +840,8 @@ local parser = P{
,
definition_match = Cmt(V"space"^1, function (s, i, this)
- warn("def-m", #this, #tracklists.currentindent, this == tracklists.currentindent, i)
- return this == tracklists.currentindent
+ warn("def-m", #this, #state.currentindent, this == state.currentindent, i)
+ return this == state.currentindent
end),
--------------------------------------------------------------------------------
@@ -1013,13 +1050,13 @@ local parser = P{
par_setindent = Cmt(V"space"^0, function (s, i, indent)
warn("par-i", #indent, "", "", i)
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end),
par_matchindent = Cmt(V"space"^0, function (s, i, indent)
- warn("par-m", tracklists.currentindent == indent, #indent, #tracklists.currentindent, i)
- return tracklists.currentindent == indent
+ warn("par-m", state.currentindent == indent, #indent, #state.currentindent, i)
+ return state.currentindent == indent
end),
link_standalone = C(V"uri")
@@ -1074,15 +1111,15 @@ local parser = P{
indent == "" then
return false
end
- tracklists.currentindent = indent
+ state.currentindent = indent
return true
end)
* (1 - V"eol")^1
* V"eol",
indented_other = Cmt(V"space"^1, function (s, i, indent)
- warn("idt-m", indent, tracklists.currentindent, indent == tracklists.currentindent, i)
- return indent == tracklists.currentindent
+ warn("idt-m", indent, state.currentindent, indent == state.currentindent, i)
+ return indent == state.currentindent
end)
* (1 - V"eol")^1
* V"eol",
@@ -1242,25 +1279,22 @@ local function save_file (name, data)
return 0
end
-local function addfootnotes ()
- local tf = tracklists.footnotes
- local fn = [[
-
+local function get_setups ()
+ local setups = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-% Footnotes %
+%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
+%{ Setups }%
+%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+\setupcolors[state=start]
+\setupinteraction[state=start,color=darkgreen,contrastcolor=darkgreen]
]]
- local buffer = [[
-\startbuffer[%s]
-%s
-\stopbuffer
-]]
-
- for nf, note in ipairs(tf.numbered) do
- fn = fn .. string.format(buffer, "__autonumber"..nf, note)
+ for item, _ in next, state.addme do
+ local f = optional_setups[item]
+ setups = setups .. f()
end
- return fn
+ return setups .. "\\starttext"
end
local function main()
@@ -1269,18 +1303,17 @@ local function main()
local processeddata = parser:match(testdata)
- local setups = ""
- setups = setups .. addfootnotes()
+ local setups = get_setups()
- processeddata = setups .. processeddata
+ processeddata = setups .. processeddata .. "\\stoptext"
if processeddata then
save_file(arg[2], processeddata)
else
return 1
end
- for i,j in next, tracklists.footnotes do
+ for i,j in next, state.footnotes do
if type(j) == "table" then
for k,l in next, j do
print(k,l)
@@ -1288,8 +1321,8 @@ local function main()
end
end
- --print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<")
- --print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<")
+ --print(">>>Last used char>: " ..state.lastbullet.." <<<<")
+ --print(">>>Max list nestin>: "..state.bullets.max .." <<<<")
--for i,j in next, rst.collected_references do
--print (string.format("== %7s => %s <=", i,j))