Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: HTMLParser.py
"""A parser for HTML and XHTML."""
[0] Fix | Delete
[1] Fix | Delete
# This file is based on sgmllib.py, but the API is slightly different.
[2] Fix | Delete
[3] Fix | Delete
# XXX There should be a way to distinguish between PCDATA (parsed
[4] Fix | Delete
# character data -- the normal case), RCDATA (replaceable character
[5] Fix | Delete
# data -- only char and entity references and end tags are special)
[6] Fix | Delete
# and CDATA (character data -- only end tags are special).
[7] Fix | Delete
[8] Fix | Delete
[9] Fix | Delete
import markupbase
[10] Fix | Delete
import re
[11] Fix | Delete
[12] Fix | Delete
# Regular expressions used for parsing
[13] Fix | Delete
[14] Fix | Delete
interesting_normal = re.compile('[&<]')
[15] Fix | Delete
incomplete = re.compile('&[a-zA-Z#]')
[16] Fix | Delete
[17] Fix | Delete
entityref = re.compile('&([a-zA-Z][-.a-zA-Z0-9]*)[^a-zA-Z0-9]')
[18] Fix | Delete
charref = re.compile('&#(?:[0-9]+|[xX][0-9a-fA-F]+)[^0-9a-fA-F]')
[19] Fix | Delete
[20] Fix | Delete
starttagopen = re.compile('<[a-zA-Z]')
[21] Fix | Delete
piclose = re.compile('>')
[22] Fix | Delete
commentclose = re.compile(r'--\s*>')
[23] Fix | Delete
[24] Fix | Delete
# see http://www.w3.org/TR/html5/tokenization.html#tag-open-state
[25] Fix | Delete
# and http://www.w3.org/TR/html5/tokenization.html#tag-name-state
[26] Fix | Delete
# note: if you change tagfind/attrfind remember to update locatestarttagend too
[27] Fix | Delete
tagfind = re.compile('([a-zA-Z][^\t\n\r\f />\x00]*)(?:\s|/(?!>))*')
[28] Fix | Delete
# this regex is currently unused, but left for backward compatibility
[29] Fix | Delete
tagfind_tolerant = re.compile('[a-zA-Z][^\t\n\r\f />\x00]*')
[30] Fix | Delete
[31] Fix | Delete
attrfind = re.compile(
[32] Fix | Delete
r'((?<=[\'"\s/])[^\s/>][^\s/=>]*)(\s*=+\s*'
[33] Fix | Delete
r'(\'[^\']*\'|"[^"]*"|(?![\'"])[^>\s]*))?(?:\s|/(?!>))*')
[34] Fix | Delete
[35] Fix | Delete
locatestarttagend = re.compile(r"""
[36] Fix | Delete
<[a-zA-Z][^\t\n\r\f />\x00]* # tag name
[37] Fix | Delete
(?:[\s/]* # optional whitespace before attribute name
[38] Fix | Delete
(?:(?<=['"\s/])[^\s/>][^\s/=>]* # attribute name
[39] Fix | Delete
(?:\s*=+\s* # value indicator
[40] Fix | Delete
(?:'[^']*' # LITA-enclosed value
[41] Fix | Delete
|"[^"]*" # LIT-enclosed value
[42] Fix | Delete
|(?!['"])[^>\s]* # bare value
[43] Fix | Delete
)
[44] Fix | Delete
)?(?:\s|/(?!>))*
[45] Fix | Delete
)*
[46] Fix | Delete
)?
[47] Fix | Delete
\s* # trailing whitespace
[48] Fix | Delete
""", re.VERBOSE)
[49] Fix | Delete
endendtag = re.compile('>')
[50] Fix | Delete
# the HTML 5 spec, section 8.1.2.2, doesn't allow spaces between
[51] Fix | Delete
# </ and the tag name, so maybe this should be fixed
[52] Fix | Delete
endtagfind = re.compile('</\s*([a-zA-Z][-.a-zA-Z0-9:_]*)\s*>')
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
class HTMLParseError(Exception):
[56] Fix | Delete
"""Exception raised for all parse errors."""
[57] Fix | Delete
[58] Fix | Delete
def __init__(self, msg, position=(None, None)):
[59] Fix | Delete
assert msg
[60] Fix | Delete
self.msg = msg
[61] Fix | Delete
self.lineno = position[0]
[62] Fix | Delete
self.offset = position[1]
[63] Fix | Delete
[64] Fix | Delete
def __str__(self):
[65] Fix | Delete
result = self.msg
[66] Fix | Delete
if self.lineno is not None:
[67] Fix | Delete
result = result + ", at line %d" % self.lineno
[68] Fix | Delete
if self.offset is not None:
[69] Fix | Delete
result = result + ", column %d" % (self.offset + 1)
[70] Fix | Delete
return result
[71] Fix | Delete
[72] Fix | Delete
[73] Fix | Delete
class HTMLParser(markupbase.ParserBase):
[74] Fix | Delete
"""Find tags and other markup and call handler functions.
[75] Fix | Delete
[76] Fix | Delete
Usage:
[77] Fix | Delete
p = HTMLParser()
[78] Fix | Delete
p.feed(data)
[79] Fix | Delete
...
[80] Fix | Delete
p.close()
[81] Fix | Delete
[82] Fix | Delete
Start tags are handled by calling self.handle_starttag() or
[83] Fix | Delete
self.handle_startendtag(); end tags by self.handle_endtag(). The
[84] Fix | Delete
data between tags is passed from the parser to the derived class
[85] Fix | Delete
by calling self.handle_data() with the data as argument (the data
[86] Fix | Delete
may be split up in arbitrary chunks). Entity references are
[87] Fix | Delete
passed by calling self.handle_entityref() with the entity
[88] Fix | Delete
reference as the argument. Numeric character references are
[89] Fix | Delete
passed to self.handle_charref() with the string containing the
[90] Fix | Delete
reference as the argument.
[91] Fix | Delete
"""
[92] Fix | Delete
[93] Fix | Delete
CDATA_CONTENT_ELEMENTS = ("script", "style")
[94] Fix | Delete
[95] Fix | Delete
[96] Fix | Delete
def __init__(self):
[97] Fix | Delete
"""Initialize and reset this instance."""
[98] Fix | Delete
self.reset()
[99] Fix | Delete
[100] Fix | Delete
def reset(self):
[101] Fix | Delete
"""Reset this instance. Loses all unprocessed data."""
[102] Fix | Delete
self.rawdata = ''
[103] Fix | Delete
self.lasttag = '???'
[104] Fix | Delete
self.interesting = interesting_normal
[105] Fix | Delete
self.cdata_elem = None
[106] Fix | Delete
markupbase.ParserBase.reset(self)
[107] Fix | Delete
[108] Fix | Delete
def feed(self, data):
[109] Fix | Delete
r"""Feed data to the parser.
[110] Fix | Delete
[111] Fix | Delete
Call this as often as you want, with as little or as much text
[112] Fix | Delete
as you want (may include '\n').
[113] Fix | Delete
"""
[114] Fix | Delete
self.rawdata = self.rawdata + data
[115] Fix | Delete
self.goahead(0)
[116] Fix | Delete
[117] Fix | Delete
def close(self):
[118] Fix | Delete
"""Handle any buffered data."""
[119] Fix | Delete
self.goahead(1)
[120] Fix | Delete
[121] Fix | Delete
def error(self, message):
[122] Fix | Delete
raise HTMLParseError(message, self.getpos())
[123] Fix | Delete
[124] Fix | Delete
__starttag_text = None
[125] Fix | Delete
[126] Fix | Delete
def get_starttag_text(self):
[127] Fix | Delete
"""Return full source of start tag: '<...>'."""
[128] Fix | Delete
return self.__starttag_text
[129] Fix | Delete
[130] Fix | Delete
def set_cdata_mode(self, elem):
[131] Fix | Delete
self.cdata_elem = elem.lower()
[132] Fix | Delete
self.interesting = re.compile(r'</\s*%s\s*>' % self.cdata_elem, re.I)
[133] Fix | Delete
[134] Fix | Delete
def clear_cdata_mode(self):
[135] Fix | Delete
self.interesting = interesting_normal
[136] Fix | Delete
self.cdata_elem = None
[137] Fix | Delete
[138] Fix | Delete
# Internal -- handle data as far as reasonable. May leave state
[139] Fix | Delete
# and data to be processed by a subsequent call. If 'end' is
[140] Fix | Delete
# true, force handling all data as if followed by EOF marker.
[141] Fix | Delete
def goahead(self, end):
[142] Fix | Delete
rawdata = self.rawdata
[143] Fix | Delete
i = 0
[144] Fix | Delete
n = len(rawdata)
[145] Fix | Delete
while i < n:
[146] Fix | Delete
match = self.interesting.search(rawdata, i) # < or &
[147] Fix | Delete
if match:
[148] Fix | Delete
j = match.start()
[149] Fix | Delete
else:
[150] Fix | Delete
if self.cdata_elem:
[151] Fix | Delete
break
[152] Fix | Delete
j = n
[153] Fix | Delete
if i < j: self.handle_data(rawdata[i:j])
[154] Fix | Delete
i = self.updatepos(i, j)
[155] Fix | Delete
if i == n: break
[156] Fix | Delete
startswith = rawdata.startswith
[157] Fix | Delete
if startswith('<', i):
[158] Fix | Delete
if starttagopen.match(rawdata, i): # < + letter
[159] Fix | Delete
k = self.parse_starttag(i)
[160] Fix | Delete
elif startswith("</", i):
[161] Fix | Delete
k = self.parse_endtag(i)
[162] Fix | Delete
elif startswith("<!--", i):
[163] Fix | Delete
k = self.parse_comment(i)
[164] Fix | Delete
elif startswith("<?", i):
[165] Fix | Delete
k = self.parse_pi(i)
[166] Fix | Delete
elif startswith("<!", i):
[167] Fix | Delete
k = self.parse_html_declaration(i)
[168] Fix | Delete
elif (i + 1) < n:
[169] Fix | Delete
self.handle_data("<")
[170] Fix | Delete
k = i + 1
[171] Fix | Delete
else:
[172] Fix | Delete
break
[173] Fix | Delete
if k < 0:
[174] Fix | Delete
if not end:
[175] Fix | Delete
break
[176] Fix | Delete
k = rawdata.find('>', i + 1)
[177] Fix | Delete
if k < 0:
[178] Fix | Delete
k = rawdata.find('<', i + 1)
[179] Fix | Delete
if k < 0:
[180] Fix | Delete
k = i + 1
[181] Fix | Delete
else:
[182] Fix | Delete
k += 1
[183] Fix | Delete
self.handle_data(rawdata[i:k])
[184] Fix | Delete
i = self.updatepos(i, k)
[185] Fix | Delete
elif startswith("&#", i):
[186] Fix | Delete
match = charref.match(rawdata, i)
[187] Fix | Delete
if match:
[188] Fix | Delete
name = match.group()[2:-1]
[189] Fix | Delete
self.handle_charref(name)
[190] Fix | Delete
k = match.end()
[191] Fix | Delete
if not startswith(';', k-1):
[192] Fix | Delete
k = k - 1
[193] Fix | Delete
i = self.updatepos(i, k)
[194] Fix | Delete
continue
[195] Fix | Delete
else:
[196] Fix | Delete
if ";" in rawdata[i:]: # bail by consuming '&#'
[197] Fix | Delete
self.handle_data(rawdata[i:i+2])
[198] Fix | Delete
i = self.updatepos(i, i+2)
[199] Fix | Delete
break
[200] Fix | Delete
elif startswith('&', i):
[201] Fix | Delete
match = entityref.match(rawdata, i)
[202] Fix | Delete
if match:
[203] Fix | Delete
name = match.group(1)
[204] Fix | Delete
self.handle_entityref(name)
[205] Fix | Delete
k = match.end()
[206] Fix | Delete
if not startswith(';', k-1):
[207] Fix | Delete
k = k - 1
[208] Fix | Delete
i = self.updatepos(i, k)
[209] Fix | Delete
continue
[210] Fix | Delete
match = incomplete.match(rawdata, i)
[211] Fix | Delete
if match:
[212] Fix | Delete
# match.group() will contain at least 2 chars
[213] Fix | Delete
if end and match.group() == rawdata[i:]:
[214] Fix | Delete
self.error("EOF in middle of entity or char ref")
[215] Fix | Delete
# incomplete
[216] Fix | Delete
break
[217] Fix | Delete
elif (i + 1) < n:
[218] Fix | Delete
# not the end of the buffer, and can't be confused
[219] Fix | Delete
# with some other construct
[220] Fix | Delete
self.handle_data("&")
[221] Fix | Delete
i = self.updatepos(i, i + 1)
[222] Fix | Delete
else:
[223] Fix | Delete
break
[224] Fix | Delete
else:
[225] Fix | Delete
assert 0, "interesting.search() lied"
[226] Fix | Delete
# end while
[227] Fix | Delete
if end and i < n and not self.cdata_elem:
[228] Fix | Delete
self.handle_data(rawdata[i:n])
[229] Fix | Delete
i = self.updatepos(i, n)
[230] Fix | Delete
self.rawdata = rawdata[i:]
[231] Fix | Delete
[232] Fix | Delete
# Internal -- parse html declarations, return length or -1 if not terminated
[233] Fix | Delete
# See w3.org/TR/html5/tokenization.html#markup-declaration-open-state
[234] Fix | Delete
# See also parse_declaration in _markupbase
[235] Fix | Delete
def parse_html_declaration(self, i):
[236] Fix | Delete
rawdata = self.rawdata
[237] Fix | Delete
if rawdata[i:i+2] != '<!':
[238] Fix | Delete
self.error('unexpected call to parse_html_declaration()')
[239] Fix | Delete
if rawdata[i:i+4] == '<!--':
[240] Fix | Delete
# this case is actually already handled in goahead()
[241] Fix | Delete
return self.parse_comment(i)
[242] Fix | Delete
elif rawdata[i:i+3] == '<![':
[243] Fix | Delete
return self.parse_marked_section(i)
[244] Fix | Delete
elif rawdata[i:i+9].lower() == '<!doctype':
[245] Fix | Delete
# find the closing >
[246] Fix | Delete
gtpos = rawdata.find('>', i+9)
[247] Fix | Delete
if gtpos == -1:
[248] Fix | Delete
return -1
[249] Fix | Delete
self.handle_decl(rawdata[i+2:gtpos])
[250] Fix | Delete
return gtpos+1
[251] Fix | Delete
else:
[252] Fix | Delete
return self.parse_bogus_comment(i)
[253] Fix | Delete
[254] Fix | Delete
# Internal -- parse bogus comment, return length or -1 if not terminated
[255] Fix | Delete
# see http://www.w3.org/TR/html5/tokenization.html#bogus-comment-state
[256] Fix | Delete
def parse_bogus_comment(self, i, report=1):
[257] Fix | Delete
rawdata = self.rawdata
[258] Fix | Delete
if rawdata[i:i+2] not in ('<!', '</'):
[259] Fix | Delete
self.error('unexpected call to parse_comment()')
[260] Fix | Delete
pos = rawdata.find('>', i+2)
[261] Fix | Delete
if pos == -1:
[262] Fix | Delete
return -1
[263] Fix | Delete
if report:
[264] Fix | Delete
self.handle_comment(rawdata[i+2:pos])
[265] Fix | Delete
return pos + 1
[266] Fix | Delete
[267] Fix | Delete
# Internal -- parse processing instr, return end or -1 if not terminated
[268] Fix | Delete
def parse_pi(self, i):
[269] Fix | Delete
rawdata = self.rawdata
[270] Fix | Delete
assert rawdata[i:i+2] == '<?', 'unexpected call to parse_pi()'
[271] Fix | Delete
match = piclose.search(rawdata, i+2) # >
[272] Fix | Delete
if not match:
[273] Fix | Delete
return -1
[274] Fix | Delete
j = match.start()
[275] Fix | Delete
self.handle_pi(rawdata[i+2: j])
[276] Fix | Delete
j = match.end()
[277] Fix | Delete
return j
[278] Fix | Delete
[279] Fix | Delete
# Internal -- handle starttag, return end or -1 if not terminated
[280] Fix | Delete
def parse_starttag(self, i):
[281] Fix | Delete
self.__starttag_text = None
[282] Fix | Delete
endpos = self.check_for_whole_start_tag(i)
[283] Fix | Delete
if endpos < 0:
[284] Fix | Delete
return endpos
[285] Fix | Delete
rawdata = self.rawdata
[286] Fix | Delete
self.__starttag_text = rawdata[i:endpos]
[287] Fix | Delete
[288] Fix | Delete
# Now parse the data between i+1 and j into a tag and attrs
[289] Fix | Delete
attrs = []
[290] Fix | Delete
match = tagfind.match(rawdata, i+1)
[291] Fix | Delete
assert match, 'unexpected call to parse_starttag()'
[292] Fix | Delete
k = match.end()
[293] Fix | Delete
self.lasttag = tag = match.group(1).lower()
[294] Fix | Delete
[295] Fix | Delete
while k < endpos:
[296] Fix | Delete
m = attrfind.match(rawdata, k)
[297] Fix | Delete
if not m:
[298] Fix | Delete
break
[299] Fix | Delete
attrname, rest, attrvalue = m.group(1, 2, 3)
[300] Fix | Delete
if not rest:
[301] Fix | Delete
attrvalue = None
[302] Fix | Delete
elif attrvalue[:1] == '\'' == attrvalue[-1:] or \
[303] Fix | Delete
attrvalue[:1] == '"' == attrvalue[-1:]:
[304] Fix | Delete
attrvalue = attrvalue[1:-1]
[305] Fix | Delete
if attrvalue:
[306] Fix | Delete
attrvalue = self.unescape(attrvalue)
[307] Fix | Delete
attrs.append((attrname.lower(), attrvalue))
[308] Fix | Delete
k = m.end()
[309] Fix | Delete
[310] Fix | Delete
end = rawdata[k:endpos].strip()
[311] Fix | Delete
if end not in (">", "/>"):
[312] Fix | Delete
lineno, offset = self.getpos()
[313] Fix | Delete
if "\n" in self.__starttag_text:
[314] Fix | Delete
lineno = lineno + self.__starttag_text.count("\n")
[315] Fix | Delete
offset = len(self.__starttag_text) \
[316] Fix | Delete
- self.__starttag_text.rfind("\n")
[317] Fix | Delete
else:
[318] Fix | Delete
offset = offset + len(self.__starttag_text)
[319] Fix | Delete
self.handle_data(rawdata[i:endpos])
[320] Fix | Delete
return endpos
[321] Fix | Delete
if end.endswith('/>'):
[322] Fix | Delete
# XHTML-style empty tag: <span attr="value" />
[323] Fix | Delete
self.handle_startendtag(tag, attrs)
[324] Fix | Delete
else:
[325] Fix | Delete
self.handle_starttag(tag, attrs)
[326] Fix | Delete
if tag in self.CDATA_CONTENT_ELEMENTS:
[327] Fix | Delete
self.set_cdata_mode(tag)
[328] Fix | Delete
return endpos
[329] Fix | Delete
[330] Fix | Delete
# Internal -- check to see if we have a complete starttag; return end
[331] Fix | Delete
# or -1 if incomplete.
[332] Fix | Delete
def check_for_whole_start_tag(self, i):
[333] Fix | Delete
rawdata = self.rawdata
[334] Fix | Delete
m = locatestarttagend.match(rawdata, i)
[335] Fix | Delete
if m:
[336] Fix | Delete
j = m.end()
[337] Fix | Delete
next = rawdata[j:j+1]
[338] Fix | Delete
if next == ">":
[339] Fix | Delete
return j + 1
[340] Fix | Delete
if next == "/":
[341] Fix | Delete
if rawdata.startswith("/>", j):
[342] Fix | Delete
return j + 2
[343] Fix | Delete
if rawdata.startswith("/", j):
[344] Fix | Delete
# buffer boundary
[345] Fix | Delete
return -1
[346] Fix | Delete
# else bogus input
[347] Fix | Delete
self.updatepos(i, j + 1)
[348] Fix | Delete
self.error("malformed empty start tag")
[349] Fix | Delete
if next == "":
[350] Fix | Delete
# end of input
[351] Fix | Delete
return -1
[352] Fix | Delete
if next in ("abcdefghijklmnopqrstuvwxyz=/"
[353] Fix | Delete
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
[354] Fix | Delete
# end of input in or before attribute value, or we have the
[355] Fix | Delete
# '/' from a '/>' ending
[356] Fix | Delete
return -1
[357] Fix | Delete
if j > i:
[358] Fix | Delete
return j
[359] Fix | Delete
else:
[360] Fix | Delete
return i + 1
[361] Fix | Delete
raise AssertionError("we should not get here!")
[362] Fix | Delete
[363] Fix | Delete
# Internal -- parse endtag, return end or -1 if incomplete
[364] Fix | Delete
def parse_endtag(self, i):
[365] Fix | Delete
rawdata = self.rawdata
[366] Fix | Delete
assert rawdata[i:i+2] == "</", "unexpected call to parse_endtag"
[367] Fix | Delete
match = endendtag.search(rawdata, i+1) # >
[368] Fix | Delete
if not match:
[369] Fix | Delete
return -1
[370] Fix | Delete
gtpos = match.end()
[371] Fix | Delete
match = endtagfind.match(rawdata, i) # </ + tag + >
[372] Fix | Delete
if not match:
[373] Fix | Delete
if self.cdata_elem is not None:
[374] Fix | Delete
self.handle_data(rawdata[i:gtpos])
[375] Fix | Delete
return gtpos
[376] Fix | Delete
# find the name: w3.org/TR/html5/tokenization.html#tag-name-state
[377] Fix | Delete
namematch = tagfind.match(rawdata, i+2)
[378] Fix | Delete
if not namematch:
[379] Fix | Delete
# w3.org/TR/html5/tokenization.html#end-tag-open-state
[380] Fix | Delete
if rawdata[i:i+3] == '</>':
[381] Fix | Delete
return i+3
[382] Fix | Delete
else:
[383] Fix | Delete
return self.parse_bogus_comment(i)
[384] Fix | Delete
tagname = namematch.group(1).lower()
[385] Fix | Delete
# consume and ignore other stuff between the name and the >
[386] Fix | Delete
# Note: this is not 100% correct, since we might have things like
[387] Fix | Delete
# </tag attr=">">, but looking for > after tha name should cover
[388] Fix | Delete
# most of the cases and is much simpler
[389] Fix | Delete
gtpos = rawdata.find('>', namematch.end())
[390] Fix | Delete
self.handle_endtag(tagname)
[391] Fix | Delete
return gtpos+1
[392] Fix | Delete
[393] Fix | Delete
elem = match.group(1).lower() # script or style
[394] Fix | Delete
if self.cdata_elem is not None:
[395] Fix | Delete
if elem != self.cdata_elem:
[396] Fix | Delete
self.handle_data(rawdata[i:gtpos])
[397] Fix | Delete
return gtpos
[398] Fix | Delete
[399] Fix | Delete
self.handle_endtag(elem)
[400] Fix | Delete
self.clear_cdata_mode()
[401] Fix | Delete
return gtpos
[402] Fix | Delete
[403] Fix | Delete
# Overridable -- finish processing of start+end tag: <tag.../>
[404] Fix | Delete
def handle_startendtag(self, tag, attrs):
[405] Fix | Delete
self.handle_starttag(tag, attrs)
[406] Fix | Delete
self.handle_endtag(tag)
[407] Fix | Delete
[408] Fix | Delete
# Overridable -- handle start tag
[409] Fix | Delete
def handle_starttag(self, tag, attrs):
[410] Fix | Delete
pass
[411] Fix | Delete
[412] Fix | Delete
# Overridable -- handle end tag
[413] Fix | Delete
def handle_endtag(self, tag):
[414] Fix | Delete
pass
[415] Fix | Delete
[416] Fix | Delete
# Overridable -- handle character reference
[417] Fix | Delete
def handle_charref(self, name):
[418] Fix | Delete
pass
[419] Fix | Delete
[420] Fix | Delete
# Overridable -- handle entity reference
[421] Fix | Delete
def handle_entityref(self, name):
[422] Fix | Delete
pass
[423] Fix | Delete
[424] Fix | Delete
# Overridable -- handle data
[425] Fix | Delete
def handle_data(self, data):
[426] Fix | Delete
pass
[427] Fix | Delete
[428] Fix | Delete
# Overridable -- handle comment
[429] Fix | Delete
def handle_comment(self, data):
[430] Fix | Delete
pass
[431] Fix | Delete
[432] Fix | Delete
# Overridable -- handle declaration
[433] Fix | Delete
def handle_decl(self, decl):
[434] Fix | Delete
pass
[435] Fix | Delete
[436] Fix | Delete
# Overridable -- handle processing instruction
[437] Fix | Delete
def handle_pi(self, data):
[438] Fix | Delete
pass
[439] Fix | Delete
[440] Fix | Delete
def unknown_decl(self, data):
[441] Fix | Delete
pass
[442] Fix | Delete
[443] Fix | Delete
# Internal -- helper to remove special character quoting
[444] Fix | Delete
entitydefs = None
[445] Fix | Delete
def unescape(self, s):
[446] Fix | Delete
if '&' not in s:
[447] Fix | Delete
return s
[448] Fix | Delete
def replaceEntities(s):
[449] Fix | Delete
s = s.groups()[0]
[450] Fix | Delete
try:
[451] Fix | Delete
if s[0] == "#":
[452] Fix | Delete
s = s[1:]
[453] Fix | Delete
if s[0] in ['x','X']:
[454] Fix | Delete
c = int(s[1:], 16)
[455] Fix | Delete
else:
[456] Fix | Delete
c = int(s)
[457] Fix | Delete
return unichr(c)
[458] Fix | Delete
except ValueError:
[459] Fix | Delete
return '&#'+s+';'
[460] Fix | Delete
else:
[461] Fix | Delete
# Cannot use name2codepoint directly, because HTMLParser supports apos,
[462] Fix | Delete
# which is not part of HTML 4
[463] Fix | Delete
if HTMLParser.entitydefs is None:
[464] Fix | Delete
import htmlentitydefs
[465] Fix | Delete
entitydefs = {'apos':u"'"}
[466] Fix | Delete
for k, v in htmlentitydefs.name2codepoint.iteritems():
[467] Fix | Delete
entitydefs[k] = unichr(v)
[468] Fix | Delete
HTMLParser.entitydefs = entitydefs
[469] Fix | Delete
try:
[470] Fix | Delete
return self.entitydefs[s]
[471] Fix | Delete
except KeyError:
[472] Fix | Delete
return '&'+s+';'
[473] Fix | Delete
[474] Fix | Delete
return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, s)
[475] Fix | Delete
[476] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function