wrap-commit-msg.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. ## Copyright (c) 2012 The WebM project authors. All Rights Reserved.
  3. ##
  4. ## Use of this source code is governed by a BSD-style license
  5. ## that can be found in the LICENSE file in the root of the source
  6. ## tree. An additional intellectual property rights grant can be found
  7. ## in the file PATENTS. All contributing project authors may
  8. ## be found in the AUTHORS file in the root of the source tree.
  9. ##
  10. """Wraps paragraphs of text, preserving manual formatting
  11. This is like fold(1), but has the special convention of not modifying lines
  12. that start with whitespace. This allows you to intersperse blocks with
  13. special formatting, like code blocks, with written prose. The prose will
  14. be wordwrapped, and the manual formatting will be preserved.
  15. * This won't handle the case of a bulleted (or ordered) list specially, so
  16. manual wrapping must be done.
  17. Occasionally it's useful to put something with explicit formatting that
  18. doesn't look at all like a block of text inline.
  19. indicator = has_leading_whitespace(line);
  20. if (indicator)
  21. preserve_formatting(line);
  22. The intent is that this docstring would make it through the transform
  23. and still be legible and presented as it is in the source. If additional
  24. cases are handled, update this doc to describe the effect.
  25. """
  26. __author__ = "jkoleszar@google.com"
  27. import textwrap
  28. import sys
  29. def wrap(text):
  30. if text:
  31. return textwrap.fill(text, break_long_words=False) + '\n'
  32. return ""
  33. def main(fileobj):
  34. text = ""
  35. output = ""
  36. while True:
  37. line = fileobj.readline()
  38. if not line:
  39. break
  40. if line.lstrip() == line:
  41. text += line
  42. else:
  43. output += wrap(text)
  44. text=""
  45. output += line
  46. output += wrap(text)
  47. # Replace the file or write to stdout.
  48. if fileobj == sys.stdin:
  49. fileobj = sys.stdout
  50. else:
  51. fileobj.seek(0)
  52. fileobj.truncate(0)
  53. fileobj.write(output)
  54. if __name__ == "__main__":
  55. if len(sys.argv) > 1:
  56. main(open(sys.argv[1], "r+"))
  57. else:
  58. main(sys.stdin)