~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to contrib/news_merge/parser.py

Merge news_merge example.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
def simple_parse(content):
 
4
    """Returns blocks, where each block is a 2-tuple (kind, text)."""
 
5
    blocks = content.split('\n\n')
 
6
    for block in blocks:
 
7
        if block.startswith('###'):
 
8
            # First line is ###...: Top heading
 
9
            yield 'heading', block
 
10
            continue
 
11
        last_line = block.rsplit('\n', 1)[-1]
 
12
        if last_line.startswith('###'):
 
13
            # last line is ###...: 2nd-level heading
 
14
            yield 'release', block
 
15
        elif last_line.startswith('***'):
 
16
            # last line is ***...: 3rd-level heading
 
17
            yield 'section', block
 
18
        elif block.startswith('* '):
 
19
            # bullet
 
20
            yield 'bullet', block
 
21
        elif block.strip() == '':
 
22
            # empty
 
23
            yield 'empty', block
 
24
        else:
 
25
            # plain text
 
26
            yield 'text', block
 
27
 
 
28
 
 
29
if __name__ == '__main__':
 
30
    import sys
 
31
    content = open(sys.argv[1], 'rb').read()
 
32
    for result in simple_parse(content):
 
33
        print result