~bzr-pqm/bzr/bzr.dev

4634.38.5 by Ian Clatworthy
Split Release Notes into topics so easier to navigate and print from chm & html
1
#!/usr/bin/python
2
3
# Copyright 2009 Canonical Ltd.
4
#
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19
import os
20
import sys
21
from optparse import OptionParser
22
23
24
def split_into_topics(lines, out_file, out_dir):
25
    """Split a large NEWS file into topics, one per release.
26
27
    Releases are detected by matching headings that look like
28
    release names. Topics are created with matching names
29
    replacing spaces with dashes.
30
    """
31
    topic_file = None
32
    for index, line in enumerate(lines):
5193.6.11 by Vincent Ladeuil
Fix bug in release notes generation.
33
        maybe_new_topic = line[:4] in ('bzr ', 'bzr-',)
4634.38.5 by Ian Clatworthy
Split Release Notes into topics so easier to navigate and print from chm & html
34
        if maybe_new_topic and lines[index + 1].startswith('####'):
35
            release = line.strip()
36
            if topic_file is None:
37
                # First topic found
38
                out_file.write(".. toctree::\n   :maxdepth: 1\n\n")
39
            else:
40
                # close the current topic
41
                topic_file.close()
42
            topic_file = open_topic_file(out_file, out_dir, release)
43
        elif topic_file:
44
            topic_file.write(line)
45
        else:
5193.6.8 by Vincent Ladeuil
Fix NEWS splitting.
46
            # FIXME: the 'content' directive is used for rst2html (and
47
            # conflicts with the 'toctree' we insert), we should get rid of
48
            # that once we fully switch to sphinx -- vila 20100505
49
            if (line.startswith('.. contents::')
50
                or line.startswith('   :depth:')):
51
                    continue
4634.38.5 by Ian Clatworthy
Split Release Notes into topics so easier to navigate and print from chm & html
52
            # Still in the header - dump content straight to output
53
            out_file.write(line)
5193.6.11 by Vincent Ladeuil
Fix bug in release notes generation.
54
    if topic_file is not None:
55
        # Close the last topic_file opened
56
        topic_file.close()
4634.38.5 by Ian Clatworthy
Split Release Notes into topics so easier to navigate and print from chm & html
57
58
59
def open_topic_file(out_file, out_dir, release):
60
    topic_name = release.replace(' ', '-')
5193.6.11 by Vincent Ladeuil
Fix bug in release notes generation.
61
    out_file.write("   %s <%s>\n" % (release, topic_name,))
4634.38.5 by Ian Clatworthy
Split Release Notes into topics so easier to navigate and print from chm & html
62
    topic_path = os.path.join(out_dir, "%s.txt" % (topic_name,))
63
    result = open(topic_path, 'w')
64
    result.write("%s\n" % (release,))
65
    return result
66
67
68
def main(argv):
69
    # Check usage
70
    parser = OptionParser(usage="%prog SOURCE DESTINATION")
71
    (options, args) = parser.parse_args(argv)
72
    if len(args) != 2:
73
        parser.print_help()
74
        sys.exit(1)
75
76
    # Open the files and do the work
77
    infile_name = args[0]
78
    outfile_name = args[1]
79
    outdir = os.path.dirname(outfile_name)
80
    infile = open(infile_name, 'r')
81
    try:
82
        lines = infile.readlines()
83
    finally:
84
        infile.close()
85
    outfile = open(outfile_name, 'w')
86
    try:
87
        split_into_topics(lines, outfile, outdir)
88
    finally:
89
        outfile.close()
90
91
92
if __name__ == '__main__':
93
    main(sys.argv[1:])