mirror of
https://github.com/suitenumerique/docs.git
synced 2026-09-27 03:55:01 +02:00
its stateis fetched from yhub and seeded into the copy instead of being copied from the content stored by Django
49 lines
1.2 KiB
Python
49 lines
1.2 KiB
Python
"""Yjs document conversion utilities."""
|
|
|
|
import base64
|
|
import re
|
|
|
|
import pycrdt
|
|
from bs4 import BeautifulSoup
|
|
|
|
from core import enums
|
|
|
|
|
|
def yjs_to_xml(update):
|
|
"""Extract xml from a raw yjs update."""
|
|
|
|
doc = pycrdt.Doc()
|
|
doc.apply_update(update)
|
|
return str(doc.get("document-store", type=pycrdt.XmlFragment))
|
|
|
|
|
|
def base64_yjs_to_xml(base64_string):
|
|
"""Extract xml from base64 yjs document."""
|
|
|
|
return yjs_to_xml(base64.b64decode(base64_string))
|
|
|
|
|
|
def base64_yjs_to_text(base64_string):
|
|
"""Extract text from base64 yjs document."""
|
|
|
|
blocknote_structure = base64_yjs_to_xml(base64_string)
|
|
soup = BeautifulSoup(blocknote_structure, "lxml-xml")
|
|
return soup.get_text(separator=" ", strip=True)
|
|
|
|
|
|
def extract_attachments(content):
|
|
"""Helper method to extract media paths from a document's content."""
|
|
if not content:
|
|
return []
|
|
|
|
xml_content = base64_yjs_to_xml(content)
|
|
return re.findall(enums.MEDIA_STORAGE_URL_EXTRACT, xml_content)
|
|
|
|
|
|
def extract_attachments_from_update(update):
|
|
"""Helper method to extract media paths from a raw yjs update."""
|
|
if not update:
|
|
return []
|
|
|
|
return re.findall(enums.MEDIA_STORAGE_URL_EXTRACT, yjs_to_xml(update))
|