From f87e85ad1dd07bfb4f3c07f2571f5af6401733c9 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 6 Sep 2010 15:22:53 +0200 Subject: grid table prototype --- rst_context.lua | 41 +++++++++++++++++++ rst_parser.lua | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 1 deletion(-) diff --git a/rst_context.lua b/rst_context.lua index 6a0d2f3..7603509 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -458,4 +458,45 @@ function rst_context.block_quote (tab) ]] end +function rst_context.table (str) + return [[ +\\startlinecorrection +]] .. str .. [[ + +\\stoplinecorrection +]] +end + +function rst_context.grid_table (str) + return [[ +\\setupTABLE[c][first] [background=color, backgroundcolor=grey, style=\tt] +\\setupTABLE[c][each] [frame=off] +\\setupTABLE[r][each] [frame=off] +\\bTABLE[split=repeat,option=stretch] +\\bTABLEhead +\\bTR + \\bTH Option \\eTH + \\bTH Description \\eTH +\\eTR +\\eTABLEhead +\\bTABLEbody +]] .. str .. [[ + +\\eTABLEbody +\\eTABLE +]] +end + +function rst_context.table_row (tab) + local tmp = [[\\bTR]] + for n, cell in ipairs(tab) do + tmp = tmp .. [[\\bTC]] .. cell .. [[\\eTC]] .. "\n" + print (cell) + end + + tmp = tmp .. [[\\eTR +]] + return tmp +end + return rst_context diff --git a/rst_parser.lua b/rst_parser.lua index 4e767b5..6630767 100644 --- a/rst_parser.lua +++ b/rst_parser.lua @@ -54,6 +54,7 @@ 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.currentlayout = {} -- table layout local enclosed_mapping = { ["'"] = "'", @@ -197,6 +198,7 @@ local parser = P{ block = V"target_block" + V"comment" + V"line_block" + + Cs(V"table_block") / rst.escape + Cs(V"section") / rst.escape + Cs(V"transition") --/ rst.escape + V"literal_block" @@ -205,6 +207,121 @@ local parser = P{ + Cs(V"paragraph") / rst.escape , +-------------------------------------------------------------------------------- +-- Table block +-------------------------------------------------------------------------------- + + table_block = V"grid_table" + --+ V"simple_table" + / rst.table + , + + +-------------------------------------------------------------------------------- +-- Grid table +-------------------------------------------------------------------------------- + + grid_table = Cs(V"gt_first_row" + * V"gt_other_rows") + * V"blank_line"^1 + / rst.grid_table + , + + gt_first_row = V"gt_setindent" + * V"gt_setlayout" + * V"eol" + / "" + , + + --gt_setindent = Cg(V"space"^0, "tableindent"), + gt_setindent = Cmt(V"space"^0, function(s, i, indent) + warn("tab-i", true, #indent, "set", i) + tracklists.currentindent = indent + return true + end) + , + + gt_layoutmarkers = V"table_intersection" + V"table_hline" + V"table_header_hline", + + gt_setlayout = Cmt(V"gt_layoutmarkers"^3, function (s, i, layout) + --warn("tab-l", layout) + local l = {} + for n, dimension in ipairs(string.explode(layout, "++")) do + l[n] = #dimension + end + l["length"] = #layout + warn("tab-l", unpack(l)) + tracklists.currentlayout = l + return true + end) + , + + gt_other_rows = V"gt_head"^-1 + * V"gt_body" + , + + --gt_matchindent = Cmt(V"space"^0 * Cb"tableindent", function (s, i, this, matchme) + gt_matchindent = Cmt(V"space"^0, function (s, i, this) + local matchme = tracklists.currentindent + warn("tab-m", this == matchme, #this, #matchme, i) + return this == matchme + end) + , + + --gt_cell = Cmt(V"gt_content_cell" + V"gt_line_cell", function (s, i, cell) + --print("NEXT CELL:",cell) + --return true + --end), + + gt_cell = C(V"gt_content_cell" + V"gt_line_cell"), + + gt_content_cell = (V"table_vline" + V"table_intersection") + * C((1 - V"table_vline" - V"table_intersection" - V"eol")^1), + + gt_line_cell = V"table_intersection" + --* C((V"table_hline" - V"table_intersection")^1) + * C(V"table_hline"^1) + , + + gt_contentrow = Ct(V"gt_matchindent" + * V"gt_cell"^1 + * (V"table_vline" + V"table_intersection") + * V"eol" + ) + / rst.table_row + , + + gt_body = V"gt_row"^1, + + gt_row = (V"gt_contentrow" - V"gt_bodysep")^1 + * Cmt(V"gt_bodysep", function(s,i,str)print(str)return true end), + + gt_bodysep = V"gt_matchindent" + * Cmt(V"table_intersection" + * (V"table_hline"^1 * V"table_intersection")^1, function(s, i, separator) + warn("tab-s", #separator, #separator == tracklists.currentlayout.length, "",i) + local l = {} + for n, dimension in ipairs(string.explode(separator, "++")) do + l[n] = #dimension + end + l["length"] = #separator + --return l == tracklists.currentlayout + return l.length == tracklists.currentlayout.length + end) + * V"eol" + , + + gt_head = V"gt_contentrow"^1 + * Cmt(V"gt_headsep", function(s,i,str)print(str)return true end) + , + + gt_headsep = V"gt_matchindent" + * V"table_intersection" + * (V"table_header_hline"^1 * V"table_intersection")^1 + * V"eol" + , + + -------------------------------------------------------------------------------- -- Block quotes -------------------------------------------------------------------------------- @@ -997,9 +1114,14 @@ local parser = P{ angle_right = P">", enclosed_open = S[['"([{<]], enclosed_close = S[['")]}>]], + + table_intersection = P"+", + table_hline = V"dash", + table_vline = V"bar", + table_header_hline = P"=", } -f = io.open("blockquote.rst", "r") +f = io.open("table.rst", "r") testdata = f:read("*all") f:close() -- cgit v1.2.3