home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_597 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  16.2 KB  |  571 lines

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