Skip to content

Commit

Permalink
feat(templ): add csssyntaxraw (#92)
Browse files Browse the repository at this point in the history
* feat(templ): add csssyntaxraw

* settle for raw
  • Loading branch information
fiji-flo authored Jan 22, 2025
1 parent 6e6deec commit 25808bd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
48 changes: 42 additions & 6 deletions crates/css-syntax/src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,28 +684,64 @@ impl From<Node> for Constituent {
}
}
}

pub fn write_formal_syntax_from_syntax(
syntax_str: impl Into<String>,
locale_str: &str,
value_definition_url: &str,
syntax_tooltip: &'_ HashMap<LinkedToken, String>,
) -> Result<String, SyntaxError> {
let syntax = Syntax {
name: "dummy".to_string(),
syntax: syntax_str.into(),
};
write_formal_syntax_internal(
syntax,
locale_str,
value_definition_url,
syntax_tooltip,
true,
)
}

pub fn write_formal_syntax(
css: CssType,
locale_str: &str,
value_definition_url: &str,
syntax_tooltip: &'_ HashMap<LinkedToken, String>,
) -> Result<String, SyntaxError> {
let syntax: Syntax = get_syntax_internal(css, true);
if syntax.syntax.is_empty() {
return Err(SyntaxError::NoSyntaxFound);
}
write_formal_syntax_internal(
syntax,
locale_str,
value_definition_url,
syntax_tooltip,
false,
)
}

fn write_formal_syntax_internal(
syntax: Syntax,
locale_str: &str,
value_definition_url: &str,
syntax_tooltip: &'_ HashMap<LinkedToken, String>,
skip_first: bool,
) -> Result<String, SyntaxError> {
let mut renderer = SyntaxRenderer {
locale_str,
value_definition_url,
syntax_tooltip,
constituents: Default::default(),
};
let syntax: Syntax = get_syntax_internal(css, true);
if syntax.syntax.is_empty() {
return Err(SyntaxError::NoSyntaxFound);
}
let mut out = String::new();
write!(out, r#"<pre class="notranslate">"#)?;
let constituents = renderer.get_constituent_syntaxes(syntax)?;

for constituent in constituents {
renderer.render(&mut out, &constituent)?;
for constituent in constituents.iter().skip(if skip_first { 1 } else { 0 }) {
renderer.render(&mut out, constituent)?;
out.push_str("<br/>");
}

Expand Down
17 changes: 16 additions & 1 deletion crates/rari-doc/src/templ/templs/csssyntax.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;
use std::sync::LazyLock;

use css_syntax::syntax::{write_formal_syntax, CssType, LinkedToken};
use css_syntax::syntax::{
write_formal_syntax, write_formal_syntax_from_syntax, CssType, LinkedToken,
};
use rari_templ_func::rari_f;
use tracing::{error, warn};

Expand Down Expand Up @@ -63,3 +65,16 @@ pub fn csssyntax(name: Option<String>) -> Result<String, DocError> {
&TOOLTIPS,
)?)
}

#[rari_f]
pub fn csssyntaxraw(syntax: String) -> Result<String, DocError> {
Ok(write_formal_syntax_from_syntax(
syntax,
env.locale.as_url_str(),
&format!(
"/{}/docs/Web/CSS/Value_definition_syntax",
env.locale.as_url_str()
),
&TOOLTIPS,
)?)
}
1 change: 1 addition & 0 deletions crates/rari-doc/src/templ/templs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub fn invoke(
"specifications" => specification::specification_any,
"glossary" => glossary::glossary_any,
"csssyntax" => csssyntax::csssyntax_any,
"csssyntaxraw" => csssyntax::csssyntaxraw_any,
"listsubpages" => listsubpages::list_sub_pages_any,
"cssinfo" => cssinfo::cssinfo_any,
"inheritancediagram" => inheritance_diagram::inheritance_diagram_any,
Expand Down

0 comments on commit 25808bd

Please sign in to comment.