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/

posted on   荣锋亮  阅读(41)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-12-28 gitlab 集成的一些SAST安全扫描工具
2022-12-28 dremio ClassCompilerSelector 简单说明
2022-12-28 dremio SabotContext 简单说明
2021-12-28 juicefs 多s3 bucket 使用
2021-12-28 juicefs s3 gateway 试用
2020-12-28 记一次mysql 异常停电数据库损坏恢复的方法
2020-12-28 go plugins 容器构建

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示