1
# Copyright (C) 2010 Canonical Ltd
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.
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.
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
17
"""Simple parser for bzr's NEWS file.
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'.
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).
27
def simple_parse_lines(lines):
28
"""Same as simple_parse, but takes an iterable of strs rather than a single
31
return simple_parse(''.join(lines))
34
def simple_parse(content):
35
"""Returns blocks, where each block is a 2-tuple (kind, text).
37
:kind: one of 'heading', 'release', 'section', 'empty' or 'text'.
38
:text: a str, including newlines.
40
blocks = content.split('\n\n')
42
if block.startswith('###'):
43
# First line is ###...: Top heading
44
yield 'heading', block
46
last_line = block.rsplit('\n', 1)[-1]
47
if last_line.startswith('###'):
48
# last line is ###...: 2nd-level heading
49
yield 'release', block
50
elif last_line.startswith('***'):
51
# last line is ***...: 3rd-level heading
52
yield 'section', block
53
elif block.startswith('* '):
56
elif block.strip() == '':
64
if __name__ == '__main__':
66
content = open(sys.argv[1], 'rb').read()
67
for result in simple_parse(content):