John Koleszar | a46ec16 | 2012-03-28 16:41:16 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
John Koleszar | a46ec16 | 2012-03-28 16:41:16 -0700 | [diff] [blame] | 2 | ## |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 3 | ## Copyright (c) 2016, Alliance for Open Media. All rights reserved |
| 4 | ## |
| 5 | ## This source code is subject to the terms of the BSD 2 Clause License and |
| 6 | ## the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
| 7 | ## was not distributed with this source code in the LICENSE file, you can |
| 8 | ## obtain it at www.aomedia.org/license/software. If the Alliance for Open |
| 9 | ## Media Patent License 1.0 was not distributed with this source code in the |
| 10 | ## PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
John Koleszar | a46ec16 | 2012-03-28 16:41:16 -0700 | [diff] [blame] | 11 | ## |
| 12 | """Wraps paragraphs of text, preserving manual formatting |
| 13 | |
| 14 | This is like fold(1), but has the special convention of not modifying lines |
| 15 | that start with whitespace. This allows you to intersperse blocks with |
| 16 | special formatting, like code blocks, with written prose. The prose will |
| 17 | be wordwrapped, and the manual formatting will be preserved. |
| 18 | |
| 19 | * This won't handle the case of a bulleted (or ordered) list specially, so |
| 20 | manual wrapping must be done. |
| 21 | |
| 22 | Occasionally it's useful to put something with explicit formatting that |
| 23 | doesn't look at all like a block of text inline. |
| 24 | |
| 25 | indicator = has_leading_whitespace(line); |
| 26 | if (indicator) |
| 27 | preserve_formatting(line); |
| 28 | |
| 29 | The intent is that this docstring would make it through the transform |
| 30 | and still be legible and presented as it is in the source. If additional |
| 31 | cases are handled, update this doc to describe the effect. |
| 32 | """ |
| 33 | |
| 34 | __author__ = "jkoleszar@google.com" |
| 35 | import textwrap |
| 36 | import sys |
| 37 | |
| 38 | def wrap(text): |
| 39 | if text: |
| 40 | return textwrap.fill(text, break_long_words=False) + '\n' |
| 41 | return "" |
| 42 | |
| 43 | |
| 44 | def main(fileobj): |
| 45 | text = "" |
| 46 | output = "" |
| 47 | while True: |
| 48 | line = fileobj.readline() |
| 49 | if not line: |
| 50 | break |
| 51 | |
| 52 | if line.lstrip() == line: |
| 53 | text += line |
| 54 | else: |
| 55 | output += wrap(text) |
| 56 | text="" |
| 57 | output += line |
| 58 | output += wrap(text) |
| 59 | |
| 60 | # Replace the file or write to stdout. |
| 61 | if fileobj == sys.stdin: |
| 62 | fileobj = sys.stdout |
| 63 | else: |
James Zern | 00794a9 | 2012-03-29 18:13:27 -0700 | [diff] [blame] | 64 | fileobj.seek(0) |
John Koleszar | a46ec16 | 2012-03-28 16:41:16 -0700 | [diff] [blame] | 65 | fileobj.truncate(0) |
| 66 | fileobj.write(output) |
| 67 | |
| 68 | if __name__ == "__main__": |
| 69 | if len(sys.argv) > 1: |
| 70 | main(open(sys.argv[1], "r+")) |
| 71 | else: |
| 72 | main(sys.stdin) |