1
- use std:: fmt;
1
+ use std:: { fmt, rc :: Rc } ;
2
2
3
3
use rant:: {
4
4
compiler:: { CompilerErrorKind , CompilerMessage } ,
5
- runtime:: RuntimeError ,
6
- Rant , RantOptions , RantValue ,
5
+ runtime:: { RuntimeError , VM } ,
6
+ AsRantFunction , Rant , RantOptions , RantValue ,
7
7
} ;
8
8
use wasm_bindgen:: prelude:: * ;
9
9
@@ -22,6 +22,8 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
22
22
..Default :: default ( )
23
23
} ;
24
24
let mut rant = Rant :: with_options ( options) ;
25
+ register_markdown_functions ( & mut rant) ;
26
+
25
27
let mut msgs: Vec < CompilerMessage > = vec ! [ ] ;
26
28
let program = rant. compile ( input, & mut msgs) ;
27
29
match program {
@@ -30,6 +32,40 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
30
32
}
31
33
}
32
34
35
+ fn register_markdown_functions ( rant : & mut Rant ) {
36
+ use RantValue :: Function ;
37
+ rant. set_global ( "italic" , Function ( Rc :: new ( italic. as_rant_func ( ) ) ) ) ;
38
+ rant. set_global ( "bold" , Function ( Rc :: new ( bold. as_rant_func ( ) ) ) ) ;
39
+ rant. set_global ( "bold-italic" , Function ( Rc :: new ( bold_italic. as_rant_func ( ) ) ) ) ;
40
+ rant. set_global ( "highlight" , Function ( Rc :: new ( highlight. as_rant_func ( ) ) ) ) ;
41
+ rant. set_global ( "link" , Function ( Rc :: new ( link. as_rant_func ( ) ) ) ) ;
42
+ }
43
+
44
+ fn italic ( vm : & mut VM , val : RantValue ) -> Result < ( ) , RuntimeError > {
45
+ vm. cur_frame_mut ( ) . write ( format ! ( "*{}*" , val) ) ;
46
+ Ok ( ( ) )
47
+ }
48
+
49
+ fn bold ( vm : & mut VM , val : RantValue ) -> Result < ( ) , RuntimeError > {
50
+ vm. cur_frame_mut ( ) . write ( format ! ( "**{}**" , val) ) ;
51
+ Ok ( ( ) )
52
+ }
53
+
54
+ fn bold_italic ( vm : & mut VM , val : RantValue ) -> Result < ( ) , RuntimeError > {
55
+ vm. cur_frame_mut ( ) . write ( format ! ( "***{}***" , val) ) ;
56
+ Ok ( ( ) )
57
+ }
58
+
59
+ fn highlight ( vm : & mut VM , val : RantValue ) -> Result < ( ) , RuntimeError > {
60
+ vm. cur_frame_mut ( ) . write ( format ! ( "=={}==" , val) ) ;
61
+ Ok ( ( ) )
62
+ }
63
+
64
+ fn link ( vm : & mut VM , val : RantValue ) -> Result < ( ) , RuntimeError > {
65
+ vm. cur_frame_mut ( ) . write ( format ! ( "[[{}]]" , val) ) ;
66
+ Ok ( ( ) )
67
+ }
68
+
33
69
#[ derive( Debug ) ]
34
70
enum RantError {
35
71
Compiler ( CompilerErrorWithMsgs ) ,
0 commit comments