Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pgconvert.py
diff options
context:
space:
mode:
authorJames Simmons <jim@simmons.olpc>2009-11-22 20:52:52 (GMT)
committer James Simmons <jim@simmons.olpc>2009-11-22 20:52:52 (GMT)
commitf9cd8557dadb1ce754cebccc3f2909ef03a162c2 (patch)
treefab5084fecdccd72c580bebce561f8c23d42be97 /pgconvert.py
parent91e102086f0ad60fd15ab7e6d3f0fcba37a3bcf5 (diff)
modified: ReadEtextsActivity.py
modified: activity/activity.info modified: help.txt new file: pgconvert.py Add word wrapping conversion for text files.
Diffstat (limited to 'pgconvert.py')
-rwxr-xr-xpgconvert.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/pgconvert.py b/pgconvert.py
new file mode 100755
index 0000000..b9e6c4d
--- /dev/null
+++ b/pgconvert.py
@@ -0,0 +1,63 @@
+#! /usr/bin/env python
+
+# Copyright (C) 2009 James D. Simmons
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+import getopt
+import sys
+
+# This is a script to take the a file in PG format and convert it to a text file readable by Read Etexts that does
+# not have newlines at the end of each line.
+
+def convert(file_path, output_path):
+
+ pg_file = open(file_path,"r")
+ out = open(output_path, 'w')
+ previous_line_length = 0
+
+ while pg_file:
+ line = pg_file.readline()
+ outline = ''
+ if not line:
+ break
+ # print line
+ if len(line) == 2 and not previous_line_length == 2:
+ # Blank line separates paragraphs
+ outline = line + '\r\n'
+ # print 'first blank line'''
+ elif len(line) == 2 and previous_line_length == 2:
+ outline = line
+ # print 'multiple blank lines'
+ elif line[0] == ' ' or (line[0] >= '0' and line[0] <= '9'):
+ outline = '\r\n' + line[0:len(line)-2]
+ # print 'non-wrapped line;'
+ else:
+ outline = line[0:len(line)-2] + ' '
+ # print 'wrapped line'
+ out.write(outline)
+ previous_line_length = len(line)
+ pg_file.close()
+ out.close()
+ print "All done!"
+
+if __name__ == "__main__":
+ try:
+ opts, args = getopt.getopt(sys.argv[1:], "")
+ convert(args[0], args[1])
+ except getopt.error, msg:
+ print msg
+ print "This program has no options"
+ sys.exit(2)