1
1
extern crate core;
2
2
3
- use std:: path:: PathBuf ;
4
-
5
- use :: syntect:: highlighting:: ThemeSet ;
6
- use comrak:: {
7
- adapters:: SyntaxHighlighterAdapter ,
8
- markdown_to_html, markdown_to_html_with_plugins, parse_document,
9
- plugins:: syntect:: { SyntectAdapter , SyntectAdapterBuilder } ,
10
- ComrakOptions , ComrakPlugins ,
11
- } ;
12
- use magnus:: {
13
- define_module, exception, function, r_hash:: ForEach , scan_args, Error , RHash , Symbol , Value ,
14
- } ;
3
+ use comrak:: { markdown_to_html_with_plugins, parse_document, ComrakOptions } ;
4
+ use magnus:: { define_module, function, r_hash:: ForEach , scan_args, Error , RHash , Symbol , Value } ;
15
5
use node:: CommonmarkerNode ;
6
+ use plugins:: syntax_highlighting:: construct_syntax_highlighter_from_plugin;
16
7
17
8
mod options;
18
9
use options:: iterate_options_hash;
19
10
20
11
mod plugins;
21
- use plugins:: {
22
- syntax_highlighting:: { fetch_syntax_highlighter_path, fetch_syntax_highlighter_theme} ,
23
- SYNTAX_HIGHLIGHTER_PLUGIN ,
24
- } ;
12
+
25
13
use typed_arena:: Arena ;
26
14
27
15
mod node;
@@ -63,6 +51,32 @@ fn commonmark_to_html(args: &[Value]) -> Result<String, magnus::Error> {
63
51
) ?;
64
52
let ( rb_options, rb_plugins) = kwargs. optional ;
65
53
54
+ let comrak_options = match format_options ( rb_options) {
55
+ Ok ( options) => options,
56
+ Err ( err) => return Err ( err) ,
57
+ } ;
58
+
59
+ let mut comrak_plugins = comrak:: Plugins :: default ( ) ;
60
+
61
+ let syntect_adapter = match construct_syntax_highlighter_from_plugin ( rb_plugins) {
62
+ Ok ( Some ( adapter) ) => Some ( adapter) ,
63
+ Ok ( None ) => None ,
64
+ Err ( err) => return Err ( err) ,
65
+ } ;
66
+
67
+ match syntect_adapter {
68
+ Some ( ref adapter) => comrak_plugins. render . codefence_syntax_highlighter = Some ( adapter) ,
69
+ None => comrak_plugins. render . codefence_syntax_highlighter = None ,
70
+ }
71
+
72
+ Ok ( markdown_to_html_with_plugins (
73
+ & rb_commonmark,
74
+ & comrak_options,
75
+ & comrak_plugins,
76
+ ) )
77
+ }
78
+
79
+ fn format_options ( rb_options : Option < RHash > ) -> Result < comrak:: Options , magnus:: Error > {
66
80
let mut comrak_options = ComrakOptions :: default ( ) ;
67
81
68
82
if let Some ( rb_options) = rb_options {
@@ -72,101 +86,7 @@ fn commonmark_to_html(args: &[Value]) -> Result<String, magnus::Error> {
72
86
} ) ?;
73
87
}
74
88
75
- if let Some ( rb_plugins) = rb_plugins {
76
- let mut comrak_plugins = ComrakPlugins :: default ( ) ;
77
-
78
- let syntax_highlighter: Option < & dyn SyntaxHighlighterAdapter > ;
79
- let adapter: SyntectAdapter ;
80
-
81
- let theme = match rb_plugins. get ( Symbol :: new ( SYNTAX_HIGHLIGHTER_PLUGIN ) ) {
82
- Some ( syntax_highlighter_options) => {
83
- match fetch_syntax_highlighter_theme ( syntax_highlighter_options) {
84
- Ok ( theme) => theme,
85
- Err ( e) => {
86
- return Err ( e) ;
87
- }
88
- }
89
- }
90
- None => None , // no `syntax_highlighter:` defined
91
- } ;
92
-
93
- match theme {
94
- None => syntax_highlighter = None ,
95
- Some ( theme) => {
96
- if theme. is_empty ( ) {
97
- // no theme? uss css classes
98
- adapter = SyntectAdapter :: new ( None ) ;
99
- syntax_highlighter = Some ( & adapter) ;
100
- } else {
101
- let path = match rb_plugins. get ( Symbol :: new ( SYNTAX_HIGHLIGHTER_PLUGIN ) ) {
102
- Some ( syntax_highlighter_options) => {
103
- fetch_syntax_highlighter_path ( syntax_highlighter_options) ?
104
- }
105
- None => PathBuf :: from ( "" . to_string ( ) ) , // no `syntax_highlighter:` defined
106
- } ;
107
-
108
- if path. exists ( ) {
109
- if !path. is_dir ( ) {
110
- return Err ( Error :: new (
111
- exception:: arg_error ( ) ,
112
- "`path` needs to be a directory" ,
113
- ) ) ;
114
- }
115
-
116
- let builder = SyntectAdapterBuilder :: new ( ) ;
117
- let mut ts = ThemeSet :: load_defaults ( ) ;
118
-
119
- match ts. add_from_folder ( & path) {
120
- Ok ( _) => { }
121
- Err ( e) => {
122
- return Err ( Error :: new (
123
- exception:: arg_error ( ) ,
124
- format ! ( "failed to load theme set from path: {e}" ) ,
125
- ) ) ;
126
- }
127
- }
128
-
129
- // check if the theme exists in the dir
130
- match ts. themes . get ( & theme) {
131
- Some ( theme) => theme,
132
- None => {
133
- return Err ( Error :: new (
134
- exception:: arg_error ( ) ,
135
- format ! ( "theme `{}` does not exist" , theme) ,
136
- ) ) ;
137
- }
138
- } ;
139
-
140
- adapter = builder. theme_set ( ts) . theme ( & theme) . build ( ) ;
141
-
142
- syntax_highlighter = Some ( & adapter) ;
143
- } else {
144
- // no path? default theme lookup
145
- ThemeSet :: load_defaults ( )
146
- . themes
147
- . get ( & theme)
148
- . ok_or_else ( || {
149
- Error :: new (
150
- exception:: arg_error ( ) ,
151
- format ! ( "theme `{}` does not exist" , theme) ,
152
- )
153
- } ) ?;
154
- adapter = SyntectAdapter :: new ( Some ( & theme) ) ;
155
- syntax_highlighter = Some ( & adapter) ;
156
- }
157
- }
158
- }
159
- }
160
- comrak_plugins. render . codefence_syntax_highlighter = syntax_highlighter;
161
-
162
- Ok ( markdown_to_html_with_plugins (
163
- & rb_commonmark,
164
- & comrak_options,
165
- & comrak_plugins,
166
- ) )
167
- } else {
168
- Ok ( markdown_to_html ( & rb_commonmark, & comrak_options) )
169
- }
89
+ Ok ( comrak_options)
170
90
}
171
91
172
92
#[ magnus:: init]
0 commit comments