Mercurial > p > roundup > code
comparison roundup/cgi/TAL/markupbase.py @ 2348:8c2402a78bb0
beginning getting ZPT up to date: TAL first
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Fri, 21 May 2004 05:36:30 +0000 |
| parents | fc52d57c6c3e |
| children | 12fe83f90f0d |
comparison
equal
deleted
inserted
replaced
| 2347:fbbda3b1816d | 2348:8c2402a78bb0 |
|---|---|
| 1 """Shared support for scanning document type declarations in HTML and XHTML. | 1 """Shared support for scanning document type declarations in HTML and XHTML.""" |
| 2 """ | 2 |
| 3 __docformat__ = 'restructuredtext' | 3 import re, string |
| 4 | |
| 5 import re | |
| 6 import string | |
| 7 | 4 |
| 8 _declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match | 5 _declname_match = re.compile(r'[a-zA-Z][-_.a-zA-Z0-9]*\s*').match |
| 9 _declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match | 6 _declstringlit_match = re.compile(r'(\'[^\']*\'|"[^"]*")\s*').match |
| 10 | 7 |
| 11 del re | 8 del re |
| 20 self.offset = 0 | 17 self.offset = 0 |
| 21 | 18 |
| 22 def getpos(self): | 19 def getpos(self): |
| 23 """Return current line number and offset.""" | 20 """Return current line number and offset.""" |
| 24 return self.lineno, self.offset | 21 return self.lineno, self.offset |
| 22 | |
| 23 def error(self, message): | |
| 24 """Return an error, showing current line number and offset. | |
| 25 | |
| 26 Concrete subclasses *must* override this method. | |
| 27 """ | |
| 28 raise NotImplementedError | |
| 25 | 29 |
| 26 # Internal -- update line number and offset. This should be | 30 # Internal -- update line number and offset. This should be |
| 27 # called for each piece of data exactly once, in order -- in other | 31 # called for each piece of data exactly once, in order -- in other |
| 28 # words the concatenation of all the input strings to this | 32 # words the concatenation of all the input strings to this |
| 29 # function should be exactly the entire input. | 33 # function should be exactly the entire input. |
| 30 def updatepos(self, i, j): | 34 def updatepos(self, i, j): |
| 31 if i >= j: | 35 if i >= j: |
| 32 return j | 36 return j |
| 33 rawdata = self.rawdata | 37 rawdata = self.rawdata |
| 34 nlines = string.count(rawdata, "\n", i, j) | 38 nlines = rawdata.count("\n", i, j) |
| 35 if nlines: | 39 if nlines: |
| 36 self.lineno = self.lineno + nlines | 40 self.lineno = self.lineno + nlines |
| 37 pos = string.rindex(rawdata, "\n", i, j) # Should not fail | 41 pos = rawdata.rindex("\n", i, j) # Should not fail |
| 38 self.offset = j-(pos+1) | 42 self.offset = j-(pos+1) |
| 39 else: | 43 else: |
| 40 self.offset = self.offset + j-i | 44 self.offset = self.offset + j-i |
| 41 return j | 45 return j |
| 42 | 46 |
| 169 name, j = self._scan_name(i, declstartpos) | 173 name, j = self._scan_name(i, declstartpos) |
| 170 if j == -1: | 174 if j == -1: |
| 171 return -1 | 175 return -1 |
| 172 # style content model; just skip until '>' | 176 # style content model; just skip until '>' |
| 173 if '>' in rawdata[j:]: | 177 if '>' in rawdata[j:]: |
| 174 return string.find(rawdata, ">", j) + 1 | 178 return rawdata.find(">", j) + 1 |
| 175 return -1 | 179 return -1 |
| 176 | 180 |
| 177 # Internal -- scan past <!ATTLIST declarations | 181 # Internal -- scan past <!ATTLIST declarations |
| 178 def _parse_doctype_attlist(self, i, declstartpos): | 182 def _parse_doctype_attlist(self, i, declstartpos): |
| 179 rawdata = self.rawdata | 183 rawdata = self.rawdata |
| 193 if c == "": | 197 if c == "": |
| 194 return -1 | 198 return -1 |
| 195 if c == "(": | 199 if c == "(": |
| 196 # an enumerated type; look for ')' | 200 # an enumerated type; look for ')' |
| 197 if ")" in rawdata[j:]: | 201 if ")" in rawdata[j:]: |
| 198 j = string.find(rawdata, ")", j) + 1 | 202 j = rawdata.find(")", j) + 1 |
| 199 else: | 203 else: |
| 200 return -1 | 204 return -1 |
| 201 while rawdata[j:j+1] in string.whitespace: | 205 while rawdata[j:j+1].isspace(): |
| 202 j = j + 1 | 206 j = j + 1 |
| 203 if not rawdata[j:]: | 207 if not rawdata[j:]: |
| 204 # end of buffer, incomplete | 208 # end of buffer, incomplete |
| 205 return -1 | 209 return -1 |
| 206 else: | 210 else: |
| 297 if i == n: | 301 if i == n: |
| 298 return None, -1 | 302 return None, -1 |
| 299 m = _declname_match(rawdata, i) | 303 m = _declname_match(rawdata, i) |
| 300 if m: | 304 if m: |
| 301 s = m.group() | 305 s = m.group() |
| 302 name = string.strip(s) | 306 name = s.strip() |
| 303 if (i + len(s)) == n: | 307 if (i + len(s)) == n: |
| 304 return None, -1 # end of buffer | 308 return None, -1 # end of buffer |
| 305 return string.lower(name), m.end() | 309 return name.lower(), m.end() |
| 306 else: | 310 else: |
| 307 self.updatepos(declstartpos, i) | 311 self.updatepos(declstartpos, i) |
| 308 self.error("expected name token", self.getpos()) | 312 self.error("expected name token") |
