Skip to content

Commit 9049b21

Browse files
committed
feat: Provide global rant functions for common markdown stylings (bold, italic, bold-italic, highlight, link)
1 parent 92f866c commit 9049b21

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

src/lib.rs

+39-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use std::fmt;
1+
use std::{fmt, rc::Rc};
22

33
use rant::{
44
compiler::{CompilerErrorKind, CompilerMessage},
5-
runtime::RuntimeError,
6-
Rant, RantOptions, RantValue,
5+
runtime::{RuntimeError, VM},
6+
AsRantFunction, Rant, RantOptions, RantValue,
77
};
88
use wasm_bindgen::prelude::*;
99

@@ -22,6 +22,8 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
2222
..Default::default()
2323
};
2424
let mut rant = Rant::with_options(options);
25+
register_markdown_functions(&mut rant);
26+
2527
let mut msgs: Vec<CompilerMessage> = vec![];
2628
let program = rant.compile(input, &mut msgs);
2729
match program {
@@ -30,6 +32,40 @@ fn _rant(input: &str, seed: u32) -> Result<RantValue, RantError> {
3032
}
3133
}
3234

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+
3369
#[derive(Debug)]
3470
enum RantError {
3571
Compiler(CompilerErrorWithMsgs),

0 commit comments

Comments
 (0)