Mercurial > jouvence
comparison fontaine/renderer.py @ 1:74b83e3d921e
Add more states, add more tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 02 Jan 2017 21:54:59 -0800 |
parents | |
children | 6019eee799bf |
comparison
equal
deleted
inserted
replaced
0:243401c49520 | 1:74b83e3d921e |
---|---|
1 import re | |
2 | |
3 | |
4 RE_ITALICS = re.compile( | |
5 r"(?P<before>^|\s)(?P<esc>\\)?\*(?P<text>.*[^\s\*])\*(?=[^a-zA-Z0-9\*]|$)") | |
6 RE_BOLD = re.compile( | |
7 r"(?P<before>^|\s)(?P<esc>\\)?\*\*(?P<text>.*[^\s])\*\*(?=[^a-zA-Z0-9]|$)") | |
8 RE_UNDERLINE = re.compile( | |
9 r"(?P<before>^|\s)(?P<esc>\\)?_(?P<text>.*[^\s])_(?=[^a-zA-Z0-9]|$)") | |
10 | |
11 | |
12 class BaseRenderer: | |
13 def render_text(self, text): | |
14 # Replace bold stuff to catch double asterisks. | |
15 text = RE_BOLD.sub(self._do_write_bold, text) | |
16 text = RE_ITALICS.sub(self._do_write_italics, text) | |
17 text = RE_UNDERLINE.sub(self._do_write_underline, text) | |
18 | |
19 return text | |
20 | |
21 def _do_write_italics(self, m): | |
22 if m.group('esc'): | |
23 return m.group('before') + '*' + m.group('text') + '*' | |
24 return ( | |
25 m.group('before') + | |
26 self.write_italics(m.group('text'))) | |
27 | |
28 def _do_write_bold(self, m): | |
29 if m.group('esc'): | |
30 return m.group('before') + '**' + m.group('text') + '**' | |
31 return ( | |
32 m.group('before') + | |
33 self.write_bold(m.group('text'))) | |
34 | |
35 def _do_write_underline(self, m): | |
36 if m.group('esc'): | |
37 return m.group('before') + '_' + m.group('text') + '_' | |
38 return ( | |
39 m.group('before') + | |
40 self.write_underline(m.group('text'))) | |
41 | |
42 def write_italics(self, text): | |
43 raise NotImplementedError() | |
44 | |
45 def write_bold(self, text): | |
46 raise NotImplementedError() | |
47 | |
48 def write_underline(self, text): | |
49 raise NotImplementedError() |