def set_custom_embed_code(self, data): """ Return the code that embed the code. Could be with the original size or the custom chosen. """ if 'embed_html' not in data: return tree = etree.HTML(data['embed_html']) sel = cssselect.CSSSelector('body > *') el = sel(tree) # add a div around if there is more than one element into code if len(el) > 1: el = DIV(*el) else: el = el[0] # width and height attributes should not be set in a div tag if el.tag in ['iframe', 'object']: if data.get('width', None): el.attrib['width'] = data['width'] and str( data['width']) or el.attrib['width'] if data.get('height', None): el.attrib['height'] = data['height'] and str( data['height']) or el.attrib['height'] data['embed_html'] = sanitize_iframe_tag(html.tostring(el))
def apply_frames(self): for run in filter(None, self.framed): style = run[0][1] paras = tuple(x[0] for x in run) parent = paras[0].getparent() idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = style.css( self.page_map[self.object_map[paras[0]]]) self.styles.register(css, 'frame') if not self.block_runs: return rmap = {v: k for k, v in self.object_map.iteritems()} for border_style, blocks in self.block_runs: paras = tuple(rmap[p] for p in blocks) parent = paras[0].getparent() if parent.tag in ('ul', 'ol'): ul = parent parent = ul.getparent() idx = parent.index(ul) frame = DIV(ul) else: idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = border_style.css self.styles.register(css, 'frame')
def _generate(self, title, masthead, datefmt, feeds, extra_css=None, style=None): self.IS_HTML = False date = '%s, %s %s, %s' % (strftime('%A'), strftime('%B'), strftime('%d').lstrip('0'), strftime('%Y')) masthead_p = etree.Element("p") masthead_p.set("style","text-align:center") masthead_img = etree.Element("img") masthead_img.set("src",masthead) masthead_img.set("alt","masthead") masthead_p.append(masthead_img) head = HEAD(TITLE(title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) toc = TABLE(CLASS('toc'),width="100%",border="0",cellpadding="3px") for i, feed in enumerate(feeds): if feed: tr = TR() tr.append(TD(CLASS('calibre_rescale_120'), A(feed.title, href='feed_%d/index.html'%i))) tr.append(TD('%s' % len(feed.articles), style="text-align:right")) toc.append(tr) div = DIV( masthead_p, H3(CLASS('publish_date'),date), DIV(CLASS('divider')), toc) self.root = HTML(head, BODY(div)) if self.html_lang: self.root.set('lang', self.html_lang)
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) navbar = DIV() navbar_t = TABLE(CLASS('touchscreen_navbar')) navbar_tr = TR() # | Previous if art > 0: link = A(CLASS('article_link'), _('Previous'), href='%s../article_%d/index.html' % (prefix, art - 1)) navbar_tr.append(TD(CLASS('article_prev'), link)) else: navbar_tr.append(TD(CLASS('article_prev'), '')) # | Articles | Sections | link = A(CLASS('articles_link'), _('Articles'), href='%s../index.html#article_%d' % (prefix, art)) navbar_tr.append(TD(CLASS('article_articles_list'), link)) link = A(CLASS('sections_link'), _('Sections'), href='%s../../index.html#feed_%d' % (prefix, feed)) navbar_tr.append(TD(CLASS('article_sections_list'), link)) # | Next next = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' link = A(CLASS('article_link'), _('Next'), href='%s%s/%s/index.html' % (prefix, up, next)) navbar_tr.append(TD(CLASS('article_next'), link)) navbar_t.append(navbar_tr) navbar.append(navbar_t) #print "\n%s\n" % etree.tostring(navbar, pretty_print=True) self.root = HTML(head, BODY(navbar))
def apply_frames(self): for run in filter(None, self.framed): style = run[0][1] paras = tuple(x[0] for x in run) parent = paras[0].getparent() idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = style.css( self.page_map[self.object_map[paras[0]]]) self.styles.register(css, 'frame') if not self.block_runs: return rmap = {v: k for k, v in self.object_map.items()} for border_style, blocks in self.block_runs: paras = tuple(rmap[p] for p in blocks) for p in paras: if p.tag == 'li': has_li = True break else: has_li = False parent = paras[0].getparent() if parent.tag in ('ul', 'ol'): ul = parent parent = ul.getparent() idx = parent.index(ul) frame = DIV(ul) elif has_li: def top_level_tag(x): while True: q = x.getparent() if q is parent or q is None: break x = q return x paras = tuple(map(top_level_tag, paras)) idx = parent.index(paras[0]) frame = DIV(*paras) else: idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = border_style.css self.styles.register(css, 'frame')
def _generate(self, article, style=None, extra_css=None): content = article.content if article.content else '' summary = article.summary if article.summary else '' text = content if len(content) > len(summary) else summary head = HEAD(TITLE(article.title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) if isbytestring(text): text = text.decode('utf-8', 'replace') elements = html.fragments_fromstring(text) self.root = HTML(head, BODY(H2(article.title), DIV())) div = self.root.find('body').find('div') if elements and isinstance(elements[0], unicode_type): div.text = elements[0] elements = list(elements)[1:] for elem in elements: if hasattr(elem, 'getparent'): elem.getparent().remove(elem) else: elem = SPAN(elem) div.append(elem)
def _generate(self, title, masthead, datefmt, feeds, extra_css=None, style=None): self.IS_HTML = False date = strftime(datefmt) head = HEAD(TITLE(title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) ul = UL(attrs('calibre_feed_list')) for i, feed in enumerate(feeds): if len(feed): li = LI(A( feed.title, attrs('feed', rescale=120, href='feed_%d/index.html' % i)), id='feed_%d' % i) ul.append(li) div = DIV( PT(IMG(src=masthead, alt="masthead"), style='text-align:center'), PT(date, style='text-align:right'), ul, attrs(rescale=100)) self.root = HTML(head, BODY(div)) if self.html_lang: self.root.set('lang', self.html_lang)
def _generate(self, title, masthead, datefmt, feeds, extra_css=None, style=None): self.IS_HTML = False if isinstance(datefmt, unicode): datefmt = datefmt.encode(preferred_encoding) date = strftime(datefmt) head = HEAD(TITLE(title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) ul = UL(CLASS('calibre_feed_list')) for i, feed in enumerate(feeds): if feed: li = LI(A( feed.title, CLASS('feed', 'calibre_rescale_120', href='feed_%d/index.html' % i)), id='feed_%d' % i) ul.append(li) div = DIV( PT(IMG(src=masthead, alt="masthead"), style='text-align:center'), PT(date, style='text-align:right'), ul, CLASS('calibre_rescale_100')) self.root = HTML(head, BODY(div)) if self.html_lang: self.root.set('lang', self.html_lang)
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) navbar = DIV() navbar_t = TABLE(CLASS('touchscreen_navbar')) navbar_tr = TR() if bottom and not url.startswith('file://'): navbar.append(HR()) text = 'This article was downloaded by ' p = PT(text, STRONG(__appname__), A(url, href=url, rel='calibre-downloaded-from'), style='text-align:left; max-width: 100%; overflow: hidden;') p[0].tail = ' from ' navbar.append(p) navbar.append(BR()) # | Previous if art > 0: link = A(CLASS('article_link'),_('Previous'),href='%s../article_%d/index.html'%(prefix, art-1)) navbar_tr.append(TD(CLASS('article_prev'),link)) else: navbar_tr.append(TD(CLASS('article_prev'),'')) # | Articles | Sections | link = A(CLASS('articles_link'),_('Articles'), href='%s../index.html#article_%d'%(prefix, art)) navbar_tr.append(TD(CLASS('article_articles_list'),link)) link = A(CLASS('sections_link'),_('Sections'), href='%s../../index.html#feed_%d'%(prefix, feed)) navbar_tr.append(TD(CLASS('article_sections_list'),link)) # | Next next = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' link = A(CLASS('article_link'), _('Next'), href='%s%s/%s/index.html'%(prefix, up, next)) navbar_tr.append(TD(CLASS('article_next'),link)) navbar_t.append(navbar_tr) navbar.append(navbar_t) # print "\n%s\n" % etree.tostring(navbar, pretty_print=True) self.root = HTML(head, BODY(navbar))
def apply_frames(self): for run in filter(None, self.framed): style = run[0][1] paras = tuple(x[0] for x in run) parent = paras[0].getparent() idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = style.css(self.page_map[self.object_map[paras[0]]]) self.styles.register(css, 'frame')
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) navbar = DIV() navbar_t = TABLE(CLASS('touchscreen_navbar')) navbar_tr = TR() if bottom and not url.startswith('file://'): navbar.append(HR()) text = 'This article was downloaded by ' p = PT(text, STRONG(__appname__), A(url, href=url, rel='calibre-downloaded-from'), style='text-align:left; max-width: 100%; overflow: hidden;') p[0].tail = ' from ' navbar.append(p) navbar.append(BR()) # | Previous if art > 0: link = A(CLASS('article_link'),_('Previous'),href='%s../article_%d/index.html'%(prefix, art-1)) navbar_tr.append(TD(CLASS('article_prev'),link)) else: navbar_tr.append(TD(CLASS('article_prev'),'')) # | Articles | Sections | link = A(CLASS('articles_link'),_('Articles'), href='%s../index.html#article_%d'%(prefix, art)) navbar_tr.append(TD(CLASS('article_articles_list'),link)) link = A(CLASS('sections_link'),_('Sections'), href='%s../../index.html#feed_%d'%(prefix, feed)) navbar_tr.append(TD(CLASS('article_sections_list'),link)) # | Next next_art = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' link = A(CLASS('article_link'), _('Next'), href='%s%s/%s/index.html'%(prefix, up, next_art)) navbar_tr.append(TD(CLASS('article_next'),link)) navbar_t.append(navbar_tr) navbar.append(navbar_t) # print "\n%s\n" % etree.tostring(navbar, pretty_print=True) self.root = HTML(head, BODY(navbar))
def build_search_box(num, search, sort, order, prefix): # {{{ div = DIV(id='search_box') form = FORM('Show ', method='get', action=prefix + '/mobile') form.set('accept-charset', 'UTF-8') div.append(form) num_select = SELECT(name='num') for option in (5, 10, 25, 100): kwargs = {'value': str(option)} if option == num: kwargs['SELECTED'] = 'SELECTED' num_select.append(OPTION(str(option), **kwargs)) num_select.tail = ' books matching ' form.append(num_select) searchf = INPUT(name='search', id='s', value=search if search else '') searchf.tail = ' sorted by ' form.append(searchf) sort_select = SELECT(name='sort') for option in ('date', 'author', 'title', 'rating', 'size', 'tags', 'series'): kwargs = {'value': option} if option == sort: kwargs['SELECTED'] = 'SELECTED' sort_select.append(OPTION(option, **kwargs)) form.append(sort_select) order_select = SELECT(name='order') for option in ('ascending', 'descending'): kwargs = {'value': option} if option == order: kwargs['SELECTED'] = 'SELECTED' order_select.append(OPTION(option, **kwargs)) form.append(order_select) form.append(INPUT(id='go', type='submit', value='Search')) return div
def build_search_box(num, search, sort, order, prefix): # {{{ div = DIV(id='search_box') form = FORM('Show ', method='get', action=prefix+'/mobile') form.set('accept-charset', 'UTF-8') div.append(form) num_select = SELECT(name='num') for option in (5, 10, 25, 100): kwargs = {'value':str(option)} if option == num: kwargs['SELECTED'] = 'SELECTED' num_select.append(OPTION(str(option), **kwargs)) num_select.tail = ' books matching ' form.append(num_select) searchf = INPUT(name='search', id='s', value=search if search else '') searchf.tail = ' sorted by ' form.append(searchf) sort_select = SELECT(name='sort') for option in ('date','author','title','rating','size','tags','series'): kwargs = {'value':option} if option == sort: kwargs['SELECTED'] = 'SELECTED' sort_select.append(OPTION(option, **kwargs)) form.append(sort_select) order_select = SELECT(name='order') for option in ('ascending','descending'): kwargs = {'value':option} if option == order: kwargs['SELECTED'] = 'SELECTED' order_select.append(OPTION(option, **kwargs)) form.append(order_select) form.append(INPUT(id='go', type='submit', value='Search')) return div
def build_search_box(num, search, sort, order, prefix): # {{{ div = DIV(id="search_box") form = FORM("Show ", method="get", action=prefix + "/mobile") form.set("accept-charset", "UTF-8") div.append(form) num_select = SELECT(name="num") for option in (5, 10, 25, 100): kwargs = {"value": str(option)} if option == num: kwargs["SELECTED"] = "SELECTED" num_select.append(OPTION(str(option), **kwargs)) num_select.tail = " books matching " form.append(num_select) searchf = INPUT(name="search", id="s", value=search if search else "") searchf.tail = " sorted by " form.append(searchf) sort_select = SELECT(name="sort") for option in ("date", "author", "title", "rating", "size", "tags", "series"): kwargs = {"value": option} if option == sort: kwargs["SELECTED"] = "SELECTED" sort_select.append(OPTION(option, **kwargs)) form.append(sort_select) order_select = SELECT(name="order") for option in ("ascending", "descending"): kwargs = {"value": option} if option == order: kwargs["SELECTED"] = "SELECTED" order_select.append(OPTION(option, **kwargs)) form.append(order_select) form.append(INPUT(id="go", type="submit", value="Search")) return div
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) navbar = DIV() navbar_t = TABLE(CLASS('touchscreen_navbar')) navbar_tr = TR() # | Previous if art > 0: link = A(CLASS('article_link'),_('Previous'),href='%s../article_%d/index.html'%(prefix, art-1)) navbar_tr.append(TD(CLASS('article_prev'),link)) else: navbar_tr.append(TD(CLASS('article_prev'),'')) # | Articles | Sections | link = A(CLASS('articles_link'),_('Articles'), href='%s../index.html#article_%d'%(prefix, art)) navbar_tr.append(TD(CLASS('article_articles_list'),link)) link = A(CLASS('sections_link'),_('Sections'), href='%s../../index.html#feed_%d'%(prefix, feed)) navbar_tr.append(TD(CLASS('article_sections_list'),link)) # | Next next = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' link = A(CLASS('article_link'), _('Next'), href='%s%s/%s/index.html'%(prefix, up, next)) navbar_tr.append(TD(CLASS('article_next'),link)) navbar_t.append(navbar_tr) navbar.append(navbar_t) #print "\n%s\n" % etree.tostring(navbar, pretty_print=True) self.root = HTML(head, BODY(navbar))
def apply_frames(self): for run in [_f for _f in self.framed if _f]: style = run[0][1] paras = tuple(x[0] for x in run) parent = paras[0].getparent() idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = style.css(self.page_map[self.object_map[paras[0]]]) self.styles.register(css, 'frame') if not self.block_runs: return rmap = {v:k for k, v in self.object_map.items()} for border_style, blocks in self.block_runs: paras = tuple(rmap[p] for p in blocks) parent = paras[0].getparent() idx = parent.index(paras[0]) frame = DIV(*paras) parent.insert(idx, frame) self.framed_map[frame] = css = border_style.css self.styles.register(css, 'frame')
def set_custom_embed_code(self, data): """ Return the code that embed the code. Could be with the original size or the custom chosen. """ if 'embed_html' not in data: return tree = etree.HTML(data['embed_html']) sel = cssselect.CSSSelector('body > *') el = sel(tree) # add a div around if there is more than one element into code if len(el) > 1: el = DIV(*el) else: el = el[0] # width and height attributes should not be set in a div tag if el.tag in ['iframe', 'object']: if data.get('width', None): el.attrib['width'] = data['width'] and str(data['width']) or el.attrib['width'] if data.get('height', None): el.attrib['height'] = data['height'] and str(data['height']) or el.attrib['height'] data['embed_html'] = html.tostring(el)
def build_navigation(start, num, total, url_base): # {{{ end = min((start + num - 1), total) tagline = SPAN('Books %d to %d of %d' % (start, end, total), style='display: block; text-align: center;') left_buttons = TD(CLASS('button', style='text-align:left')) right_buttons = TD(CLASS('button', style='text-align:right')) if start > 1: for t, s in [('First', 1), ('Previous', max(start - num, 1))]: left_buttons.append(A(t, href='%s;start=%d' % (url_base, s))) if total > start + num: for t, s in [('Next', start + num), ('Last', total - num + 1)]: right_buttons.append(A(t, href='%s;start=%d' % (url_base, s))) buttons = TABLE(TR(left_buttons, right_buttons), CLASS('buttons')) return DIV(tagline, buttons, CLASS('navigation'))
def donode(item, parent, base, subpath): for child in item: title = child.title if not title: continue raw = unquote_path(child.href or '') rsrcname = os.path.basename(raw) rsrcpath = os.path.join(subpath, rsrcname) if (not os.path.exists(os.path.join(base, rsrcpath)) and os.path.exists(os.path.join(base, raw))): rsrcpath = raw if '%' not in rsrcpath: rsrcpath = urlquote(rsrcpath) if not raw: rsrcpath = '' c = DIV(A(title, href=rsrcpath)) donode(child, c, base, subpath) parent.append(c)
def _generate(self, f, feeds, cutoff, extra_css=None, style=None): feed = feeds[f] head = HEAD(TITLE(feed.title)) if style: head.append(STYLE(style, type="text/css")) if extra_css: head.append(STYLE(extra_css, type="text/css")) body = BODY() body.append(self.get_navbar(f, feeds)) div = DIV(H2(feed.title, CLASS("calibre_feed_title", "calibre_rescale_160")), CLASS("calibre_rescale_100")) body.append(div) if getattr(feed, "image", None): div.append( DIV(IMG(alt=feed.image_alt if feed.image_alt else "", src=feed.image_url), CLASS("calibre_feed_image")) ) if getattr(feed, "description", None): d = DIV(feed.description, CLASS("calibre_feed_description", "calibre_rescale_80")) d.append(BR()) div.append(d) ul = UL(CLASS("calibre_article_list")) for i, article in enumerate(feed.articles): if not getattr(article, "downloaded", False): continue 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"), ) if article.summary: li.append(DIV(cutoff(article.text_summary), CLASS("article_description", "calibre_rescale_70"))) ul.append(li) div.append(ul) div.append(self.get_navbar(f, feeds, top=False)) self.root = HTML(head, body) if self.html_lang: self.root.set("lang", self.html_lang)
def get_navbar(self, f, feeds, top=True): if len(feeds) < 2: return DIV() navbar = DIV("| ", CLASS("calibre_navbar", "calibre_rescale_70", style="text-align:center")) if not top: hr = HR() navbar.append(hr) navbar.text = None hr.tail = "| " if f + 1 < len(feeds): link = A(_("Next section"), href="../feed_%d/index.html" % (f + 1)) link.tail = " | " navbar.append(link) link = A(_("Main menu"), href="../index.html") link.tail = " | " navbar.append(link) if f > 0: link = A(_("Previous section"), href="../feed_%d/index.html" % (f - 1)) link.tail = " |" navbar.append(link) if top: navbar.append(HR()) return navbar
def build_index(books, num, search, sort, order, start, total, url_base, CKEYS, prefix, have_kobo_browser=False): logo = DIV(IMG(src=prefix + "/static/calibre.png", alt=__appname__), id="logo") search_box = build_search_box(num, search, sort, order, prefix) navigation = build_navigation(start, num, total, prefix + url_base) navigation2 = build_navigation(start, num, total, prefix + url_base) bookt = TABLE(id="listing") body = BODY(logo, search_box, navigation, HR(CLASS("spacer")), bookt, HR(CLASS("spacer")), navigation2) # Book list {{{ for book in books: thumbnail = TD( IMG(type="image/jpeg", border="0", src=prefix + "/get/thumb/%s" % book["id"]), CLASS("thumbnail") ) data = TD() for fmt in book["formats"].split(","): if not fmt or fmt.lower().startswith("original_"): continue file_extension = "kepub.epub" if have_kobo_browser and fmt.lower() == "kepub" else fmt a = quote(ascii_filename(book["authors"])) t = quote(ascii_filename(book["title"])) s = SPAN( A(fmt.lower(), href=prefix + "/get/%s/%s-%s_%d.%s" % (fmt, a, t, book["id"], file_extension.lower())), CLASS("button"), ) s.tail = u"" data.append(s) div = DIV(CLASS("data-container")) data.append(div) series = u"[%s - %s]" % (book["series"], book["series_index"]) if book["series"] else "" tags = u"Tags=[%s]" % book["tags"] if book["tags"] else "" ctext = "" for key in CKEYS: val = book.get(key, None) if val: ctext += "%s=[%s] " % tuple(val.split(":#:")) first = SPAN( u"\u202f%s %s by %s" % (clean_xml_chars(book["title"]), clean_xml_chars(series), clean_xml_chars(book["authors"])), CLASS("first-line"), ) div.append(first) second = SPAN(u"%s - %s %s %s" % (book["size"], book["timestamp"], tags, ctext), CLASS("second-line")) div.append(second) bookt.append(TR(thumbnail, data)) # }}} body.append( DIV( A( _("Switch to the full interface (non-mobile interface)"), href=prefix + "/browse", style="text-decoration: none; color: blue", title=_( "The full interface gives you many more features, " "but it may not work well on a small screen" ), ), style="text-align:center", ) ) return HTML( HEAD( TITLE(__appname__ + " Library"), LINK(rel="icon", href="//calibre-ebook.com/favicon.ico", type="image/x-icon"), LINK(rel="stylesheet", type="text/css", href=prefix + "/mobile/style.css"), LINK(rel="apple-touch-icon", href="/static/calibre.png"), META(name="robots", content="noindex"), ), # End head body, ) # End html
def _generate(self, f, feeds, cutoff, extra_css=None, style=None): from calibre.utils.cleantext import clean_xml_chars def trim_title(title,clip=18): if len(title)>clip: tokens = title.split(' ') new_title_tokens = [] new_title_len = 0 if len(tokens[0]) > clip: return tokens[0][:clip] + '...' for token in tokens: if len(token) + new_title_len < clip: new_title_tokens.append(token) new_title_len += len(token) else: new_title_tokens.append('...') title = ' '.join(new_title_tokens) break return title self.IS_HTML = False feed = feeds[f] # Construct the navbar navbar_t = TABLE(CLASS('touchscreen_navbar')) navbar_tr = TR() # Previous Section link = '' if f > 0: link = A(CLASS('feed_link'), trim_title(feeds[f-1].title), href='../feed_%d/index.html' % int(f-1)) navbar_tr.append(TD(CLASS('feed_prev'),link)) # Up to Sections link = A(_('Sections'), href="../index.html") navbar_tr.append(TD(CLASS('feed_up'),link)) # Next Section link = '' if f < len(feeds)-1: link = A(CLASS('feed_link'), trim_title(feeds[f+1].title), href='../feed_%d/index.html' % int(f+1)) navbar_tr.append(TD(CLASS('feed_next'),link)) navbar_t.append(navbar_tr) top_navbar = navbar_t bottom_navbar = copy.copy(navbar_t) # print "\n%s\n" % etree.tostring(navbar_t, pretty_print=True) # Build the page head = HEAD(TITLE(feed.title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) body = BODY() div = DIV( top_navbar, H2(feed.title, CLASS('feed_title')) ) body.append(div) if getattr(feed, 'image', None): div.append(DIV(IMG( alt=feed.image_alt if feed.image_alt else '', src=feed.image_url ), CLASS('calibre_feed_image'))) if getattr(feed, 'description', None): d = DIV(clean_xml_chars(feed.description), CLASS('calibre_feed_description', 'calibre_rescale_80')) d.append(BR()) div.append(d) for i, article in enumerate(feed.articles): if not getattr(article, 'downloaded', False): continue div_td = DIV(CLASS('article_summary'), A(article.title, CLASS('summary_headline','calibre_rescale_120', href=article.url))) if article.author: div_td.append(DIV(article.author, CLASS('summary_byline', 'calibre_rescale_100'))) if article.summary: div_td.append(DIV(cutoff(article.text_summary), CLASS('summary_text', 'calibre_rescale_100'))) div.append(div_td) div.append(bottom_navbar) self.root = HTML(head, body) if self.html_lang: self.root.set('lang', self.html_lang)
def _generate(self, f, feeds, cutoff, extra_css=None, style=None): from calibre.utils.cleantext import clean_xml_chars feed = feeds[f] head = HEAD(TITLE(feed.title)) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) body = BODY() body.append(self.get_navbar(f, feeds)) div = DIV( H2(feed.title, CLASS('calibre_feed_title', 'calibre_rescale_160')), CLASS('calibre_rescale_100') ) body.append(div) if getattr(feed, 'image', None): div.append(DIV(IMG( alt=feed.image_alt if feed.image_alt else '', src=feed.image_url ), CLASS('calibre_feed_image'))) if getattr(feed, 'description', None): d = DIV(clean_xml_chars(feed.description), CLASS('calibre_feed_description', 'calibre_rescale_80')) d.append(BR()) div.append(d) ul = UL(CLASS('calibre_article_list')) for i, article in enumerate(feed.articles): if not getattr(article, 'downloaded', False): continue 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') ) if article.summary: li.append(DIV(clean_xml_chars(cutoff(article.text_summary)), CLASS('article_description', 'calibre_rescale_70'))) ul.append(li) div.append(ul) div.append(self.get_navbar(f, feeds, top=False)) self.root = HTML(head, body) if self.html_lang: self.root.set('lang', self.html_lang)
def _generate( self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix="", center=True, extra_css=None, style=None, ): head = HEAD(TITLE("navbar")) if style: head.append(STYLE(style, type="text/css")) if extra_css: head.append(STYLE(extra_css, type="text/css")) navbar = DIV() navbar_t = TABLE(CLASS("touchscreen_navbar")) navbar_tr = TR() if bottom and not url.startswith("file://"): navbar.append(HR()) text = "This article was downloaded by " p = PT( text, STRONG(__appname__), A(url, href=url, rel="calibre-downloaded-from"), style="text-align:left; max-width: 100%; overflow: hidden;", ) p[0].tail = " from " navbar.append(p) navbar.append(BR()) # | Previous if art > 0: link = A(CLASS("article_link"), _("Previous"), href="%s../article_%d/index.html" % (prefix, art - 1)) navbar_tr.append(TD(CLASS("article_prev"), link)) else: navbar_tr.append(TD(CLASS("article_prev"), "")) # | Articles | Sections | link = A(CLASS("articles_link"), _("Articles"), href="%s../index.html#article_%d" % (prefix, art)) navbar_tr.append(TD(CLASS("article_articles_list"), link)) link = A(CLASS("sections_link"), _("Sections"), href="%s../../index.html#feed_%d" % (prefix, feed)) navbar_tr.append(TD(CLASS("article_sections_list"), link)) # | Next next = "feed_%d" % (feed + 1) if art == number_of_articles_in_feed - 1 else "article_%d" % (art + 1) up = "../.." if art == number_of_articles_in_feed - 1 else ".." link = A(CLASS("article_link"), _("Next"), href="%s%s/%s/index.html" % (prefix, up, next)) navbar_tr.append(TD(CLASS("article_next"), link)) navbar_t.append(navbar_tr) navbar.append(navbar_t) # print "\n%s\n" % etree.tostring(navbar, pretty_print=True) self.root = HTML(head, BODY(navbar))
def get_navbar(self, f, feeds, top=True): if len(feeds) < 2: return DIV() navbar = DIV('| ', CLASS('calibre_navbar', 'calibre_rescale_70', style='text-align:center')) if not top: hr = HR() navbar.append(hr) navbar.text = None hr.tail = '| ' if f+1 < len(feeds): link = A(_('Next section'), href='../feed_%d/index.html'%(f+1)) link.tail = ' | ' navbar.append(link) link = A(_('Main menu'), href="../index.html") link.tail = ' | ' navbar.append(link) if f > 0: link = A(_('Previous section'), href='../feed_%d/index.html'%(f-1)) link.tail = ' |' navbar.append(link) if top: navbar.append(HR()) return navbar
def build_index(books, num, search, sort, order, start, total, url_base, CKEYS, prefix): logo = DIV(IMG(src=prefix + '/static/calibre.png', alt=__appname__), id='logo') search_box = build_search_box(num, search, sort, order, prefix) navigation = build_navigation(start, num, total, prefix + url_base) navigation2 = build_navigation(start, num, total, prefix + url_base) bookt = TABLE(id='listing') body = BODY(logo, search_box, navigation, HR(CLASS('spacer')), bookt, HR(CLASS('spacer')), navigation2) # Book list {{{ for book in books: thumbnail = TD( IMG(type='image/jpeg', border='0', src=prefix + '/get/thumb/%s' % book['id']), CLASS('thumbnail')) data = TD() for fmt in book['formats'].split(','): if not fmt or fmt.lower().startswith('original_'): continue a = quote(ascii_filename(book['authors'])) t = quote(ascii_filename(book['title'])) s = SPAN( A(fmt.lower(), href=prefix + '/get/%s/%s-%s_%d.%s' % (fmt, a, t, book['id'], fmt.lower())), CLASS('button')) s.tail = u'' data.append(s) div = DIV(CLASS('data-container')) data.append(div) series = u'[%s - %s]'%(book['series'], book['series_index']) \ if book['series'] else '' tags = u'Tags=[%s]' % book['tags'] if book['tags'] else '' ctext = '' for key in CKEYS: val = book.get(key, None) if val: ctext += '%s=[%s] ' % tuple(val.split(':#:')) first = SPAN( u'\u202f%s %s by %s' % (book['title'], series, book['authors']), CLASS('first-line')) div.append(first) second = SPAN( u'%s - %s %s %s' % (book['size'], book['timestamp'], tags, ctext), CLASS('second-line')) div.append(second) bookt.append(TR(thumbnail, data)) # }}} body.append( DIV(A(_('Switch to the full interface (non-mobile interface)'), href=prefix + "/browse", style="text-decoration: none; color: blue", title=_('The full interface gives you many more features, ' 'but it may not work well on a small screen')), style="text-align:center")) return HTML( HEAD( TITLE(__appname__ + ' Library'), LINK(rel='icon', href='http://calibre-ebook.com/favicon.ico', type='image/x-icon'), LINK(rel='stylesheet', type='text/css', href=prefix + '/mobile/style.css'), LINK(rel='apple-touch-icon', href="/static/calibre.png")), # End head body) # End html
def build_index(books, num, search, sort, order, start, total, url_base, CKEYS, prefix): logo = DIV(IMG(src=prefix+'/static/calibre.png', alt=__appname__), id='logo') search_box = build_search_box(num, search, sort, order, prefix) navigation = build_navigation(start, num, total, prefix+url_base) navigation2 = build_navigation(start, num, total, prefix+url_base) bookt = TABLE(id='listing') body = BODY( logo, search_box, navigation, HR(CLASS('spacer')), bookt, HR(CLASS('spacer')), navigation2 ) # Book list {{{ for book in books: thumbnail = TD( IMG(type='image/jpeg', border='0', src=prefix+'/get/thumb/%s' % book['id']), CLASS('thumbnail')) data = TD() for fmt in book['formats'].split(','): if not fmt or fmt.lower().startswith('original_'): continue a = quote(ascii_filename(book['authors'])) t = quote(ascii_filename(book['title'])) s = SPAN( A( fmt.lower(), href=prefix+'/get/%s/%s-%s_%d.%s' % (fmt, a, t, book['id'], fmt.lower()) ), CLASS('button')) s.tail = u'' data.append(s) div = DIV(CLASS('data-container')) data.append(div) series = u'[%s - %s]'%(book['series'], book['series_index']) \ if book['series'] else '' tags = u'Tags=[%s]'%book['tags'] if book['tags'] else '' ctext = '' for key in CKEYS: val = book.get(key, None) if val: ctext += '%s=[%s] '%tuple(val.split(':#:')) first = SPAN(u'\u202f%s %s by %s' % (book['title'], series, book['authors']), CLASS('first-line')) div.append(first) second = SPAN(u'%s - %s %s %s' % ( book['size'], book['timestamp'], tags, ctext), CLASS('second-line')) div.append(second) bookt.append(TR(thumbnail, data)) # }}} body.append(DIV( A(_('Switch to the full interface (non-mobile interface)'), href=prefix+"/browse", style="text-decoration: none; color: blue", title=_('The full interface gives you many more features, ' 'but it may not work well on a small screen')), style="text-align:center")) return HTML( HEAD( TITLE(__appname__ + ' Library'), LINK(rel='icon', href='http://calibre-ebook.com/favicon.ico', type='image/x-icon'), LINK(rel='stylesheet', type='text/css', href=prefix+'/mobile/style.css'), LINK(rel='apple-touch-icon', href="/static/calibre.png") ), # End head body ) # End html
def _create_html_root(self, hhcpath, log, encoding): from lxml import html from polyglot.urllib import unquote as _unquote from calibre.ebooks.oeb.base import urlquote from calibre.ebooks.chardet import xml_to_unicode hhcdata = self._read_file(hhcpath) hhcdata = hhcdata.decode(encoding) hhcdata = xml_to_unicode(hhcdata, verbose=True, strip_encoding_pats=True, resolve_entities=True)[0] hhcroot = html.fromstring(hhcdata) toc = self._process_nodes(hhcroot) # print("=============================") # print("Printing hhcroot") # print(etree.tostring(hhcroot, pretty_print=True)) # print("=============================") log.debug('Found %d section nodes' % toc.count()) htmlpath = os.path.splitext(hhcpath)[0] + ".html" base = os.path.dirname(os.path.abspath(htmlpath)) def unquote(x): if isinstance(x, unicode_type): x = x.encode('utf-8') return _unquote(x).decode('utf-8') def unquote_path(x): y = unquote(x) if (not os.path.exists(os.path.join(base, x)) and os.path.exists(os.path.join(base, y))): x = y return x def donode(item, parent, base, subpath): for child in item: title = child.title if not title: continue raw = unquote_path(child.href or '') rsrcname = os.path.basename(raw) rsrcpath = os.path.join(subpath, rsrcname) if (not os.path.exists(os.path.join(base, rsrcpath)) and os.path.exists(os.path.join(base, raw))): rsrcpath = raw if '%' not in rsrcpath: rsrcpath = urlquote(rsrcpath) if not raw: rsrcpath = '' c = DIV(A(title, href=rsrcpath)) donode(child, c, base, subpath) parent.append(c) with open(htmlpath, 'wb') as f: if toc.count() > 1: from lxml.html.builder import HTML, BODY, DIV, A path0 = toc[0].href path0 = unquote_path(path0) subpath = os.path.dirname(path0) base = os.path.dirname(f.name) root = DIV() donode(toc, root, base, subpath) raw = html.tostring(HTML(BODY(root)), encoding='utf-8', pretty_print=True) f.write(raw) else: f.write(as_bytes(hhcdata)) return htmlpath, toc
def _generate( self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix="", center=True, extra_css=None, style=None, ): head = HEAD(TITLE("navbar")) if style: head.append(STYLE(style, type="text/css")) if extra_css: head.append(STYLE(extra_css, type="text/css")) if prefix and not prefix.endswith("/"): prefix += "/" align = "center" if center else "left" navbar = DIV(CLASS("calibre_navbar", "calibre_rescale_70", style="text-align:" + align)) if bottom: if not url.startswith("file://"): navbar.append(HR()) text = "This article was downloaded by " p = PT( text, STRONG(__appname__), A(url, href=url, rel="calibre-downloaded-from"), style="text-align:left; max-width: 100%; overflow: hidden;", ) p[0].tail = " from " navbar.append(p) navbar.append(BR()) navbar.append(BR()) else: next = "feed_%d" % (feed + 1) if art == number_of_articles_in_feed - 1 else "article_%d" % (art + 1) up = "../.." if art == number_of_articles_in_feed - 1 else ".." href = "%s%s/%s/index.html" % (prefix, up, next) navbar.text = "| " navbar.append(A(_("Next"), href=href)) href = "%s../index.html#article_%d" % (prefix, art) navbar.iterchildren(reversed=True).next().tail = " | " navbar.append(A(_("Section Menu"), href=href)) href = "%s../../index.html#feed_%d" % (prefix, feed) navbar.iterchildren(reversed=True).next().tail = " | " navbar.append(A(_("Main Menu"), href=href)) if art > 0 and not bottom: href = "%s../article_%d/index.html" % (prefix, art - 1) navbar.iterchildren(reversed=True).next().tail = " | " navbar.append(A(_("Previous"), href=href)) navbar.iterchildren(reversed=True).next().tail = " | " if not bottom: navbar.append(HR()) self.root = HTML(head, BODY(navbar))
def _generate(self, f, feeds, cutoff, extra_css=None, style=None): def trim_title(title, clip=18): if len(title) > clip: tokens = title.split(" ") new_title_tokens = [] new_title_len = 0 if len(tokens[0]) > clip: return tokens[0][:clip] + "..." for token in tokens: if len(token) + new_title_len < clip: new_title_tokens.append(token) new_title_len += len(token) else: new_title_tokens.append("...") title = " ".join(new_title_tokens) break return title self.IS_HTML = False feed = feeds[f] # Construct the navbar navbar_t = TABLE(CLASS("touchscreen_navbar")) navbar_tr = TR() # Previous Section link = "" if f > 0: link = A(CLASS("feed_link"), trim_title(feeds[f - 1].title), href="../feed_%d/index.html" % int(f - 1)) navbar_tr.append(TD(CLASS("feed_prev"), link)) # Up to Sections link = A(_("Sections"), href="../index.html") navbar_tr.append(TD(CLASS("feed_up"), link)) # Next Section link = "" if f < len(feeds) - 1: link = A(CLASS("feed_link"), trim_title(feeds[f + 1].title), href="../feed_%d/index.html" % int(f + 1)) navbar_tr.append(TD(CLASS("feed_next"), link)) navbar_t.append(navbar_tr) top_navbar = navbar_t bottom_navbar = copy.copy(navbar_t) # print "\n%s\n" % etree.tostring(navbar_t, pretty_print=True) # Build the page head = HEAD(TITLE(feed.title)) if style: head.append(STYLE(style, type="text/css")) if extra_css: head.append(STYLE(extra_css, type="text/css")) body = BODY() div = DIV(top_navbar, H2(feed.title, CLASS("feed_title"))) body.append(div) if getattr(feed, "image", None): div.append( DIV(IMG(alt=feed.image_alt if feed.image_alt else "", src=feed.image_url), CLASS("calibre_feed_image")) ) if getattr(feed, "description", None): d = DIV(feed.description, CLASS("calibre_feed_description", "calibre_rescale_80")) d.append(BR()) div.append(d) for i, article in enumerate(feed.articles): if not getattr(article, "downloaded", False): continue div_td = DIV( CLASS("article_summary"), A(article.title, CLASS("summary_headline", "calibre_rescale_120", href=article.url)), ) if article.author: div_td.append(DIV(article.author, CLASS("summary_byline", "calibre_rescale_100"))) if article.summary: div_td.append(DIV(cutoff(article.text_summary), CLASS("summary_text", "calibre_rescale_100"))) div.append(div_td) div.append(bottom_navbar) self.root = HTML(head, body) if self.html_lang: self.root.set("lang", self.html_lang)
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) if prefix and not prefix.endswith('/'): prefix += '/' align = 'center' if center else 'left' navbar = DIV(CLASS('calibre_navbar', 'calibre_rescale_70', style='text-align:'+align)) if bottom: if not url.startswith('file://'): navbar.append(HR()) text = 'This article was downloaded by ' p = PT(text, STRONG(__appname__), A(url, href=url, rel='calibre-downloaded-from'), style='text-align:left; max-width: 100%; overflow: hidden;') p[0].tail = ' from ' navbar.append(p) navbar.append(BR()) navbar.append(BR()) else: next_art = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' href = '%s%s/%s/index.html'%(prefix, up, next_art) navbar.text = '| ' navbar.append(A(_('Next'), href=href)) href = '%s../index.html#article_%d'%(prefix, art) next(navbar.iterchildren(reversed=True)).tail = ' | ' navbar.append(A(_('Section menu'), href=href)) href = '%s../../index.html#feed_%d'%(prefix, feed) next(navbar.iterchildren(reversed=True)).tail = ' | ' navbar.append(A(_('Main menu'), href=href)) if art > 0 and not bottom: href = '%s../article_%d/index.html'%(prefix, art-1) next(navbar.iterchildren(reversed=True)).tail = ' | ' navbar.append(A(_('Previous'), href=href)) next(navbar.iterchildren(reversed=True)).tail = ' | ' if not bottom: navbar.append(HR()) self.root = HTML(head, BODY(navbar))
def _generate(self, bottom, feed, art, number_of_articles_in_feed, two_levels, url, __appname__, prefix='', center=True, extra_css=None, style=None): head = HEAD(TITLE('navbar')) if style: head.append(STYLE(style, type='text/css')) if extra_css: head.append(STYLE(extra_css, type='text/css')) if prefix and not prefix.endswith('/'): prefix += '/' align = 'center' if center else 'left' navbar = DIV(CLASS('calibre_navbar', 'calibre_rescale_70', style='text-align:'+align)) if bottom: if not url.startswith('file://'): navbar.append(HR()) text = 'This article was downloaded by ' p = PT(text, STRONG(__appname__), A(url, href=url, rel='calibre-downloaded-from'), style='text-align:left; max-width: 100%; overflow: hidden;') p[0].tail = ' from ' navbar.append(p) navbar.append(BR()) navbar.append(BR()) else: next = 'feed_%d'%(feed+1) if art == number_of_articles_in_feed - 1 \ else 'article_%d'%(art+1) up = '../..' if art == number_of_articles_in_feed - 1 else '..' href = '%s%s/%s/index.html'%(prefix, up, next) navbar.text = '| ' navbar.append(A(_('Next'), href=href)) href = '%s../index.html#article_%d'%(prefix, art) navbar.iterchildren(reversed=True).next().tail = ' | ' navbar.append(A(_('Section menu'), href=href)) href = '%s../../index.html#feed_%d'%(prefix, feed) navbar.iterchildren(reversed=True).next().tail = ' | ' navbar.append(A(_('Main menu'), href=href)) if art > 0 and not bottom: href = '%s../article_%d/index.html'%(prefix, art-1) navbar.iterchildren(reversed=True).next().tail = ' | ' navbar.append(A(_('Previous'), href=href)) navbar.iterchildren(reversed=True).next().tail = ' | ' if not bottom: navbar.append(HR()) self.root = HTML(head, BODY(navbar))