From 94b3ba12f88fcad274c42e09b8dd515663d467d9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 30 Oct 2008 20:03:20 -0700 Subject: [PATCH] IGN:Allow extra_css to override template styles --- src/calibre/manual/news_recipe.rst | 5 +++- src/calibre/web/feeds/news.py | 38 ++++++++++++++++++++++++++---- src/calibre/web/feeds/templates.py | 27 ++++----------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/calibre/manual/news_recipe.rst b/src/calibre/manual/news_recipe.rst index d486b7570b..6872d8e532 100644 --- a/src/calibre/manual/news_recipe.rst +++ b/src/calibre/manual/news_recipe.rst @@ -59,7 +59,7 @@ Customizing e-book download .. automember:: BasicNewsRecipe.no_stylesheets .. automember:: BasicNewsRecipe.encoding - + .. automethod:: BasicNewsRecipe.get_browser .. automethod:: BasicNewsRecipe.get_cover_url @@ -103,11 +103,14 @@ Pre/post processing of downloaded HTML .. automember:: BasicNewsRecipe.preprocess_regexps +.. automember:: BasicNewsRecipe.template_css + .. automethod:: BasicNewsRecipe.preprocess_html .. automethod:: BasicNewsRecipe.postprocess_html + Convenience methods ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/calibre/web/feeds/news.py b/src/calibre/web/feeds/news.py index c7fb812508..3c5be7c7e6 100644 --- a/src/calibre/web/feeds/news.py +++ b/src/calibre/web/feeds/news.py @@ -206,6 +206,32 @@ class BasicNewsRecipe(object, LoggingInterface): #: will remove everythong from `` to ``. preprocess_regexps = [] + #: The CSS that is used to styles the templates, i.e., the navigation bars and + #: the Tables of Contents. Rather than overriding this variable, you should + #: use :member:`extra_css` in your recipe to customize look and feel. + template_css = u''' + .article_date { + font-size: x-small; color: gray; font-family: monospace; + } + + .article_description { + font-size: small; font-family: sans; text-indent: 0pt; + } + + a.article { + font-weight: bold; font-size: large; + } + + a.feed { + font-weight: bold; font-size: large; + } + + .navbar { + font-family:monospace; font-size:8pt + } +''' + + # See the built-in profiles for examples of these settings. def get_cover_url(self): @@ -471,11 +497,13 @@ class BasicNewsRecipe(object, LoggingInterface): def _postprocess_html(self, soup, first_fetch, job_info): - if self.extra_css is not None: - head = soup.find('head') - if head: - style = BeautifulSoup(u''%self.extra_css).find('style') - head.insert(len(head.contents), style) + head = soup.find('head') + if not head: + head = soup.find('body') + if not head: + head = soup.find(True) + style = BeautifulSoup(u''%(self.template_css +'\n\n'+self.extra_css)).find('style') + head.insert(len(head.contents), style) if first_fetch and job_info: url, f, a, feed_len = job_info body = soup.find('body') diff --git a/src/calibre/web/feeds/templates.py b/src/calibre/web/feeds/templates.py index 4438b18f0a..cfbcffba2a 100644 --- a/src/calibre/web/feeds/templates.py +++ b/src/calibre/web/feeds/templates.py @@ -8,28 +8,9 @@ from calibre import preferred_encoding, strftime class Template(MarkupTemplate): - STYLE = u'''\ - .article_date { - font-size: x-small; color: gray; font-family: monospace; - } - - .article_description { - font-size: small; font-family: sans; text-indent: 0pt; - } - - a.article { - font-weight: bold; font-size: large; - } - - a.feed { - font-weight: bold; font-size: large; - } - -''' - def generate(self, *args, **kwargs): if not kwargs.has_key('style'): - kwargs['style'] = self.STYLE + kwargs['style'] = '' for key in kwargs.keys(): if isinstance(kwargs[key], basestring) and not isinstance(kwargs[key], unicode): kwargs[key] = unicode(kwargs[key], 'utf-8', 'replace') @@ -52,7 +33,7 @@ class NavBarTemplate(Template): > -