1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
"""
conv_text.py - convert Doc to/from text
$Id: conv_text.py,v 1.4 1998/09/09 11:55:30 rob Exp $
Copyright 1998 Rob Tillotson <rob@io.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
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 the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
"""
__version__ = '$Id: conv_text.py,v 1.4 1998/09/09 11:55:30 rob Exp $'
import sys, os, string, formatter, re
from PDA.Palm.App import Doc
import DocWriter
class TextParser:
def __init__(self, w, options):
self.writer = w
self.options = options
self.do_markup = 0
self.tag_regex = ''
self.case_fold_tags = 0
self.tag_prefix = None
def __call__(self, fd):
self.fd = fd
w = self.writer
self.verbatim = w.has_option('verbatim')
if self.do_markup: self.do_markup = not w.has_option('no-tags')
if self.do_markup:
if self.tag_prefix:
rx_markup = re.compile(self.tag_regex %
re.escape(w.get_option('tag-prefix',
self.tag_prefix)))
else:
rx_markup = re.compile(self.tag_regex)
self.formatter = formatter.AbstractFormatter(w)
self.in_para = 0
while 1:
l = self.fd.readline()
if not l: break
if self.do_markup:
m = rx_markup.match(string.rstrip(l))
if m:
tag, arg = m.group('tag','arg')
if self.case_fold_tags: tag = string.lower(tag)
meth = 'tag_' + tag
if hasattr(self, meth):
apply(getattr(self, meth), (arg,))
continue
elif hasattr(self, 'tag'):
if not self.tag(tag, arg):
continue
if self.verbatim:
self.formatter.add_literal_data(l)
else:
if not string.strip(l):
if self.in_para:
in_para = 0
self.formatter.end_paragraph(1)
else:
self.in_para = 1
self.formatter.add_flowing_data(l)
class TextWithTagsParser(TextParser):
def __init__(self, *a, **kw):
apply(TextParser.__init__, (self,)+a, kw)
self.do_markup = 1
self.tag_regex = "^%s\s*(?P<tag>[a-zA-Z0-9/_]+)(?:\s*(?P<arg>.*)\s*)?"
self.case_fold_tags = 0
self.tag_prefix = '.'
def tag_TITLE(self, arg):
if not self.writer.has_title(): self.writer.set_title(arg)
def tag_BOOKMARK(self, arg):
self.writer.set_bookmark(arg)
tag_BM = tag_BOOKMARK
def tag_H1(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 1)
def tag_H2(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 2)
def tag_H3(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 3)
def tag_H4(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 4)
def tag_H5(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 5)
def tag_H6(self, arg):
self.formatter.end_paragraph(1)
self.writer.send_heading(arg, 6)
def tag_PRE(self, arg):
self.verbatim = 1
def tag(self, tag, arg):
if tag == '/PRE': self.verbatim = 0
else:
return 1
def tag_HR(self, arg):
self.writer.send_hor_rule()
parsers = {
'none': TextParser,
'default': TextWithTagsParser,
}
# The input-type option is not yet supported
def text_to_doc(fd, w, title=None, options=None, default_title='Untitled'):
p = parsers.get(w.get_option('input-type','default'),
TextParser)(w, options)
p(fd)
if not w.has_title():
w.set_title(default_title)
def doc_to_text(docname, fd, dlp=None):
if dlp:
doc = Doc.openDocRemote(dlp, docname, 'r')
else:
doc = Doc.openDoc(docname, 'r')
while 1:
l = doc.readline()
if not l: break
fd.write(l)
doc.close()
# really need to flesh this out :)
def totext_main(argv):
for fn in argv[1:]:
doc_to_text(fn, sys.stdout)
def todoc_main(argv):
from cmdproc import CmdLine
c = CmdLine('text',
[('verbatim', 'direct conversion; do not process text'),
('no-tags', 'do not process embedded tags'),
('tag-prefix', 'string that marks embedded tags')])
c.process_options(argv)
c.process_files(text_to_doc)
|