summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--rst_context.lua59
-rw-r--r--rst_parser.lua130
2 files changed, 175 insertions, 14 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 96e7f89..bc96a9a 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -48,6 +48,23 @@ rst_context.last_section_level = 0
rst_context.anonymous_links = 0
rst_context.context_references = {}
+rst_context.current_footnote_number = 0
+
+function rst_context.footnote_reference (label)
+ local tf = tracklists.footnotes
+ if label:match("^%d+$") then -- all digits
+ -- TODO
+ 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.."]}"
+ elseif label:match("^#.+$") then
+ -- TODO
+ elseif label == "*" then
+ -- TODO
+ end
+end
+
-- So we can use crefs[n][2] to refer to the place where the reference was
-- created.
local function get_context_reference (str)
@@ -153,14 +170,13 @@ local inline_parser = P{
block = Cs((V"inline_element" + 1)^1),
- inline_element = Cs(V"precede_inline"
- * (V"strong_emphasis"
+ inline_element = Cs((V"strong_emphasis"
+ V"emphasis"
+ V"inline_literal"
+ V"interpreted_text"
-- + V"inline_internal_target" -- TODO
+ V"reference"
--- + V"footnote_reference" -- TODO
+ + V"footnote_reference" -- TODO
-- + V"substitution_reference" -- TODO
+ V"link_standalone")
* V"succede_inline"),
@@ -196,7 +212,6 @@ local inline_parser = P{
triple_dash = V"double_dash" * V"dash",
hyphen = P"‐",
dashes = V"dash" + P"‒" + P"–" + V"emdash" + P"―",
- letter = R"az" + R"AZ",
groupchars = S"()[]{}",
apostrophe = P"’" + P"'",
@@ -205,6 +220,13 @@ local inline_parser = P{
solidus= P"⁄",
slash = P"/",
+ gartenzaun = P"#",
+ lsquare = P"[",
+ rsquare = P"]",
+
+ digit = R"09",
+ letter = R"az" + R"AZ",
+
punctuation = V"apostrophe"
+ V"colon"
+ V"comma"
@@ -277,6 +299,16 @@ local inline_parser = P{
_reference = (1 - V"underscore" - V"spacing" - V"eol" - V"punctuation" - V"groupchars")^1 * V"underscore",
+ footnote_reference = V"lsquare" * Cs(V"footnote_label") * V"rsquare" * V"underscore"
+ / rst_context.footnote_reference
+ ,
+
+ footnote_label = V"digit"^1
+ + V"gartenzaun"
+ + V"gartenzaun" * V"letter"^1
+ + V"asterisk"
+ ,
+
--------------------------------------------------------------------------------
-- Urls
--------------------------------------------------------------------------------
@@ -807,4 +839,23 @@ function rst_context.simple_table(tab)
return head .. body .. tail
end
+function rst_context.footnote(label, content)
+ local tf = tracklists.footnotes
+ if label:match("^%d+$") then -- all digits
+ tf.numbered[tonumber(label)] = content
+ elseif label == "#" then --autonumber
+ repeat -- until next unrequested number
+ tf.autonumber = tf.autonumber + 1
+ until tf.numbered[tf.autonumber] == nil
+ tf.numbered[tf.autonumber] = content
+ elseif label:match("^#.+$") then
+ local thelabel = label:match("^#(.+)$")
+ tf.autolabel[thelabel] = content
+ elseif label == "*" then
+ tf.symbol[#tf.symbol+1] = content
+ end
+ return ""
+end
+
+
return rst_context
diff --git a/rst_parser.lua b/rst_parser.lua
index ddd13a8..6430692 100644
--- a/rst_parser.lua
+++ b/rst_parser.lua
@@ -48,7 +48,7 @@ local utf = unicode.utf8
local eol = P"\n"
-local tracklists = {}
+tracklists = {}
tracklists.depth = 0
tracklists.bullets = {} -- mapping bullet forms to depth
tracklists.bullets.max = 0
@@ -58,6 +58,13 @@ 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 = {}
+
local enclosed_mapping = {
["'"] = "'",
['"'] = '"',
@@ -206,10 +213,71 @@ local parser = P{
+ V"literal_block"
+ Cs(V"block_quote") / rst.escape
+ Cs(V"list") / rst.escape
+ + V"explicit_markup"
+ Cs(V"paragraph") / rst.escape
,
--------------------------------------------------------------------------------
+-- Explicit markup block
+--------------------------------------------------------------------------------
+
+ --explicit_markup = V"double_dot" * V"whitespace"
+ --* V"explicit_body"
+ --* V"whitespace"
+ --,
+
+ explicit_markup_start = V"double_dot" * V"whitespace",
+
+ explicit_markup = V"footnote_block"
+ --+ V"citation" -- = \footcite
+ --+ V"hyperlink_target"
+ --+ V"directive"
+ --+ V"substitution_definition"
+ --+ V"comment block"
+ ,
+
+--------------------------------------------------------------------------------
+-- Explicit markup footnote block
+--------------------------------------------------------------------------------
+
+ footnote_block = V"footnote"^1 * V"end_block",
+
+ footnote = V"explicit_markup_start"
+ * V"footnote_marker"
+ * C(V"footnote_content")
+ --* C(V"block")
+ / rst.footnote
+ ,
+
+ footnote_marker = V"lsquare" * C(V"footnote_label") * V"rsquare" * V"whitespace"^0
+ ,
+
+ footnote_label = V"digit"^1
+ + V"gartenzaun"
+ + V"gartenzaun" * V"letter"^1
+ + V"asterisk"
+ ,
+
+ footnote_content = V"footnote_simple" -- single line
+ + V"footnote_long"
+ ,
+
+ footnote_simple = (1 - V"eol")^1 * V"eol",
+
+ footnote_long = 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
+ ,
+
+--------------------------------------------------------------------------------
-- Table block
--------------------------------------------------------------------------------
@@ -224,7 +292,7 @@ local parser = P{
simple_table = Ct(V"st_first_row"
* V"st_other_rows")
--* V"blank_line"^1
- * V"end_block"^1
+ * V"end_block"
/ function (tab)
return rst.simple_table(helpers.table.simple(tab))
end
@@ -940,8 +1008,7 @@ local parser = P{
paragraph = V"par_setindent"
* Ct(C((1 - V"eol")^1) * V"eol"
* (V"par_matchindent" * C((1 - V"eol")^1) * V"eol")^0)
- --* V"blank_line"^1
- * V"end_block"^1
+ * V"end_block"
/ rst.paragraph,
par_setindent = Cmt(V"space"^0, function (s, i, indent)
@@ -980,16 +1047,17 @@ local parser = P{
-- Comments
--------------------------------------------------------------------------------
- comment = V"block_comment" + V"line_comment",
+ comment = V"double_dot" * V"whitespace"^0
+ * ((V"block_comment" + V"line_comment") - V"footnote_marker")
+ ,
- block_comment = V"double_dot" * V"whitespace"^0 * V"eol"
+ block_comment = V"eol"
* Cs(V"indented_lines")
* V"eol"^0
--* V"blank_line"^0
/ rst.block_comment,
- line_comment = V"double_dot" * V"whitespace"^0
- * Cs((1 - V"eol")^0 * V"eol")
+ line_comment = Cs((1 - V"eol")^0 * V"eol")
/ rst.line_comment,
--------------------------------------------------------------------------------
@@ -1080,6 +1148,8 @@ local parser = P{
apostrophe = P"’" + P"'",
--brackets = P"[ ], (",, { }, ⟨ ⟩ )
+ lsquare = P"[",
+ rsquare = P"]",
ellipsis = P"…" + P"...",
guillemets = P"«" + P"»",
quotationmarks= P"‘" + P"’" + P"“" + P"”",
@@ -1125,9 +1195,12 @@ local parser = P{
eol = P"\n",
eof = V"eol"^0 * -P(1),
+
end_block = V"blank_line"^1
- + (V"whitespace"^0 * V"eol"
- * (V"whitespace"^0 * V"eol")^0 * V"eof"),
+ + V"eof"
+ + (V"whitespace"^0 * V"eol"
+ * (V"whitespace"^0 * V"eol")^0 * V"eof")
+ ,
-- diverse markup character sets
delimiters = P"‐" + P"‑" + P"‒" + P"–" + V"emdash" + V"space", -- inline markup
@@ -1145,6 +1218,8 @@ local parser = P{
enclosed_open = S[['"([{<]],
enclosed_close = S[['")]}>]],
+ gartenzaun = P"#",
+
table_intersection = P"+",
table_hline = V"dash",
table_vline = V"bar",
@@ -1167,18 +1242,52 @@ local function save_file (name, data)
return 0
end
+local function addfootnotes ()
+ local tf = tracklists.footnotes
+ local fn = [[
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Footnotes %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+]]
+ local buffer = [[
+
+\startbuffer[%s]
+%s
+\stopbuffer
+]]
+
+ for nf, note in ipairs(tf.numbered) do
+ fn = fn .. string.format(buffer, "__autonumber"..nf, note)
+ end
+ return fn
+end
+
local function main()
local testdata = load_file(arg[1])
if testdata == 1 then return 1 end
local processeddata = parser:match(testdata)
+ local setups = ""
+
+ setups = setups .. addfootnotes()
+
+ processeddata = setups .. processeddata
if processeddata then
save_file(arg[2], processeddata)
else
return 1
end
+ for i,j in next, tracklists.footnotes do
+ if type(j) == "table" then
+ for k,l in next, j do
+ print(k,l)
+ end
+ end
+ end
+
--print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<")
--print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<")
@@ -1189,4 +1298,5 @@ local function main()
return 0
end
+
return main()