Skip to content

Commit b0d6c4f

Browse files
Better block merging
1 parent 5597d3d commit b0d6c4f

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

app/backend/src/couchers/email/rendering.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -310,24 +310,8 @@ def render(
310310
)
311311
)
312312

313-
# Merge any two subsequent action blocks into a single two-button block
314-
block_index = 0
315-
while block_index + 1 < len(blocks):
316-
block = blocks[block_index]
317-
next_block = blocks[block_index + 1]
318-
if isinstance(block, ActionBlock) and isinstance(next_block, ActionBlock):
319-
blocks[block_index] = TwoButtonHTMLBlock(
320-
target_url_1=block.target_url,
321-
text_1=block.text,
322-
target_url_2=next_block.target_url,
323-
text_2=next_block.target_url,
324-
)
325-
del blocks[block_index + 1]
326-
327-
block_index += 1
328-
329313
# Render each block
330-
for block in blocks:
314+
for block in type(self)._merge_action_blocks(blocks):
331315
match block:
332316
case ParaBlock():
333317
concats.append(self.para_block_template.render(block.__dict__, loc_context))
@@ -360,6 +344,28 @@ def render(
360344

361345
return "\n".join(concats)
362346

347+
@staticmethod
348+
def _merge_action_blocks(blocks: list[EmailBlock]) -> list[EmailBlock]:
349+
"""Merge any two subsequent action blocks into a single two-button block."""
350+
blocks = blocks.copy()
351+
352+
block_index = 0
353+
while block_index + 1 < len(blocks):
354+
block = blocks[block_index]
355+
next_block = blocks[block_index + 1]
356+
if isinstance(block, ActionBlock) and isinstance(next_block, ActionBlock):
357+
blocks[block_index] = TwoButtonHTMLBlock(
358+
target_url_1=block.target_url,
359+
text_1=block.text,
360+
target_url_2=next_block.target_url,
361+
text_2=next_block.target_url,
362+
)
363+
blocks.pop(block_index + 1)
364+
365+
block_index += 1
366+
367+
return blocks
368+
363369
@lru_cache(maxsize=1)
364370
@staticmethod
365371
def default() -> HTMLRenderer:

0 commit comments

Comments
 (0)