mrml python 以及webassembly 实现简单说明
简单说明下mrml python 以及webassembly 的实现
python
python 是基于了pyo3,利用pyo3 提供的能力,暴露了python 模块
- 参考处理
// 暴露的mrml 模块
#[pymodule]
#[pyo3(name = "mrml")]
fn register(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
// mrml 方法的注册
m.add_class::<NoopIncludeLoaderOptions>()?;
m.add_class::<MemoryIncludeLoaderOptions>()?;
m.add_class::<ParserOptions>()?;
m.add_class::<RenderOptions>()?;
m.add_function(wrap_pyfunction!(to_html, m)?)?;
m.add_function(wrap_pyfunction!(noop_loader, m)?)?;
m.add_function(wrap_pyfunction!(memory_loader, m)?)?;
Ok(())
}
- 基于maturinpip 的包构建
基于了pyo3 提供的maturin 做为pip 的backend
pyproject.toml
[build-system]
requires = ["maturin>=1,<2"]
build-backend = "maturin"
[project]
name = "mrml"
description = "A Python wrapper for MRML (Rust port of MJML)."
readme = "readme.md"
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
[project.urls]
"Homepage" = "https://github.com/jdrouet/mrml"
"Bug Tracker" = "https://github.com/jdrouet/mrml/issues"
webassembly
webassembly 的构建使用了 wasm-bindgen,暴露了Engine 以及一些操作方法
- 部分参考代码
#[derive(Debug, Default)]
#[wasm_bindgen]
pub struct Engine {
parser: Rc<mrml::prelude::parser::ParserOptions>,
#[cfg(feature = "async")]
async_parser: Rc<mrml::prelude::parser::AsyncParserOptions>,
render: mrml::prelude::render::RenderOptions,
}
#[wasm_bindgen]
impl Engine {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}
/// Defines the parsing options.
#[allow(clippy::arc_with_non_send_sync)]
#[wasm_bindgen(js_name = "setParserOptions")]
pub fn set_parser_options(&mut self, value: ParserOptions) {
self.parser = Rc::new(value.into());
}
/// Defines the async parsing options.
#[cfg(feature = "async")]
#[allow(clippy::arc_with_non_send_sync)]
#[wasm_bindgen(js_name = "setAsyncParserOptions")]
pub fn set_async_parser_options(&mut self, value: AsyncParserOptions) {
self.async_parser = Rc::new(value.into());
}
/// Defines the rendering options.
#[wasm_bindgen(js_name = "setRenderOptions")]
pub fn set_render_options(&mut self, value: RenderOptions) {
self.render = value.into();
}
/// Renders the mjml input into html.
#[wasm_bindgen(js_name = "toHtml")]
pub fn to_html(&self, input: &str) -> ToHtmlResult {
match to_html(input, &self.parser, &self.render) {
Ok(content) => ToHtmlResult::Success { content },
Err(error) => ToHtmlResult::Error(error),
}
}
- webassembly 的构建
基于了wasm-pack
参考命令
wasm-pack build --target nodejs --release
说明
mrml 对于python 以及webassembly 的支持都是基于了rust 的周边能力,集成起来还是比较方便的,对于mrml 构建相关的可以看看
github 代码中的github action workflow 配置
参考资料
https://github.com/jdrouet/mrml
https://pyo3.rs/
https://github.com/PyO3/pyo3
https://github.com/rustwasm/wasm-pack
https://rustwasm.github.io/wasm-pack/