forked from LiveCarta/BookConverter
epub converter: fix headings levels
This commit is contained in:
@@ -2,7 +2,7 @@ import re
|
||||
from typing import Union
|
||||
|
||||
from ebooklib.epub import Section, Link
|
||||
|
||||
from livecarta_config import LawCartaConfig
|
||||
|
||||
"""
|
||||
These are data structures which form mapping from NCX to python data structures.
|
||||
@@ -10,13 +10,13 @@ These are data structures which form mapping from NCX to python data structures.
|
||||
|
||||
|
||||
class NavPoint:
|
||||
def __init__(self, obj: Union[Link, Section]=None, ):
|
||||
def __init__(self, obj: Union[Link, Section] = None, ):
|
||||
self.href, self.id = self.parse_href_id(obj)
|
||||
self.title = obj.title
|
||||
|
||||
@staticmethod
|
||||
def parse_href_id(item: Union[Link, Section]):
|
||||
reg = '(.+\..+\#)(.+)'
|
||||
reg = r'(.+\..+\#)(.+)'
|
||||
match = re.search(reg, item.href)
|
||||
href, div_id = None, None
|
||||
if match:
|
||||
@@ -24,7 +24,7 @@ class NavPoint:
|
||||
if match.group(1):
|
||||
href = match.group(1)[:-1]
|
||||
else:
|
||||
reg2 = '(.+\..+)'
|
||||
reg2 = r'(.+\..+)'
|
||||
match2 = re.search(reg2, item.href)
|
||||
if match2 and match2.group(1):
|
||||
href = match2.group(1)
|
||||
@@ -39,6 +39,14 @@ class NavPoint:
|
||||
These are data structures which form mapping to livecarta json structure.
|
||||
"""
|
||||
|
||||
atom = lambda x: not isinstance(x, list)
|
||||
nil = lambda x: not x
|
||||
car = lambda x: x[0]
|
||||
cdr = lambda x: x[1:]
|
||||
cons = lambda x, y: x + y
|
||||
|
||||
flatten = lambda x: [x] if atom(x) else x if nil(x) else cons(*map(flatten, [car(x), cdr(x)]))
|
||||
|
||||
|
||||
class ChapterItem:
|
||||
def __init__(self, title, content, sub_items):
|
||||
@@ -46,16 +54,30 @@ class ChapterItem:
|
||||
self.content = content
|
||||
self.sub_items = sub_items
|
||||
|
||||
def to_dict(self):
|
||||
tmp = []
|
||||
def to_dict(self, lvl=1):
|
||||
sub_dicts = []
|
||||
if self.sub_items:
|
||||
for i in self.sub_items:
|
||||
tmp.append(i.to_dict())
|
||||
sub_dicts.append(i.to_dict(lvl + 1))
|
||||
|
||||
if lvl > LawCartaConfig.SUPPORTED_LEVELS:
|
||||
return {
|
||||
"title": self.title,
|
||||
"contents": [self.content] + [x['contents'] for x in sub_dicts],
|
||||
"sub_items": []
|
||||
}
|
||||
|
||||
if (lvl == LawCartaConfig.SUPPORTED_LEVELS) and sub_dicts:
|
||||
return {
|
||||
"title": self.title,
|
||||
"contents": [self.content] + flatten([x['contents'] for x in sub_dicts]),
|
||||
"sub_items": []
|
||||
}
|
||||
|
||||
return {
|
||||
"title": self.title,
|
||||
"contents": [self.content],
|
||||
"sub_items": tmp
|
||||
"sub_items": sub_dicts
|
||||
}
|
||||
|
||||
def __str__(self):
|
||||
|
||||
Reference in New Issue
Block a user