From 0e03fb445570cd787a61edc2050a46d96b4be491 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 13 Sep 2010 02:35:02 +0200 Subject: numbered footnote stub --- rst_parser.lua | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 120 insertions(+), 10 deletions(-) (limited to 'rst_parser.lua') 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,9 +213,70 @@ 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() -- cgit v1.2.3