home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / htmllib.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  16.1 KB  |  541 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. from warnings import warnpy3k
  5. warnpy3k('the htmllib module has been removed in Python 3.0', stacklevel = 2)
  6. del warnpy3k
  7. import sgmllib
  8. from formatter import AS_IS
  9. __all__ = [
  10.     'HTMLParser',
  11.     'HTMLParseError']
  12.  
  13. class HTMLParseError(sgmllib.SGMLParseError):
  14.     pass
  15.  
  16.  
  17. class HTMLParser(sgmllib.SGMLParser):
  18.     from htmlentitydefs import entitydefs
  19.     
  20.     def __init__(self, formatter, verbose = 0):
  21.         sgmllib.SGMLParser.__init__(self, verbose)
  22.         self.formatter = formatter
  23.  
  24.     
  25.     def error(self, message):
  26.         raise HTMLParseError(message)
  27.  
  28.     
  29.     def reset(self):
  30.         sgmllib.SGMLParser.reset(self)
  31.         self.savedata = None
  32.         self.isindex = 0
  33.         self.title = None
  34.         self.base = None
  35.         self.anchor = None
  36.         self.anchorlist = []
  37.         self.nofill = 0
  38.         self.list_stack = []
  39.  
  40.     
  41.     def handle_data(self, data):
  42.         if self.savedata is not None:
  43.             self.savedata = self.savedata + data
  44.         elif self.nofill:
  45.             self.formatter.add_literal_data(data)
  46.         else:
  47.             self.formatter.add_flowing_data(data)
  48.  
  49.     
  50.     def save_bgn(self):
  51.         self.savedata = ''
  52.  
  53.     
  54.     def save_end(self):
  55.         data = self.savedata
  56.         self.savedata = None
  57.         if not self.nofill:
  58.             data = ' '.join(data.split())
  59.         return data
  60.  
  61.     
  62.     def anchor_bgn(self, href, name, type):
  63.         self.anchor = href
  64.         if self.anchor:
  65.             self.anchorlist.append(href)
  66.  
  67.     
  68.     def anchor_end(self):
  69.         if self.anchor:
  70.             self.handle_data('[%d]' % len(self.anchorlist))
  71.             self.anchor = None
  72.  
  73.     
  74.     def handle_image(self, src, alt, *args):
  75.         self.handle_data(alt)
  76.  
  77.     
  78.     def start_html(self, attrs):
  79.         pass
  80.  
  81.     
  82.     def end_html(self):
  83.         pass
  84.  
  85.     
  86.     def start_head(self, attrs):
  87.         pass
  88.  
  89.     
  90.     def end_head(self):
  91.         pass
  92.  
  93.     
  94.     def start_body(self, attrs):
  95.         pass
  96.  
  97.     
  98.     def end_body(self):
  99.         pass
  100.  
  101.     
  102.     def start_title(self, attrs):
  103.         self.save_bgn()
  104.  
  105.     
  106.     def end_title(self):
  107.         self.title = self.save_end()
  108.  
  109.     
  110.     def do_base(self, attrs):
  111.         for a, v in attrs:
  112.             if a == 'href':
  113.                 self.base = v
  114.                 continue
  115.  
  116.     
  117.     def do_isindex(self, attrs):
  118.         self.isindex = 1
  119.  
  120.     
  121.     def do_link(self, attrs):
  122.         pass
  123.  
  124.     
  125.     def do_meta(self, attrs):
  126.         pass
  127.  
  128.     
  129.     def do_nextid(self, attrs):
  130.         pass
  131.  
  132.     
  133.     def start_h1(self, attrs):
  134.         self.formatter.end_paragraph(1)
  135.         self.formatter.push_font(('h1', 0, 1, 0))
  136.  
  137.     
  138.     def end_h1(self):
  139.         self.formatter.end_paragraph(1)
  140.         self.formatter.pop_font()
  141.  
  142.     
  143.     def start_h2(self, attrs):
  144.         self.formatter.end_paragraph(1)
  145.         self.formatter.push_font(('h2', 0, 1, 0))
  146.  
  147.     
  148.     def end_h2(self):
  149.         self.formatter.end_paragraph(1)
  150.         self.formatter.pop_font()
  151.  
  152.     
  153.     def start_h3(self, attrs):
  154.         self.formatter.end_paragraph(1)
  155.         self.formatter.push_font(('h3', 0, 1, 0))
  156.  
  157.     
  158.     def end_h3(self):
  159.         self.formatter.end_paragraph(1)
  160.         self.formatter.pop_font()
  161.  
  162.     
  163.     def start_h4(self, attrs):
  164.         self.formatter.end_paragraph(1)
  165.         self.formatter.push_font(('h4', 0, 1, 0))
  166.  
  167.     
  168.     def end_h4(self):
  169.         self.formatter.end_paragraph(1)
  170.         self.formatter.pop_font()
  171.  
  172.     
  173.     def start_h5(self, attrs):
  174.         self.formatter.end_paragraph(1)
  175.         self.formatter.push_font(('h5', 0, 1, 0))
  176.  
  177.     
  178.     def end_h5(self):
  179.         self.formatter.end_paragraph(1)
  180.         self.formatter.pop_font()
  181.  
  182.     
  183.     def start_h6(self, attrs):
  184.         self.formatter.end_paragraph(1)
  185.         self.formatter.push_font(('h6', 0, 1, 0))
  186.  
  187.     
  188.     def end_h6(self):
  189.         self.formatter.end_paragraph(1)
  190.         self.formatter.pop_font()
  191.  
  192.     
  193.     def do_p(self, attrs):
  194.         self.formatter.end_paragraph(1)
  195.  
  196.     
  197.     def start_pre(self, attrs):
  198.         self.formatter.end_paragraph(1)
  199.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  200.         self.nofill = self.nofill + 1
  201.  
  202.     
  203.     def end_pre(self):
  204.         self.formatter.end_paragraph(1)
  205.         self.formatter.pop_font()
  206.         self.nofill = max(0, self.nofill - 1)
  207.  
  208.     
  209.     def start_xmp(self, attrs):
  210.         self.start_pre(attrs)
  211.         self.setliteral('xmp')
  212.  
  213.     
  214.     def end_xmp(self):
  215.         self.end_pre()
  216.  
  217.     
  218.     def start_listing(self, attrs):
  219.         self.start_pre(attrs)
  220.         self.setliteral('listing')
  221.  
  222.     
  223.     def end_listing(self):
  224.         self.end_pre()
  225.  
  226.     
  227.     def start_address(self, attrs):
  228.         self.formatter.end_paragraph(0)
  229.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  230.  
  231.     
  232.     def end_address(self):
  233.         self.formatter.end_paragraph(0)
  234.         self.formatter.pop_font()
  235.  
  236.     
  237.     def start_blockquote(self, attrs):
  238.         self.formatter.end_paragraph(1)
  239.         self.formatter.push_margin('blockquote')
  240.  
  241.     
  242.     def end_blockquote(self):
  243.         self.formatter.end_paragraph(1)
  244.         self.formatter.pop_margin()
  245.  
  246.     
  247.     def start_ul(self, attrs):
  248.         self.formatter.end_paragraph(not (self.list_stack))
  249.         self.formatter.push_margin('ul')
  250.         self.list_stack.append([
  251.             'ul',
  252.             '*',
  253.             0])
  254.  
  255.     
  256.     def end_ul(self):
  257.         if self.list_stack:
  258.             del self.list_stack[-1]
  259.         self.formatter.end_paragraph(not (self.list_stack))
  260.         self.formatter.pop_margin()
  261.  
  262.     
  263.     def do_li(self, attrs):
  264.         self.formatter.end_paragraph(0)
  265.         if self.list_stack:
  266.             (dummy, label, counter) = top = self.list_stack[-1]
  267.             top[2] = counter = counter + 1
  268.         else:
  269.             (label, counter) = ('*', 0)
  270.         self.formatter.add_label_data(label, counter)
  271.  
  272.     
  273.     def start_ol(self, attrs):
  274.         self.formatter.end_paragraph(not (self.list_stack))
  275.         self.formatter.push_margin('ol')
  276.         label = '1.'
  277.         for a, v in attrs:
  278.             if a == 'type' or len(v) == 1:
  279.                 v = v + '.'
  280.             label = v
  281.         
  282.         self.list_stack.append([
  283.             'ol',
  284.             label,
  285.             0])
  286.  
  287.     
  288.     def end_ol(self):
  289.         if self.list_stack:
  290.             del self.list_stack[-1]
  291.         self.formatter.end_paragraph(not (self.list_stack))
  292.         self.formatter.pop_margin()
  293.  
  294.     
  295.     def start_menu(self, attrs):
  296.         self.start_ul(attrs)
  297.  
  298.     
  299.     def end_menu(self):
  300.         self.end_ul()
  301.  
  302.     
  303.     def start_dir(self, attrs):
  304.         self.start_ul(attrs)
  305.  
  306.     
  307.     def end_dir(self):
  308.         self.end_ul()
  309.  
  310.     
  311.     def start_dl(self, attrs):
  312.         self.formatter.end_paragraph(1)
  313.         self.list_stack.append([
  314.             'dl',
  315.             '',
  316.             0])
  317.  
  318.     
  319.     def end_dl(self):
  320.         self.ddpop(1)
  321.         if self.list_stack:
  322.             del self.list_stack[-1]
  323.  
  324.     
  325.     def do_dt(self, attrs):
  326.         self.ddpop()
  327.  
  328.     
  329.     def do_dd(self, attrs):
  330.         self.ddpop()
  331.         self.formatter.push_margin('dd')
  332.         self.list_stack.append([
  333.             'dd',
  334.             '',
  335.             0])
  336.  
  337.     
  338.     def ddpop(self, bl = 0):
  339.         self.formatter.end_paragraph(bl)
  340.         if self.list_stack and self.list_stack[-1][0] == 'dd':
  341.             del self.list_stack[-1]
  342.             self.formatter.pop_margin()
  343.         
  344.  
  345.     
  346.     def start_cite(self, attrs):
  347.         self.start_i(attrs)
  348.  
  349.     
  350.     def end_cite(self):
  351.         self.end_i()
  352.  
  353.     
  354.     def start_code(self, attrs):
  355.         self.start_tt(attrs)
  356.  
  357.     
  358.     def end_code(self):
  359.         self.end_tt()
  360.  
  361.     
  362.     def start_em(self, attrs):
  363.         self.start_i(attrs)
  364.  
  365.     
  366.     def end_em(self):
  367.         self.end_i()
  368.  
  369.     
  370.     def start_kbd(self, attrs):
  371.         self.start_tt(attrs)
  372.  
  373.     
  374.     def end_kbd(self):
  375.         self.end_tt()
  376.  
  377.     
  378.     def start_samp(self, attrs):
  379.         self.start_tt(attrs)
  380.  
  381.     
  382.     def end_samp(self):
  383.         self.end_tt()
  384.  
  385.     
  386.     def start_strong(self, attrs):
  387.         self.start_b(attrs)
  388.  
  389.     
  390.     def end_strong(self):
  391.         self.end_b()
  392.  
  393.     
  394.     def start_var(self, attrs):
  395.         self.start_i(attrs)
  396.  
  397.     
  398.     def end_var(self):
  399.         self.end_i()
  400.  
  401.     
  402.     def start_i(self, attrs):
  403.         self.formatter.push_font((AS_IS, 1, AS_IS, AS_IS))
  404.  
  405.     
  406.     def end_i(self):
  407.         self.formatter.pop_font()
  408.  
  409.     
  410.     def start_b(self, attrs):
  411.         self.formatter.push_font((AS_IS, AS_IS, 1, AS_IS))
  412.  
  413.     
  414.     def end_b(self):
  415.         self.formatter.pop_font()
  416.  
  417.     
  418.     def start_tt(self, attrs):
  419.         self.formatter.push_font((AS_IS, AS_IS, AS_IS, 1))
  420.  
  421.     
  422.     def end_tt(self):
  423.         self.formatter.pop_font()
  424.  
  425.     
  426.     def start_a(self, attrs):
  427.         href = ''
  428.         name = ''
  429.         type = ''
  430.         for attrname, value in attrs:
  431.             value = value.strip()
  432.             if attrname == 'href':
  433.                 href = value
  434.             if attrname == 'name':
  435.                 name = value
  436.             if attrname == 'type':
  437.                 type = value.lower()
  438.                 continue
  439.         self.anchor_bgn(href, name, type)
  440.  
  441.     
  442.     def end_a(self):
  443.         self.anchor_end()
  444.  
  445.     
  446.     def do_br(self, attrs):
  447.         self.formatter.add_line_break()
  448.  
  449.     
  450.     def do_hr(self, attrs):
  451.         self.formatter.add_hor_rule()
  452.  
  453.     
  454.     def do_img(self, attrs):
  455.         align = ''
  456.         alt = '(image)'
  457.         ismap = ''
  458.         src = ''
  459.         width = 0
  460.         height = 0
  461.         for attrname, value in attrs:
  462.             if attrname == 'align':
  463.                 align = value
  464.             if attrname == 'alt':
  465.                 alt = value
  466.             if attrname == 'ismap':
  467.                 ismap = value
  468.             if attrname == 'src':
  469.                 src = value
  470.             if attrname == 'width':
  471.                 
  472.                 try:
  473.                     width = int(value)
  474.                 except ValueError:
  475.                     pass
  476.                 
  477.  
  478.             if attrname == 'height':
  479.                 
  480.                 try:
  481.                     height = int(value)
  482.                 except ValueError:
  483.                     pass
  484.                 
  485.  
  486.         self.handle_image(src, alt, ismap, align, width, height)
  487.  
  488.     
  489.     def do_plaintext(self, attrs):
  490.         self.start_pre(attrs)
  491.         self.setnomoretags()
  492.  
  493.     
  494.     def unknown_starttag(self, tag, attrs):
  495.         pass
  496.  
  497.     
  498.     def unknown_endtag(self, tag):
  499.         pass
  500.  
  501.  
  502.  
  503. def test(args = None):
  504.     import sys
  505.     import formatter
  506.     if not args:
  507.         args = sys.argv[1:]
  508.     if args:
  509.         pass
  510.     silent = args[0] == '-s'
  511.     if silent:
  512.         del args[0]
  513.     if args:
  514.         file = args[0]
  515.     else:
  516.         file = 'test.html'
  517.     if file == '-':
  518.         f = sys.stdin
  519.     else:
  520.         
  521.         try:
  522.             f = open(file, 'r')
  523.         except IOError:
  524.             msg = None
  525.             print file, ':', msg
  526.             sys.exit(1)
  527.  
  528.     data = f.read()
  529.     if f is not sys.stdin:
  530.         f.close()
  531.     if silent:
  532.         f = formatter.NullFormatter()
  533.     else:
  534.         f = formatter.AbstractFormatter(formatter.DumbWriter())
  535.     p = HTMLParser(f)
  536.     p.feed(data)
  537.     p.close()
  538.  
  539. if __name__ == '__main__':
  540.     test()
  541.