Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python2....
File: htmllib.py
"""HTML 2.0 parser.
[0] Fix | Delete
[1] Fix | Delete
See the HTML 2.0 specification:
[2] Fix | Delete
http://www.w3.org/hypertext/WWW/MarkUp/html-spec/html-spec_toc.html
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
from warnings import warnpy3k
[6] Fix | Delete
warnpy3k("the htmllib module has been removed in Python 3.0",
[7] Fix | Delete
stacklevel=2)
[8] Fix | Delete
del warnpy3k
[9] Fix | Delete
[10] Fix | Delete
import sgmllib
[11] Fix | Delete
[12] Fix | Delete
from formatter import AS_IS
[13] Fix | Delete
[14] Fix | Delete
__all__ = ["HTMLParser", "HTMLParseError"]
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
class HTMLParseError(sgmllib.SGMLParseError):
[18] Fix | Delete
"""Error raised when an HTML document can't be parsed."""
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
class HTMLParser(sgmllib.SGMLParser):
[22] Fix | Delete
"""This is the basic HTML parser class.
[23] Fix | Delete
[24] Fix | Delete
It supports all entity names required by the XHTML 1.0 Recommendation.
[25] Fix | Delete
It also defines handlers for all HTML 2.0 and many HTML 3.0 and 3.2
[26] Fix | Delete
elements.
[27] Fix | Delete
[28] Fix | Delete
"""
[29] Fix | Delete
[30] Fix | Delete
from htmlentitydefs import entitydefs
[31] Fix | Delete
[32] Fix | Delete
def __init__(self, formatter, verbose=0):
[33] Fix | Delete
"""Creates an instance of the HTMLParser class.
[34] Fix | Delete
[35] Fix | Delete
The formatter parameter is the formatter instance associated with
[36] Fix | Delete
the parser.
[37] Fix | Delete
[38] Fix | Delete
"""
[39] Fix | Delete
sgmllib.SGMLParser.__init__(self, verbose)
[40] Fix | Delete
self.formatter = formatter
[41] Fix | Delete
[42] Fix | Delete
def error(self, message):
[43] Fix | Delete
raise HTMLParseError(message)
[44] Fix | Delete
[45] Fix | Delete
def reset(self):
[46] Fix | Delete
sgmllib.SGMLParser.reset(self)
[47] Fix | Delete
self.savedata = None
[48] Fix | Delete
self.isindex = 0
[49] Fix | Delete
self.title = None
[50] Fix | Delete
self.base = None
[51] Fix | Delete
self.anchor = None
[52] Fix | Delete
self.anchorlist = []
[53] Fix | Delete
self.nofill = 0
[54] Fix | Delete
self.list_stack = []
[55] Fix | Delete
[56] Fix | Delete
# ------ Methods used internally; some may be overridden
[57] Fix | Delete
[58] Fix | Delete
# --- Formatter interface, taking care of 'savedata' mode;
[59] Fix | Delete
# shouldn't need to be overridden
[60] Fix | Delete
[61] Fix | Delete
def handle_data(self, data):
[62] Fix | Delete
if self.savedata is not None:
[63] Fix | Delete
self.savedata = self.savedata + data
[64] Fix | Delete
else:
[65] Fix | Delete
if self.nofill:
[66] Fix | Delete
self.formatter.add_literal_data(data)
[67] Fix | Delete
else:
[68] Fix | Delete
self.formatter.add_flowing_data(data)
[69] Fix | Delete
[70] Fix | Delete
# --- Hooks to save data; shouldn't need to be overridden
[71] Fix | Delete
[72] Fix | Delete
def save_bgn(self):
[73] Fix | Delete
"""Begins saving character data in a buffer instead of sending it
[74] Fix | Delete
to the formatter object.
[75] Fix | Delete
[76] Fix | Delete
Retrieve the stored data via the save_end() method. Use of the
[77] Fix | Delete
save_bgn() / save_end() pair may not be nested.
[78] Fix | Delete
[79] Fix | Delete
"""
[80] Fix | Delete
self.savedata = ''
[81] Fix | Delete
[82] Fix | Delete
def save_end(self):
[83] Fix | Delete
"""Ends buffering character data and returns all data saved since
[84] Fix | Delete
the preceding call to the save_bgn() method.
[85] Fix | Delete
[86] Fix | Delete
If the nofill flag is false, whitespace is collapsed to single
[87] Fix | Delete
spaces. A call to this method without a preceding call to the
[88] Fix | Delete
save_bgn() method will raise a TypeError exception.
[89] Fix | Delete
[90] Fix | Delete
"""
[91] Fix | Delete
data = self.savedata
[92] Fix | Delete
self.savedata = None
[93] Fix | Delete
if not self.nofill:
[94] Fix | Delete
data = ' '.join(data.split())
[95] Fix | Delete
return data
[96] Fix | Delete
[97] Fix | Delete
# --- Hooks for anchors; should probably be overridden
[98] Fix | Delete
[99] Fix | Delete
def anchor_bgn(self, href, name, type):
[100] Fix | Delete
"""This method is called at the start of an anchor region.
[101] Fix | Delete
[102] Fix | Delete
The arguments correspond to the attributes of the <A> tag with
[103] Fix | Delete
the same names. The default implementation maintains a list of
[104] Fix | Delete
hyperlinks (defined by the HREF attribute for <A> tags) within
[105] Fix | Delete
the document. The list of hyperlinks is available as the data
[106] Fix | Delete
attribute anchorlist.
[107] Fix | Delete
[108] Fix | Delete
"""
[109] Fix | Delete
self.anchor = href
[110] Fix | Delete
if self.anchor:
[111] Fix | Delete
self.anchorlist.append(href)
[112] Fix | Delete
[113] Fix | Delete
def anchor_end(self):
[114] Fix | Delete
"""This method is called at the end of an anchor region.
[115] Fix | Delete
[116] Fix | Delete
The default implementation adds a textual footnote marker using an
[117] Fix | Delete
index into the list of hyperlinks created by the anchor_bgn()method.
[118] Fix | Delete
[119] Fix | Delete
"""
[120] Fix | Delete
if self.anchor:
[121] Fix | Delete
self.handle_data("[%d]" % len(self.anchorlist))
[122] Fix | Delete
self.anchor = None
[123] Fix | Delete
[124] Fix | Delete
# --- Hook for images; should probably be overridden
[125] Fix | Delete
[126] Fix | Delete
def handle_image(self, src, alt, *args):
[127] Fix | Delete
"""This method is called to handle images.
[128] Fix | Delete
[129] Fix | Delete
The default implementation simply passes the alt value to the
[130] Fix | Delete
handle_data() method.
[131] Fix | Delete
[132] Fix | Delete
"""
[133] Fix | Delete
self.handle_data(alt)
[134] Fix | Delete
[135] Fix | Delete
# --------- Top level elememts
[136] Fix | Delete
[137] Fix | Delete
def start_html(self, attrs): pass
[138] Fix | Delete
def end_html(self): pass
[139] Fix | Delete
[140] Fix | Delete
def start_head(self, attrs): pass
[141] Fix | Delete
def end_head(self): pass
[142] Fix | Delete
[143] Fix | Delete
def start_body(self, attrs): pass
[144] Fix | Delete
def end_body(self): pass
[145] Fix | Delete
[146] Fix | Delete
# ------ Head elements
[147] Fix | Delete
[148] Fix | Delete
def start_title(self, attrs):
[149] Fix | Delete
self.save_bgn()
[150] Fix | Delete
[151] Fix | Delete
def end_title(self):
[152] Fix | Delete
self.title = self.save_end()
[153] Fix | Delete
[154] Fix | Delete
def do_base(self, attrs):
[155] Fix | Delete
for a, v in attrs:
[156] Fix | Delete
if a == 'href':
[157] Fix | Delete
self.base = v
[158] Fix | Delete
[159] Fix | Delete
def do_isindex(self, attrs):
[160] Fix | Delete
self.isindex = 1
[161] Fix | Delete
[162] Fix | Delete
def do_link(self, attrs):
[163] Fix | Delete
pass
[164] Fix | Delete
[165] Fix | Delete
def do_meta(self, attrs):
[166] Fix | Delete
pass
[167] Fix | Delete
[168] Fix | Delete
def do_nextid(self, attrs): # Deprecated
[169] Fix | Delete
pass
[170] Fix | Delete
[171] Fix | Delete
# ------ Body elements
[172] Fix | Delete
[173] Fix | Delete
# --- Headings
[174] Fix | Delete
[175] Fix | Delete
def start_h1(self, attrs):
[176] Fix | Delete
self.formatter.end_paragraph(1)
[177] Fix | Delete
self.formatter.push_font(('h1', 0, 1, 0))
[178] Fix | Delete
[179] Fix | Delete
def end_h1(self):
[180] Fix | Delete
self.formatter.end_paragraph(1)
[181] Fix | Delete
self.formatter.pop_font()
[182] Fix | Delete
[183] Fix | Delete
def start_h2(self, attrs):
[184] Fix | Delete
self.formatter.end_paragraph(1)
[185] Fix | Delete
self.formatter.push_font(('h2', 0, 1, 0))
[186] Fix | Delete
[187] Fix | Delete
def end_h2(self):
[188] Fix | Delete
self.formatter.end_paragraph(1)
[189] Fix | Delete
self.formatter.pop_font()
[190] Fix | Delete
[191] Fix | Delete
def start_h3(self, attrs):
[192] Fix | Delete
self.formatter.end_paragraph(1)
[193] Fix | Delete
self.formatter.push_font(('h3', 0, 1, 0))
[194] Fix | Delete
[195] Fix | Delete
def end_h3(self):
[196] Fix | Delete
self.formatter.end_paragraph(1)
[197] Fix | Delete
self.formatter.pop_font()
[198] Fix | Delete
[199] Fix | Delete
def start_h4(self, attrs):
[200] Fix | Delete
self.formatter.end_paragraph(1)
[201] Fix | Delete
self.formatter.push_font(('h4', 0, 1, 0))
[202] Fix | Delete
[203] Fix | Delete
def end_h4(self):
[204] Fix | Delete
self.formatter.end_paragraph(1)
[205] Fix | Delete
self.formatter.pop_font()
[206] Fix | Delete
[207] Fix | Delete
def start_h5(self, attrs):
[208] Fix | Delete
self.formatter.end_paragraph(1)
[209] Fix | Delete
self.formatter.push_font(('h5', 0, 1, 0))
[210] Fix | Delete
[211] Fix | Delete
def end_h5(self):
[212] Fix | Delete
self.formatter.end_paragraph(1)
[213] Fix | Delete
self.formatter.pop_font()
[214] Fix | Delete
[215] Fix | Delete
def start_h6(self, attrs):
[216] Fix | Delete
self.formatter.end_paragraph(1)
[217] Fix | Delete
self.formatter.push_font(('h6', 0, 1, 0))
[218] Fix | Delete
[219] Fix | Delete
def end_h6(self):
[220] Fix | Delete
self.formatter.end_paragraph(1)
[221] Fix | Delete
self.formatter.pop_font()
[222] Fix | Delete
[223] Fix | Delete
# --- Block Structuring Elements
[224] Fix | Delete
[225] Fix | Delete
def do_p(self, attrs):
[226] Fix | Delete
self.formatter.end_paragraph(1)
[227] Fix | Delete
[228] Fix | Delete
def start_pre(self, attrs):
[229] Fix | Delete
self.formatter.end_paragraph(1)
[230] Fix | Delete
self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
[231] Fix | Delete
self.nofill = self.nofill + 1
[232] Fix | Delete
[233] Fix | Delete
def end_pre(self):
[234] Fix | Delete
self.formatter.end_paragraph(1)
[235] Fix | Delete
self.formatter.pop_font()
[236] Fix | Delete
self.nofill = max(0, self.nofill - 1)
[237] Fix | Delete
[238] Fix | Delete
def start_xmp(self, attrs):
[239] Fix | Delete
self.start_pre(attrs)
[240] Fix | Delete
self.setliteral('xmp') # Tell SGML parser
[241] Fix | Delete
[242] Fix | Delete
def end_xmp(self):
[243] Fix | Delete
self.end_pre()
[244] Fix | Delete
[245] Fix | Delete
def start_listing(self, attrs):
[246] Fix | Delete
self.start_pre(attrs)
[247] Fix | Delete
self.setliteral('listing') # Tell SGML parser
[248] Fix | Delete
[249] Fix | Delete
def end_listing(self):
[250] Fix | Delete
self.end_pre()
[251] Fix | Delete
[252] Fix | Delete
def start_address(self, attrs):
[253] Fix | Delete
self.formatter.end_paragraph(0)
[254] Fix | Delete
self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
[255] Fix | Delete
[256] Fix | Delete
def end_address(self):
[257] Fix | Delete
self.formatter.end_paragraph(0)
[258] Fix | Delete
self.formatter.pop_font()
[259] Fix | Delete
[260] Fix | Delete
def start_blockquote(self, attrs):
[261] Fix | Delete
self.formatter.end_paragraph(1)
[262] Fix | Delete
self.formatter.push_margin('blockquote')
[263] Fix | Delete
[264] Fix | Delete
def end_blockquote(self):
[265] Fix | Delete
self.formatter.end_paragraph(1)
[266] Fix | Delete
self.formatter.pop_margin()
[267] Fix | Delete
[268] Fix | Delete
# --- List Elements
[269] Fix | Delete
[270] Fix | Delete
def start_ul(self, attrs):
[271] Fix | Delete
self.formatter.end_paragraph(not self.list_stack)
[272] Fix | Delete
self.formatter.push_margin('ul')
[273] Fix | Delete
self.list_stack.append(['ul', '*', 0])
[274] Fix | Delete
[275] Fix | Delete
def end_ul(self):
[276] Fix | Delete
if self.list_stack: del self.list_stack[-1]
[277] Fix | Delete
self.formatter.end_paragraph(not self.list_stack)
[278] Fix | Delete
self.formatter.pop_margin()
[279] Fix | Delete
[280] Fix | Delete
def do_li(self, attrs):
[281] Fix | Delete
self.formatter.end_paragraph(0)
[282] Fix | Delete
if self.list_stack:
[283] Fix | Delete
[dummy, label, counter] = top = self.list_stack[-1]
[284] Fix | Delete
top[2] = counter = counter+1
[285] Fix | Delete
else:
[286] Fix | Delete
label, counter = '*', 0
[287] Fix | Delete
self.formatter.add_label_data(label, counter)
[288] Fix | Delete
[289] Fix | Delete
def start_ol(self, attrs):
[290] Fix | Delete
self.formatter.end_paragraph(not self.list_stack)
[291] Fix | Delete
self.formatter.push_margin('ol')
[292] Fix | Delete
label = '1.'
[293] Fix | Delete
for a, v in attrs:
[294] Fix | Delete
if a == 'type':
[295] Fix | Delete
if len(v) == 1: v = v + '.'
[296] Fix | Delete
label = v
[297] Fix | Delete
self.list_stack.append(['ol', label, 0])
[298] Fix | Delete
[299] Fix | Delete
def end_ol(self):
[300] Fix | Delete
if self.list_stack: del self.list_stack[-1]
[301] Fix | Delete
self.formatter.end_paragraph(not self.list_stack)
[302] Fix | Delete
self.formatter.pop_margin()
[303] Fix | Delete
[304] Fix | Delete
def start_menu(self, attrs):
[305] Fix | Delete
self.start_ul(attrs)
[306] Fix | Delete
[307] Fix | Delete
def end_menu(self):
[308] Fix | Delete
self.end_ul()
[309] Fix | Delete
[310] Fix | Delete
def start_dir(self, attrs):
[311] Fix | Delete
self.start_ul(attrs)
[312] Fix | Delete
[313] Fix | Delete
def end_dir(self):
[314] Fix | Delete
self.end_ul()
[315] Fix | Delete
[316] Fix | Delete
def start_dl(self, attrs):
[317] Fix | Delete
self.formatter.end_paragraph(1)
[318] Fix | Delete
self.list_stack.append(['dl', '', 0])
[319] Fix | Delete
[320] Fix | Delete
def end_dl(self):
[321] Fix | Delete
self.ddpop(1)
[322] Fix | Delete
if self.list_stack: del self.list_stack[-1]
[323] Fix | Delete
[324] Fix | Delete
def do_dt(self, attrs):
[325] Fix | Delete
self.ddpop()
[326] Fix | Delete
[327] Fix | Delete
def do_dd(self, attrs):
[328] Fix | Delete
self.ddpop()
[329] Fix | Delete
self.formatter.push_margin('dd')
[330] Fix | Delete
self.list_stack.append(['dd', '', 0])
[331] Fix | Delete
[332] Fix | Delete
def ddpop(self, bl=0):
[333] Fix | Delete
self.formatter.end_paragraph(bl)
[334] Fix | Delete
if self.list_stack:
[335] Fix | Delete
if self.list_stack[-1][0] == 'dd':
[336] Fix | Delete
del self.list_stack[-1]
[337] Fix | Delete
self.formatter.pop_margin()
[338] Fix | Delete
[339] Fix | Delete
# --- Phrase Markup
[340] Fix | Delete
[341] Fix | Delete
# Idiomatic Elements
[342] Fix | Delete
[343] Fix | Delete
def start_cite(self, attrs): self.start_i(attrs)
[344] Fix | Delete
def end_cite(self): self.end_i()
[345] Fix | Delete
[346] Fix | Delete
def start_code(self, attrs): self.start_tt(attrs)
[347] Fix | Delete
def end_code(self): self.end_tt()
[348] Fix | Delete
[349] Fix | Delete
def start_em(self, attrs): self.start_i(attrs)
[350] Fix | Delete
def end_em(self): self.end_i()
[351] Fix | Delete
[352] Fix | Delete
def start_kbd(self, attrs): self.start_tt(attrs)
[353] Fix | Delete
def end_kbd(self): self.end_tt()
[354] Fix | Delete
[355] Fix | Delete
def start_samp(self, attrs): self.start_tt(attrs)
[356] Fix | Delete
def end_samp(self): self.end_tt()
[357] Fix | Delete
[358] Fix | Delete
def start_strong(self, attrs): self.start_b(attrs)
[359] Fix | Delete
def end_strong(self): self.end_b()
[360] Fix | Delete
[361] Fix | Delete
def start_var(self, attrs): self.start_i(attrs)
[362] Fix | Delete
def end_var(self): self.end_i()
[363] Fix | Delete
[364] Fix | Delete
# Typographic Elements
[365] Fix | Delete
[366] Fix | Delete
def start_i(self, attrs):
[367] Fix | Delete
self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
[368] Fix | Delete
def end_i(self):
[369] Fix | Delete
self.formatter.pop_font()
[370] Fix | Delete
[371] Fix | Delete
def start_b(self, attrs):
[372] Fix | Delete
self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS))
[373] Fix | Delete
def end_b(self):
[374] Fix | Delete
self.formatter.pop_font()
[375] Fix | Delete
[376] Fix | Delete
def start_tt(self, attrs):
[377] Fix | Delete
self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
[378] Fix | Delete
def end_tt(self):
[379] Fix | Delete
self.formatter.pop_font()
[380] Fix | Delete
[381] Fix | Delete
def start_a(self, attrs):
[382] Fix | Delete
href = ''
[383] Fix | Delete
name = ''
[384] Fix | Delete
type = ''
[385] Fix | Delete
for attrname, value in attrs:
[386] Fix | Delete
value = value.strip()
[387] Fix | Delete
if attrname == 'href':
[388] Fix | Delete
href = value
[389] Fix | Delete
if attrname == 'name':
[390] Fix | Delete
name = value
[391] Fix | Delete
if attrname == 'type':
[392] Fix | Delete
type = value.lower()
[393] Fix | Delete
self.anchor_bgn(href, name, type)
[394] Fix | Delete
[395] Fix | Delete
def end_a(self):
[396] Fix | Delete
self.anchor_end()
[397] Fix | Delete
[398] Fix | Delete
# --- Line Break
[399] Fix | Delete
[400] Fix | Delete
def do_br(self, attrs):
[401] Fix | Delete
self.formatter.add_line_break()
[402] Fix | Delete
[403] Fix | Delete
# --- Horizontal Rule
[404] Fix | Delete
[405] Fix | Delete
def do_hr(self, attrs):
[406] Fix | Delete
self.formatter.add_hor_rule()
[407] Fix | Delete
[408] Fix | Delete
# --- Image
[409] Fix | Delete
[410] Fix | Delete
def do_img(self, attrs):
[411] Fix | Delete
align = ''
[412] Fix | Delete
alt = '(image)'
[413] Fix | Delete
ismap = ''
[414] Fix | Delete
src = ''
[415] Fix | Delete
width = 0
[416] Fix | Delete
height = 0
[417] Fix | Delete
for attrname, value in attrs:
[418] Fix | Delete
if attrname == 'align':
[419] Fix | Delete
align = value
[420] Fix | Delete
if attrname == 'alt':
[421] Fix | Delete
alt = value
[422] Fix | Delete
if attrname == 'ismap':
[423] Fix | Delete
ismap = value
[424] Fix | Delete
if attrname == 'src':
[425] Fix | Delete
src = value
[426] Fix | Delete
if attrname == 'width':
[427] Fix | Delete
try: width = int(value)
[428] Fix | Delete
except ValueError: pass
[429] Fix | Delete
if attrname == 'height':
[430] Fix | Delete
try: height = int(value)
[431] Fix | Delete
except ValueError: pass
[432] Fix | Delete
self.handle_image(src, alt, ismap, align, width, height)
[433] Fix | Delete
[434] Fix | Delete
# --- Really Old Unofficial Deprecated Stuff
[435] Fix | Delete
[436] Fix | Delete
def do_plaintext(self, attrs):
[437] Fix | Delete
self.start_pre(attrs)
[438] Fix | Delete
self.setnomoretags() # Tell SGML parser
[439] Fix | Delete
[440] Fix | Delete
# --- Unhandled tags
[441] Fix | Delete
[442] Fix | Delete
def unknown_starttag(self, tag, attrs):
[443] Fix | Delete
pass
[444] Fix | Delete
[445] Fix | Delete
def unknown_endtag(self, tag):
[446] Fix | Delete
pass
[447] Fix | Delete
[448] Fix | Delete
[449] Fix | Delete
def test(args = None):
[450] Fix | Delete
import sys, formatter
[451] Fix | Delete
[452] Fix | Delete
if not args:
[453] Fix | Delete
args = sys.argv[1:]
[454] Fix | Delete
[455] Fix | Delete
silent = args and args[0] == '-s'
[456] Fix | Delete
if silent:
[457] Fix | Delete
del args[0]
[458] Fix | Delete
[459] Fix | Delete
if args:
[460] Fix | Delete
file = args[0]
[461] Fix | Delete
else:
[462] Fix | Delete
file = 'test.html'
[463] Fix | Delete
[464] Fix | Delete
if file == '-':
[465] Fix | Delete
f = sys.stdin
[466] Fix | Delete
else:
[467] Fix | Delete
try:
[468] Fix | Delete
f = open(file, 'r')
[469] Fix | Delete
except IOError, msg:
[470] Fix | Delete
print file, ":", msg
[471] Fix | Delete
sys.exit(1)
[472] Fix | Delete
[473] Fix | Delete
data = f.read()
[474] Fix | Delete
[475] Fix | Delete
if f is not sys.stdin:
[476] Fix | Delete
f.close()
[477] Fix | Delete
[478] Fix | Delete
if silent:
[479] Fix | Delete
f = formatter.NullFormatter()
[480] Fix | Delete
else:
[481] Fix | Delete
f = formatter.AbstractFormatter(formatter.DumbWriter())
[482] Fix | Delete
[483] Fix | Delete
p = HTMLParser(f)
[484] Fix | Delete
p.feed(data)
[485] Fix | Delete
p.close()
[486] Fix | Delete
[487] Fix | Delete
[488] Fix | Delete
if __name__ == '__main__':
[489] Fix | Delete
test()
[490] Fix | Delete
[491] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function