Mercurial > hgbook
comparison en/examples/run-example @ 77:773f4a9e7975
Fix escaping of backslashes. Finally!
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Mon, 04 Sep 2006 11:57:31 -0700 |
parents | 2bfa2499e971 |
children | a893de25bc24 |
comparison
equal
deleted
inserted
replaced
76:df88df78288d | 77:773f4a9e7975 |
---|---|
14 import stat | 14 import stat |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
17 import time | 17 import time |
18 | 18 |
19 tex_subs = { | |
20 '\\': '\\textbackslash{}', | |
21 '{': '\\{', | |
22 '}': '\\}', | |
23 } | |
24 | |
25 def gensubs(s): | |
26 start = 0 | |
27 for i, c in enumerate(s): | |
28 sub = tex_subs.get(c) | |
29 if sub: | |
30 yield s[start:i] | |
31 start = i + 1 | |
32 yield sub | |
33 yield s[start:] | |
34 | |
19 def tex_escape(s): | 35 def tex_escape(s): |
20 if '\\' in s: | 36 return ''.join(gensubs(s)) |
21 s = s.replace('\\', '\\\\') | |
22 if '{' in s: | |
23 s = s.replace('{', '\\{') | |
24 if '}' in s: | |
25 s = s.replace('}', '\\}') | |
26 return s | |
27 | 37 |
28 class example: | 38 class example: |
29 shell = '/usr/bin/env bash' | 39 shell = '/usr/bin/env bash' |
30 prompt = '__run_example_prompt__ ' | 40 prompt = '__run_example_prompt__ ' |
31 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$') | 41 pi_re = re.compile(r'#\$\s*(name):\s*(.*)$') |