pylit 0.7.10
[pylit.git] / test / regexp-test.py
blob15850114cfea1a259237dcd3d1d0d604a4fda507
1 import re
3 literal = """\
4 ::
5 ::
6 t ::
7 text::
8 indented::
9 indented ::
10 more text ::
11 indented text ::
12 . no-directive::
13 a .. directive:: somewhere::
14 """
16 directive = """\
17 .. code-block:: python
18 .. code-block:: python
19 .. code-block:: python listings
20 .. code-block:: python listings
21 """
23 misses = """\
24 .. comment string ::
25 .. ::
26 text:
27 """
29 def get_regexp(marker):
30 class self: pass # dummy to make the definitions compatible to pylit
31 if marker == '::':
32 self.marker_regexp = re.compile('^( *(?!\.\.).*)(%s)([ \n]*)$'
33 % marker)
34 else:
35 # assume code_block_marker is a directive like '.. code-block::'
36 self.marker_regexp = re.compile('^( *)(%s)(.*\n?)$' % marker)
37 return self.marker_regexp
39 for marker in ('::', '.. code-block::'):
40 print 'regexp test for %r' % marker
41 regexp = get_regexp(marker)
42 for sample in (literal + directive + misses).splitlines(True):
43 match = regexp.search(sample)
44 print '%-40r'%(sample),
45 if match:
46 print '-> ', match.groups()
47 # print '-> ', repr(match.group())
48 else:
49 print '-> ', match
51 options = """\
52 :lineno:
53 :lineno: 2
54 :line-no:
55 :line+no:
56 :lineno+:
57 :x:x:
58 """
60 no_options = [' :lineno:2', # no space before option arg
61 ':lineno:', # no leading whitespace
62 ' ::', # empty option
63 ' :lin$no:', # invalid character
65 option_regexp = re.compile(r' +:(\w|[-._+:])+:( |$)')
67 print 'regexp test for option_regexp'
68 for sample in (options).splitlines(True) + no_options:
69 match = option_regexp.search(sample)
70 print '%-40r'%(sample),
71 if match:
72 print '-> ', match.groups()
73 # print '-> ', repr(match.group())
74 else:
75 print '-> ', match