annotate curlers.py @ 6:da3fb2312c88

Leave bodies of <pre> tags alone.
author David Barts <n5jrn@me.com>
date Thu, 26 Dec 2019 20:38:37 -0800
parents 7a83e82e65a6
children 9df9ff8cecde
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
1 #!/usr/bin/env python3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
4 # Classes for curling both HTML and plain text.
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
5
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
6 # I m p o r t s
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
7
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import os, sys
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
9 from workspace import Workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
10
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
11 # V a r i a b l e s
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
12
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
13 # Quote types
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
14 LSQUO = "\u2018"
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
15 APOS = RSQUO = "\u2019"
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
16 LDQUO = "\u201C"
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
17 RDQUO = "\u201D"
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
18
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
19 # Words that start with an apostrophe. Cribbed from Wordpress.
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
20 _ASTART = [ "'tain't", "'twere", "'twas", "'tis", "'twill", "'til",
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
21 "'bout", "'nuff", "'round", "'cause" , "'em" ]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
22
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
23 # HTML tags that enclose raw data
6
da3fb2312c88 Leave bodies of <pre> tags alone.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
24 _RAW = set(["script", "style", "pre"])
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
25
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
26 # HTML block elements
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
27 _BLOCK = set([
6
da3fb2312c88 Leave bodies of <pre> tags alone.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
28 "address", "blockquote", "div", "dl", "fieldset", "form", "h1", "h2",
da3fb2312c88 Leave bodies of <pre> tags alone.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
29 "h3", "h4", "h5", "h6", "hr", "noscript", "ol", "p", "table", "ul"
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
30 ])
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
31
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
32 # F u n c t i o n s
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
33
4
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
34 def uncurl(ws):
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
35 """
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
36 Makes all quotes in the workspace non-curly.
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
37 """
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
38 for i in range(len(ws)):
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
39 ch = ws[i]
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
40 if ch in set([LDQUO, RDQUO]):
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
41 ws[i] = '"'
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
42 elif ch in set([LSQUO, RSQUO]):
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
43 ws[i] = "'"
7a83e82e65a6 Remove some deadwood.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
44
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
45 def _is_cockney(pos, ws):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
46 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
47 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
48 for i in _ASTART:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
49 li = len(i)
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
50 if ws[pos:pos+li].lower() == i and not ws[pos+li].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
51 return True
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
52
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
53 # C l a s s e s
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
54
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
55 class BaseCurler():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
56 def feed(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
57 raise NotImplementedError()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
58
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
59 class TextCurler(BaseCurler):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
60 """
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
61 For processing plain text. Assumes the entire text is a block; it is
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
62 the responsibility of the caller to break the input into paragraphs.
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
63 """
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
64 def __init__(self, workspace):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
65 self.workspace = workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
66 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
67 self._pos = 0
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
68
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
69 def feed(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
70 self._pos = 0
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
71 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
72 for self._pos in range(len(self.workspace)):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
73 self._state()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
74
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
75 def _is_cockney(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
76 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
77 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
78 for i in _ASTART:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
79 li = len(i)
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
80 print("comparing {0!r} and {1!r}\n".format(ws[pos:pos+li].lower(), i))
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
81 if ws[pos:pos+li].lower() == i and not ws[pos+li].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
82 return True
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
83
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
84 def _norm(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
85 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
86 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
87 char = ws[pos]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
88 if char == "\"":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
89 # opening double quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
90 ws[pos] = LDQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
91 self._state = self._seen_ld
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
92 elif char == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
93 # in this state, ' is always an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
94 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
95
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
96 def _seen_ld(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
97 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
98 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
99 char = ws[pos]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
100 if char == "\"":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
101 # closing double quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
102 ws[pos] = RDQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
103 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
104 elif char == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
105 if ws[pos-1].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
106 # either an inter-word, or an end of word, apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
107 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
108 elif ws[pos+1].isdecimal() or _is_cockney(pos, ws):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
109 # also an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
110 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
111 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
112 # opening single quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
113 ws[pos] = LSQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
114 self._state = self._seen_ls
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
115
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
116 def _seen_ls(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
117 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
118 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
119 if ws[pos] == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
120 if ws[pos-1].isalpha() and ws[pos+1].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
121 # obvious apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
122 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
123 elif ws[pos+1].isdecimal() or _is_cockney(pos, ws):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
124 # also an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
125 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
126 elif ws[pos-1].isspace():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
127 # start of word apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
128 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
129 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
130 # closing single quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
131 ws[pos] = RSQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
132 self._state = self._seen_ld
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
133
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
134 class HtmlCurler(BaseCurler):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
135 """
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
136 For processing HTML. Uses HTML block tags to delimit blocks.
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
137 """
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
138 def __init__(self, workspace):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
139 self.workspace = workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
140 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
141 self._pos = 0
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
142 self._ltpos = 0
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
143 self._endtag = None
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
144 self._ltstate = None
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
145
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
146 def feed(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
147 self._pos = 0
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
148 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
149 for self._pos in range(len(self.workspace)):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
150 self._state()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
151
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
152 def _is_cockney(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
153 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
154 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
155 for i in _ASTART:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
156 li = len(i)
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
157 print("comparing {0!r} and {1!r}\n".format(ws[pos:pos+li].lower(), i))
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
158 if ws[pos:pos+li].lower() == i and not ws[pos+li].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
159 return True
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
160
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
161 def _goto_lt(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
162 self._ltpos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
163 self._ltstate = self._state
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
164 self._state = self._seen_lt
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
165
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
166 def _norm(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
167 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
168 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
169 char = ws[pos]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
170 if char == "<":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
171 self._goto_lt()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
172 elif char == "\"":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
173 # opening double quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
174 ws[pos] = LDQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
175 self._state = self._seen_ld
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
176 elif char == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
177 # in this state, ' is always an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
178 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
179
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
180 def _gettag(self, start):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
181 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
182 end = start
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
183 while ws[end].isalnum():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
184 end += 1
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
185 return ws[start:end].lower()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
186
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
187 def _seen_lt(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
188 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
189 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
190 if ws[pos] == ">":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
191 start = self._ltpos + 1
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
192 if ws[start] == '/':
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
193 if self._gettag(start + 1) in _BLOCK:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
194 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
195 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
196 self._state = self._ltstate
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
197 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
198 tag = self._gettag(start)
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
199 if tag in _BLOCK:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
200 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
201 elif tag in _RAW:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
202 self._state = self._raw
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
203 self._endtag = "</" + tag
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
204 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
205 self._state = self._ltstate
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
206
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
207 def _raw(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
208 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
209 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
210 end = pos + len(self._endtag)
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
211 # only a matching end tag gets us out of the raw state
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
212 if ws[pos] == '<' and ws[pos:end].lower() == self._endtag and (not ws[end].isalnum()):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
213 self._ltpos = pos
6
da3fb2312c88 Leave bodies of <pre> tags alone.
David Barts <n5jrn@me.com>
parents: 4
diff changeset
214 self._state = self._norm if self._endtag == "</pre" else self._ltstate
3
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
215
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
216 def _seen_ld(self):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
217 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
218 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
219 char = ws[pos]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
220 if char == "<":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
221 self._goto_lt()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
222 elif char == "\"":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
223 # closing double quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
224 ws[pos] = RDQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
225 self._state = self._norm
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
226 elif char == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
227 if ws[pos-1].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
228 # either an inter-word, or an end of word, apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
229 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
230 elif ws[pos+1].isdecimal() or _is_cockney(pos, ws):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
231 # also an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
232 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
233 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
234 # opening single quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
235 ws[pos] = LSQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
236 self._state = self._seen_ls
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
237
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
238 def _seen_ls():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
239 pos = self._pos
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
240 ws = self.workspace
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
241 char = ws[pos]
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
242 if char == "<":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
243 self._goto_lt()
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
244 elif char == "'":
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
245 if ws[pos-1].isalpha() and ws[pos+1].isalpha():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
246 # obvious apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
247 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
248 elif ws[pos+1].isdecimal() or _is_cockney(pos, ws):
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
249 # also an apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
250 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
251 elif ws[pos-1].isspace():
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
252 # start of word apostrophe
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
253 ws[pos] = APOS
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
254 else:
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
255 # closing single quote
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
256 ws[pos] = RSQUO
091c03f1b2e8 Getting it working...
David Barts <n5jrn@me.com>
parents:
diff changeset
257 self._state = self._seen_ld