summaryrefslogtreecommitdiff
path: root/rst_context.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-09 00:58:24 +0200
committerPhilipp Gesang <pgesang@ix.urz.uni-heidelberg.de>2010-09-09 00:58:24 +0200
commitd0253ecefdd3acac717e6215d46b17977ac28319 (patch)
tree236f5f6c75033d87707d456250cda6ce5f58777b /rst_context.lua
parent988814b6f26f4fbf54b70c66624cddece04d27bb (diff)
downloadcontext-rst-d0253ecefdd3acac717e6215d46b17977ac28319.tar.gz
moved inline elements formatting to formatter
Diffstat (limited to 'rst_context.lua')
-rw-r--r--rst_context.lua185
1 files changed, 165 insertions, 20 deletions
diff --git a/rst_context.lua b/rst_context.lua
index 13e4522..033c252 100644
--- a/rst_context.lua
+++ b/rst_context.lua
@@ -13,6 +13,8 @@
require "lpeg"
+require "rst_helpers"
+
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
if not context then -- standard context lpeg stripper from l-string.lua
@@ -27,8 +29,9 @@ if not context then -- standard context lpeg stripper from l-string.lua
end
end
-
local rst_context = {}
+
+
rst_context.collected_references = {}
rst_context.collected_adornments = {}
rst_context.last_section_level = 0
@@ -52,17 +55,6 @@ function rst_context.strong_emphasis (str)
return [[{\\sc ]] .. str .. [[}]]
end
-function rst_context.paragraph (str)
- -- 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)
str = str:gsub([[\]], [[\\]]) -- evade escaping of backslashes
return [[\\type{]] .. str .. [[}]]
@@ -145,6 +137,159 @@ function rst_context.joinindented (tab)
return table.concat (tab, "")
end
+local inline_parser = P{
+ [1] = "block",
+
+ block = Cs((V"inline_element" + 1)^1),
+
+
+ inline_element = Cs(V"precede_inline"
+ * (V"strong_emphasis"
+ + V"emphasis"
+ + V"inline_literal"
+ + V"interpreted_text"
+-- + V"inline_internal_target" -- TODO
+ + V"reference"
+-- + V"footnote_reference" -- TODO
+-- + V"substitution_reference" -- TODO
+ + V"link_standalone")
+ * V"succede_inline"),
+
+ space = P" ",
+ whitespace = (P" " + Cs(P"\t") / " " + Cs(S"\v") / " "),
+ spacing = V"whitespace"^1,
+
+ eol = P"\n",
+ inline_delimiters = P"‐" + P"‑" + P"‒" + P"–" + V"emdash" + V"space", -- inline markup
+ --inline_delimiter = P"**" + P"``" + S"*`",
+ asterisk = P"*",
+ double_asterisk = V"asterisk" * V"asterisk",
+ bareia = P"`",
+ backslash = P"\\",
+ bar = P"|",
+ double_bareia = V"bareia" * V"bareia",
+ escaped_bareia = (Cs(V"backslash") / "" * V"bareia") + 1,
+ colon = P":",
+ semicolon = P";",
+ underscore = P"_",
+ double_underscore = V"underscore" * V"underscore",
+ dot = P".",
+ interpunct = P"·",
+ comma = P",",
+ dash = P"-",
+ emdash = P"—",
+ ellipsis = P"…" + P"...",
+ exclamationmark = P"!",
+ questionmark = P"?",
+ interrobang = P"‽",
+ double_dash = V"dash" * V"dash",
+ 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"'",
+
+ guillemets = P"«" + P"»",
+ quotationmarks= P"‘" + P"’" + P"“" + P"”",
+ solidus= P"⁄",
+ slash = P"/",
+
+ punctuation = V"apostrophe"
+ + V"colon"
+ + V"comma"
+ + V"dashes"
+ + V"dot"
+ + V"ellipsis"
+ + V"exclamationmark"
+ + V"guillemets"
+ + V"hyphen"
+ + V"interpunct"
+ + V"interrobang"
+ + V"questionmark"
+ + V"quotationmarks"
+ + V"semicolon"
+ + V"slash"
+ + V"solidus"
+ + V"underscore"
+ ,
+
+ precede_inline = V"spacing"
+ + V"eol"
+ + S[['"([{<-/:]]
+ + P"‘" + P"“" + P"’" + P"«" + P"¡" + P"¿"
+ + V"inline_delimiters"
+ + P"„", -- not in standard Murkin reST
+
+ succede_inline = V"spacing"
+ + S[['")]}>-/:.,;!?\]]
+ + P"’" + P"”" + P"»"
+ + V"inline_delimiters"
+ + P"“", -- non-standard again but who cares
+
+ emphasis = (V"asterisk" - V"double_asterisk")
+ * Cs((1 - V"spacing" - V"eol" - V"asterisk")
+ * ((1 - (1 * V"asterisk"))^0
+ * (1 - V"spacing" - V"eol" - V"asterisk"))^-1)
+ * V"asterisk"
+ / rst_context.emphasis,
+
+ strong_emphasis = V"double_asterisk"
+ * Cs((1 - V"spacing" - V"eol" - V"asterisk")
+ * ((1 - (1 * V"double_asterisk"))^0
+ * (1 - V"spacing" - V"eol" - V"asterisk"))^-1)
+ * V"double_asterisk"
+ / rst_context.strong_emphasis,
+
+ inline_literal = V"double_bareia"
+ * C ((V"escaped_bareia" - V"spacing" - V"eol" - V"bareia")
+ * ((V"escaped_bareia" - (1 * V"double_bareia"))^0
+ * (V"escaped_bareia" - V"spacing" - V"eol" - V"bareia"))^-1)
+ * V"double_bareia"
+ / rst_context.literal,
+
+ interpreted_text = C(V"role_marker"^-1)
+ * (V"bareia" - V"double_bareia")
+ * C ((1 - V"spacing" - V"eol" - V"bareia")
+ * ((1 - (1 * V"bareia"))^0
+ * (1 - V"spacing" - V"eol" - V"bareia"))^-1)
+ * V"bareia"
+ * C(V"role_marker"^-1)
+ / rst_context.interpreted_text,
+
+ role_marker = V"colon" * (V"letter" + V"dash" + V"underscore" + V"dot")^1 * V"colon",
+
+ link_standalone = C(V"uri")
+ / rst_context.link_standalone,
+
+ reference = Cs(V"_reference")
+ / rst_context.reference,
+
+ _reference = (1 - V"underscore" - V"spacing" - V"eol" - V"punctuation" - V"groupchars")^1 * V"underscore",
+
+--------------------------------------------------------------------------------
+-- Urls
+--------------------------------------------------------------------------------
+ uri = V"url_protocol" * V"url_domain" * (V"slash" * V"url_path")^0,
+
+ url_protocol = (P"http" + P"ftp" + P"shttp" + P"sftp") * P"://",
+ url_domain_char = 1 - V"dot" - V"spacing" - V"eol" - V"punctuation",
+ url_domain = V"url_domain_char"^1 * (V"dot" * V"url_domain_char"^1)^0,
+ url_path_char = R("az", "AZ", "09") + S"-_.!~*'()",
+ url_path = V"slash" * (V"url_path_char"^1 * V"slash"^-1)^1,
+}
+
+function rst_context.paragraph (tab)
+ local str = inline_parser:match(table.concat(tab, " "))
+ print(inline_parser:match(table.concat(tab, " ")))
+ return string.format([[
+
+\\startparagraph
+%s
+\\stopparagraph
+]], str)
+end
+
local sectionlevels = {
[1] = "chapter",
[2] = "section",
@@ -350,7 +495,7 @@ function rst_context.field (tab)
\\fieldname{%s}
\\fieldbody{%s}
\\stopfield
-]], name, body)
+]], name, inline_parser:match(body))
end
function rst_context.line_comment (str)
@@ -379,7 +524,7 @@ function rst_context.option_list (str)
\\eTR
\\eTABLEhead
\\bTABLEbody
-]] .. str .. [[
+]] .. inline_parser:match(str) .. [[
\\eTABLEbody
\\eTABLE
@@ -422,7 +567,7 @@ function rst_context.line_block (str)
return [[
\\startlines
-]] .. str .. [[\\stoplines
+]] .. inline_parser:match(str) .. [[\\stoplines
]]
end
@@ -443,7 +588,7 @@ function rst_context.block_quote (tab)
\\startlinecorrection
\\startblockquote
-]] .. tab[1] .. [[
+]] .. inline_parser:match(tab[1]) .. [[
\\stopblockquote
]]
@@ -498,7 +643,7 @@ function rst_context.grid_table (tab)
\\eTABLEbody
\\eTABLE
]]
- local test = ""
+ local body = ""
for i,r in ipairs(tab.rows) do
local isempty = true
for n, cell in ipairs(r) do
@@ -513,7 +658,7 @@ function rst_context.grid_table (tab)
for n,c in ipairs(r) do
if not (c.parent or
c.variant == "separator") then
- local celltext = c.stripped
+ local celltext = inline_parser:match(c.stripped)
if c.span.x or c.span.y then
local span_exp = "["
if c.span.x then
@@ -529,10 +674,10 @@ function rst_context.grid_table (tab)
row = row .. "\n " .. [[\\bTC ]] .. celltext .. [[\\eTC]]
end
end
- test = test .. row .. "\n" .. [[\\eTR]] .. "\n"
+ body = body .. row .. "\n" .. [[\\eTR]] .. "\n"
end
end
- return head .. test .. tail
+ return head .. body .. tail
end
function rst_context.table_row (tab)