IGN:Allow extra_css to override template styles

This commit is contained in:
Kovid Goyal 2008-10-30 20:03:20 -07:00
parent 102aa48708
commit 94b3ba12f8
3 changed files with 41 additions and 29 deletions

View File

@ -103,12 +103,15 @@ Pre/post processing of downloaded HTML
.. automember:: BasicNewsRecipe.preprocess_regexps
.. automember:: BasicNewsRecipe.template_css
.. automethod:: BasicNewsRecipe.preprocess_html
.. automethod:: BasicNewsRecipe.postprocess_html
Convenience methods
~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -206,6 +206,32 @@ class BasicNewsRecipe(object, LoggingInterface):
#: will remove everythong from `<!--Article ends here-->` to `</body>`.
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,10 +497,12 @@ 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'<style type="text/css" title="override_css">%s</style>'%self.extra_css).find('style')
if not head:
head = soup.find('body')
if not head:
head = soup.find(True)
style = BeautifulSoup(u'<style type="text/css" title="override_css">%s</style>'%(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

View File

@ -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):
>
<body>
<div class="navbar" style="text-align:${'center' if center else 'left'}; font-family:monospace; font-size:8pt">
<div class="navbar" style="text-align:${'center' if center else 'left'};">
<hr py:if="bottom" />
<p py:if="bottom" style="text-align:left">
This article was downloaded by <b>${__appname__}</b> from <a href="${url}">${url}</a>
@ -104,7 +85,7 @@ class IndexTemplate(Template):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>${title}</title>
<style type="text/css">
<style py:if="style" type="text/css">
${style}
</style>
</head>
@ -144,7 +125,7 @@ class FeedTemplate(Template):
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>${feed.title}</title>
<style type="text/css">
<style py:if="style" type="text/css">
${style}
</style>
</head>