Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tokenize empty string and treat as none #1

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions crates/rari-doc/src/templ/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn to_arg(pair: Pair<'_, Rule>) -> Option<Arg> {
Rule::single_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::double_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::backquoted_quoted_string => pair.into_inner().next().and_then(to_arg),
Rule::empty_string => None,
Rule::sq_string => {
let s = pair.as_span().as_str();
Some(Arg::String(
Expand Down Expand Up @@ -129,21 +130,7 @@ pub fn parse(input: &str) -> Result<Vec<Token>, DocError> {
.into_inner()
.filter_map(|t| match t.as_rule() {
Rule::text => Some(Token::Text(t.into())),
Rule::macro_tag => {
let mut macro_roken: MacroToken = t.into();
// replace empty string args with None
// this is because we will use some judgements like `if let Some(arg) = arg`
// to determine whether the arg is empty or not
macro_roken.args = macro_roken
.args
.into_iter()
.map(|arg| match arg {
Some(Arg::String(s, _)) if s.is_empty() => None,
_ => arg,
})
.collect();
Some(Token::Macro(macro_roken))
}
Rule::macro_tag => Some(Token::Macro(t.into())),
_ => None,
})
.collect();
Expand Down Expand Up @@ -196,8 +183,8 @@ mod test {
fn with_empty_string_arg() {
let p = parse(r#"{{foo("")}}"#);
assert!(matches!(
p.unwrap().get(0),
Some(Token::Macro(macro_token)) if macro_token.args.get(0) == Some(&None)
p.unwrap().first(),
Some(Token::Macro(macro_token)) if macro_token.args.first() == Some(&None)
));
}
}
3 changes: 2 additions & 1 deletion crates/rari-doc/src/templ/rari-templ.pest
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ string = _{

boolean = { "true" | "false" }
none = { "" }
empty_string = { "\"\"" | "''" | "``" }


all_chars = _{'a'..'z' | 'A'..'Z' | "_" | "-" | '0'..'9'}
Expand All @@ -47,7 +48,7 @@ ident = ${
all_chars*
}

arg = _{ string | float | int | boolean | none }
arg = _{ empty_string | string | float | int | boolean | none }
kwargs = !{ arg ~ ("," ~ arg )* ~ ","? }
fn_call = !{ ident ~ "(" ~ kwargs? ~ ")" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@ pub fn embed_gh_live_sample(
let mut out = String::new();
out.push_str("<iframe ");
if let Some(width) = width {
if !width.is_empty() {
write!(&mut out, r#"width="{}" "#, width)?;
}
write!(&mut out, r#"width="{}" "#, width)?;
}
if let Some(height) = height {
if !height.is_empty() {
write!(&mut out, r#"height="{}" "#, height)?;
}
write!(&mut out, r#"height="{}" "#, height)?;
}

out.extend([
Expand Down
16 changes: 6 additions & 10 deletions crates/rari-doc/src/templ/templs/embeds/embed_live_sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,14 @@ pub fn embed_live_sample(
r#"" "#
]);
if let Some(width) = width {
if !width.is_empty() {
write!(&mut out, r#"width="{}" "#, width)?;
}
write!(&mut out, r#"width="{}" "#, width)?;
}
if let Some(height) = height {
if !height.is_empty() {
// TODO: fix this
if height.as_int() < 60 {
write!(&mut out, r#"height="60" "#)?;
} else {
write!(&mut out, r#"height="{}" "#, height)?;
}
// TODO: fix this
if height.as_int() < 60 {
write!(&mut out, r#"height="60" "#)?;
} else {
write!(&mut out, r#"height="{}" "#, height)?;
}
}
out.extend([
Expand Down
10 changes: 4 additions & 6 deletions crates/rari-doc/src/templ/templs/embeds/jsfiddle_embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ pub fn embded_jsfiddle(
let mut out = String::new();
out.push_str(r#"<p><iframe allowfullscreen="allowfullscreen" width="756" "#);
if let Some(height) = height {
if !height.is_empty() {
write!(&mut out, r#"height="{}" "#, height)?;
}
write!(&mut out, r#"height="{}" "#, height)?;
}
out.extend([
r#"src=""#,
url.as_str(),
"embedded/",
options.as_deref().unwrap_or_default(),
if options.as_ref().map(|s| s.is_empty()).unwrap_or_default() {
""
} else {
if options.as_ref().map(|s| !s.is_empty()).unwrap_or_default() {
"/"
} else {
""
},
r#""></iframe></p>"#,
]);
Expand Down
16 changes: 7 additions & 9 deletions crates/rari-doc/src/templ/templs/links/domxref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ pub fn domxref(
&api[first_char_index..],
);
if let Some(anchor) = anchor {
if !anchor.is_empty() {
if !anchor.starts_with('#') {
url.push('#');
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
url.push_str(&anchor);
if let Some(anchor) = anchor.strip_prefix('#') {
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
if !anchor.starts_with('#') {
url.push('#');
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
url.push_str(&anchor);
if let Some(anchor) = anchor.strip_prefix('#') {
display_with_fallback = Cow::Owned(format!("{}.{}", display_with_fallback, anchor));
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/rari-doc/src/templ/templs/links/rfc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ pub fn rfc(
content: Option<String>,
anchor: Option<AnyArg>,
) -> Result<String, DocError> {
let content = content.and_then(|c| if c.is_empty() { None } else { Some(c) });
let anchor_str = anchor.and_then(|a| if a.is_empty() { None } else { Some(a) });
let (content, anchor): (String, String) = match (content, anchor_str) {
let (content, anchor): (String, String) = match (content, anchor) {
(None, None) => Default::default(),
(None, Some(anchor)) => (
format!(
Expand Down
7 changes: 0 additions & 7 deletions crates/rari-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ pub struct AnyArg {
}

impl AnyArg {
pub fn is_empty(&self) -> bool {
if let Arg::String(s, _) = &self.value {
s.is_empty()
} else {
false
}
}
pub fn as_int(&self) -> i64 {
match &self.value {
Arg::String(s, _) => s
Expand Down