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. """ class NavPoint: 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 = r'(.+\..+\#)(.+)' match = re.search(reg, item.href) href, div_id = None, None if match: div_id = match.group(2) if match.group(1): href = match.group(1)[:-1] else: reg2 = r'(.+\..+)' match2 = re.search(reg2, item.href) if match2 and match2.group(1): href = match2.group(1) return href, div_id def __str__(self): return '' % (self.href, self.id) """ 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): self.title = title self.content = content self.sub_items = sub_items def to_dict(self, lvl=1): sub_dicts = [] if self.sub_items: for i in self.sub_items: 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": sub_dicts } def __str__(self): return '' % self.title