diff --git a/src/types.rs b/src/types.rs index ac87f8d..e939815 100644 --- a/src/types.rs +++ b/src/types.rs @@ -147,6 +147,13 @@ impl From<&String> for Case { } } +impl From<&[u8]> for Case { + fn from(s: &[u8]) -> Self { + let s = String::from_utf8(s.to_vec()).unwrap(); + Self::from_str(s.as_ref()).unwrap() + } +} + impl FromStr for StyleGuide { type Err = Error; fn from_str(s: &str) -> Result { diff --git a/typst/decasify.typ b/typst/decasify.typ index 4b73c26..bf50f14 100644 --- a/typst/decasify.typ +++ b/typst/decasify.typ @@ -1,37 +1,46 @@ #let _plugin = plugin("decasify.wasm") -#let string-to-titlecase(text, lang, style) = { +#let decasify-string(text, case, lang, style) = { + str(_plugin.case(bytes(text), bytes(case), bytes(lang), bytes(style))) +} + +#let decasify(body, case, style: "default") = { + show regex(".+"): it => decasify(it.text, case, text.lang, style) + body +} + +#let titlecase-string(text, lang, style) = { str(_plugin.titlecase(bytes(text), bytes(lang), bytes(style))) } #let titlecase(body, style: "default") = { - show regex(".+"): it => string-to-titlecase(it.text, text.lang, style) + show regex(".+"): it => titlecase-string(it.text, text.lang, style) body } -#let string-to-lowercase(text, lang) = { +#let lowercase-string(text, lang) = { str(_plugin.lowercase(bytes(text), bytes(lang))) } #let lowercase(body) = { - show regex(".+"): it => string-to-lowercase(it.text, text.lang) + show regex(".+"): it => lowercase-string(it.text, text.lang) body } -#let string-to-uppercase(text, lang) = { +#let uppercase-string(text, lang) = { str(_plugin.uppercase(bytes(text), bytes(lang))) } #let uppercase(body) = { - show regex(".+"): it => string-to-uppercase(it.text, text.lang) + show regex(".+"): it => uppercase-string(it.text, text.lang) body } -#let string-to-sentencecase(text, lang) = { +#let sentencecase-string(text, lang) = { str(_plugin.sentencecase(bytes(text), bytes(lang))) } #let sentencecase(body) = { - show regex(".+"): it => string-to-sentencecase(it.text, text.lang) + show regex(".+"): it => sentencecase-string(it.text, text.lang) body } diff --git a/typst/src/lib.rs b/typst/src/lib.rs index 7438775..f98d4ab 100644 --- a/typst/src/lib.rs +++ b/typst/src/lib.rs @@ -1,9 +1,18 @@ use anyhow::Result; -use decasify::{Locale, StyleGuide}; +use decasify::{Case, Locale, StyleGuide}; use wasm_minimal_protocol::{initiate_protocol, wasm_func}; initiate_protocol!(); +#[wasm_func] +pub fn decasify(data: &[u8], case: &[u8], lang: &[u8], style: &[u8]) -> Result> { + let chunk = String::from_utf8(data.to_vec())?; + let case = Case::from(case); + let locale = Locale::from(lang); + let style = StyleGuide::from(style); + Ok(decasify::case(&chunk, case, locale, style).into_bytes()) +} + #[wasm_func] pub fn titlecase(data: &[u8], lang: &[u8], style: &[u8]) -> Result> { let chunk = String::from_utf8(data.to_vec())?;