Skip to content

Commit 80f918f

Browse files
committed
add: ordered list
1 parent 6aff9c9 commit 80f918f

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

src/list.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,36 @@ use extism_pdk::{plugin_fn, FnResult, Json, WithReturnCode};
44
#[plugin_fn]
55
pub fn metadata_unordered_list() -> FnResult<Json<MetaData>> {
66
Ok(Json(MetaData {
7-
command_name: "list".to_string(),
7+
command_name: "-list".to_string(),
88
call_name: "unordered_list".to_string(),
99
argument_types: vec![("elems".to_string(), Type::TArray(Box::new(Type::TInline)))],
1010
return_type: Type::TBlock,
1111
}))
1212
}
1313

14+
#[plugin_fn]
15+
pub fn metadata_ordered_list() -> FnResult<Json<MetaData>> {
16+
Ok(Json(MetaData {
17+
command_name: "#list".to_string(),
18+
call_name: "ordered_list".to_string(),
19+
argument_types: vec![("elems".to_string(), Type::TArray(Box::new(Type::TInline)))],
20+
return_type: Type::TBlock,
21+
}))
22+
}
23+
24+
fn is_list(text: &str) -> bool {
25+
bool is_unordered_list = text.starts_with("<ul>") && text.ends_with("</ul>");
26+
bool is_ordered_list = text.starts_with("<ol>") && text.ends_with("</ol>");
27+
return is_unordered_list && is_ordered_list
28+
}
29+
1430
#[plugin_fn]
1531
pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
1632
if args.len() != 1 {
17-
return Err(WithReturnCode::new(anyhow::anyhow!("Usage: [std.list elem1, elem2, ..., elemN]"), 1));
33+
return Err(WithReturnCode::new(
34+
anyhow::anyhow!("Usage: {{std.-list elem1, elem2, ..., elemN}}"),
35+
1,
36+
));
1837
}
1938
let elems = match &args[0] {
2039
Value::TextArray(t) => t,
@@ -27,7 +46,7 @@ pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
2746
};
2847
let mut result = String::new();
2948
for elem in elems {
30-
if elem.starts_with("<ul>") && elem.ends_with("</ul>") {
49+
if is_list(text) {
3150
result += elem;
3251
continue;
3352
}
@@ -36,3 +55,31 @@ pub fn unordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
3655
Ok(format!("<ul>{}</ul>", result))
3756
}
3857

58+
59+
#[plugin_fn]
60+
pub fn ordered_list(Json(args): Json<Vec<Value>>) -> FnResult<String> {
61+
if args.len() != 1 {
62+
return Err(WithReturnCode::new(
63+
anyhow::anyhow!("Usage: {{std.#list elem1, elem2, ..., elemN}}"),
64+
1,
65+
));
66+
}
67+
let elems = match &args[0] {
68+
Value::TextArray(t) => t,
69+
_ => {
70+
return Err(WithReturnCode::new(
71+
anyhow::anyhow!("elems must be Value::TextArray"),
72+
1,
73+
))
74+
}
75+
};
76+
let mut result = String::new();
77+
for elem in elems {
78+
if is_list(elem) {
79+
result += elem;
80+
continue;
81+
}
82+
result += &format!("<li>{}</li>", elem);
83+
}
84+
Ok(format!("<ol>{}</ol>", result))
85+
}

0 commit comments

Comments
 (0)