Added support for multiple bodies for fb2 document

This commit is contained in:
Dmitriy Shishkov 2021-07-01 22:38:31 +05:00
parent aad879a6ed
commit f717291823
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060

View File

@ -24,6 +24,8 @@ async def fb22html(file: SpooledTemporaryFile) -> str:
# TODO: join tokens to HTML # TODO: join tokens to HTML
html_content = "" html_content = ""
... ...
print(tokens.keys())
return html_content return html_content
except Exception as e: except Exception as e:
@ -44,14 +46,14 @@ def fb22tokens(file: SpooledTemporaryFile) -> dict[str, str]:
"\<asset_id\>": "\<base64_data\>" } "\<asset_id\>": "\<base64_data\>" }
""" """
tokens = {"metadata": {}, "content": ""} tokens = {"metadata": {}, "content": b""}
book = ET.parse(file) book = ET.parse(file)
description, body, *assets = book.getroot() root = book.getroot()
description: Element description = root.find("./description", namespaces)
body: Element bodies = root.findall("./body", namespaces)
assets: list[Element] assets = root.findall("./binary", namespaces)
# Reading book metadata # Reading book metadata
@ -69,7 +71,8 @@ def fb22tokens(file: SpooledTemporaryFile) -> dict[str, str]:
# Reading book content # Reading book content
tokens["content"] = ET.tostring(body).replace(b"ns0:", b"") for body in bodies:
tokens["content"] += ET.tostring(body).replace(b"ns0:", b"")
# Reading assets # Reading assets