~homeworkprod/byceps

4525cabeafa4db830bad169cc7d85a4cb62c4ab4 — Jochen Kupperschmidt 1 year, 7 months ago e6109c3
Extract function to render news image caption
1 files changed, 16 insertions(+), 17 deletions(-)

M byceps/services/news/html_service.py
M byceps/services/news/html_service.py => byceps/services/news/html_service.py +16 -17
@@ 42,8 42,6 @@ def _render_image(
    if image is None:
        raise Exception(f'Unknown image number "{number}"')

    img_src = image.url_path

    figure_attrs = ''
    img_attrs = ''
    figcaption_attrs = ''


@@ 57,25 55,26 @@ def _render_image(
    if height:
        img_attrs += f' height="{height}"'

    caption = image.caption
    if caption is None:
        caption = ''

    if image.attribution:
        if caption:
            caption += ' '
        caption += f'<small>Bild: {image.attribution}</small>'

    caption_elem = (
        f'<figcaption{figcaption_attrs}>{caption}</figcaption>'
        if caption
        else ''
    )
    caption_elem = _render_image_caption(image, figcaption_attrs)

    html = f"""\
<figure{figure_attrs}>
  <img src="{img_src}"{img_attrs}>
  <img src="{image.url_path}"{img_attrs}>
  {caption_elem}
</figure>"""

    return Markup(html)


def _render_image_caption(image: Image, attrs: str) -> str:
    caption = image.caption or ''

    if image.attribution:
        if caption:
            caption += ' '
        caption += f'<small>Bild: {image.attribution}</small>'

    if not caption:
        return ''

    return f'<figcaption{attrs}>{caption}</figcaption>'