home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1517 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  14.4 KB  |  381 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.             if not url.startswith('file://'):
  197.                 navbar.append(HR())
  198.                 text = 'This article was downloaded by '
  199.                 p = PT(text, STRONG(__appname__), A(url, href = url), style = 'text-align:left; max-width: 100%; overflow: hidden;')
  200.                 p[0].tail = ' from '
  201.                 navbar.append(p)
  202.                 navbar.append(BR())
  203.             
  204.             navbar.append(BR())
  205.         elif art == number_of_articles_in_feed - 1:
  206.             pass
  207.         
  208.         next = 'article_%d' % (art + 1)
  209.         up = None if art == number_of_articles_in_feed - 1 else '..'
  210.         href = '%s%s/%s/index.html' % (prefix, up, next)
  211.         navbar.text = '| '
  212.         navbar.append(A('Next', href = href))
  213.         href = '%s../index.html#article_%d' % (prefix, art)
  214.         navbar.iterchildren(reversed = True).next().tail = ' | '
  215.         navbar.append(A('Section Menu', href = href))
  216.         href = '%s../../index.html#feed_%d' % (prefix, feed)
  217.         navbar.iterchildren(reversed = True).next().tail = ' | '
  218.         navbar.append(A('Main Menu', href = href))
  219.         if art > 0 and not bottom:
  220.             href = '%s../article_%d/index.html' % (prefix, art - 1)
  221.             navbar.iterchildren(reversed = True).next().tail = ' | '
  222.             navbar.append(A('Previous', href = href))
  223.         
  224.         navbar.iterchildren(reversed = True).next().tail = ' | '
  225.         if not bottom:
  226.             navbar.append(HR())
  227.         
  228.         self.root = HTML(head, BODY(navbar))
  229.  
  230.  
  231.  
  232. class TouchscreenIndexTemplate(Template):
  233.     
  234.     def _generate(self, title, masthead, datefmt, feeds, extra_css = None, style = None):
  235.         self.IS_HTML = False
  236.         if isinstance(datefmt, unicode):
  237.             datefmt = datefmt.encode(preferred_encoding)
  238.         
  239.         date = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y'))
  240.         masthead_p = etree.Element('p')
  241.         masthead_p.set('style', 'text-align:center')
  242.         masthead_img = etree.Element('img')
  243.         masthead_img.set('src', masthead)
  244.         masthead_img.set('alt', 'masthead')
  245.         masthead_p.append(masthead_img)
  246.         head = HEAD(TITLE(title))
  247.         if style:
  248.             head.append(STYLE(style, type = 'text/css'))
  249.         
  250.         if extra_css:
  251.             head.append(STYLE(extra_css, type = 'text/css'))
  252.         
  253.         toc = TABLE(CLASS('toc'), width = '100%', border = '0', cellpadding = '3px')
  254.         for i, feed in enumerate(feeds):
  255.             if feed:
  256.                 tr = TR()
  257.                 tr.append(TD(CLASS('calibre_rescale_120'), A(feed.title, href = 'feed_%d/index.html' % i)))
  258.                 tr.append(TD('%s' % len(feed.articles), style = 'text-align:right'))
  259.                 toc.append(tr)
  260.                 continue
  261.         
  262.         div = DIV(masthead_p, H3(CLASS('publish_date'), date), DIV(CLASS('divider')), toc)
  263.         self.root = HTML(head, BODY(div))
  264.  
  265.  
  266.  
  267. class TouchscreenFeedTemplate(Template):
  268.     
  269.     def _generate(self, f, feeds, cutoff, extra_css = None, style = None):
  270.         
  271.         def trim_title(title, clip = 18):
  272.             if len(title) > clip:
  273.                 tokens = title.split(' ')
  274.                 new_title_tokens = []
  275.                 new_title_len = 0
  276.                 if len(tokens[0]) > clip:
  277.                     return tokens[0][:clip] + '...'
  278.                 for token in tokens:
  279.                     if len(token) + new_title_len < clip:
  280.                         new_title_tokens.append(token)
  281.                         new_title_len += len(token)
  282.                         continue
  283.                     len(tokens[0]) > clip
  284.                     new_title_tokens.append('...')
  285.                     title = ' '.join(new_title_tokens)
  286.                 
  287.             
  288.             return title
  289.  
  290.         self.IS_HTML = False
  291.         feed = feeds[f]
  292.         navbar_t = TABLE(CLASS('touchscreen_navbar'))
  293.         navbar_tr = TR()
  294.         link = ''
  295.         if f > 0:
  296.             link = A(CLASS('feed_link'), trim_title(feeds[f - 1].title), href = '../feed_%d/index.html' % int(f - 1))
  297.         
  298.         navbar_tr.append(TD(CLASS('feed_prev'), link))
  299.         link = A('Sections', href = '../index.html')
  300.         navbar_tr.append(TD(CLASS('feed_up'), link))
  301.         link = ''
  302.         if f < len(feeds) - 1:
  303.             link = A(CLASS('feed_link'), trim_title(feeds[f + 1].title), href = '../feed_%d/index.html' % int(f + 1))
  304.         
  305.         navbar_tr.append(TD(CLASS('feed_next'), link))
  306.         navbar_t.append(navbar_tr)
  307.         top_navbar = navbar_t
  308.         bottom_navbar = copy.copy(navbar_t)
  309.         head = HEAD(TITLE(feed.title))
  310.         if style:
  311.             head.append(STYLE(style, type = 'text/css'))
  312.         
  313.         if extra_css:
  314.             head.append(STYLE(extra_css, type = 'text/css'))
  315.         
  316.         body = BODY(style = 'page-break-before:always')
  317.         div = DIV(top_navbar, H2(feed.title, CLASS('feed_title')))
  318.         body.append(div)
  319.         if getattr(feed, 'image', None):
  320.             None(div.append(DIV(IMG = 'alt' if feed.image_alt else '', src = feed.image_url), CLASS('calibre_feed_image')))
  321.         
  322.         if getattr(feed, 'description', None):
  323.             d = DIV(feed.description, CLASS('calibre_feed_description', 'calibre_rescale_80'))
  324.             d.append(BR())
  325.             div.append(d)
  326.         
  327.         toc = TABLE(CLASS('toc'), width = '100%', border = '0', cellpadding = '3px')
  328.         for i, article in enumerate(feed.articles):
  329.             if not getattr(article, 'downloaded', False):
  330.                 continue
  331.             
  332.             tr = TR()
  333.             div_td = DIV(CLASS('article_summary'), A(article.title, CLASS('summary_headline', 'calibre_rescale_120', href = article.url)))
  334.             if article.author:
  335.                 div_td.append(DIV(article.author, CLASS('summary_byline', 'calibre_rescale_100')))
  336.             
  337.             if article.summary:
  338.                 div_td.append(DIV(cutoff(article.text_summary), CLASS('summary_text', 'calibre_rescale_100')))
  339.             
  340.             tr.append(TD(div_td))
  341.             toc.append(tr)
  342.         
  343.         div.append(toc)
  344.         div.append(BR())
  345.         div.append(bottom_navbar)
  346.         self.root = HTML(head, body)
  347.  
  348.  
  349.  
  350. class TouchscreenNavBarTemplate(Template):
  351.     
  352.     def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix = '', center = True, extra_css = None, style = None):
  353.         head = HEAD(TITLE('navbar'))
  354.         if style:
  355.             head.append(STYLE(style, type = 'text/css'))
  356.         
  357.         if extra_css:
  358.             head.append(STYLE(extra_css, type = 'text/css'))
  359.         
  360.         navbar = DIV()
  361.         navbar_t = TABLE(CLASS('touchscreen_navbar'))
  362.         navbar_tr = TR()
  363.         if art > 0:
  364.             link = A(CLASS('article_link'), 'Previous', href = '%s../article_%d/index.html' % (prefix, art - 1))
  365.             navbar_tr.append(TD(CLASS('article_prev'), link))
  366.         else:
  367.             navbar_tr.append(TD(CLASS('article_prev'), ''))
  368.         link = A(CLASS('articles_link'), 'Articles', href = '%s../index.html#article_%d' % (prefix, art))
  369.         navbar_tr.append(TD(CLASS('article_articles_list'), link))
  370.         link = A(CLASS('sections_link'), 'Sections', href = '%s../../index.html#feed_%d' % (prefix, feed))
  371.         navbar_tr.append(TD(CLASS('article_sections_list'), link))
  372.         next = None if art == number_of_articles_in_feed - 1 else 'article_%d' % (art + 1)
  373.         up = None if art == number_of_articles_in_feed - 1 else '..'
  374.         link = A(CLASS('article_link'), 'Next', href = '%s%s/%s/index.html' % (prefix, up, next))
  375.         navbar_tr.append(TD(CLASS('article_next'), link))
  376.         navbar_t.append(navbar_tr)
  377.         navbar.append(navbar_t)
  378.         self.root = HTML(head, BODY(navbar))
  379.  
  380.  
  381.