|
1 | 1 | extern crate core;
|
2 | 2 |
|
3 |
| -use comrak::{markdown_to_html, ComrakOptions}; |
4 |
| -use magnus::{define_module, function, r_hash::ForEach, Error, RHash, Symbol}; |
| 3 | +use comrak::{ |
| 4 | + adapters::SyntaxHighlighterAdapter, markdown_to_html, markdown_to_html_with_plugins, |
| 5 | + plugins::syntect::SyntectAdapter, ComrakOptions, ComrakPlugins, |
| 6 | +}; |
| 7 | +use magnus::{define_module, function, r_hash::ForEach, scan_args, Error, RHash, Symbol, Value}; |
5 | 8 |
|
6 |
| -mod comrak_options; |
7 |
| -use comrak_options::iterate_options_hash; |
| 9 | +mod options; |
| 10 | +use options::iterate_options_hash; |
| 11 | + |
| 12 | +mod plugins; |
| 13 | +use plugins::{ |
| 14 | + syntax_highlighting::{ |
| 15 | + fetch_syntax_highlighter_theme, SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME, |
| 16 | + }, |
| 17 | + SYNTAX_HIGHLIGHTER_PLUGIN, |
| 18 | +}; |
| 19 | + |
| 20 | +mod utils; |
| 21 | + |
| 22 | +pub const EMPTY_STR: &str = ""; |
| 23 | + |
| 24 | +fn commonmark_to_html<'a>(args: &[Value]) -> Result<String, magnus::Error> { |
| 25 | + let args = scan_args::scan_args(args)?; |
| 26 | + let (rb_commonmark,): (String,) = args.required; |
| 27 | + let _: () = args.optional; |
| 28 | + let _: () = args.splat; |
| 29 | + let _: () = args.trailing; |
| 30 | + let _: () = args.block; |
| 31 | + |
| 32 | + let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>), ()>( |
| 33 | + args.keywords, |
| 34 | + &[], |
| 35 | + &["options", "plugins"], |
| 36 | + )?; |
| 37 | + let (rb_options, rb_plugins) = kwargs.optional; |
8 | 38 |
|
9 |
| -fn commonmark_to_html(rb_commonmark: String, rb_options: magnus::RHash) -> String { |
10 | 39 | let mut comrak_options = ComrakOptions::default();
|
11 | 40 |
|
12 |
| - rb_options |
13 |
| - .foreach(|key: Symbol, value: RHash| { |
14 |
| - iterate_options_hash(&mut comrak_options, key, value).unwrap(); |
| 41 | + if let Some(rb_options) = rb_options { |
| 42 | + rb_options.foreach(|key: Symbol, value: RHash| { |
| 43 | + iterate_options_hash(&mut comrak_options, key, value)?; |
15 | 44 | Ok(ForEach::Continue)
|
16 |
| - }) |
17 |
| - .unwrap(); |
| 45 | + })?; |
| 46 | + } |
| 47 | + |
| 48 | + if let Some(rb_plugins) = rb_plugins { |
| 49 | + let mut comrak_plugins = ComrakPlugins::default(); |
| 50 | + |
| 51 | + let syntax_highlighter: Option<&dyn SyntaxHighlighterAdapter>; |
| 52 | + let adapter: SyntectAdapter; |
| 53 | + |
| 54 | + let theme = match rb_plugins.get(Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN)) { |
| 55 | + Some(theme_val) => fetch_syntax_highlighter_theme(theme_val)?, |
| 56 | + None => SYNTAX_HIGHLIGHTER_PLUGIN_DEFAULT_THEME.to_string(), // no `syntax_highlighter:` defined |
| 57 | + }; |
| 58 | + |
| 59 | + if theme.is_empty() || theme == "none" { |
| 60 | + syntax_highlighter = None; |
| 61 | + } else { |
| 62 | + adapter = SyntectAdapter::new(&theme); |
| 63 | + syntax_highlighter = Some(&adapter); |
| 64 | + } |
| 65 | + |
| 66 | + comrak_plugins.render.codefence_syntax_highlighter = syntax_highlighter; |
18 | 67 |
|
19 |
| - markdown_to_html(&rb_commonmark, &comrak_options) |
| 68 | + Ok(markdown_to_html_with_plugins( |
| 69 | + &rb_commonmark, |
| 70 | + &comrak_options, |
| 71 | + &comrak_plugins, |
| 72 | + )) |
| 73 | + } else { |
| 74 | + Ok(markdown_to_html(&rb_commonmark, &comrak_options)) |
| 75 | + } |
20 | 76 | }
|
21 | 77 |
|
22 | 78 | #[magnus::init]
|
23 | 79 | fn init() -> Result<(), Error> {
|
24 | 80 | let module = define_module("Commonmarker")?;
|
25 | 81 |
|
26 |
| - module.define_module_function("commonmark_to_html", function!(commonmark_to_html, 2))?; |
| 82 | + module.define_module_function("commonmark_to_html", function!(commonmark_to_html, -1))?; |
27 | 83 |
|
28 | 84 | Ok(())
|
29 | 85 | }
|
0 commit comments