~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/plugins/news_merge/parser.py

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2010 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Simple parser for bzr's NEWS file.
 
18
 
 
19
Simple as this is, it's a bit over-powered for news_merge's needs, which only
 
20
cares about 'bullet' and 'everything else'.
 
21
 
 
22
This module can be run as a standalone Python program; pass it a filename and
 
23
it will print the parsed form of a file (a series of 2-tuples, see
 
24
simple_parse's docstring).
 
25
"""
 
26
 
 
27
 
 
28
def simple_parse(content):
 
29
    """Returns blocks, where each block is a 2-tuple (kind, text).
 
30
    
 
31
    :kind: one of 'heading', 'release', 'section', 'empty' or 'text'.
 
32
    :text: a str, including newlines.
 
33
    """
 
34
    blocks = content.split('\n\n')
 
35
    for block in blocks:
 
36
        if block.startswith('###'):
 
37
            # First line is ###...: Top heading
 
38
            yield 'heading', block
 
39
            continue
 
40
        last_line = block.rsplit('\n', 1)[-1]
 
41
        if last_line.startswith('###'):
 
42
            # last line is ###...: 2nd-level heading
 
43
            yield 'release', block
 
44
        elif last_line.startswith('***'):
 
45
            # last line is ***...: 3rd-level heading
 
46
            yield 'section', block
 
47
        elif block.startswith('* '):
 
48
            # bullet
 
49
            yield 'bullet', block
 
50
        elif block.strip() == '':
 
51
            # empty
 
52
            yield 'empty', block
 
53
        else:
 
54
            # plain text
 
55
            yield 'text', block
 
56
 
 
57
 
 
58
if __name__ == '__main__':
 
59
    import sys
 
60
    content = open(sys.argv[1], 'rb').read()
 
61
    for result in simple_parse(content):
 
62
        print result