11b362b15af34006e6a11974088a46d42b903418eJohann#!/usr/bin/env python 21b362b15af34006e6a11974088a46d42b903418eJohann## Copyright (c) 2012 The WebM project authors. All Rights Reserved. 31b362b15af34006e6a11974088a46d42b903418eJohann## 41b362b15af34006e6a11974088a46d42b903418eJohann## Use of this source code is governed by a BSD-style license 51b362b15af34006e6a11974088a46d42b903418eJohann## that can be found in the LICENSE file in the root of the source 61b362b15af34006e6a11974088a46d42b903418eJohann## tree. An additional intellectual property rights grant can be found 71b362b15af34006e6a11974088a46d42b903418eJohann## in the file PATENTS. All contributing project authors may 81b362b15af34006e6a11974088a46d42b903418eJohann## be found in the AUTHORS file in the root of the source tree. 91b362b15af34006e6a11974088a46d42b903418eJohann## 101b362b15af34006e6a11974088a46d42b903418eJohann"""Wraps paragraphs of text, preserving manual formatting 111b362b15af34006e6a11974088a46d42b903418eJohann 121b362b15af34006e6a11974088a46d42b903418eJohannThis is like fold(1), but has the special convention of not modifying lines 131b362b15af34006e6a11974088a46d42b903418eJohannthat start with whitespace. This allows you to intersperse blocks with 141b362b15af34006e6a11974088a46d42b903418eJohannspecial formatting, like code blocks, with written prose. The prose will 151b362b15af34006e6a11974088a46d42b903418eJohannbe wordwrapped, and the manual formatting will be preserved. 161b362b15af34006e6a11974088a46d42b903418eJohann 171b362b15af34006e6a11974088a46d42b903418eJohann * This won't handle the case of a bulleted (or ordered) list specially, so 181b362b15af34006e6a11974088a46d42b903418eJohann manual wrapping must be done. 191b362b15af34006e6a11974088a46d42b903418eJohann 201b362b15af34006e6a11974088a46d42b903418eJohannOccasionally it's useful to put something with explicit formatting that 211b362b15af34006e6a11974088a46d42b903418eJohanndoesn't look at all like a block of text inline. 221b362b15af34006e6a11974088a46d42b903418eJohann 231b362b15af34006e6a11974088a46d42b903418eJohann indicator = has_leading_whitespace(line); 241b362b15af34006e6a11974088a46d42b903418eJohann if (indicator) 251b362b15af34006e6a11974088a46d42b903418eJohann preserve_formatting(line); 261b362b15af34006e6a11974088a46d42b903418eJohann 271b362b15af34006e6a11974088a46d42b903418eJohannThe intent is that this docstring would make it through the transform 281b362b15af34006e6a11974088a46d42b903418eJohannand still be legible and presented as it is in the source. If additional 291b362b15af34006e6a11974088a46d42b903418eJohanncases are handled, update this doc to describe the effect. 301b362b15af34006e6a11974088a46d42b903418eJohann""" 311b362b15af34006e6a11974088a46d42b903418eJohann 321b362b15af34006e6a11974088a46d42b903418eJohann__author__ = "jkoleszar@google.com" 331b362b15af34006e6a11974088a46d42b903418eJohannimport textwrap 341b362b15af34006e6a11974088a46d42b903418eJohannimport sys 351b362b15af34006e6a11974088a46d42b903418eJohann 361b362b15af34006e6a11974088a46d42b903418eJohanndef wrap(text): 371b362b15af34006e6a11974088a46d42b903418eJohann if text: 381b362b15af34006e6a11974088a46d42b903418eJohann return textwrap.fill(text, break_long_words=False) + '\n' 391b362b15af34006e6a11974088a46d42b903418eJohann return "" 401b362b15af34006e6a11974088a46d42b903418eJohann 411b362b15af34006e6a11974088a46d42b903418eJohann 421b362b15af34006e6a11974088a46d42b903418eJohanndef main(fileobj): 431b362b15af34006e6a11974088a46d42b903418eJohann text = "" 441b362b15af34006e6a11974088a46d42b903418eJohann output = "" 451b362b15af34006e6a11974088a46d42b903418eJohann while True: 461b362b15af34006e6a11974088a46d42b903418eJohann line = fileobj.readline() 471b362b15af34006e6a11974088a46d42b903418eJohann if not line: 481b362b15af34006e6a11974088a46d42b903418eJohann break 491b362b15af34006e6a11974088a46d42b903418eJohann 501b362b15af34006e6a11974088a46d42b903418eJohann if line.lstrip() == line: 511b362b15af34006e6a11974088a46d42b903418eJohann text += line 521b362b15af34006e6a11974088a46d42b903418eJohann else: 531b362b15af34006e6a11974088a46d42b903418eJohann output += wrap(text) 541b362b15af34006e6a11974088a46d42b903418eJohann text="" 551b362b15af34006e6a11974088a46d42b903418eJohann output += line 561b362b15af34006e6a11974088a46d42b903418eJohann output += wrap(text) 571b362b15af34006e6a11974088a46d42b903418eJohann 581b362b15af34006e6a11974088a46d42b903418eJohann # Replace the file or write to stdout. 591b362b15af34006e6a11974088a46d42b903418eJohann if fileobj == sys.stdin: 601b362b15af34006e6a11974088a46d42b903418eJohann fileobj = sys.stdout 611b362b15af34006e6a11974088a46d42b903418eJohann else: 621b362b15af34006e6a11974088a46d42b903418eJohann fileobj.seek(0) 631b362b15af34006e6a11974088a46d42b903418eJohann fileobj.truncate(0) 641b362b15af34006e6a11974088a46d42b903418eJohann fileobj.write(output) 651b362b15af34006e6a11974088a46d42b903418eJohann 661b362b15af34006e6a11974088a46d42b903418eJohannif __name__ == "__main__": 671b362b15af34006e6a11974088a46d42b903418eJohann if len(sys.argv) > 1: 681b362b15af34006e6a11974088a46d42b903418eJohann main(open(sys.argv[1], "r+")) 691b362b15af34006e6a11974088a46d42b903418eJohann else: 701b362b15af34006e6a11974088a46d42b903418eJohann main(sys.stdin) 71