changeset 77:773f4a9e7975

Fix escaping of backslashes. Finally!
author Bryan O'Sullivan <bos@serpentine.com>
date Mon, 04 Sep 2006 11:57:31 -0700
parents df88df78288d
children a893de25bc24
files en/examples/run-example
diffstat 1 files changed, 17 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/en/examples/run-example	Mon Sep 04 10:58:09 2006 -0700
+++ b/en/examples/run-example	Mon Sep 04 11:57:31 2006 -0700
@@ -16,14 +16,24 @@
 import tempfile
 import time
 
+tex_subs = {
+    '\\': '\\textbackslash{}',
+    '{': '\\{',
+    '}': '\\}',
+    }
+
+def gensubs(s):
+    start = 0
+    for i, c in enumerate(s):
+        sub = tex_subs.get(c)
+        if sub:
+            yield s[start:i]
+            start = i + 1
+            yield sub
+    yield s[start:]
+
 def tex_escape(s):
-    if '\\' in s:
-        s = s.replace('\\', '\\\\')
-    if '{' in s:
-        s = s.replace('{', '\\{')
-    if '}' in s:
-        s = s.replace('}', '\\}')
-    return s
+    return ''.join(gensubs(s))
         
 class example:
     shell = '/usr/bin/env bash'