epub converter: update

This commit is contained in:
shirshasa
2021-04-23 15:21:24 +03:00
parent d5dc3529b7
commit 6cd4f9401d
4 changed files with 16 additions and 5 deletions

View File

@@ -1,4 +1,8 @@
bs4>=0.0.1 bs4>=0.0.1
pika>=1.1.0 pika>=1.1.0
requests>=2.22.0 requests>=2.22.0
lxml>=4.5.0 lxml>=4.5.0
cssutils~=2.2.0
ebooklib~=0.17.1
premailer~=3.8.0
webcolors==1.3

View File

@@ -1,5 +1,6 @@
import codecs import codecs
import json import json
from os.path import dirname, normpath, join
from collections import defaultdict from collections import defaultdict
from typing import Dict, Union from typing import Dict, Union
@@ -89,8 +90,12 @@ class EpubPostprocessor:
css_href = tag.attrs.get('href') css_href = tag.attrs.get('href')
self.html_href2css_href[item.file_name] = css_href self.html_href2css_href[item.file_name] = css_href
if css_href not in self.css_href2content: if css_href not in self.css_href2content:
print(css_href) path_to_css_from_html = css_href
css_content: str = self.ebooklib_book.get_item_with_href(css_href).get_content().decode() html_folder = dirname(item.file_name)
path_to_css_from_root = normpath(join(html_folder, path_to_css_from_html))
css_obj = self.ebooklib_book.get_item_with_href(path_to_css_from_root)
assert css_obj, f'Css style {css_href} was not in manifest.'
css_content: str = css_obj.get_content().decode()
self.css_href2content[css_href] = clean_css(css_content) self.css_href2content[css_href] = clean_css(css_content)
for i, tag in enumerate(soup.find_all('style')): for i, tag in enumerate(soup.find_all('style')):

View File

@@ -59,6 +59,8 @@ def str2color_name(s: str):
name = get_hex_colour_name(s) name = get_hex_colour_name(s)
return name return name
elif s in html4_hex_to_names.items():
return s
else: else:
return '' return ''

View File

@@ -76,8 +76,8 @@ LIVECARTA_STYLE_ATTRS_MAPPING = {
'font': lambda x: '', 'font': lambda x: '',
'font-family': lambda x: LawCartaConfig.font_correspondence_table.get(x.capitalize()), 'font-family': lambda x: LawCartaConfig.font_correspondence_table.get(x.capitalize()),
'font-size': convert_font_size, 'font-size': convert_font_size,
'color': str2color_name, 'color': lambda x: LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), ''),
'background-color': str2color_name, 'background-color': lambda x: LawCartaConfig.HTML42LIVECARTA_COLORS.get(str2color_name(x), ''),
} }
LIVECARTA_STYLE_ATTRS_SHOULD_BE_TAG = { LIVECARTA_STYLE_ATTRS_SHOULD_BE_TAG = {