hgbook
diff en/examples/run-example @ 77:773f4a9e7975
Fix escaping of backslashes. Finally!
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Mon Sep 04 11:57:31 2006 -0700 (2006-09-04) |
parents | 2bfa2499e971 |
children | a893de25bc24 |
line diff
1.1 --- a/en/examples/run-example Wed Aug 30 00:11:17 2006 -0700 1.2 +++ b/en/examples/run-example Mon Sep 04 11:57:31 2006 -0700 1.3 @@ -16,14 +16,24 @@ 1.4 import tempfile 1.5 import time 1.6 1.7 +tex_subs = { 1.8 + '\\': '\\textbackslash{}', 1.9 + '{': '\\{', 1.10 + '}': '\\}', 1.11 + } 1.12 + 1.13 +def gensubs(s): 1.14 + start = 0 1.15 + for i, c in enumerate(s): 1.16 + sub = tex_subs.get(c) 1.17 + if sub: 1.18 + yield s[start:i] 1.19 + start = i + 1 1.20 + yield sub 1.21 + yield s[start:] 1.22 + 1.23 def tex_escape(s): 1.24 - if '\\' in s: 1.25 - s = s.replace('\\', '\\\\') 1.26 - if '{' in s: 1.27 - s = s.replace('{', '\\{') 1.28 - if '}' in s: 1.29 - s = s.replace('}', '\\}') 1.30 - return s 1.31 + return ''.join(gensubs(s)) 1.32 1.33 class example: 1.34 shell = '/usr/bin/env bash'