forked from LiveCarta/BookConverter
small refactoring
This commit is contained in:
@@ -173,11 +173,8 @@ class HTMLDocxPreprocessor:
|
|||||||
margin_bottom = re.search(
|
margin_bottom = re.search(
|
||||||
r'margin-bottom: ([\d.]{1,4})in', style)
|
r'margin-bottom: ([\d.]{1,4})in', style)
|
||||||
else:
|
else:
|
||||||
indent = None
|
indent = margin_left = margin_right = \
|
||||||
margin_left = None
|
margin_top = margin_bottom = None
|
||||||
margin_right = None
|
|
||||||
margin_top = None
|
|
||||||
margin_bottom = None
|
|
||||||
|
|
||||||
if margin_left and margin_right and margin_top and margin_bottom and \
|
if margin_left and margin_right and margin_top and margin_bottom and \
|
||||||
margin_left.group(1) == '0.6' and margin_right.group(1) == '0.6' and \
|
margin_left.group(1) == '0.6' and margin_right.group(1) == '0.6' and \
|
||||||
@@ -328,23 +325,23 @@ class HTMLDocxPreprocessor:
|
|||||||
"a", {'name': re.compile(r'^_Toc\d+')})
|
"a", {'name': re.compile(r'^_Toc\d+')})
|
||||||
headers = [link.parent for link in toc_links]
|
headers = [link.parent for link in toc_links]
|
||||||
outline_level = "1" # All the unknown outlines will be predicted as <h1>
|
outline_level = "1" # All the unknown outlines will be predicted as <h1>
|
||||||
for tag in headers:
|
for h_tag in headers:
|
||||||
if re.search(r"^h\d$", tag.name):
|
if re.search(r"^h\d$", h_tag.name):
|
||||||
tag.a.unwrap()
|
h_tag.a.unwrap()
|
||||||
# outline_level = tag.name[-1] # TODO: add prediction of the outline level
|
# outline_level = tag.name[-1] # TODO: add prediction of the outline level
|
||||||
elif tag.name == "p":
|
elif h_tag.name == "p":
|
||||||
exist_in_toc = self._check_parent_link_exist_in_toc(tag)
|
exist_in_toc = self._check_parent_link_exist_in_toc(h_tag)
|
||||||
if tag in self.body_tag.find_all("p") and exist_in_toc:
|
if h_tag in self.body_tag.find_all("p") and exist_in_toc:
|
||||||
new_tag = BeautifulSoup(
|
new_tag = BeautifulSoup(
|
||||||
features="lxml").new_tag("h" + outline_level)
|
features="lxml").new_tag("h" + outline_level)
|
||||||
text = tag.text
|
text = h_tag.text
|
||||||
tag.replaceWith(new_tag)
|
h_tag.replaceWith(new_tag)
|
||||||
new_tag.string = text
|
new_tag.string = text
|
||||||
else:
|
else:
|
||||||
# rethink document structure when you have toc_links, other cases?
|
# rethink document structure when you have toc_links, other cases?
|
||||||
self.logger_object.log(f'Something went wrong in processing toc_links.'
|
self.logger_object.log(f'Something went wrong in processing toc_links.'
|
||||||
f' Check the structure of the file. '
|
f' Check the structure of the file. '
|
||||||
f'Tag name: {tag.name}')
|
f'Tag name: {h_tag.name}')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def clean_title_from_numbering(title: str):
|
def clean_title_from_numbering(title: str):
|
||||||
@@ -568,7 +565,7 @@ class HTMLDocxPreprocessor:
|
|||||||
ind = self.content.index(toc_tag) + 1
|
ind = self.content.index(toc_tag) + 1
|
||||||
self.content = self.content[ind:]
|
self.content = self.content[ind:]
|
||||||
|
|
||||||
def process_html(self, access=None, html_path='', book_id='local'):
|
def process_html(self, access=None, html_path='', book_id=0):
|
||||||
"""Process html code to satisfy LiveCarta formatting."""
|
"""Process html code to satisfy LiveCarta formatting."""
|
||||||
self.logger_object.log('Beginning of processing .html file.')
|
self.logger_object.log('Beginning of processing .html file.')
|
||||||
try:
|
try:
|
||||||
@@ -600,7 +597,8 @@ class HTMLDocxPreprocessor:
|
|||||||
f'{len(self.footnotes)} footnotes have been processed.')
|
f'{len(self.footnotes)} footnotes have been processed.')
|
||||||
|
|
||||||
self.logger_object.log('Image processing.')
|
self.logger_object.log('Image processing.')
|
||||||
self.images = process_images(self.body_tag, access=access, html_path=html_path, book_id=book_id)
|
self.images = process_images(access=access, html_path=html_path,
|
||||||
|
book_id=book_id, body_tag=self.body_tag)
|
||||||
self.logger_object.log(
|
self.logger_object.log(
|
||||||
f'{len(self.images)} images have been processed.')
|
f'{len(self.images)} images have been processed.')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user