Handle default para and char styles

This commit is contained in:
Kovid Goyal 2013-05-10 11:28:04 +05:30
parent 9136a8cc6a
commit a2d82df26a

View File

@ -6,6 +6,7 @@ from __future__ import (unicode_literals, division, absolute_import,
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>' __copyright__ = '2013, Kovid Goyal <kovid at kovidgoyal.net>'
import textwrap
from collections import OrderedDict, Counter from collections import OrderedDict, Counter
from calibre.ebooks.docx.block_styles import ParagraphStyle, inherit from calibre.ebooks.docx.block_styles import ParagraphStyle, inherit
@ -31,6 +32,7 @@ class Style(object):
self.based_on = get(based_on[0], 'w:val') if based_on else None self.based_on = get(based_on[0], 'w:val') if based_on else None
if self.style_type == 'numbering': if self.style_type == 'numbering':
self.based_on = None self.based_on = None
self.is_default = get(elem, 'w:default') in {'1', 'on', 'true'}
self.paragraph_style = self.character_style = None self.paragraph_style = self.character_style = None
@ -74,6 +76,7 @@ class Styles(object):
self.run_cache = {} self.run_cache = {}
self.classes = {} self.classes = {}
self.counter = Counter() self.counter = Counter()
self.default_styles = {}
def __iter__(self): def __iter__(self):
for s in self.id_map.itervalues(): for s in self.id_map.itervalues():
@ -93,6 +96,8 @@ class Styles(object):
s = Style(s) s = Style(s)
if s.style_id: if s.style_id:
self.id_map[s.style_id] = s self.id_map[s.style_id] = s
if s.is_default:
self.default_styles[s.style_type] = s
self.default_paragraph_style = self.default_character_style = None self.default_paragraph_style = self.default_character_style = None
@ -168,6 +173,8 @@ class Styles(object):
parent_styles = [] parent_styles = []
if self.default_paragraph_style is not None: if self.default_paragraph_style is not None:
parent_styles.append(self.default_paragraph_style) parent_styles.append(self.default_paragraph_style)
default_para = self.default_styles.get('paragraph', None)
if direct_formatting.linked_style is not None: if direct_formatting.linked_style is not None:
ls = self.get(direct_formatting.linked_style) ls = self.get(direct_formatting.linked_style)
if ls is not None: if ls is not None:
@ -177,6 +184,11 @@ class Styles(object):
parent_styles.append(ps) parent_styles.append(ps)
if ls.character_style is not None: if ls.character_style is not None:
self.para_char_cache[p] = ls.character_style self.para_char_cache[p] = ls.character_style
elif default_para is not None:
if default_para.paragraph_style is not None:
parent_styles.append(default_para.paragraph_style)
if default_para.character_style is not None:
self.para_char_cache[p] = default_para.character_style
for attr in ans.all_properties: for attr in ans.all_properties:
setattr(ans, attr, self.para_val(parent_styles, direct_formatting, attr)) setattr(ans, attr, self.para_val(parent_styles, direct_formatting, attr))
@ -199,6 +211,7 @@ class Styles(object):
direct_formatting = RunStyle() direct_formatting = RunStyle()
parent_styles = [] parent_styles = []
default_char = self.default_styles.get('character', None)
if self.default_character_style is not None: if self.default_character_style is not None:
parent_styles.append(self.default_character_style) parent_styles.append(self.default_character_style)
pstyle = self.para_char_cache.get(p, None) pstyle = self.para_char_cache.get(p, None)
@ -208,6 +221,8 @@ class Styles(object):
ls = self.get(direct_formatting.linked_style).character_style ls = self.get(direct_formatting.linked_style).character_style
if ls is not None: if ls is not None:
parent_styles.append(ls) parent_styles.append(ls)
elif default_char is not None and default_char.character_style is not None:
parent_styles.append(default_char.character_style)
for attr in ans.all_properties: for attr in ans.all_properties:
setattr(ans, attr, self.run_val(parent_styles, direct_formatting, attr)) setattr(ans, attr, self.run_val(parent_styles, direct_formatting, attr))
@ -244,10 +259,15 @@ class Styles(object):
return self.classes.get(h, (None, None))[0] return self.classes.get(h, (None, None))[0]
def generate_css(self): def generate_css(self):
prefix = textwrap.dedent(
'''\
p { margin: 0; padding: 0; text-indent: 1.5em }
''')
ans = [] ans = []
for (cls, css) in sorted(self.classes.itervalues(), key=lambda x:x[0]): for (cls, css) in sorted(self.classes.itervalues(), key=lambda x:x[0]):
b = ('\t%s: %s;' % (k, v) for k, v in css.iteritems()) b = ('\t%s: %s;' % (k, v) for k, v in css.iteritems())
b = '\n'.join(b) b = '\n'.join(b)
ans.append('.%s {\n%s\n}\n' % (cls, b.rstrip(';'))) ans.append('.%s {\n%s\n}\n' % (cls, b.rstrip(';')))
return '\n'.join(ans) return prefix + '\n' + '\n'.join(ans)