From 327c881beb87e751e38c17c06b5e0490d2299d80 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 3 Sep 2010 23:42:09 +0200 Subject: definition lists --- rst_context.lua | 35 ++++++++++++++++++++ rst_parser.lua | 99 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 121 insertions(+), 13 deletions(-) diff --git a/rst_context.lua b/rst_context.lua index 40dd719..e6c20ee 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -277,4 +277,39 @@ function rst_context.bullet_item (str) ]] end +-------------------------------------------------------------------------------- +-- Definition lists +-------------------------------------------------------------------------------- +-- TODO define proper setups (probably bnf-like and some narrower for def-paragraphs) + +function rst_context.deflist (str) + return [[ + +\\startdefinitionlist +]] .. str .. [[ + +\\stopdefinitionlist +]] +end + +function rst_context.deflist_item (str) + return [[\\definitionitem{%]] .. str .. [[}]] +end + +function rst_context.deflist_classifier (str) + return [[\\definitionclassifier{]] .. str .. [[}]] +end + +function rst_context.deflist_term (str) + return [[ + + \\definitionterm{]] .. str .. [[}]] +end + +function rst_context.deflist_def (str) + return [[ + + \\definitiondef{%]] .. str .. [[}]] +end + return rst_context diff --git a/rst_parser.lua b/rst_parser.lua index cc322de..5795ebd 100644 --- a/rst_parser.lua +++ b/rst_parser.lua @@ -15,7 +15,7 @@ require "lpeg" rst = require "rst_context" -local rst_debug = true +--local rst_debug = true local warn = function(str, ...) if not rst_debug then return false end @@ -35,6 +35,8 @@ local warn = function(str, ...) return 0 end +local debugme = function(x) print ("HERE >"..x.."<") return x end + local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match local utf = unicode.utf8 @@ -47,8 +49,9 @@ 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 -n = 0 +--n = 0 local enclosed_mapping = { ["'"] = "'", @@ -208,7 +211,74 @@ local parser = P{ -- Lists -------------------------------------------------------------------------------- - list = V"bullet_list", + --list = V"bullet_list" + --+ V"definition_list", + + list = V"definition_list" + + V"bullet_list", + +-------------------------------------------------------------------------------- +-- Definition lists +-------------------------------------------------------------------------------- + + definition_list = Cs(V"definition_item" + * (V"blank_line" * V"definition_item")^0) + * V"eol" + / rst.deflist + , + + definition_item = Cs(V"definition_term" + * V"definition_classifiers" + * V"eol" + * V"definition_def") + / rst.deflist_item, + + definition_term = Cs((1 - V"eol" - V"definition_classifier_separator")^1) + / rst.deflist_term, + + definition_classifier_separator = V"space" * V"colon" * V"space", + + definition_classifiers = V"definition_classifier"^0, + + definition_classifier = V"definition_classifier_separator" + * Cs((1 - V"eol" - V"definition_classifier_separator")^1) + / rst.deflist_classifier, + + definition_def = Cs(V"definition_firstpar" + * V"definition_par"^0) + / 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 + return true + end), + + definition_firstpar = Cs(V"definition_parinit" + * (V"definition_parline" - V"blank_line")^0) + / rst.paragraph, + + definition_par = V"blank_line"^0 + * Cs((V"definition_parline" + - V"blank_line")^1) + / rst.paragraph, + --* V"eol"^1), + + definition_parinit = V"definition_indent" + --* (R"az" + R"AZ" + S",.")^1 --testing + * (1 - V"eol")^1 + * V"eol" + , + + definition_parline = V"definition_match" + * (1 - V"eol")^1 + * V"eol" + , + + definition_match = Cmt(V"space"^1, function (s, i, this) + warn("def-m", #this, #tracklists.currentindent, this == tracklists.currentindent, i) + return this == tracklists.currentindent + end), -------------------------------------------------------------------------------- -- Bullet lists and enumerations @@ -229,7 +299,8 @@ local parser = P{ return true end), - bullet_stop = Cs(Cc("")) / rst.stopitemize, + --bullet_stop =V"blank_line" * Cs(Cc("")) / rst.stopitemize, + bullet_stop =V"endpar" * Cs(Cc("")) / rst.stopitemize, bullet_init = V"eol"^0 * V"bullet_first" * V"bullet_itemrest", @@ -289,14 +360,14 @@ local parser = P{ -- ^^^^^ -- otherwise returns the value of V"bullet_indent", not sure why … - bullet_continue = V"bullet_blank" + bullet_continue = V"blank_line" * V"bullet_cont" * V"bullet_itemrest", bullet_itemrest = Cs(V"bullet_rest" -- first line * ((V"bullet_match" * V"bullet_rest")^0 -- any successive lines --* (V"eol" - * (V"bullet_blank" + * (V"blank_line" * (V"bullet_match" * (V"bullet_rest" - V"bullet_indent"))^1)^0)) / rst.bullet_item, -- ^^^^^^^^^^^^^ @@ -304,8 +375,6 @@ local parser = P{ bullet_rest = Cs((1 - V"eol")^1 * V"eol"), -- rest of one line - bullet_blank = V"eol" + V"space"^1 * V"eol", - bullet_next = V"space"^1, bullet_match = #Cmt(V"bullet_next", function (s, i, this) local t = tracklists @@ -410,7 +479,7 @@ local parser = P{ -- Paragraphs * Inline Markup -------------------------------------------------------------------------------- - paragraph = -(V"doubledot" + V"double_underscore" + V"bullet_indent") + paragraph = -(V"doubledot" + V"double_underscore") -- + V"bullet_indent") * Cs((V"enclosed_inline" + V"inline_elements" + V"word" @@ -553,10 +622,14 @@ local parser = P{ whitespace = (P" " + Cs(P"\t") / " " + Cs(S"\v") / " "), --whitespace = (P" " + Cs(P"\t") / " " + Cs(S"\v\n") / " ") - V"endpar", spacing = V"whitespace"^1, + --blank_line = V"eol" + V"space"^0 * V"eol", + blank_line = V"space"^0 * V"eol", + eol = P"\n", eof = V"eol"^0 * -P(1), - endpar = V"eol" * (V"eol"^1 + V"eof"), + --endpar = V"eol" * (V"eol"^1 + V"eof"), + endpar = V"eol" * V"blank_line"^1, delimiters = P"‐" + P"‑" + P"‒" + P"–" + P"—" + V"space", @@ -573,13 +646,13 @@ local parser = P{ enclosed_close = S[['")]}>]], } -f = io.open("list.rst", "r") +f = io.open("deflist.rst", "r") testdata = f:read("*all") f:close() print(parser:match(testdata)) -print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<") -print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<") +--print(">>>Last used char>: " ..tracklists.lastbullet.." <<<<") +--print(">>>Max list nestin>: "..tracklists.bullets.max .." <<<<") --for i,j in next, rst.collected_references do --print (string.format("== %7s => %s <=", i,j)) -- cgit v1.2.3