3 # reformat-news.py: Reformat the NEWS file properly
5 # Copyright (C) 2017 Red Hat, Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library. If not, see
19 # <http://www.gnu.org/licenses/>.
21 from __future__
import print_function
27 def reformat_with_indent(text
, initial_indent
, indent
):
32 for word
in text
.split():
34 # If adding one more word (plus a whitespace, plus a newline)
35 # to the current line would push us over the desired number
36 # of columns we start a new line instead
37 if len(line
) + len(word
) > (COLUMNS
- 2):
38 res
= res
+ line
+ "\n"
41 # We need to take care when we've just started a new line,
42 # as we don't want to add any additional leading whitespace
44 if line
== indent
or line
== initial_indent
:
47 line
= line
+ " " + word
49 # Append whatever's left
57 # Empty lines don't need to be reformatted or even inspected
61 # For all non-empty lines, we decide the indentation level based
62 # on the first character
81 # In this one case, the marker should not ultimately show
82 # up in the output file, so we strip it before moving on
84 # Anything else should be left as-is
88 return reformat_with_indent(line
, " " * initial_indent
, " " * indent
)
94 sys
.stdout
.write("Usage: " + args
[0] + " FILE\n")
97 with
open(args
[1], 'r') as f
:
99 print(reformat(line
.strip()))
102 if __name__
== "__main__":