|
发表于 2010-10-19 18:34:37
|
显示全部楼层
- #!/usr/bin/python
- # vim:ts=8:sw=4:expandtab:encoding=utf-8
- """
- Add an empty line between paragraphs.
- Released under GPL v3 or later.
- """
- __version__ = "0.1"
- import sys
- import os
- import optparse
- NATIVE=sys.getfilesystemencoding()
- def fen_duan(fname, options):
- print 'Fixing "%s"...' % fname
- parts = os.path.splitext(fname)
- newname = '%s-fenduan%s' % parts # output file name
- fdin = open(fname, 'rU')
- fdout = open(newname, 'w')
- last_line_space=0
- for line in fdin:
- if line.isspace():
- # remove extra empty lines
- last_line_space += 1
- if last_line_space <= 1:
- fdout.write(line)
- continue
- else:
- last_line_space = 0
- fdout.write(line)
- # Append new line to short lines
- # use gb18030 to find line length.
- uline = line.decode(options.encoding, 'replace').encode('gb18030')
- if len(uline) < options.line_length:
- # Find new line char: \n, \r\n, \r
- nl = fdin.newlines or '\n'
- if isinstance(nl, tuple):
- nl = nl[0]
- fdout.write(nl)
- last_line_space += 1
- fdin.close()
- fdout.close()
- print 'Save as "%s"' % newname
- def main():
- LINELEN = 78
- parser = optparse.OptionParser(version=__version__)
- parser.add_option('-e', '--encoding', action='store',
- help="text encoding [%s]" % NATIVE)
- parser.add_option('-l', '--line-length', action='store', type=int,
- help="minimum characters per line [%i]" % LINELEN)
- parser.set_defaults(encoding=NATIVE, line_length=LINELEN)
- options, fnames = parser.parse_args()
- #print options
- if not fnames:
- parser.print_help()
- for f in fnames:
- fen_duan(f, options)
- if __name__ == '__main__':
- main()
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|