Add new settings
This commit is contained in:
@@ -16,6 +16,7 @@ class Config:
|
|||||||
with open(cfg_path, 'r', encoding='UTF-8') as f:
|
with open(cfg_path, 'r', encoding='UTF-8') as f:
|
||||||
self._data = json.load(f)
|
self._data = json.load(f)
|
||||||
else:
|
else:
|
||||||
|
self._data['add_images_to_single_page'] = False
|
||||||
self._data['db'] = 'cwm.db'
|
self._data['db'] = 'cwm.db'
|
||||||
self._data['save_to_config'] = True
|
self._data['save_to_config'] = True
|
||||||
self._data['type'] = 'epub,txt'
|
self._data['type'] = 'epub,txt'
|
||||||
@@ -43,6 +44,10 @@ class Config:
|
|||||||
self._data[key] = default
|
self._data[key] = default
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def add_images_to_single_page(self):
|
||||||
|
return self.get_arg('add_images_to_single_page', False)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def book_id(self):
|
def book_id(self):
|
||||||
return getattr(self._args, 'bid', None)
|
return getattr(self._args, 'bid', None)
|
||||||
@@ -130,7 +135,7 @@ class Config:
|
|||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def save_to_config(self):
|
def save_to_config(self):
|
||||||
return getattr(self._args, 'save_to_config', True)
|
return getattr(self._data, 'save_to_config', True)
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
with open(self._path, 'w', encoding='UTF-8') as f:
|
with open(self._path, 'w', encoding='UTF-8') as f:
|
||||||
|
|||||||
34
epub.py
34
epub.py
@@ -139,7 +139,7 @@ def perform_convert(image_path: str) -> Optional[str]:
|
|||||||
|
|
||||||
|
|
||||||
class HTMLImage:
|
class HTMLImage:
|
||||||
def __init__(self, attrs, cfg: Config):
|
def __init__(self, attrs, cfg: Config, chapter_id):
|
||||||
self.src = None
|
self.src = None
|
||||||
self.alt = None
|
self.alt = None
|
||||||
self.path = None
|
self.path = None
|
||||||
@@ -151,6 +151,7 @@ class HTMLImage:
|
|||||||
self.alt = value
|
self.alt = value
|
||||||
self.cfg = cfg
|
self.cfg = cfg
|
||||||
self.footnote = ''
|
self.footnote = ''
|
||||||
|
self.chapter_id = chapter_id
|
||||||
|
|
||||||
def is_valid(self):
|
def is_valid(self):
|
||||||
return self.src is not None and self.src != ''
|
return self.src is not None and self.src != ''
|
||||||
@@ -185,22 +186,23 @@ class HTMLImage:
|
|||||||
if self.cfg.image_type == 'inline':
|
if self.cfg.image_type == 'inline':
|
||||||
return ET.tostring(img, 'unicode')
|
return ET.tostring(img, 'unicode')
|
||||||
else:
|
else:
|
||||||
link = ET.Element('a', {'href': f'#img{index}',
|
target = ''
|
||||||
|
if self.cfg.add_images_to_single_page:
|
||||||
|
target = f'{self.chapter_id}_img.xhtml'
|
||||||
|
link = ET.Element('a', {'href': f'{target}#img{index}',
|
||||||
'epub:type': 'noteref'})
|
'epub:type': 'noteref'})
|
||||||
if self.alt:
|
if self.alt:
|
||||||
link.text = self.alt
|
link.text = self.alt
|
||||||
aside = ET.Element('aside', {'epub:type': 'footnote',
|
aside = ET.Element('aside', {'epub:type': 'footnote',
|
||||||
'id': f'img{index}'})
|
'id': f'img{index}'})
|
||||||
p = ET.Element('p')
|
aside.append(img)
|
||||||
p.append(img)
|
|
||||||
aside.append(p)
|
|
||||||
self.footnote = ET.tostring(aside, 'unicode') + '\n'
|
self.footnote = ET.tostring(aside, 'unicode') + '\n'
|
||||||
return ET.tostring(link, 'unicode')
|
return ET.tostring(link, 'unicode')
|
||||||
|
|
||||||
|
|
||||||
# Used to parse content
|
# Used to parse content
|
||||||
class ContentParser(HTMLParser):
|
class ContentParser(HTMLParser):
|
||||||
def __init__(self, cfg: Config):
|
def __init__(self, cfg: Config, chapter_id):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._in_paragraph = False
|
self._in_paragraph = False
|
||||||
self.data = []
|
self.data = []
|
||||||
@@ -210,6 +212,7 @@ class ContentParser(HTMLParser):
|
|||||||
self.cfg = cfg
|
self.cfg = cfg
|
||||||
self.footnote = ''
|
self.footnote = ''
|
||||||
self.img_index = 0
|
self.img_index = 0
|
||||||
|
self.chapter_id = chapter_id
|
||||||
|
|
||||||
def handle_data(self, data: str):
|
def handle_data(self, data: str):
|
||||||
if self._in_paragraph:
|
if self._in_paragraph:
|
||||||
@@ -225,7 +228,8 @@ class ContentParser(HTMLParser):
|
|||||||
self._paragraph_data = [self._paragraph_data]
|
self._paragraph_data = [self._paragraph_data]
|
||||||
else:
|
else:
|
||||||
self._paragraph_data = []
|
self._paragraph_data = []
|
||||||
self._paragraph_data.append(HTMLImage(attrs, self.cfg))
|
self._paragraph_data.append(HTMLImage(attrs, self.cfg,
|
||||||
|
self.chapter_id))
|
||||||
else:
|
else:
|
||||||
self.data.append(HTMLImage(attrs, self.cfg))
|
self.data.append(HTMLImage(attrs, self.cfg))
|
||||||
elif tag == 'p':
|
elif tag == 'p':
|
||||||
@@ -289,7 +293,8 @@ class ContentParser(HTMLParser):
|
|||||||
if self._paragraph_data:
|
if self._paragraph_data:
|
||||||
data += f'<p>{self._paragraph_data}</p>\n'
|
data += f'<p>{self._paragraph_data}</p>\n'
|
||||||
if default_data_list:
|
if default_data_list:
|
||||||
data += self.footnote
|
if not self.cfg.add_images_to_single_page:
|
||||||
|
data += self.footnote
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
@@ -355,7 +360,7 @@ class EpubFile:
|
|||||||
uid=f'ch{chapter_id}',
|
uid=f'ch{chapter_id}',
|
||||||
)
|
)
|
||||||
ch.is_linear = is_linear
|
ch.is_linear = is_linear
|
||||||
parser = ContentParser(self.cfg)
|
parser = ContentParser(self.cfg, chapter_id)
|
||||||
contents = content.splitlines()
|
contents = content.splitlines()
|
||||||
try:
|
try:
|
||||||
parser.feed('<p>' + '</p>\n<p>'.join(contents) + '</p>')
|
parser.feed('<p>' + '</p>\n<p>'.join(contents) + '</p>')
|
||||||
@@ -391,6 +396,17 @@ class EpubFile:
|
|||||||
else:
|
else:
|
||||||
self.EpubList.append(ch)
|
self.EpubList.append(ch)
|
||||||
self.epub.spine.append(ch)
|
self.epub.spine.append(ch)
|
||||||
|
if self.cfg.add_images_to_single_page and parser.footnote:
|
||||||
|
ch_img = epub.EpubHtml(
|
||||||
|
title=f"{chapter_title} (图片)",
|
||||||
|
file_name=f'{chapter_id}_img.xhtml',
|
||||||
|
lang='zh-CN',
|
||||||
|
uid=f'ch{chapter_id}_img',
|
||||||
|
)
|
||||||
|
ch_img.is_linear = False
|
||||||
|
ch_img.content = parser.footnote
|
||||||
|
self.epub.add_item(ch_img)
|
||||||
|
self.epub.spine.append(ch_img)
|
||||||
|
|
||||||
def add_nodownload_chapter(self, chapter, division_name: str,
|
def add_nodownload_chapter(self, chapter, division_name: str,
|
||||||
is_linear: bool):
|
is_linear: bool):
|
||||||
|
|||||||
1
main.py
1
main.py
@@ -25,6 +25,7 @@ parser.add_argument('-i', '--image-type', help='How to handle images in EPUB. Av
|
|||||||
parser.add_argument('-f', '--force', help='Force import keys.', action='store_true') # noqa: E501
|
parser.add_argument('-f', '--force', help='Force import keys.', action='store_true') # noqa: E501
|
||||||
parser.add_argument('-D', '--division-id', help='The division id.', type=int, metavar='ID') # noqa: E501
|
parser.add_argument('-D', '--division-id', help='The division id.', type=int, metavar='ID') # noqa: E501
|
||||||
parser.add_argument('-l', '--linear', help='Mark as linear.', type=parse_bool, metavar='BOOL', default=False) # noqa: E501
|
parser.add_argument('-l', '--linear', help='Mark as linear.', type=parse_bool, metavar='BOOL', default=False) # noqa: E501
|
||||||
|
parser.add_argument('-A', '--add-images-to-single-page', help='Add images to single page.', type=parse_bool, metavar='BOOL') # noqa: E501
|
||||||
parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook', 'export', 'exportall', 'markaslinear', 'ik', 'ec', 'eb', 'e', 'ea', 'mal'], nargs='?', default='export') # noqa: E501
|
parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook', 'export', 'exportall', 'markaslinear', 'ik', 'ec', 'eb', 'e', 'ea', 'mal'], nargs='?', default='export') # noqa: E501
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user