FelixSchwarz / mjml-python

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

Use mjml-stub inside a script #4

Closed buttle closed 3 years ago

buttle commented 3 years ago

Hi,

I would like to be able to do something like this

from mjml import parser

with open(file.mjml, 'r') as mjml_file:
    html = parser(mjml_file)

Is that possible?

Cheers.

FelixSchwarz commented 3 years ago

I think this code should solve your problem (untested, though mostly copied from mjml.py):

from pathlib import Path
from mjml.mjml2html import mjml_to_html

with Path(mjml_filename).open('rb') as mjml_fp:
    result = mjml_to_html(mjml_fp)
assert not result.errors
html_str = result.html
buttle commented 3 years ago

Yes sir!

To take this one step further. Could I pass the mjml file content directly to a mjml-stub function?

result = mjml.some_funtion(mjml_file_content)
FelixSchwarz commented 3 years ago

Currently such a convenience function is not present though adding it would be probably a good idea.

These functions could be named mjml.render_html_from_file(<fp>/<path>) and mjml.render_html_from_string(<str>) and would just return the HTML as str (raising an exception whenever result.errors is not empty). Users who need more control could call mjml.process_mjml_from_file(<fp>/<path>) which would return the result instance.

I'd be happy to merge your pull request 😀 You don't have to implement all the functions but I like to map out the API in advance so the final API is somewhat consistently named.

FelixSchwarz commented 3 years ago

implemented in e779836

So far no additional API (e.g. mjml.render_html_from_file(<fp>/<path>)) but that can be done in a future PR.