Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- """\
- %prog <infile>
- Remove line breaks from block paragraphs of plain text, but retain multiple
- consecutive newlines used for e.g. LaTeX, MarkDown, or plain text formats.
- TODO:
- * Provide a -o flag to write out to file
- * Read in from stdin if no files provided
- """
- import optparse
- op = optparse.OptionParser(usage=__doc__)
- opts, args = op.parse_args()
- lines = None
- infile = args[0]
- with open(infile, "r") as f:
- lines = f.readlines()
- newtext = ""
- inpara = False
- for l in lines:
- l2 = l.rstrip() # lose newline and trailing whitespace
- if not inpara:
- if l2:
- newtext += l2
- inpara = True
- else:
- newtext += l
- continue
- else: # in para
- if l2:
- newtext += " " + l2.lstrip()
- else:
- newtext += "\n" + l
- inpara = False
- print newtext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement