Add support to add images to footnote
This commit is contained in:
@@ -24,6 +24,7 @@ class Config:
|
||||
self._data['export_nodownload'] = True
|
||||
self._data['img_cache_dir'] = 'img_cache'
|
||||
self._data['page_size'] = 10
|
||||
self._data['image_type'] = 'inline'
|
||||
self.save()
|
||||
|
||||
def add_args(self, args: Namespace):
|
||||
@@ -103,6 +104,10 @@ class Config:
|
||||
def img_cache_dir(self):
|
||||
return self.get_arg('img_cache_dir', 'img_cache')
|
||||
|
||||
@cached_property
|
||||
def image_type(self):
|
||||
return self.get_arg('image_type', 'inline')
|
||||
|
||||
@cached_property
|
||||
def key(self):
|
||||
return self.get_arg('key', None)
|
||||
|
||||
34
epub.py
34
epub.py
@@ -150,6 +150,7 @@ class HTMLImage:
|
||||
elif key == 'alt':
|
||||
self.alt = value
|
||||
self.cfg = cfg
|
||||
self.footnote = ''
|
||||
|
||||
def is_valid(self):
|
||||
return self.src is not None and self.src != ''
|
||||
@@ -163,7 +164,7 @@ class HTMLImage:
|
||||
except Exception:
|
||||
print_exc()
|
||||
|
||||
def to_local(self):
|
||||
def to_local(self, index: int):
|
||||
if not self.is_valid():
|
||||
return ""
|
||||
if not self.download_image():
|
||||
@@ -180,7 +181,21 @@ class HTMLImage:
|
||||
d = {'src': self.epub_path}
|
||||
if self.alt:
|
||||
d['alt'] = self.alt
|
||||
return ET.tostring(ET.Element('img', d), 'unicode')
|
||||
img = ET.Element('img', d)
|
||||
if self.cfg.image_type == 'inline':
|
||||
return ET.tostring(img, 'unicode')
|
||||
else:
|
||||
link = ET.Element('a', {'href': f'#img{index}',
|
||||
'epub:type': 'noteref'})
|
||||
if self.alt:
|
||||
link.text = self.alt
|
||||
aside = ET.Element('aside', {'epub:type': 'footnote',
|
||||
'id': f'img{index}'})
|
||||
p = ET.Element('p')
|
||||
p.append(img)
|
||||
aside.append(p)
|
||||
self.footnote = ET.tostring(aside, 'unicode') + '\n'
|
||||
return ET.tostring(link, 'unicode')
|
||||
|
||||
|
||||
# Used to parse content
|
||||
@@ -193,6 +208,7 @@ class ContentParser(HTMLParser):
|
||||
self.images = []
|
||||
self._paragraph_data = ''
|
||||
self.cfg = cfg
|
||||
self.footnote = ''
|
||||
|
||||
def handle_data(self, data: str):
|
||||
if self._in_paragraph:
|
||||
@@ -241,12 +257,15 @@ class ContentParser(HTMLParser):
|
||||
return True
|
||||
return False
|
||||
|
||||
def to_local(self, data_list=None) -> str:
|
||||
def to_local(self, data_list=None, root=None) -> str:
|
||||
default_data_list = False
|
||||
if data_list is None:
|
||||
data_list = self.data
|
||||
default_data_list = True
|
||||
root = self
|
||||
self.footnote = ''
|
||||
data = ''
|
||||
img_index = 0
|
||||
for i in data_list:
|
||||
if isinstance(i, str):
|
||||
if default_data_list:
|
||||
@@ -256,16 +275,21 @@ class ContentParser(HTMLParser):
|
||||
elif isinstance(i, HTMLImage):
|
||||
if i.is_valid():
|
||||
try:
|
||||
data += i.to_local()
|
||||
data += i.to_local(img_index)
|
||||
self.images.append(i)
|
||||
if i.footnote:
|
||||
root.footnote += i.footnote
|
||||
img_index += 1
|
||||
except ValueError:
|
||||
print("the image is not valid.", i.src)
|
||||
elif isinstance(i, list):
|
||||
data += f'<p>{self.to_local(i)}</p>\n'
|
||||
data += f'<p>{self.to_local(i, self)}</p>\n'
|
||||
else:
|
||||
raise NotImplementedError()
|
||||
if self._paragraph_data:
|
||||
data += f'<p>{self._paragraph_data}</p>\n'
|
||||
if default_data_list:
|
||||
data += self.footnote
|
||||
return data
|
||||
|
||||
|
||||
|
||||
1
main.py
1
main.py
@@ -21,6 +21,7 @@ parser.add_argument('--ebt', '--export-book-template', help='The template of the
|
||||
parser.add_argument('--icd', '--image-cache-dir', help='Path to image cache directory.', metavar='PATH') # noqa: E501
|
||||
parser.add_argument('-s', '--page-size', help='Maximum size of a page when asking for choices.', type=int, metavar='SIZE') # noqa: E501
|
||||
parser.add_argument('-a', '--export-nodownload', help='export not downloaded chapter when exporting book.', type=parse_bool, metavar='BOOL') # noqa: E501
|
||||
parser.add_argument('-i', '--image-type', help='How to handle images in EPUB. Available types: inline, footnote. Default: inline', choices=['inline', 'footnote'], metavar='TYPE') # noqa: E501
|
||||
parser.add_argument('action', help='The action to do.', choices=['importkey', 'exportchapter', 'exportbook', 'export', 'exportall', 'ik', 'ec', 'eb', 'e', 'ea'], nargs='?', default='export') # noqa: E501
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user