1233d2500723e5594f3e7c70896ffeeef32b9c950ywan#!/usr/bin/env python
2233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  Copyright (c) 2012 The WebM project authors. All Rights Reserved.
3233d2500723e5594f3e7c70896ffeeef32b9c950ywan##
4233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  Use of this source code is governed by a BSD-style license
5233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  that can be found in the LICENSE file in the root of the source
6233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  tree. An additional intellectual property rights grant can be found
7233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  in the file PATENTS.  All contributing project authors may
8233d2500723e5594f3e7c70896ffeeef32b9c950ywan##  be found in the AUTHORS file in the root of the source tree.
9233d2500723e5594f3e7c70896ffeeef32b9c950ywan##
10233d2500723e5594f3e7c70896ffeeef32b9c950ywan"""Wraps paragraphs of text, preserving manual formatting
11233d2500723e5594f3e7c70896ffeeef32b9c950ywan
12233d2500723e5594f3e7c70896ffeeef32b9c950ywanThis is like fold(1), but has the special convention of not modifying lines
13233d2500723e5594f3e7c70896ffeeef32b9c950ywanthat start with whitespace. This allows you to intersperse blocks with
14233d2500723e5594f3e7c70896ffeeef32b9c950ywanspecial formatting, like code blocks, with written prose. The prose will
15233d2500723e5594f3e7c70896ffeeef32b9c950ywanbe wordwrapped, and the manual formatting will be preserved.
16233d2500723e5594f3e7c70896ffeeef32b9c950ywan
17233d2500723e5594f3e7c70896ffeeef32b9c950ywan * This won't handle the case of a bulleted (or ordered) list specially, so
18233d2500723e5594f3e7c70896ffeeef32b9c950ywan   manual wrapping must be done.
19233d2500723e5594f3e7c70896ffeeef32b9c950ywan
20233d2500723e5594f3e7c70896ffeeef32b9c950ywanOccasionally it's useful to put something with explicit formatting that
21233d2500723e5594f3e7c70896ffeeef32b9c950ywandoesn't look at all like a block of text inline.
22233d2500723e5594f3e7c70896ffeeef32b9c950ywan
23233d2500723e5594f3e7c70896ffeeef32b9c950ywan  indicator = has_leading_whitespace(line);
24233d2500723e5594f3e7c70896ffeeef32b9c950ywan  if (indicator)
25233d2500723e5594f3e7c70896ffeeef32b9c950ywan    preserve_formatting(line);
26233d2500723e5594f3e7c70896ffeeef32b9c950ywan
27233d2500723e5594f3e7c70896ffeeef32b9c950ywanThe intent is that this docstring would make it through the transform
28233d2500723e5594f3e7c70896ffeeef32b9c950ywanand still be legible and presented as it is in the source. If additional
29233d2500723e5594f3e7c70896ffeeef32b9c950ywancases are handled, update this doc to describe the effect.
30233d2500723e5594f3e7c70896ffeeef32b9c950ywan"""
31233d2500723e5594f3e7c70896ffeeef32b9c950ywan
32233d2500723e5594f3e7c70896ffeeef32b9c950ywan__author__ = "jkoleszar@google.com"
33233d2500723e5594f3e7c70896ffeeef32b9c950ywanimport textwrap
34233d2500723e5594f3e7c70896ffeeef32b9c950ywanimport sys
35233d2500723e5594f3e7c70896ffeeef32b9c950ywan
36233d2500723e5594f3e7c70896ffeeef32b9c950ywandef wrap(text):
37233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if text:
38233d2500723e5594f3e7c70896ffeeef32b9c950ywan        return textwrap.fill(text, break_long_words=False) + '\n'
39233d2500723e5594f3e7c70896ffeeef32b9c950ywan    return ""
40233d2500723e5594f3e7c70896ffeeef32b9c950ywan
41233d2500723e5594f3e7c70896ffeeef32b9c950ywan
42233d2500723e5594f3e7c70896ffeeef32b9c950ywandef main(fileobj):
43233d2500723e5594f3e7c70896ffeeef32b9c950ywan    text = ""
44233d2500723e5594f3e7c70896ffeeef32b9c950ywan    output = ""
45233d2500723e5594f3e7c70896ffeeef32b9c950ywan    while True:
46233d2500723e5594f3e7c70896ffeeef32b9c950ywan        line = fileobj.readline()
47233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if not line:
48233d2500723e5594f3e7c70896ffeeef32b9c950ywan            break
49233d2500723e5594f3e7c70896ffeeef32b9c950ywan
50233d2500723e5594f3e7c70896ffeeef32b9c950ywan        if line.lstrip() == line:
51233d2500723e5594f3e7c70896ffeeef32b9c950ywan            text += line
52233d2500723e5594f3e7c70896ffeeef32b9c950ywan        else:
53233d2500723e5594f3e7c70896ffeeef32b9c950ywan            output += wrap(text)
54233d2500723e5594f3e7c70896ffeeef32b9c950ywan            text=""
55233d2500723e5594f3e7c70896ffeeef32b9c950ywan            output += line
56233d2500723e5594f3e7c70896ffeeef32b9c950ywan    output += wrap(text)
57233d2500723e5594f3e7c70896ffeeef32b9c950ywan
58233d2500723e5594f3e7c70896ffeeef32b9c950ywan    # Replace the file or write to stdout.
59233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if fileobj == sys.stdin:
60233d2500723e5594f3e7c70896ffeeef32b9c950ywan        fileobj = sys.stdout
61233d2500723e5594f3e7c70896ffeeef32b9c950ywan    else:
62233d2500723e5594f3e7c70896ffeeef32b9c950ywan        fileobj.seek(0)
63233d2500723e5594f3e7c70896ffeeef32b9c950ywan        fileobj.truncate(0)
64233d2500723e5594f3e7c70896ffeeef32b9c950ywan    fileobj.write(output)
65233d2500723e5594f3e7c70896ffeeef32b9c950ywan
66233d2500723e5594f3e7c70896ffeeef32b9c950ywanif __name__ == "__main__":
67233d2500723e5594f3e7c70896ffeeef32b9c950ywan    if len(sys.argv) > 1:
68233d2500723e5594f3e7c70896ffeeef32b9c950ywan        main(open(sys.argv[1], "r+"))
69233d2500723e5594f3e7c70896ffeeef32b9c950ywan    else:
70233d2500723e5594f3e7c70896ffeeef32b9c950ywan        main(sys.stdin)
71