| 1 |
# License: GPLv3 Copyright: 2008, Kovid Goyal <kovid at kovidgoyal.net> |
| 2 |
|
| 3 |
from lxml import etree |
| 4 |
|
| 5 |
|
| 6 |
class InlineClass(etree.XSLTExtension): |
| 7 |
FMTS = ('italics', 'bold', 'strike-through', 'small-caps') |
| 8 |
|
| 9 |
def __init__(self, log): |
| 10 |
etree.XSLTExtension.__init__(self) |
| 11 |
self.log = log |
| 12 |
self.font_sizes = [] |
| 13 |
self.colors = [] |
| 14 |
|
| 15 |
def execute(self, context, self_node, input_node, output_parent): |
| 16 |
classes = ['none'] |
| 17 |
for x in self.FMTS: |
| 18 |
if input_node.get(x, None) == 'true': |
| 19 |
classes.append(x) |
| 20 |
# underlined is special |
| 21 |
if input_node.get('underlined', 'false') != 'false': |
| 22 |
classes.append('underlined') |
| 23 |
fs = input_node.get('font-size', False) |
| 24 |
if fs: |
| 25 |
if fs not in self.font_sizes: |
| 26 |
self.font_sizes.append(fs) |
| 27 |
classes.append(f'fs{self.font_sizes.index(fs)}') |
| 28 |
fc = input_node.get('font-color', False) |
| 29 |
if fc: |
| 30 |
if fc not in self.colors: |
| 31 |
self.colors.append(fc) |
| 32 |
classes.append(f'col{self.colors.index(fc)}') |
| 33 |
|
| 34 |
output_parent.text = ' '.join(classes) |