From 678cfc0650d01258fc594d995480e41623efc496 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Wed, 22 Sep 2010 23:56:16 +0200 Subject: selected text roles, including colors --- rst_context.lua | 43 ++++++++++++++++++++++++++++++++++++++++--- rst_helpers.lua | 10 ++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/rst_context.lua b/rst_context.lua index f41fd6a..9e69374 100644 --- a/rst_context.lua +++ b/rst_context.lua @@ -19,6 +19,8 @@ require "lpeg" help = require "rst_helpers" rst_directives = require "rst_directives" +local utf = unicode.utf8 + local dbg_write = help.dbg_writef 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 @@ -132,6 +134,36 @@ function rst_context.literal (str) --return [[\\starttyping ]] .. str .. [[\\stoptyping]] end +rst_context.roles = {} +rst_context.roles.emphasis = rst_context.emphasis +rst_context.roles.strong_emphasis = rst_context.strong_emphasis +rst_context.roles.literal = rst_context.literal +rst_context.roles.bold = function(str) + return [[{\\bf ]] .. str .. [[}]] +end + +rst_context.roles.ss = function(str) + return [[{\\ss ]] .. str .. [[}]] +end +rst_context.roles.sans_serif = rst_context.roles.ss + +rst_context.roles.uppercase = function(str) + return utf.upper(str) +end + +rst_context.roles.lowercase = function(str) + return utf.lower(str) +end + +rst_context.roles.color = function(color, str) + local p = helpers.patterns + local definition = color:match("^color_(.+)$") + if definition:match("^rgb_") then -- assume rgb + local rgb = p.rgbvalues:match(definition) + definition = string.format([[r=%s,g=%s,b=%s]], rgb[1], rgb[2], rgb[3]) + end + return string.format([[\\colored[%s]{%s}]], definition, str) +end function rst_context.interpreted_text (...) local tab = { ... } @@ -143,7 +175,11 @@ function rst_context.interpreted_text (...) role = "emphasis" end - return rst_context[role](str) + if role:match("^color_") then + return rst_context.roles.color(role, str) + end + + return rst_context.roles[role](str) end function rst_context.link_standalone (str) @@ -264,9 +300,9 @@ local inline_parser = P{ inline_do_elements = V"strong_emphasis" + V"substitution_reference" + V"anon_reference" + + V"inline_literal" + V"reference" + V"emphasis" - + V"inline_literal" + V"interpreted_text" + V"inline_internal_target" + V"link_standalone" @@ -320,6 +356,7 @@ local inline_parser = P{ double_bareia = V"bareia" * V"bareia", escaped_bareia = (Cs(V"backslash") / "" * V"bareia") + 1, colon = P":", + escaped_colon = (Cs(V"backslash") / "" * V"colon") + 1, semicolon = P";", underscore = P"_", double_underscore = V"underscore" * V"underscore", @@ -410,7 +447,7 @@ local inline_parser = P{ * C(V"role_marker"^-1) / rst_context.interpreted_text, - role_marker = V"colon" * (V"letter" + V"dash" + V"underscore" + V"dot")^1 * V"colon", + role_marker = V"colon" * (V"backslash" * V"colon" + V"letter" + V"digit" + V"dash" + V"underscore" + V"dot")^1 * V"colon", link_standalone = C(V"uri") / rst_context.link_standalone, diff --git a/rst_helpers.lua b/rst_helpers.lua index ade8b23..869b4d9 100644 --- a/rst_helpers.lua +++ b/rst_helpers.lua @@ -97,6 +97,16 @@ do local nocolon = (escaped_colon + (1 - colon))^1 p.colon_right = nocolon * colon p.colon_keyval = C(nocolon) * colon * p.space^1 * C((1 - (p.space^0 * P(-1)))^1) + + -- color expression matching for text roles + local digit = R"09" + local dot = P"." + local colvalue = digit * dot * digit^1 + + digit + + dot * digit^1 + local coldelim = P"_" + P"-" + p.rgbvalues = P"rgb_" + * Ct( C(colvalue) * coldelim * C(colvalue) * coldelim * C(colvalue) ) end function helpers.cell.create(raw, n_row, n_col, parent, variant) -- cgit v1.2.3