Skip to content

Commit

Permalink
fix(templ): unescape strings in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
fiji-flo committed Oct 7, 2024
1 parent b8ae516 commit 0d6b6ec
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rari-doc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ svg_metadata = "0.5"
memoize = "0.4"
phf_macros = "0.11"
phf = "0.11"
unescaper = "0.1"

rari-utils.workspace = true
rari-types.workspace = true
Expand Down
43 changes: 31 additions & 12 deletions crates/rari-doc/src/templ/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,37 @@ pub enum Token {

fn to_arg(pair: Pair<'_, Rule>) -> Option<Arg> {
match pair.as_rule() {
Rule::sq_string => Some(Arg::String(
pair.as_span().as_str().to_string(),
Quotes::Single,
)),
Rule::dq_string => Some(Arg::String(
pair.as_span().as_str().to_string(),
Quotes::Double,
)),
Rule::bq_string => Some(Arg::String(
pair.as_span().as_str().to_string(),
Quotes::Back,
)),
Rule::sq_string => {
let s = pair.as_span().as_str();
Some(Arg::String(
unescaper::unescape(s).unwrap_or_else(|e| {
tracing::error!(source = "templ_parser", "{}", e);
s.to_string()
}),
Quotes::Single,
))
}
Rule::dq_string => {
let s = pair.as_span().as_str();
Some(Arg::String(
unescaper::unescape(s).unwrap_or_else(|e| {
tracing::error!(source = "templ_parser", "{}", e);
s.to_string()
}),
Quotes::Double,
))
}
Rule::bq_string => {
let s = pair.as_span().as_str();
Some(Arg::String(
unescaper::unescape(s).unwrap_or_else(|e| {
tracing::error!(source = "templ_parser", "{}", e);
s.to_string()
}),
Quotes::Back,
))
}

Rule::int => Some(Arg::Int(
pair.as_span().as_str().parse().unwrap_or_default(),
)),
Expand Down
31 changes: 31 additions & 0 deletions crates/rari-doc/src/templ/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,34 @@ fn push_text(out: &mut String, slice: &str) {
}
out.push_str(&slice[last..]);
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_basic() -> Result<(), DocError> {
let env = RariEnv {
..Default::default()
};
let Rendered {
content, templs, ..
} = render(&env, r#"{{ echo("doom") }}"#, 0)?;
let out = decode_ref(&content, &templs)?;
assert_eq!(out, r#"doom"#);
Ok(())
}

#[test]
fn test_escape() -> Result<(), DocError> {
let env = RariEnv {
..Default::default()
};
let Rendered {
content, templs, ..
} = render(&env, r#"{{ echo("\"doom\"") }}"#, 0)?;
let out = decode_ref(&content, &templs)?;
assert_eq!(out, r#""doom""#);
Ok(())
}
}
8 changes: 8 additions & 0 deletions crates/rari-doc/src/templ/templs/echo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use rari_templ_func::rari_f;

use crate::error::DocError;

#[rari_f]
pub fn echo(s: String) -> Result<String, DocError> {
Ok(s)
}
4 changes: 4 additions & 0 deletions crates/rari-doc/src/templ/templs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod compat;
pub mod css_ref;
pub mod cssinfo;
pub mod csssyntax;
pub mod echo;
pub mod embeds;
pub mod firefox_for_developers;
pub mod glossary;
Expand Down Expand Up @@ -163,6 +164,9 @@ pub fn invoke(
// ignore
"xulelem" => return Ok((Default::default(), false)),

// debug
"echo" => echo::echo_any,

// unknown
_ if deny_warnings() => return Err(DocError::UnknownMacro(name.to_string())),
_ => {
Expand Down

0 comments on commit 0d6b6ec

Please sign in to comment.