Skip to content

Commit 2542097

Browse files
committed
add: inline code and code block
1 parent 97b8dd2 commit 2542097

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

src/code.rs

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
use std::{fs::File, io::Read};
2+
3+
use brack_sdk_rs::{MetaData, Type, Value};
4+
use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};
5+
6+
#[plugin_fn]
7+
pub fn metadata_inline_code() -> FnResult<Json<MetaData>> {
8+
Ok(Json(MetaData {
9+
command_name: "$".to_string(),
10+
call_name: "inline_code".to_string(),
11+
argument_types: vec![
12+
("src".to_string(), Type::TInline),
13+
(
14+
"language".to_string(),
15+
Type::TOption(Box::new(Type::TInline)),
16+
),
17+
],
18+
return_type: Type::TInline,
19+
}))
20+
}
21+
22+
#[plugin_fn]
23+
pub fn metadata_block_code() -> FnResult<Json<MetaData>> {
24+
Ok(Json(MetaData {
25+
command_name: "$".to_string(),
26+
call_name: "block_quote".to_string(),
27+
argument_types: vec![
28+
("src".to_string(), Type::TInline),
29+
(
30+
"language".to_string(),
31+
Type::TOption(Box::new(Type::TInline)),
32+
),
33+
],
34+
return_type: Type::TBlock,
35+
}))
36+
}
37+
38+
#[plugin_fn]
39+
pub fn inline_code(Json(args): Json<Vec<Value>>) -> FnResult<String> {
40+
if args.len() != 2 {
41+
return Err(WithReturnCode::new(
42+
anyhow::anyhow!(
43+
"Usage:
44+
1. [std.$ src]
45+
2. [std.$ src, language]"
46+
),
47+
1,
48+
));
49+
}
50+
let src = match &args[0] {
51+
Value::Text(t) => t,
52+
_ => {
53+
return Err(WithReturnCode::new(
54+
anyhow::anyhow!("src must be Value::Text"),
55+
1,
56+
))
57+
}
58+
};
59+
let language = match &args[1] {
60+
Value::TextOption(t) => t,
61+
_ => {
62+
return Err(WithReturnCode::new(
63+
anyhow::anyhow!("language must be Value::TextOption"),
64+
1,
65+
))
66+
}
67+
};
68+
let file = File::open(src);
69+
if file.is_err() {
70+
return Err(WithReturnCode::new(anyhow::anyhow!("file not found"), 1));
71+
}
72+
let mut file = file?;
73+
let mut code = String::new();
74+
if let Err(_) = file.read_to_string(&mut code) {
75+
return Err(WithReturnCode::new(
76+
anyhow::anyhow!("something went wrong reading the file"),
77+
1,
78+
));
79+
}
80+
match language {
81+
Some(language) => Ok(format!(
82+
"<code class=\"language-{}\">{}</code>",
83+
language, code
84+
)),
85+
None => Ok(format!("<code>{}</code>", code)),
86+
}
87+
}
88+
89+
#[plugin_fn]
90+
pub fn block_code(Json(args): Json<Vec<Value>>) -> FnResult<String> {
91+
if args.len() != 2 {
92+
return Err(WithReturnCode::new(
93+
anyhow::anyhow!(
94+
"Usage:
95+
1. {{std.$ src}}
96+
2. {{std.$ src, language}}"
97+
),
98+
1,
99+
));
100+
};
101+
let src = match &args[0] {
102+
Value::Text(t) => t,
103+
_ => {
104+
return Err(WithReturnCode::new(
105+
anyhow::anyhow!("src must be Value::Text"),
106+
1,
107+
))
108+
}
109+
};
110+
let language = match &args[1] {
111+
Value::TextOption(t) => t,
112+
_ => {
113+
return Err(WithReturnCode::new(
114+
anyhow::anyhow!("language must be Value::TextOption"),
115+
1,
116+
))
117+
}
118+
};
119+
let file = File::open(src);
120+
if file.is_err() {
121+
return Err(WithReturnCode::new(anyhow::anyhow!("file not found"), 1));
122+
}
123+
let mut file = file?;
124+
let mut code = String::new();
125+
if let Err(_) = file.read_to_string(&mut code) {
126+
return Err(WithReturnCode::new(
127+
anyhow::anyhow!("something went wrong reading the file"),
128+
1,
129+
));
130+
}
131+
match language {
132+
Some(language) => Ok(format!(
133+
"<pre><code class=\"language-{}\">{}</code></pre>",
134+
language, code
135+
)),
136+
None => Ok(format!("<pre><code>{}</code></pre>", code)),
137+
}
138+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod anchor;
22
pub mod bold;
3+
pub mod code;
34
pub mod footnote;
45
pub mod headings;
56
pub mod italic;

0 commit comments

Comments
 (0)