From 4dc6e76f8cbd29c762d93b05a8b32541a16890da Mon Sep 17 00:00:00 2001
From: Philipp Gesang <pgesang@ix.urz.uni-heidelberg.de>
Date: Wed, 4 May 2011 15:48:19 +0200
Subject: =?UTF-8?q?improved=20(=E2=80=9Ctyp[e|ing]=E2=80=9D-safe)=20escape?=
 =?UTF-8?q?r?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 mod/tex/context/third/rst/rst_context.lua | 37 ++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 11 deletions(-)

(limited to 'mod/tex/context/third')

diff --git a/mod/tex/context/third/rst/rst_context.lua b/mod/tex/context/third/rst/rst_context.lua
index 8663039..846b507 100644
--- a/mod/tex/context/third/rst/rst_context.lua
+++ b/mod/tex/context/third/rst/rst_context.lua
@@ -325,18 +325,33 @@ function rst_context.substitution_reference (str, underscores)
     return sub .. [[{\\RSTsubstitution]] .. str:gsub("%s", "") .. "}"
 end
 
--- see catc-sym.tex
-local escape_me = {
-    ["&"]   = [[\letterampersand ]],
-    ["$"]   = [[\letterdollar ]],
-    ["#"]   = [[\letterhash ]],
-    ["^"]   = [[\letterhat ]],
-    ["_"]   = [[\letterunderscore ]],
-}
+do
+    -- see catc-sym.tex
+    local escape_me = {
+        ["&"]   = [[\letterampersand ]],
+        ["$"]   = [[\letterdollar ]],
+        ["#"]   = [[\letterhash ]],
+        ["^"]   = [[\letterhat ]],
+        ["_"]   = [[\letterunderscore ]],
+    }
+
+    local chars
+    for chr, repl in next, escape_me do
+        chars = chars and chars + (P(chr) / repl) or P(chr) / repl
+    end
+
+    local p_escape = P{
+        [1]      = Cs((V"skip" + chars + 1)^1),
+        skip1    = P"\\starttyping" * (1 - P"\\stoptyping")^1,
+        balanced = P"{" * (V"balanced" + (1 - P"}"))^0 * P"}",
+        skip2    = P"\\type" * V"balanced",
+        skip     = V"skip1" + V"skip2",
+    }
 
-function rst_context.escape (str)
-    str = str:gsub("\\(.)", "%1")           -- literalize escaped sequences
-    return str:gsub("[&#^_$]", escape_me)   -- escape chars with special meaning
+    function rst_context.escape (str)
+        str = str:gsub("\\(.)", "%1")
+        return p_escape:match(str)
+    end
 end
 
 function rst_context.joinindented (tab)
-- 
cgit v1.2.3