Skip to content

Commit

Permalink
handle hr and log for missing handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed May 30, 2024
1 parent 50b0d72 commit c86c86c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/rer/newsletter/blocks_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ def __call__(self, context, blocks, blocks_layout):
return ""
html = []
for block in blocks:
handler = getattr(self, f"block_handler_{block.get('@type', '')}", None)
handler = getattr(
self, f"block_handler_{block.get('@type', '')}", None
)
if handler and callable(handler):
value = handler(context=context, block=block)
if value:
Expand Down Expand Up @@ -64,9 +66,11 @@ def block_handler_image(self, block, context=None):
Return converted image block to HTML
"""
scales_mapping = {"l": "large", "s": "mini", "m": "preview"}
image_scales = block.get("image_scales", {}).get("image", [])[0]
image_scales = block.get("image_scales", {}).get("image", [])
if not image_scales:
return ""

image_scales = image_scales[0]
align = block.get("align", "")
root_classes = ["block", "image"]

Expand Down Expand Up @@ -110,7 +114,9 @@ def block_handler_gridBlock(self, block, context):
tr = root.findAll("tr")[0]

for block in blocks:
handler = getattr(self, f"block_handler_{block.get('@type', '')}", None)
handler = getattr(
self, f"block_handler_{block.get('@type', '')}", None
)
if handler and callable(handler):
value = handler(block)
if value:
Expand Down
8 changes: 8 additions & 0 deletions src/rer/newsletter/blocks_converter/slate2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from lxml.html import tostring

import json
import logging

logger = logging.getLogger(__name__)

SLATE_ACCEPTED_TAGS = ACCEPTED_TAGS + ["link"]

Expand Down Expand Up @@ -47,6 +49,9 @@ def serialize(self, element):
handler = getattr(self, "handle_tag_{}".format(tagname), None)
if not handler and tagname in SLATE_ACCEPTED_TAGS:
handler = self.handle_block
if not handler:
logger.warning(f"Unhandled tag: {tagname}. Skipping.")
return []
res = handler(element)
if isinstance(res, list):
return res
Expand Down Expand Up @@ -87,6 +92,9 @@ def handle_slate_data_element(self, element):

return el(*children, **attributes)

def handle_tag_hr(self, element):
return getattr(E, "HR")()

def handle_block(self, element):
"""handle_block.
Expand Down

0 comments on commit c86c86c

Please sign in to comment.