FelixSchwarz / mjml-python

Python implementation for MJML - a framework that makes responsive-email easy
MIT License
74 stars 16 forks source link

extend functionality to handle "welcome-email" from email-templates repo #3

Closed FelixSchwarz closed 3 years ago

FelixSchwarz commented 3 years ago

The upstream project also hosts a couple of example templates in the email-templates repo. One of these examples is welcome-email.mjml but that template can not processed currently due to shortcomings (even after PR #2) in this Python port:

  File "…/mjml/mjml2html.py", line 168, in mjml_to_html
    content = processing(mjBody, bodyHelpers, applyAttributes)
  File "…/mjml/mjml2html.py", line 74, in processing
    return component.render()
  File "…/mjml/elements/mj_body.py", line 37, in render
    children_str = self.renderChildren()
  File "…/mjml/elements/_base.py", line 144, in renderChildren
    output += renderer(component)
  File "…/mjml/elements/_base.py", line 86, in <lambda>
    renderer = lambda component: component.render()
  File "…/mjml/elements/mj_section.py", line 92, in render
    return self.renderSimple()
  File "…/mjml/elements/mj_section.py", line 98, in renderSimple
    section = self.renderSection()
  File "…/mjml/elements/mj_section.py", line 145, in renderSection
    {self.renderWrappedChildren()}
  File "…/mjml/elements/mj_section.py", line 188, in renderWrappedChildren
    {self.renderChildren(children, renderer=render_child)}
  File "…/mjml/elements/_base.py", line 144, in renderChildren
    output += renderer(component)
  File "…/mjml/elements/mj_section.py", line 178, in render_child
    {component.render()}
  File "…/mjml/elements/mj_column.py", line 122, in render
    column_str = self.renderColumn() if (not self.hasGutter()) else self.renderGutter()
  File "…/mjml/elements/mj_column.py", line 233, in renderColumn
    {self.renderChildren(children, renderer=render_child)}
  File "…/mjml/elements/_base.py", line 144, in renderChildren
    output += renderer(component)
  File "…/mjml/elements/mj_column.py", line 220, in render_child
    {component.render()}
  File "…/mjml/elements/mj_text.py", line 54, in render
    return self._render_content()
  File "…/mjml/elements/mj_text.py", line 69, in _render_content
    content_html = self.getContent() + children_html
  File "…/mjml/core/api.py", line 62, in getContent
    return self.content.strip()
AttributeError: 'NoneType' object has no attribute 'strip'

I noticed the upstream JS code complains about some illegal attributes and the mjml template in general looks pretty convoluted:

Line 2 of welcome-email.mjml (mj-body) — Attribute font-size is illegal
Line 8 of welcome-email.mjml (mj-section) — Attribute vertical-align is illegal
Line 10 of welcome-email.mjml (mj-text) — Attribute padding-top has invalid value: 50 for type Unit, only accepts (px, %) units and 1 value(s)
Line 13 of welcome-email.mjml (mj-section) — Attribute padding-top has invalid value: 20 for type Unit, only accepts (px, %) units and 1 value(s)

Nevertheless I think the Python code should be able to handle that (unless the Python code becomes too messy).

Also there is an upstream pull request to clean up that template by @willhertz but even after applying that change the Python code fails.

  File "…/mjml/mjml2html.py", line 167, in mjml_to_html
    globalDatas.headRaw = processing(mjHead, headHelpers)
  File "…/mjml/mjml2html.py", line 72, in processing
    return component.handler()
  File "…/mjml/elements/head/mj_head.py", line 9, in handler
    return self.handlerChildren()
  File "…/mjml/elements/head/_head_base.py", line 28, in handlerChildren
    return tuple(map(handle_children, childrens))
  File "…/mjml/elements/head/_head_base.py", line 22, in handle_children
    component.handler()
  File "…/mjml/elements/head/mj_attributes.py", line 14, in handler
    tagName = child['tagName']
TypeError: 'NoneType' object is not subscriptable

The Python code must be able to handle the updated template as the upstream validator does not complain about any invalid attributes there.

FelixSchwarz commented 3 years ago

welcome-email.mjml (current version in the repo) is now rendered correctly, mostly due to commits b4d7d131b84b71daa6a1d7f5847efb88ed4f3069 and eab6cc4766384d2c2f0fbe486a70bfb86d68a7cd.

FelixSchwarz commented 3 years ago

With 21b2234 I can also render the updated welcome-email.mjml from the mentioned pull request if I remove the style inlining manually (CSS inlining will be implemented in #1).