diff options
-rw-r--r-- | rst_context.lua | 39 | ||||
-rw-r--r-- | rst_parser.lua | 55 |
2 files changed, 89 insertions, 5 deletions
diff --git a/rst_context.lua b/rst_context.lua index 5ca5573..7d0ef28 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -53,7 +53,14 @@ function rst_context.strong_emphasis (str) end function rst_context.paragraph (str) - return "\n" .. [[\\startparagraph]] .. "\n" .. str .. "\n".. [[\\stopparagraph]] .. "\n" + -- ugly as hell and probably slow too, but the alternative would be lots of + -- concatenation + return string.format([[ + +\\startparagraph +%s +\\stopparagraph +]], str) end function rst_context.literal (str) @@ -312,4 +319,34 @@ function rst_context.deflist_def (str) \\definitiondef{%]] .. str .. [[}]] end +-------------------------------------------------------------------------------- +-- Field lists +-------------------------------------------------------------------------------- + +function rst_context.field_list (str) + return [[ + +\\startfieldlist]] .. str .. [[\\stopfieldlist +]] +end + +function rst_context.field_name (str) + return [[\\fieldname{]] .. str .. [[}]] +end + +function rst_context.field_body (str) + return [[\\fieldbody{]] .. str .. [[}]] +end + +function rst_context.field (tab) + local name, body = tab[1], tab[2] + return string.format([[ + + \\startfield + \\fieldname{%s} + \\fieldbody{%s} + \\stopfield +]], name, body) +end + return rst_context diff --git a/rst_parser.lua b/rst_parser.lua index a88a263..5b6a1e9 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 @@ -49,7 +49,7 @@ 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 +tracklists.currentindent = "" -- used in definition lists and elsewhere --n = 0 @@ -207,7 +207,53 @@ local parser = P{ --+ V"definition_list", list = V"definition_list" - + V"bullet_list", + + V"bullet_list" + + V"field_list", + +-------------------------------------------------------------------------------- +-- Field lists (for bibliographies etc.) +-------------------------------------------------------------------------------- + + field_list = Cs(V"field"^1) + * V"blank_line"^1 + / rst.field_list, + + field = Ct(V"field_marker" + * V"whitespace" + * V"field_body") + / rst.field, + + field_marker = V"colon" + * C(V"field_name") + * V"colon", + + field_name = (V"escaped_colon" + (1 - V"colon"))^1, + + field_body = C(V"rest_of_line" * V"eol" + * V"indented_lines"^-1), + + indented_lines = V"indented_first" + * (V"indented_other" + - V"field_marker")^0, + + indented_first = Cmt(V"space"^1, function (s, i, indent) + warn("idt-f", indent, i) + if not indent or + indent == "" then + return false + end + tracklists.currentindent = indent + return true + end) + * V"rest_of_line" + * 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 + end) + * V"rest_of_line" + * V"eol", -------------------------------------------------------------------------------- -- Definition lists @@ -611,6 +657,7 @@ local parser = P{ spacing = V"whitespace"^1, blank_line = V"space"^0 * V"eol", + rest_of_line = (1 - V"eol")^1, eol = P"\n", eof = V"eol"^0 * -P(1), @@ -631,7 +678,7 @@ local parser = P{ enclosed_close = S[['")]}>]], } -f = io.open("deflist.rst", "r") +f = io.open("fieldlist.rst", "r") testdata = f:read("*all") f:close() |