├── .editorconfig
├── .github
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── dependabot.yml
└── workflows
│ ├── build.yml
│ └── cross-compile.yml
├── .gitignore
├── .rubocop.yml
├── .ruby-version
├── CODE_OF_CONDUCT.md
├── Cargo.lock
├── Cargo.toml
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
├── benchmark
├── console
└── setup
├── ext
└── mrml
│ ├── Cargo.toml
│ ├── extconf.rb
│ └── src
│ └── lib.rs
├── lib
├── mrml.rb
└── mrml
│ ├── error.rb
│ ├── template.rb
│ └── version.rb
├── mrml.gemspec
└── test
├── fixtures
├── invalid.mjml
├── valid.mjml
└── value.json
├── mrml_test.rb
└── test_helper.rb
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 2
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = true
10 |
11 | [*.md]
12 | trim_trailing_whitespace = false
13 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: [jonian]
4 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior.
15 |
16 | **Expected behavior**
17 | A clear and concise description of what you expected to happen.
18 |
19 | **Additional context**
20 | Add any other context about the problem here.
21 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is.
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: "github-actions"
4 | directory: "/"
5 | schedule:
6 | interval: "weekly"
7 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | ---
2 | name: Build
3 |
4 | on:
5 | push:
6 | branches: [master]
7 | paths:
8 | - 'ext/**'
9 | - 'lib/**'
10 | - 'test/**'
11 | - '*.gemspec'
12 | pull_request:
13 | branches: [master]
14 |
15 | permissions:
16 | contents: read
17 |
18 | jobs:
19 | build:
20 | strategy:
21 | matrix:
22 | os: [ubuntu-latest]
23 | ruby: ['2.7', '3.2', '3.3', '3.4']
24 | runs-on: ${{ matrix.os }}
25 |
26 | steps:
27 | - uses: actions/checkout@v4
28 |
29 | - name: Install Rust
30 | uses: actions-rs/toolchain@v1
31 | with:
32 | toolchain: stable
33 |
34 | - name: Set up Ruby
35 | uses: ruby/setup-ruby@v1
36 | with:
37 | ruby-version: ${{ matrix.ruby }}
38 | bundler-cache: true
39 |
40 | - name: Run tests
41 | run: |
42 | bundle exec rake
43 |
--------------------------------------------------------------------------------
/.github/workflows/cross-compile.yml:
--------------------------------------------------------------------------------
1 | ---
2 | name: Cross Compile
3 |
4 | on:
5 | push:
6 | tags:
7 | - "v*"
8 |
9 | jobs:
10 | ci-data:
11 | name: Fetch CI data
12 | runs-on: ubuntu-latest
13 | outputs:
14 | result: ${{ steps.fetch.outputs.result }}
15 | steps:
16 | - uses: oxidize-rb/actions/fetch-ci-data@v1
17 | id: fetch
18 | with:
19 | supported-ruby-platforms: true
20 | stable-ruby-versions: |
21 | exclude: [head]
22 | cross-gem:
23 | name: Compile native gem for ${{ matrix.platform }}
24 | runs-on: ubuntu-latest
25 | needs: ci-data
26 | strategy:
27 | fail-fast: false
28 | matrix:
29 | platform: ${{ fromJSON(needs.ci-data.outputs.result).supported-ruby-platforms }}
30 | steps:
31 | - uses: actions/checkout@v4
32 |
33 | - uses: ruby/setup-ruby@v1
34 | with:
35 | ruby-version: "3.4"
36 |
37 | - uses: oxidize-rb/actions/cross-gem@v1
38 | id: cross-gem
39 | with:
40 | platform: ${{ matrix.platform }}
41 | ruby-versions: "2.7,3.0,3.1,3.2,3.3,3.4"
42 |
43 | - uses: actions/upload-artifact@v4
44 | with:
45 | name: mrml-ruby-${{ matrix.platform }}
46 | path: ${{ steps.cross-gem.outputs.gem-path }}
47 | merge-artifacts:
48 | name: Merge artifacts
49 | runs-on: ubuntu-latest
50 | needs: cross-gem
51 | steps:
52 | - uses: actions/upload-artifact/merge@v4
53 | with:
54 | name: cross-gem
55 | pattern: mrml-ruby-*
56 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /.bundle/
2 | /.yardoc
3 | /_yardoc/
4 | /coverage/
5 | /doc/
6 | /pkg/
7 | /spec/reports/
8 | /tmp/
9 | Gemfile.lock
10 |
11 | *.gem
12 | target
13 | *.so
14 | mkmf.log
15 |
--------------------------------------------------------------------------------
/.rubocop.yml:
--------------------------------------------------------------------------------
1 | require:
2 | - rubocop-performance
3 |
4 | AllCops:
5 | TargetRubyVersion: 2.6
6 | NewCops: enable
7 | Include:
8 | - lib/**/*.rb
9 | - test/**/*.rb
10 | Exclude:
11 | - bin/**/*
12 |
13 | Lint/EmptyClass:
14 | Enabled: false
15 |
16 | Style/PerlBackrefs:
17 | Enabled: false
18 |
--------------------------------------------------------------------------------
/.ruby-version:
--------------------------------------------------------------------------------
1 | 3.4.0
2 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Code of Conduct
2 |
3 | All participants of this project are expected to abide by our Code of Conduct, both online and during in-person events that are hosted and/or associated with this project.
4 |
5 | ## The Pledge
6 |
7 | In the interest of fostering an open and welcoming environment, we pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
8 |
9 | ## The Standards
10 |
11 | Examples of behavior that contributes to creating a positive environment include:
12 |
13 | * Using welcoming and inclusive language
14 | * Being respectful of differing viewpoints and experiences
15 | * Referring to people by their preferred pronouns and using gender-neutral pronouns when uncertain
16 | * Gracefully accepting constructive criticism
17 | * Focusing on what is best for the community
18 | * Showing empathy towards other community members
19 |
20 | Examples of unacceptable behavior by participants include:
21 |
22 | * The use of sexualized language or imagery and unwelcome sexual attention or advances
23 | * Trolling, insulting/derogatory comments, and personal or political attacks
24 | * Public or private harassment
25 | * Publishing others' private information, such as a physical or electronic address, without explicit permission
26 | * Other conduct which could reasonably be considered inappropriate in a professional setting
27 | * Dismissing or attacking inclusion-oriented requests
28 |
29 | ## Enforcement
30 |
31 | Violations of the Code of Conduct may be reported by sending an email to info@hardpixel.eu. All reports will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. Further details of specific enforcement policies may be posted separately.
32 |
33 | We hold the right and responsibility to remove comments or other contributions that are not aligned to this Code of Conduct, or to suspend temporarily or permanently any members for other behaviors that are inappropriate, threatening, offensive, or harmful.
34 |
35 | ## Attribution
36 |
37 | This Code of Conduct is adapted from [dev.to](https://dev.to/code-of-conduct).
38 |
--------------------------------------------------------------------------------
/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 3
4 |
5 | [[package]]
6 | name = "Inflector"
7 | version = "0.11.4"
8 | source = "registry+https://github.com/rust-lang/crates.io-index"
9 | checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3"
10 | dependencies = [
11 | "lazy_static",
12 | "regex",
13 | ]
14 |
15 | [[package]]
16 | name = "aho-corasick"
17 | version = "1.1.3"
18 | source = "registry+https://github.com/rust-lang/crates.io-index"
19 | checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
20 | dependencies = [
21 | "memchr",
22 | ]
23 |
24 | [[package]]
25 | name = "bindgen"
26 | version = "0.69.5"
27 | source = "registry+https://github.com/rust-lang/crates.io-index"
28 | checksum = "271383c67ccabffb7381723dea0672a673f292304fcb45c01cc648c7a8d58088"
29 | dependencies = [
30 | "bitflags",
31 | "cexpr",
32 | "clang-sys",
33 | "itertools 0.12.1",
34 | "lazy_static",
35 | "lazycell",
36 | "proc-macro2",
37 | "quote",
38 | "regex",
39 | "rustc-hash 1.1.0",
40 | "shlex",
41 | "syn",
42 | ]
43 |
44 | [[package]]
45 | name = "bitflags"
46 | version = "2.8.0"
47 | source = "registry+https://github.com/rust-lang/crates.io-index"
48 | checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
49 |
50 | [[package]]
51 | name = "cexpr"
52 | version = "0.6.0"
53 | source = "registry+https://github.com/rust-lang/crates.io-index"
54 | checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766"
55 | dependencies = [
56 | "nom",
57 | ]
58 |
59 | [[package]]
60 | name = "cfg-if"
61 | version = "1.0.0"
62 | source = "registry+https://github.com/rust-lang/crates.io-index"
63 | checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
64 |
65 | [[package]]
66 | name = "clang-sys"
67 | version = "1.8.1"
68 | source = "registry+https://github.com/rust-lang/crates.io-index"
69 | checksum = "0b023947811758c97c59bf9d1c188fd619ad4718dcaa767947df1cadb14f39f4"
70 | dependencies = [
71 | "glob",
72 | "libc",
73 | "libloading",
74 | ]
75 |
76 | [[package]]
77 | name = "darling"
78 | version = "0.20.10"
79 | source = "registry+https://github.com/rust-lang/crates.io-index"
80 | checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989"
81 | dependencies = [
82 | "darling_core",
83 | "darling_macro",
84 | ]
85 |
86 | [[package]]
87 | name = "darling_core"
88 | version = "0.20.10"
89 | source = "registry+https://github.com/rust-lang/crates.io-index"
90 | checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5"
91 | dependencies = [
92 | "fnv",
93 | "ident_case",
94 | "proc-macro2",
95 | "quote",
96 | "strsim",
97 | "syn",
98 | ]
99 |
100 | [[package]]
101 | name = "darling_macro"
102 | version = "0.20.10"
103 | source = "registry+https://github.com/rust-lang/crates.io-index"
104 | checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806"
105 | dependencies = [
106 | "darling_core",
107 | "quote",
108 | "syn",
109 | ]
110 |
111 | [[package]]
112 | name = "either"
113 | version = "1.13.0"
114 | source = "registry+https://github.com/rust-lang/crates.io-index"
115 | checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
116 |
117 | [[package]]
118 | name = "enum-as-inner"
119 | version = "0.6.1"
120 | source = "registry+https://github.com/rust-lang/crates.io-index"
121 | checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc"
122 | dependencies = [
123 | "heck",
124 | "proc-macro2",
125 | "quote",
126 | "syn",
127 | ]
128 |
129 | [[package]]
130 | name = "enum_dispatch"
131 | version = "0.3.13"
132 | source = "registry+https://github.com/rust-lang/crates.io-index"
133 | checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd"
134 | dependencies = [
135 | "once_cell",
136 | "proc-macro2",
137 | "quote",
138 | "syn",
139 | ]
140 |
141 | [[package]]
142 | name = "equivalent"
143 | version = "1.0.1"
144 | source = "registry+https://github.com/rust-lang/crates.io-index"
145 | checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
146 |
147 | [[package]]
148 | name = "fnv"
149 | version = "1.0.7"
150 | source = "registry+https://github.com/rust-lang/crates.io-index"
151 | checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
152 |
153 | [[package]]
154 | name = "glob"
155 | version = "0.3.2"
156 | source = "registry+https://github.com/rust-lang/crates.io-index"
157 | checksum = "a8d1add55171497b4705a648c6b583acafb01d58050a51727785f0b2c8e0a2b2"
158 |
159 | [[package]]
160 | name = "hashbrown"
161 | version = "0.15.2"
162 | source = "registry+https://github.com/rust-lang/crates.io-index"
163 | checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
164 |
165 | [[package]]
166 | name = "heck"
167 | version = "0.5.0"
168 | source = "registry+https://github.com/rust-lang/crates.io-index"
169 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
170 |
171 | [[package]]
172 | name = "ident_case"
173 | version = "1.0.1"
174 | source = "registry+https://github.com/rust-lang/crates.io-index"
175 | checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
176 |
177 | [[package]]
178 | name = "indexmap"
179 | version = "2.7.0"
180 | source = "registry+https://github.com/rust-lang/crates.io-index"
181 | checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
182 | dependencies = [
183 | "equivalent",
184 | "hashbrown",
185 | "serde",
186 | ]
187 |
188 | [[package]]
189 | name = "itertools"
190 | version = "0.12.1"
191 | source = "registry+https://github.com/rust-lang/crates.io-index"
192 | checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569"
193 | dependencies = [
194 | "either",
195 | ]
196 |
197 | [[package]]
198 | name = "itertools"
199 | version = "0.13.0"
200 | source = "registry+https://github.com/rust-lang/crates.io-index"
201 | checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
202 | dependencies = [
203 | "either",
204 | ]
205 |
206 | [[package]]
207 | name = "itoa"
208 | version = "1.0.14"
209 | source = "registry+https://github.com/rust-lang/crates.io-index"
210 | checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
211 |
212 | [[package]]
213 | name = "lazy_static"
214 | version = "1.5.0"
215 | source = "registry+https://github.com/rust-lang/crates.io-index"
216 | checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
217 |
218 | [[package]]
219 | name = "lazycell"
220 | version = "1.3.0"
221 | source = "registry+https://github.com/rust-lang/crates.io-index"
222 | checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
223 |
224 | [[package]]
225 | name = "libc"
226 | version = "0.2.169"
227 | source = "registry+https://github.com/rust-lang/crates.io-index"
228 | checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
229 |
230 | [[package]]
231 | name = "libloading"
232 | version = "0.8.6"
233 | source = "registry+https://github.com/rust-lang/crates.io-index"
234 | checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
235 | dependencies = [
236 | "cfg-if",
237 | "windows-targets",
238 | ]
239 |
240 | [[package]]
241 | name = "magnus"
242 | version = "0.7.1"
243 | source = "registry+https://github.com/rust-lang/crates.io-index"
244 | checksum = "3d87ae53030f3a22e83879e666cb94e58a7bdf31706878a0ba48752994146dab"
245 | dependencies = [
246 | "magnus-macros",
247 | "rb-sys",
248 | "rb-sys-env",
249 | "seq-macro",
250 | ]
251 |
252 | [[package]]
253 | name = "magnus-macros"
254 | version = "0.6.0"
255 | source = "registry+https://github.com/rust-lang/crates.io-index"
256 | checksum = "5968c820e2960565f647819f5928a42d6e874551cab9d88d75e3e0660d7f71e3"
257 | dependencies = [
258 | "proc-macro2",
259 | "quote",
260 | "syn",
261 | ]
262 |
263 | [[package]]
264 | name = "memchr"
265 | version = "2.7.4"
266 | source = "registry+https://github.com/rust-lang/crates.io-index"
267 | checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
268 |
269 | [[package]]
270 | name = "minimal-lexical"
271 | version = "0.2.1"
272 | source = "registry+https://github.com/rust-lang/crates.io-index"
273 | checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
274 |
275 | [[package]]
276 | name = "mrml"
277 | version = "0.1.0"
278 | dependencies = [
279 | "magnus",
280 | "mrml 4.0.1",
281 | "serde",
282 | "serde_json",
283 | ]
284 |
285 | [[package]]
286 | name = "mrml"
287 | version = "4.0.1"
288 | source = "registry+https://github.com/rust-lang/crates.io-index"
289 | checksum = "6a5fb21b06edad8397bd7e0e6dcef12013b33e979e3ae14f126899ba524f491e"
290 | dependencies = [
291 | "enum-as-inner",
292 | "enum_dispatch",
293 | "indexmap",
294 | "itertools 0.13.0",
295 | "mrml-json-macros",
296 | "rustc-hash 2.1.0",
297 | "serde",
298 | "serde_json",
299 | "thiserror",
300 | "xmlparser",
301 | ]
302 |
303 | [[package]]
304 | name = "mrml-json-macros"
305 | version = "0.1.4"
306 | source = "registry+https://github.com/rust-lang/crates.io-index"
307 | checksum = "40cbf208a374588552a03b4e5763d87b7628294b50b8b93da681f660105414b3"
308 | dependencies = [
309 | "Inflector",
310 | "darling",
311 | "proc-macro2",
312 | "quote",
313 | "syn",
314 | ]
315 |
316 | [[package]]
317 | name = "nom"
318 | version = "7.1.3"
319 | source = "registry+https://github.com/rust-lang/crates.io-index"
320 | checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
321 | dependencies = [
322 | "memchr",
323 | "minimal-lexical",
324 | ]
325 |
326 | [[package]]
327 | name = "once_cell"
328 | version = "1.20.2"
329 | source = "registry+https://github.com/rust-lang/crates.io-index"
330 | checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
331 |
332 | [[package]]
333 | name = "proc-macro2"
334 | version = "1.0.93"
335 | source = "registry+https://github.com/rust-lang/crates.io-index"
336 | checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
337 | dependencies = [
338 | "unicode-ident",
339 | ]
340 |
341 | [[package]]
342 | name = "quote"
343 | version = "1.0.38"
344 | source = "registry+https://github.com/rust-lang/crates.io-index"
345 | checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
346 | dependencies = [
347 | "proc-macro2",
348 | ]
349 |
350 | [[package]]
351 | name = "rb-sys"
352 | version = "0.9.108"
353 | source = "registry+https://github.com/rust-lang/crates.io-index"
354 | checksum = "1e955384e1a4dc64b71d1e4b39ed0edbd77c7bde4a10dfd5ad208e1160fddfa7"
355 | dependencies = [
356 | "rb-sys-build",
357 | ]
358 |
359 | [[package]]
360 | name = "rb-sys-build"
361 | version = "0.9.108"
362 | source = "registry+https://github.com/rust-lang/crates.io-index"
363 | checksum = "c167c6571889b2550d6fcb315e8aa60bdb95e47e4b64793e3f65a30dc25afc85"
364 | dependencies = [
365 | "bindgen",
366 | "lazy_static",
367 | "proc-macro2",
368 | "quote",
369 | "regex",
370 | "shell-words",
371 | "syn",
372 | ]
373 |
374 | [[package]]
375 | name = "rb-sys-env"
376 | version = "0.1.2"
377 | source = "registry+https://github.com/rust-lang/crates.io-index"
378 | checksum = "a35802679f07360454b418a5d1735c89716bde01d35b1560fc953c1415a0b3bb"
379 |
380 | [[package]]
381 | name = "regex"
382 | version = "1.11.1"
383 | source = "registry+https://github.com/rust-lang/crates.io-index"
384 | checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
385 | dependencies = [
386 | "aho-corasick",
387 | "memchr",
388 | "regex-automata",
389 | "regex-syntax",
390 | ]
391 |
392 | [[package]]
393 | name = "regex-automata"
394 | version = "0.4.9"
395 | source = "registry+https://github.com/rust-lang/crates.io-index"
396 | checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
397 | dependencies = [
398 | "aho-corasick",
399 | "memchr",
400 | "regex-syntax",
401 | ]
402 |
403 | [[package]]
404 | name = "regex-syntax"
405 | version = "0.8.5"
406 | source = "registry+https://github.com/rust-lang/crates.io-index"
407 | checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
408 |
409 | [[package]]
410 | name = "rustc-hash"
411 | version = "1.1.0"
412 | source = "registry+https://github.com/rust-lang/crates.io-index"
413 | checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
414 |
415 | [[package]]
416 | name = "rustc-hash"
417 | version = "2.1.0"
418 | source = "registry+https://github.com/rust-lang/crates.io-index"
419 | checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497"
420 |
421 | [[package]]
422 | name = "ryu"
423 | version = "1.0.18"
424 | source = "registry+https://github.com/rust-lang/crates.io-index"
425 | checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
426 |
427 | [[package]]
428 | name = "seq-macro"
429 | version = "0.3.5"
430 | source = "registry+https://github.com/rust-lang/crates.io-index"
431 | checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4"
432 |
433 | [[package]]
434 | name = "serde"
435 | version = "1.0.217"
436 | source = "registry+https://github.com/rust-lang/crates.io-index"
437 | checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
438 | dependencies = [
439 | "serde_derive",
440 | ]
441 |
442 | [[package]]
443 | name = "serde_derive"
444 | version = "1.0.217"
445 | source = "registry+https://github.com/rust-lang/crates.io-index"
446 | checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
447 | dependencies = [
448 | "proc-macro2",
449 | "quote",
450 | "syn",
451 | ]
452 |
453 | [[package]]
454 | name = "serde_json"
455 | version = "1.0.135"
456 | source = "registry+https://github.com/rust-lang/crates.io-index"
457 | checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9"
458 | dependencies = [
459 | "itoa",
460 | "memchr",
461 | "ryu",
462 | "serde",
463 | ]
464 |
465 | [[package]]
466 | name = "shell-words"
467 | version = "1.1.0"
468 | source = "registry+https://github.com/rust-lang/crates.io-index"
469 | checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde"
470 |
471 | [[package]]
472 | name = "shlex"
473 | version = "1.3.0"
474 | source = "registry+https://github.com/rust-lang/crates.io-index"
475 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
476 |
477 | [[package]]
478 | name = "strsim"
479 | version = "0.11.1"
480 | source = "registry+https://github.com/rust-lang/crates.io-index"
481 | checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
482 |
483 | [[package]]
484 | name = "syn"
485 | version = "2.0.96"
486 | source = "registry+https://github.com/rust-lang/crates.io-index"
487 | checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
488 | dependencies = [
489 | "proc-macro2",
490 | "quote",
491 | "unicode-ident",
492 | ]
493 |
494 | [[package]]
495 | name = "thiserror"
496 | version = "1.0.69"
497 | source = "registry+https://github.com/rust-lang/crates.io-index"
498 | checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
499 | dependencies = [
500 | "thiserror-impl",
501 | ]
502 |
503 | [[package]]
504 | name = "thiserror-impl"
505 | version = "1.0.69"
506 | source = "registry+https://github.com/rust-lang/crates.io-index"
507 | checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
508 | dependencies = [
509 | "proc-macro2",
510 | "quote",
511 | "syn",
512 | ]
513 |
514 | [[package]]
515 | name = "unicode-ident"
516 | version = "1.0.14"
517 | source = "registry+https://github.com/rust-lang/crates.io-index"
518 | checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
519 |
520 | [[package]]
521 | name = "windows-targets"
522 | version = "0.52.6"
523 | source = "registry+https://github.com/rust-lang/crates.io-index"
524 | checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
525 | dependencies = [
526 | "windows_aarch64_gnullvm",
527 | "windows_aarch64_msvc",
528 | "windows_i686_gnu",
529 | "windows_i686_gnullvm",
530 | "windows_i686_msvc",
531 | "windows_x86_64_gnu",
532 | "windows_x86_64_gnullvm",
533 | "windows_x86_64_msvc",
534 | ]
535 |
536 | [[package]]
537 | name = "windows_aarch64_gnullvm"
538 | version = "0.52.6"
539 | source = "registry+https://github.com/rust-lang/crates.io-index"
540 | checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
541 |
542 | [[package]]
543 | name = "windows_aarch64_msvc"
544 | version = "0.52.6"
545 | source = "registry+https://github.com/rust-lang/crates.io-index"
546 | checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
547 |
548 | [[package]]
549 | name = "windows_i686_gnu"
550 | version = "0.52.6"
551 | source = "registry+https://github.com/rust-lang/crates.io-index"
552 | checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
553 |
554 | [[package]]
555 | name = "windows_i686_gnullvm"
556 | version = "0.52.6"
557 | source = "registry+https://github.com/rust-lang/crates.io-index"
558 | checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
559 |
560 | [[package]]
561 | name = "windows_i686_msvc"
562 | version = "0.52.6"
563 | source = "registry+https://github.com/rust-lang/crates.io-index"
564 | checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
565 |
566 | [[package]]
567 | name = "windows_x86_64_gnu"
568 | version = "0.52.6"
569 | source = "registry+https://github.com/rust-lang/crates.io-index"
570 | checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
571 |
572 | [[package]]
573 | name = "windows_x86_64_gnullvm"
574 | version = "0.52.6"
575 | source = "registry+https://github.com/rust-lang/crates.io-index"
576 | checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
577 |
578 | [[package]]
579 | name = "windows_x86_64_msvc"
580 | version = "0.52.6"
581 | source = "registry+https://github.com/rust-lang/crates.io-index"
582 | checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
583 |
584 | [[package]]
585 | name = "xmlparser"
586 | version = "0.13.6"
587 | source = "registry+https://github.com/rust-lang/crates.io-index"
588 | checksum = "66fee0b777b0f5ac1c69bb06d361268faafa61cd4682ae064a171c16c433e9e4"
589 |
--------------------------------------------------------------------------------
/Cargo.toml:
--------------------------------------------------------------------------------
1 | [workspace]
2 | members = ["ext/mrml"]
3 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | source 'https://rubygems.org'
4 |
5 | # Specify your gem's dependencies in mrml.gemspec
6 | gemspec
7 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2021 jonian
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MRML Ruby
2 |
3 | Ruby wrapper for [MRML](https://github.com/jdrouet/mrml), the [MJML](https://mjml.io) markup language implementation in Rust. Rust must be available on your system to install this gem if you use a version below [v1.4.2](https://github.com/hardpixel/mrml-ruby/releases/tag/v1.4.2).
4 |
5 | [](https://badge.fury.io/rb/mrml)
6 | [](https://github.com/hardpixel/mrml-ruby/actions/workflows/build.yml)
7 | [](https://codeclimate.com/github/hardpixel/mrml-ruby/maintainability)
8 |
9 | ## Installation
10 |
11 | Add this line to your application's Gemfile:
12 |
13 | ```ruby
14 | gem 'mrml'
15 | ```
16 |
17 | And then execute:
18 |
19 | $ bundle install
20 |
21 | Or install it yourself as:
22 |
23 | $ gem install mrml
24 |
25 | ## Usage
26 |
27 | ```ruby
28 | require 'mrml'
29 |
30 | mjml = <<-HTML
31 |
32 |
33 | Newsletter Title
34 | Newsletter Preview
35 |
36 |
37 |
38 |
39 | Hello World
40 |
41 |
42 |
43 |
44 | HTML
45 |
46 | # Using module methods
47 | MRML.to_html(mjml) # Generate html from mjml
48 | MRML.to_json(mjml) # Generate json from mjml
49 | MRML.to_hash(mjml) # Generate hash from mjml
50 |
51 | # Using Template class
52 | template = MRML::Template.new(mjml)
53 |
54 | template.title # Get template title
55 | template.preview # Get template preview
56 |
57 | template.to_html # Render as html
58 | template.to_mjml # Render as mjml
59 | template.to_json # Render as json
60 | template.to_hash # Render as hash
61 | ```
62 |
63 | ```ruby
64 | require 'mrml'
65 |
66 | json = <<-JSON
67 | {
68 | "type": "mjml",
69 | "children": [{
70 | "type": "mj-head",
71 | "children": [{
72 | "type": "mj-title",
73 | "children": "Newsletter Title"
74 | }, {
75 | "type": "mj-preview",
76 | "children": "Newsletter Preview"
77 | }]
78 | }, {
79 | "type": "mj-body",
80 | "children": [{
81 | "type": "mj-section",
82 | "children": [{
83 | "type": "mj-column",
84 | "children": [{
85 | "type": "mj-text",
86 | "attributes": {
87 | "font-size": "20px",
88 | "color": "#F45E43",
89 | "font-family": "helvetica"
90 | },
91 | "children": ["Hello World"]
92 | }]
93 | }]
94 | }]
95 | }]
96 | }
97 | JSON
98 |
99 | # Create Template from JSON
100 | template = MRML::Template.from_json(json)
101 |
102 | template.to_html # Render as html
103 | template.to_mjml # Render as mjml
104 | template.to_json # Render as json
105 | template.to_hash # Render as hash
106 | ```
107 |
108 | ## Benchmark
109 |
110 | ```
111 | Warming up --------------------------------------
112 | mrml 3.069k i/100ms
113 | mjml 1.000 i/100ms
114 | Calculating -------------------------------------
115 | mrml 32.537k (±16.1%) i/s - 156.519k in 5.029759s
116 | mjml 1.895 (± 0.0%) i/s - 10.000 in 5.283579s
117 |
118 | Comparison:
119 | mrml: 32537.2 i/s
120 | mjml: 1.9 i/s - 17169.16x slower
121 |
122 | Calculating -------------------------------------
123 | mrml 3.166k memsize ( 0.000 retained)
124 | 2.000 objects ( 0.000 retained)
125 | 1.000 strings ( 0.000 retained)
126 | mjml 21.253k memsize ( 1.490k retained)
127 | 107.000 objects ( 15.000 retained)
128 | 20.000 strings ( 11.000 retained)
129 |
130 | Comparison:
131 | mrml: 3166 allocated
132 | mjml: 21253 allocated - 6.71x more
133 | ```
134 |
135 | ## Development
136 |
137 | After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
138 |
139 | To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
140 |
141 | ## Contributing
142 |
143 | Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/mrml-ruby.
144 |
145 | ## License
146 |
147 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
148 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require 'bundler/gem_tasks'
4 | require 'rake/testtask'
5 | require 'rake/extensiontask'
6 | require 'rb_sys/extensiontask'
7 |
8 | GEMSPEC = Gem::Specification.load('mrml.gemspec')
9 |
10 | RbSys::ExtensionTask.new('mrml', GEMSPEC) do |ext|
11 | ext.lib_dir = 'lib/mrml'
12 | end
13 |
14 | task :native, [:platform] do |_t, platform:|
15 | sh 'bundle', 'exec', 'rb-sys-dock', '--platform', platform, '--build'
16 | end
17 |
18 | Rake::TestTask.new(:test) do |t|
19 | t.libs << 'test'
20 | t.libs << 'lib'
21 | t.test_files = FileList['test/**/*_test.rb']
22 | end
23 |
24 | task default: %i[clobber compile test]
25 |
--------------------------------------------------------------------------------
/bin/benchmark:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # frozen_string_literal: true
3 |
4 | require 'bundler/inline'
5 | require 'tempfile'
6 |
7 | gemfile true, quiet: true do
8 | source 'https://rubygems.org'
9 | gemspec
10 |
11 | gem 'benchmark-ips'
12 | gem 'benchmark-memory'
13 | gem 'rails'
14 | gem 'mjml-rails'
15 | end
16 |
17 | template = File.read(
18 | File.join(__dir__, '..', 'test/fixtures/valid.mjml')
19 | )
20 |
21 | reports = lambda do |x|
22 | x.report('mrml') do
23 | parser = MRML::Template.new(template)
24 | parser.to_html
25 | end
26 |
27 | x.report('mjml') do
28 | parser = Mjml::Parser.new(template)
29 | parser.render
30 | end
31 | end
32 |
33 | Benchmark.ips do |x|
34 | reports.call(x)
35 | x.compare!
36 | end
37 |
38 | Benchmark.memory do |x|
39 | reports.call(x)
40 | x.compare!
41 | end
42 |
--------------------------------------------------------------------------------
/bin/console:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # frozen_string_literal: true
3 |
4 | require 'bundler/setup'
5 | require 'mrml'
6 |
7 | # You can add fixtures and/or initialization code here to make experimenting
8 | # with your gem easier. You can also use a different console, if you like.
9 |
10 | # (If you use this, don't forget to add pry to your Gemfile!)
11 | # require 'pry'
12 | # Pry.start
13 |
14 | require 'irb'
15 | IRB.start(__FILE__)
16 |
--------------------------------------------------------------------------------
/bin/setup:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | set -euo pipefail
3 | IFS=$'\n\t'
4 | set -vx
5 |
6 | bundle install
7 |
8 | # Do any other automated setup that you need to do here
9 |
--------------------------------------------------------------------------------
/ext/mrml/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "mrml"
3 | version = "0.1.0"
4 | authors = ["Jonian Guveli "]
5 | edition = "2018"
6 |
7 | [dependencies]
8 | mrml = "4.0"
9 | magnus = "0.7"
10 |
11 | [dependencies.serde]
12 | version = "1.0"
13 | features = ["derive"]
14 |
15 | [dependencies.serde_json]
16 | version = "1.0"
17 |
18 | [lib]
19 | name = "mrml"
20 | crate-type = ["cdylib"]
21 |
--------------------------------------------------------------------------------
/ext/mrml/extconf.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require 'mkmf'
4 | require 'rb_sys/mkmf'
5 |
6 | create_rust_makefile('mrml/mrml')
7 |
--------------------------------------------------------------------------------
/ext/mrml/src/lib.rs:
--------------------------------------------------------------------------------
1 | use magnus::{
2 | function, method, prelude::*, value::Lazy, Ruby,
3 | Error, ExceptionClass, RModule
4 | };
5 |
6 | use mrml::mjml::Mjml;
7 | use mrml::prelude::print::Printable;
8 | use mrml::prelude::render::RenderOptions;
9 |
10 | static MODULE: Lazy =
11 | Lazy::new(|ruby| ruby.class_object().const_get("MRML").unwrap());
12 |
13 | static ERROR: Lazy =
14 | Lazy::new(|ruby| ruby.get_inner(&MODULE).const_get("Error").unwrap());
15 |
16 | fn mrml_error() -> ExceptionClass {
17 | Ruby::get().unwrap().get_inner(&ERROR)
18 | }
19 |
20 | macro_rules! error {
21 | ($ex:ident) => {
22 | Error::new(mrml_error(), $ex.to_string())
23 | };
24 | }
25 |
26 | #[magnus::wrap(class = "MRML::Template", free_immediately, size)]
27 | struct Template {
28 | res: Mjml
29 | }
30 |
31 | impl Template {
32 | fn new(input: String) -> Result {
33 | match mrml::parse(&input) {
34 | Ok(res) => Ok(Self { res }),
35 | Err(ex) => Err(error!(ex))
36 | }
37 | }
38 |
39 | fn from_json(input: String) -> Result {
40 | match serde_json::from_str::(&input) {
41 | Ok(res) => Ok(Self { res }),
42 | Err(ex) => Err(error!(ex))
43 | }
44 | }
45 |
46 | fn get_title(&self) -> Option {
47 | self.res.get_title()
48 | }
49 |
50 | fn get_preview(&self) -> Option {
51 | self.res.get_preview()
52 | }
53 |
54 | fn to_mjml(&self) -> String {
55 | self.res.print_dense().unwrap()
56 | }
57 |
58 | fn to_json(&self) -> Result {
59 | match serde_json::to_string(&self.res) {
60 | Ok(res) => Ok(res),
61 | Err(ex) => Err(error!(ex))
62 | }
63 | }
64 |
65 | fn to_html(&self) -> Result {
66 | match self.res.render(&RenderOptions::default()) {
67 | Ok(res) => Ok(res),
68 | Err(ex) => Err(error!(ex))
69 | }
70 | }
71 | }
72 |
73 | impl Clone for Template {
74 | fn clone(&self) -> Self {
75 | Self::new(self.to_mjml()).unwrap()
76 | }
77 | }
78 |
79 | #[magnus::init]
80 | fn init(ruby: &Ruby) -> Result<(), Error> {
81 | let module = ruby.define_module("MRML")?;
82 | let class = module.define_class("Template", ruby.class_object())?;
83 |
84 | class.define_singleton_method("new", function!(Template::new, 1))?;
85 | class.define_singleton_method("from_json", function!(Template::from_json, 1))?;
86 |
87 | class.define_method("title", method!(Template::get_title, 0))?;
88 | class.define_method("preview", method!(Template::get_preview, 0))?;
89 |
90 | class.define_method("to_mjml", method!(Template::to_mjml, 0))?;
91 | class.define_method("to_json", method!(Template::to_json, 0))?;
92 | class.define_method("to_html", method!(Template::to_html, 0))?;
93 |
94 | class.define_method("clone", method!(Template::clone, 0))?;
95 | class.define_method("dup", method!(Template::clone, 0))?;
96 |
97 | Ok(())
98 | }
99 |
--------------------------------------------------------------------------------
/lib/mrml.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | begin
4 | RUBY_VERSION =~ /(\d+\.\d+)/
5 | require "mrml/#{$1}/mrml"
6 | rescue LoadError
7 | require 'mrml/mrml'
8 | end
9 |
10 | require 'mrml/error'
11 | require 'mrml/template'
12 | require 'mrml/version'
13 |
14 | # Module that renders MJML templates into HTML/JSON using MRML,
15 | # a reimplementation of the MJML markup language in Rust.
16 | module MRML
17 | class << self
18 | # Render template as HTML.
19 | # @param mjml [String]
20 | # @return (see Template#to_html)
21 | # @raise (see Template#initialize)
22 |
23 | def to_html(mjml)
24 | return if mjml.nil?
25 |
26 | template = Template.new(mjml)
27 | template.to_html
28 | end
29 |
30 | # Render template as JSON.
31 | # @param mjml [String]
32 | # @return (see Template#to_json)
33 | # @raise (see Template#initialize)
34 |
35 | def to_json(mjml)
36 | return if mjml.nil?
37 |
38 | template = Template.new(mjml)
39 | template.to_json
40 | end
41 |
42 | # Render template as Hash.
43 | # @param mjml [String]
44 | # @return (see Template#to_hash)
45 | # @raise (see Template#initialize)
46 |
47 | def to_hash(mjml)
48 | return if mjml.nil?
49 |
50 | template = Template.new(mjml)
51 | template.to_hash
52 | end
53 | end
54 | end
55 |
--------------------------------------------------------------------------------
/lib/mrml/error.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module MRML
4 | class Error < StandardError
5 | end
6 | end
7 |
--------------------------------------------------------------------------------
/lib/mrml/template.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require 'json'
4 |
5 | module MRML
6 | # Template object implemented in Rust that can parse MJML templates.
7 | class Template
8 | class << self
9 | # @!method from_json(json)
10 | # A new instance of Template from JSON.
11 | # @!scope class
12 | # @param json [String]
13 | # @return [Template]
14 | # @raise [Error] if json has invalid format
15 |
16 | # A new instance of Template from Hash.
17 | # @param hash [Hash]
18 | # @return [Template]
19 | # @raise [Error] if hash has invalid format
20 |
21 | def from_hash(hash)
22 | from_json(JSON.generate(hash))
23 | end
24 | end
25 |
26 | # @!method initialize(mjml)
27 | # A new instance of Template.
28 | # @param mjml [String]
29 | # @return [Template]
30 | # @raise [Error] if mjml is not a valid template
31 |
32 | # @!attribute [r] title
33 | # Gets mj-title tag value.
34 | # @return [String]
35 |
36 | # @!attribute [r] preview
37 | # Gets mj-preview tag value.
38 | # @return [String]
39 |
40 | # @!method to_mjml
41 | # MJML representation of the template.
42 | # @return [String]
43 |
44 | # @!method to_json
45 | # JSON representation of the template.
46 | # @return [String]
47 |
48 | # @!method to_html
49 | # HTML representation of the template.
50 | # @return [String]
51 |
52 | # Hash representation of the template.
53 | # @return [Hash]
54 |
55 | def to_hash
56 | JSON.parse(to_json)
57 | end
58 | end
59 | end
60 |
--------------------------------------------------------------------------------
/lib/mrml/version.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | module MRML
4 | VERSION = '1.7.0'
5 | end
6 |
--------------------------------------------------------------------------------
/mrml.gemspec:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require_relative 'lib/mrml/version'
4 |
5 | Gem::Specification.new do |spec|
6 | spec.name = 'mrml'
7 | spec.version = MRML::VERSION
8 | spec.authors = ['Jonian Guveli']
9 | spec.email = ['jonian@hardpixel.eu']
10 |
11 | spec.summary = 'Ruby wrapper for MRML Rust'
12 | spec.description = 'Ruby wrapper for MRML, the MJML parser implementation in Rust.'
13 | spec.homepage = 'https://github.com/hardpixel/mrml-ruby'
14 | spec.license = 'MIT'
15 |
16 | spec.files = Dir['ext/**/src/*.rs', 'ext/**/Cargo.{lock,toml}', '{ext,lib}/**/*.rb', '*.{md,txt}']
17 | spec.extensions = Dir['ext/**/extconf.rb']
18 | spec.require_paths = ['lib']
19 |
20 | spec.required_ruby_version = '>= 2.7'
21 |
22 | spec.add_runtime_dependency 'rb_sys'
23 |
24 | spec.add_development_dependency 'bundler', '~> 2.0'
25 | spec.add_development_dependency 'minitest', '~> 5.0'
26 | spec.add_development_dependency 'rake-compiler', '~> 1.2'
27 | end
28 |
--------------------------------------------------------------------------------
/test/fixtures/invalid.mjml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Hello World
4 |
5 |
6 |
--------------------------------------------------------------------------------
/test/fixtures/valid.mjml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Newsletter Title
4 | Newsletter Preview
5 |
6 |
7 |
8 |
9 | Hello World
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/test/fixtures/value.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "mjml",
3 | "children": [{
4 | "type": "mj-head",
5 | "children": [{
6 | "type": "mj-title",
7 | "children": "Newsletter Title"
8 | }, {
9 | "type": "mj-preview",
10 | "children": "Newsletter Preview"
11 | }]
12 | }, {
13 | "type": "mj-body",
14 | "children": [{
15 | "type": "mj-section",
16 | "children": [{
17 | "type": "mj-column",
18 | "children": [{
19 | "type": "mj-text",
20 | "attributes": {
21 | "font-size": "20px",
22 | "color": "#F45E43",
23 | "font-family": "helvetica"
24 | },
25 | "children": ["Hello World"]
26 | }]
27 | }]
28 | }]
29 | }]
30 | }
31 |
--------------------------------------------------------------------------------
/test/mrml_test.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | require 'test_helper'
4 |
5 | class MrmlTest < Minitest::Test
6 | def test_that_it_has_a_version_number
7 | refute_nil ::MRML::VERSION
8 | end
9 |
10 | def test_that_it_loads_json
11 | result = ::MRML::Template.from_json(json_template)
12 | assert_instance_of ::MRML::Template, result
13 | end
14 |
15 | def test_that_it_loads_hash
16 | result = ::MRML::Template.from_hash(hash_template)
17 | assert_instance_of ::MRML::Template, result
18 | end
19 |
20 | def test_that_it_generates_title
21 | result = ::MRML::Template.new(valid_template)
22 | assert_equal 'Newsletter Title', result.title
23 | end
24 |
25 | def test_that_it_generates_preview
26 | result = ::MRML::Template.new(valid_template)
27 | assert_equal 'Newsletter Preview', result.preview
28 | end
29 |
30 | def test_that_it_generates_html
31 | result = ::MRML.to_html(valid_template)
32 |
33 | refute_match %r{?mj.+?>}, result
34 | assert_match %r{?body>}, result
35 | assert_match 'Hello World', result
36 | end
37 |
38 | def test_that_it_generates_json
39 | result = ::MRML.to_json(valid_template)
40 | assert_match '"type":"mjml"', result
41 | end
42 |
43 | def test_that_it_generates_hash
44 | result = ::MRML.to_hash(valid_template)
45 |
46 | assert_kind_of Hash, result
47 | assert_equal 'mjml', result['type']
48 | end
49 |
50 | def test_that_it_generates_mjml
51 | result = ::MRML::Template.from_json(json_template)
52 | assert_match %r{?mj.+?>}, result.to_mjml
53 | end
54 |
55 | def test_that_it_raises_an_exception
56 | assert_raises ::MRML::Error do
57 | ::MRML.to_html(invalid_template)
58 | end
59 | end
60 |
61 | private
62 |
63 | def valid_template
64 | @valid_template ||= File.read(
65 | File.join(__dir__, 'fixtures/valid.mjml')
66 | )
67 | end
68 |
69 | def invalid_template
70 | @invalid_template ||= File.read(
71 | File.join(__dir__, 'fixtures/invalid.mjml')
72 | )
73 | end
74 |
75 | def json_template
76 | @json_template ||= File.read(
77 | File.join(__dir__, 'fixtures/value.json')
78 | )
79 | end
80 |
81 | def hash_template
82 | @hash_template ||= JSON.parse(json_template)
83 | end
84 | end
85 |
--------------------------------------------------------------------------------
/test/test_helper.rb:
--------------------------------------------------------------------------------
1 | # frozen_string_literal: true
2 |
3 | $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
4 |
5 | require 'mrml'
6 | require 'minitest/autorun'
7 |
--------------------------------------------------------------------------------