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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  6. import copy
  7. from lxml import html, etree
  8. from lxml.html.builder import HTML, HEAD, TITLE, STYLE, DIV, BODY, STRONG, BR, SPAN, A, HR, UL, LI, H2, H3, IMG, P as PT, TABLE, TD, TR
  9. from calibre import preferred_encoding, strftime, isbytestring
  10.  
  11. def CLASS(*args, **kwargs):
  12.     kwargs['class'] = ' '.join(args)
  13.     return kwargs
  14.  
  15.  
  16. class Template(object):
  17.     IS_HTML = True
  18.     
  19.     def generate(self, *args, **kwargs):
  20.         if not kwargs.has_key('style'):
  21.             kwargs['style'] = ''
  22.         
  23.         for key in kwargs.keys():
  24.             if isbytestring(kwargs[key]):
  25.                 kwargs[key] = kwargs[key].decode('utf-8', 'replace')
  26.             
  27.             if kwargs[key] is None:
  28.                 kwargs[key] = u''
  29.                 continue
  30.         
  31.         args = list(args)
  32.         for i in range(len(args)):
  33.             if isbytestring(args[i]):
  34.                 args[i] = args[i].decode('utf-8', 'replace')
  35.             
  36.             if args[i] is None:
  37.                 args[i] = u''
  38.                 continue
  39.         
  40.         self._generate(*args, **kwargs)
  41.         return self
  42.  
  43.     
  44.     def render(self, *args, **kwargs):
  45.         if self.IS_HTML:
  46.             return html.tostring(self.root, encoding = 'utf-8', include_meta_content_type = True, pretty_print = True)
  47.         return etree.tostring(self.root, encoding = 'utf-8', xml_declaration = True, pretty_print = True)
  48.  
  49.  
  50.  
  51. class EmbeddedContent(Template):
  52.     
  53.     def _generate(self, article, style = None, extra_css = None):
  54.         content = None if article.content else ''
  55.         summary = None if article.summary else ''
  56.         text = None if len(content) > len(summary) else summary
  57.         head = HEAD(TITLE(article.title))
  58.         if style:
  59.             head.append(STYLE(style, type = 'text/css'))
  60.         
  61.         if extra_css:
  62.             head.append(STYLE(extra_css, type = 'text/css'))
  63.         
  64.         if isbytestring(text):
  65.             text = text.decode('utf-8', 'replace')
  66.         
  67.         elements = html.fragments_fromstring(text)
  68.         self.root = HTML(head, BODY(H2(article.title), DIV()))
  69.         div = self.root.find('body').find('div')
  70.         if elements and isinstance(elements[0], unicode):
  71.             div.text = elements[0]
  72.             elements = list(elements)[1:]
  73.         
  74.         for elem in elements:
  75.             if hasattr(elem, 'getparent'):
  76.                 elem.getparent().remove(elem)
  77.             else:
  78.                 elem = SPAN(elem)
  79.             div.append(elem)
  80.         
  81.  
  82.  
  83.  
  84. class IndexTemplate(Template):
  85.     
  86.     def _generate(self, title, masthead, datefmt, feeds, extra_css = None, style = None):
  87.         self.IS_HTML = False
  88.         if isinstance(datefmt, unicode):
  89.             datefmt = datefmt.encode(preferred_encoding)
  90.         
  91.         date = strftime(datefmt)
  92.         head = HEAD(TITLE(title))
  93.         if style:
  94.             head.append(STYLE(style, type = 'text/css'))
  95.         
  96.         if extra_css:
  97.             head.append(STYLE(extra_css, type = 'text/css'))
  98.         
  99.         ul = UL(CLASS('calibre_feed_list'))
  100.         for i, feed in enumerate(feeds):
  101.             if feed:
  102.                 li = LI(A(feed.title, CLASS('feed', 'calibre_rescale_120', href = 'feed_%d/index.html' % i)), id = 'feed_%d' % i)
  103.                 ul.append(li)
  104.                 continue
  105.         
  106.         div = DIV(PT(IMG(src = masthead, alt = 'masthead'), style = 'text-align:center'), PT(date, style = 'text-align:right'), ul, CLASS('calibre_rescale_100'))
  107.         self.root = HTML(head, BODY(div))
  108.  
  109.  
  110.  
  111. class FeedTemplate(Template):
  112.     
  113.     def get_navbar(self, f, feeds, top = True):
  114.         if len(feeds) < 2:
  115.             return DIV()
  116.         navbar = DIV('| ', CLASS('calibre_navbar', 'calibre_rescale_70', style = 'text-align:center'))
  117.         if not top:
  118.             hr = HR()
  119.             navbar.append(hr)
  120.             navbar.text = None
  121.             hr.tail = '| '
  122.         
  123.         if f + 1 < len(feeds):
  124.             link = A('Next section', href = '../feed_%d/index.html' % (f + 1))
  125.             link.tail = ' | '
  126.             navbar.append(link)
  127.         
  128.         link = A('Main menu', href = '../index.html')
  129.         link.tail = ' | '
  130.         navbar.append(link)
  131.         if f > 0:
  132.             link = A('Previous section', href = '../feed_%d/index.html' % (f - 1))
  133.             link.tail = ' |'
  134.             navbar.append(link)
  135.         
  136.         if top:
  137.             navbar.append(HR())
  138.         
  139.         return navbar
  140.  
  141.     
  142.     def _generate(self, f, feeds, cutoff, extra_css = None, style = None):
  143.         feed = feeds[f]
  144.         head = HEAD(TITLE(feed.title))
  145.         if style:
  146.             head.append(STYLE(style, type = 'text/css'))
  147.         
  148.         if extra_css:
  149.             head.append(STYLE(extra_css, type = 'text/css'))
  150.         
  151.         body = BODY(style = 'page-break-before:always')
  152.         body.append(self.get_navbar(f, feeds))
  153.         div = DIV(H2(feed.title, CLASS('calibre_feed_title', 'calibre_rescale_160')), CLASS('calibre_rescale_100'))
  154.         body.append(div)
  155.         if getattr(feed, 'image', None):
  156.             None(div.append(DIV(IMG = 'alt' if feed.image_alt else '', src = feed.image_url), CLASS('calibre_feed_image')))
  157.         
  158.         if getattr(feed, 'description', None):
  159.             d = DIV(feed.description, CLASS('calibre_feed_description', 'calibre_rescale_80'))
  160.             d.append(BR())
  161.             div.append(d)
  162.         
  163.         ul = UL(CLASS('calibre_article_list'))
  164.         for i, article in enumerate(feed.articles):
  165.             if not getattr(article, 'downloaded', False):
  166.                 continue
  167.             
  168.             li = LI(A(article.title, CLASS('article calibre_rescale_120', href = article.url)), SPAN(article.formatted_date, CLASS('article_date')), CLASS('calibre_rescale_100', id = 'article_%d' % i, style = 'padding-bottom:0.5em'))
  169.             if article.summary:
  170.                 li.append(DIV(cutoff(article.text_summary), CLASS('article_description', 'calibre_rescale_70')))
  171.             
  172.             ul.append(li)
  173.         
  174.         div.append(ul)
  175.         div.append(self.get_navbar(f, feeds, top = False))
  176.         self.root = HTML(head, body)
  177.  
  178.  
  179.  
  180. class NavBarTemplate(Template):
  181.     
  182.     def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix = '', center = True, extra_css = None, style = None):
  183.         head = HEAD(TITLE('navbar'))
  184.         if style:
  185.             head.append(STYLE(style, type = 'text/css'))
  186.         
  187.         if extra_css:
  188.             head.append(STYLE(extra_css, type = 'text/css'))
  189.         
  190.         if prefix and not prefix.endswith('/'):
  191.             prefix += '/'
  192.         
  193.         align = None if center else 'left'
  194.         navbar = DIV(CLASS('calibre_navbar', 'calibre_rescale_70', style = 'text-align:' + align))
  195.         if bottom:
  196.             navbar.append(HR())
  197.             text = 'This article was downloaded by '
  198.             p = PT(text, STRONG(__appname__), A(url, href = url), style = 'text-align:left')
  199.             p[0].tail = ' from '
  200.             navbar.append(p)
  201.             navbar.append(BR())
  202.             navbar.append(BR())
  203.         elif art == number_of_articles_in_feed - 1:
  204.             pass
  205.         
  206.         next = 'article_%d' % (art + 1)
  207.         up = None if art == number_of_articles_in_feed - 1 else '..'
  208.         href = '%s%s/%s/index.html' % (prefix, up, next)
  209.         navbar.text = '| '
  210.         navbar.append(A('Next', href = href))
  211.         href = '%s../index.html#article_%d' % (prefix, art)
  212.         navbar.iterchildren(reversed = True).next().tail = ' | '
  213.         navbar.append(A('Section Menu', href = href))
  214.         href = '%s../../index.html#feed_%d' % (prefix, feed)
  215.         navbar.iterchildren(reversed = True).next().tail = ' | '
  216.         navbar.append(A('Main Menu', href = href))
  217.         if art > 0 and not bottom:
  218.             href = '%s../article_%d/index.html' % (prefix, art - 1)
  219.             navbar.iterchildren(reversed = True).next().tail = ' | '
  220.             navbar.append(A('Previous', href = href))
  221.         
  222.         navbar.iterchildren(reversed = True).next().tail = ' | '
  223.         if not bottom:
  224.             navbar.append(HR())
  225.         
  226.         self.root = HTML(head, BODY(navbar))
  227.  
  228.  
  229.  
  230. class TouchscreenIndexTemplate(Template):
  231.     
  232.     def _generate(self, title, masthead, datefmt, feeds, extra_css = None, style = None):
  233.         self.IS_HTML = False
  234.         if isinstance(datefmt, unicode):
  235.             datefmt = datefmt.encode(preferred_encoding)
  236.         
  237.         date = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y'))
  238.         masthead_p = etree.Element('p')
  239.         masthead_p.set('style', 'text-align:center')
  240.         masthead_img = etree.Element('img')
  241.         masthead_img.set('src', masthead)
  242.         masthead_img.set('alt', 'masthead')
  243.         masthead_p.append(masthead_img)
  244.         head = HEAD(TITLE(title))
  245.         if style:
  246.             head.append(STYLE(style, type = 'text/css'))
  247.         
  248.         if extra_css:
  249.             head.append(STYLE(extra_css, type = 'text/css'))
  250.         
  251.         toc = TABLE(CLASS('toc'), width = '100%', border = '0', cellpadding = '3px')
  252.         for i, feed in enumerate(feeds):
  253.             if feed:
  254.                 tr = TR()
  255.                 tr.append(TD(CLASS('calibre_rescale_120'), A(feed.title, href = 'feed_%d/index.html' % i)))
  256.                 tr.append(TD('%s' % len(feed.articles), style = 'text-align:right'))
  257.                 toc.append(tr)
  258.                 continue
  259.         
  260.         div = DIV(masthead_p, H3(CLASS('publish_date'), date), DIV(CLASS('divider')), toc)
  261.         self.root = HTML(head, BODY(div))
  262.  
  263.  
  264.  
  265. class TouchscreenFeedTemplate(Template):
  266.     
  267.     def _generate(self, f, feeds, cutoff, extra_css = None, style = None):
  268.         
  269.         def trim_title(title, clip = 18):
  270.             if len(title) > clip:
  271.                 tokens = title.split(' ')
  272.                 new_title_tokens = []
  273.                 new_title_len = 0
  274.                 if len(tokens[0]) > clip:
  275.                     return tokens[0][:clip] + '...'
  276.                 for token in tokens:
  277.                     if len(token) + new_title_len < clip:
  278.                         new_title_tokens.append(token)
  279.                         new_title_len += len(token)
  280.                         continue
  281.                     len(tokens[0]) > clip
  282.                     new_title_tokens.append('...')
  283.                     title = ' '.join(new_title_tokens)
  284.                 
  285.             
  286.             return title
  287.  
  288.         self.IS_HTML = False
  289.         feed = feeds[f]
  290.         navbar_t = TABLE(CLASS('touchscreen_navbar'))
  291.         navbar_tr = TR()
  292.         link = ''
  293.         if f > 0:
  294.             link = A(CLASS('feed_link'), trim_title(feeds[f - 1].title), href = '../feed_%d/index.html' % int(f - 1))
  295.         
  296.         navbar_tr.append(TD(CLASS('feed_prev'), link))
  297.         link = A('Sections', href = '../index.html')
  298.         navbar_tr.append(TD(CLASS('feed_up'), link))
  299.         link = ''
  300.         if f < len(feeds) - 1:
  301.             link = A(CLASS('feed_link'), trim_title(feeds[f + 1].title), href = '../feed_%d/index.html' % int(f + 1))
  302.         
  303.         navbar_tr.append(TD(CLASS('feed_next'), link))
  304.         navbar_t.append(navbar_tr)
  305.         top_navbar = navbar_t
  306.         bottom_navbar = copy.copy(navbar_t)
  307.         head = HEAD(TITLE(feed.title))
  308.         if style:
  309.             head.append(STYLE(style, type = 'text/css'))
  310.         
  311.         if extra_css:
  312.             head.append(STYLE(extra_css, type = 'text/css'))
  313.         
  314.         body = BODY(style = 'page-break-before:always')
  315.         div = DIV(top_navbar, H2(feed.title, CLASS('feed_title')))
  316.         body.append(div)
  317.         if getattr(feed, 'image', None):
  318.             None(div.append(DIV(IMG = 'alt' if feed.image_alt else '', src = feed.image_url), CLASS('calibre_feed_image')))
  319.         
  320.         if getattr(feed, 'description', None):
  321.             d = DIV(feed.description, CLASS('calibre_feed_description', 'calibre_rescale_80'))
  322.             d.append(BR())
  323.             div.append(d)
  324.         
  325.         toc = TABLE(CLASS('toc'), width = '100%', border = '0', cellpadding = '3px')
  326.         for i, article in enumerate(feed.articles):
  327.             if not getattr(article, 'downloaded', False):
  328.                 continue
  329.             
  330.             tr = TR()
  331.             div_td = DIV(CLASS('article_summary'), A(article.title, CLASS('summary_headline', 'calibre_rescale_120', href = article.url)))
  332.             if article.author:
  333.                 div_td.append(DIV(article.author, CLASS('summary_byline', 'calibre_rescale_100')))
  334.             
  335.             if article.summary:
  336.                 div_td.append(DIV(cutoff(article.text_summary), CLASS('summary_text', 'calibre_rescale_100')))
  337.             
  338.             tr.append(TD(div_td))
  339.             toc.append(tr)
  340.         
  341.         div.append(toc)
  342.         div.append(BR())
  343.         div.append(bottom_navbar)
  344.         self.root = HTML(head, body)
  345.  
  346.  
  347.  
  348. class TouchscreenNavBarTemplate(Template):
  349.     
  350.     def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix = '', center = True, extra_css = None, style = None):
  351.         head = HEAD(TITLE('navbar'))
  352.         if style:
  353.             head.append(STYLE(style, type = 'text/css'))
  354.         
  355.         if extra_css:
  356.             head.append(STYLE(extra_css, type = 'text/css'))
  357.         
  358.         navbar = DIV()
  359.         navbar_t = TABLE(CLASS('touchscreen_navbar'))
  360.         navbar_tr = TR()
  361.         if art > 0:
  362.             link = A(CLASS('article_link'), 'Previous', href = '%s../article_%d/index.html' % (prefix, art - 1))
  363.             navbar_tr.append(TD(CLASS('article_prev'), link))
  364.         else:
  365.             navbar_tr.append(TD(CLASS('article_prev'), ''))
  366.         link = A(CLASS('articles_link'), 'Articles', href = '%s../index.html#article_%d' % (prefix, art))
  367.         navbar_tr.append(TD(CLASS('article_articles_list'), link))
  368.         link = A(CLASS('sections_link'), 'Sections', href = '%s../../index.html#feed_%d' % (prefix, feed))
  369.         navbar_tr.append(TD(CLASS('article_sections_list'), link))
  370.         next = None if art == number_of_articles_in_feed - 1 else 'article_%d' % (art + 1)
  371.         up = None if art == number_of_articles_in_feed - 1 else '..'
  372.         link = A(CLASS('article_link'), 'Next', href = '%s%s/%s/index.html' % (prefix, up, next))
  373.         navbar_tr.append(TD(CLASS('article_next'), link))
  374.         navbar_t.append(navbar_tr)
  375.         navbar.append(navbar_t)
  376.         self.root = HTML(head, BODY(navbar))
  377.  
  378.  
  379.