Implement --extra-css

This commit is contained in:
Kovid Goyal 2009-04-20 13:55:21 -07:00
parent 6ec37ff715
commit b34854b6e4
4 changed files with 38 additions and 20 deletions

View File

@ -116,6 +116,7 @@ def add_pipeline_options(parser, plumber):
'font_size_mapping',
'line_height',
'linearize_tables',
'extra_css',
]
),

View File

@ -219,6 +219,14 @@ OptionRecommendation(name='chapter_mark',
'to mark chapters.')
),
OptionRecommendation(name='extra_css',
recommended_value=None, level=OptionRecommendation.LOW,
help=_('Either the path to a CSS stylesheet or raw CSS. '
'This CSS will be appended to the style rules from '
'the source file, so it can be used to override those '
'rules.')
),
OptionRecommendation(name='read_metadata_from_opf',
@ -487,6 +495,9 @@ OptionRecommendation(name='language',
else:
fkey = map(float, fkey.split(','))
if self.opts.extra_css and os.path.exists(self.opts.extra_css):
self.opts.extra_css = open(self.opts.extra_css, 'rb').read()
flattener = CSSFlattener(fbase=fbase, fkey=fkey,
lineh=self.opts.line_height,
untable=self.opts.linearize_tables)

View File

@ -106,7 +106,7 @@ class CSSSelector(etree.XPath):
class Stylizer(object):
STYLESHEETS = WeakKeyDictionary()
def __init__(self, tree, path, oeb, profile=PROFILES['PRS505']):
def __init__(self, tree, path, oeb, profile=PROFILES['PRS505'], extra_css=''):
self.oeb = oeb
self.profile = profile
self.logger = oeb.logger
@ -135,6 +135,11 @@ class Stylizer(object):
(path, item.href))
continue
stylesheets.append(sitem.data)
if extra_css:
text = XHTML_CSS_NAMESPACE + extra_css
stylesheet = parser.parseString(text, href=cssname)
stylesheet.namespaces['h'] = XHTML_NS
stylesheets.append(stylesheet)
rules = []
index = 0
self.stylesheets = set()

View File

@ -116,7 +116,8 @@ class CSSFlattener(object):
profile = self.context.source
for item in self.oeb.spine:
html = item.data
stylizer = Stylizer(html, item.href, self.oeb, profile)
stylizer = Stylizer(html, item.href, self.oeb, profile,
extra_css=self.context.extra_css)
self.stylizers[item] = stylizer
def baseline_node(self, node, stylizer, sizes, csize):