annotate curlers.py @ 11:1f5e471101b0

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