diff --git a/Cargo.lock b/Cargo.lock index a9c1262d3..6008cc63b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1621,6 +1621,7 @@ dependencies = [ "fastn-builtins", "fastn-js", "fastn-resolved", + "indexmap", "indoc", "itertools 0.13.0", "once_cell", diff --git a/fastn-resolved-to-js/.gitignore b/fastn-resolved-to-js/.gitignore new file mode 100644 index 000000000..da40af56e --- /dev/null +++ b/fastn-resolved-to-js/.gitignore @@ -0,0 +1 @@ +output.html \ No newline at end of file diff --git a/fastn-resolved-to-js/Cargo.toml b/fastn-resolved-to-js/Cargo.toml index 3fd3e209f..1fd3845d3 100644 --- a/fastn-resolved-to-js/Cargo.toml +++ b/fastn-resolved-to-js/Cargo.toml @@ -20,3 +20,4 @@ indoc.workspace = true serde.workspace = true once_cell.workspace = true sha2.workspace = true +indexmap.workspace = true diff --git a/fastn-resolved-to-js/src/html.rs b/fastn-resolved-to-js/src/html.rs index 1897a481d..998cf4b47 100644 --- a/fastn-resolved-to-js/src/html.rs +++ b/fastn-resolved-to-js/src/html.rs @@ -12,18 +12,23 @@ const EMPTY_HTML_BODY: &str = ""; impl HtmlInput { pub fn to_html(&self) -> String { - let mut scripts = - fastn_resolved_to_js::utils::get_external_scripts(self.has_rive_component); - scripts.push(fastn_resolved_to_js::utils::get_js_html( - self.js_files.as_slice(), - )); - scripts.push(fastn_resolved_to_js::utils::get_css_html( - self.css_files.as_slice(), - )); + self.to_html_(false) + } + + pub fn to_test_html(&self) -> String { + self.to_html_(true) + } + + fn to_html_(&self, test: bool) -> String { + let script_file = if test { + self.get_test_script_file() + } else { + self.get_script_file() + }; format!( include_str!("../../ftd/ftd-js.html"), - fastn_package = self.package.name, + fastn_package = self.get_fastn_package_data(), base_url_tag = self .package .base_url @@ -38,24 +43,57 @@ impl HtmlInput { .unwrap_or_default() .as_str(), js_script = format!("{}{}", self.js, available_code_themes()).as_str(), - script_file = format!( - r#" + script_file = script_file.as_str(), + extra_js = "", // Todo + default_css = fastn_js::ftd_js_css(), + html_body = EMPTY_HTML_BODY // Todo: format!("{}{}", EMPTY_HTML_BODY, font_style) + ) + } + + fn get_test_script_file(&self) -> String { + format!( + r#" + + + + + + + + + + + + + + "#, + all_js_without_test(self.package.name.as_str(), self.doc.as_ref()) + ) + } + + fn get_script_file(&self) -> String { + let mut scripts = + fastn_resolved_to_js::utils::get_external_scripts(self.has_rive_component); + scripts.push(fastn_resolved_to_js::utils::get_js_html( + self.js_files.as_slice(), + )); + scripts.push(fastn_resolved_to_js::utils::get_css_html( + self.css_files.as_slice(), + )); + + format!( + r#" {} "#, - hashed_markdown_js(), - hashed_prism_js(), - hashed_default_ftd_js(self.package.name.as_str(), self.doc.as_ref()), - hashed_prism_css(), - scripts.join("").as_str() - ) - .as_str(), - extra_js = "", // Todo - default_css = fastn_js::ftd_js_css(), - html_body = EMPTY_HTML_BODY // Todo: format!("{}{}", EMPTY_HTML_BODY, font_style) + hashed_markdown_js(), + hashed_prism_js(), + hashed_default_ftd_js(self.package.name.as_str(), self.doc.as_ref()), + hashed_prism_css(), + scripts.join("").as_str() ) } @@ -182,6 +220,16 @@ pub struct Package { favicon: Option, } +impl Package { + pub fn new_name(name: &str) -> Package { + Package { + name: name.to_string(), + base_url: None, + favicon: None, + } + } +} + #[derive(Debug, Default)] pub struct Favicon { path: String, diff --git a/fastn-resolved-to-js/src/lib.rs b/fastn-resolved-to-js/src/lib.rs index 58db572f7..119d1a5ee 100644 --- a/fastn-resolved-to-js/src/lib.rs +++ b/fastn-resolved-to-js/src/lib.rs @@ -6,6 +6,8 @@ mod element; pub mod extensions; mod fastn_type_functions; mod html; +mod tdoc; +pub use tdoc::TDoc; pub mod utils; mod value; @@ -342,6 +344,7 @@ pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec, pub has_rive_components: bool, diff --git a/fastn-resolved-to-js/src/main.rs b/fastn-resolved-to-js/src/main.rs index 08334bf3f..210524232 100644 --- a/fastn-resolved-to-js/src/main.rs +++ b/fastn-resolved-to-js/src/main.rs @@ -1,8 +1,13 @@ fn main() { - let _c = fastn_resolved::ComponentInvocation { + let c = fastn_resolved::ComponentInvocation { id: None, name: "ftd#text".to_string(), - properties: vec![], // add hello-world caption etc. + properties: vec![fastn_resolved::Property { + value: fastn_resolved::Value::new_string("Hello World!").into_property_value(false, 0), + source: Default::default(), + condition: None, + line_number: 0, + }], // add hello-world caption etc. iteration: Box::new(None), condition: Box::new(None), events: vec![], @@ -11,5 +16,40 @@ fn main() { line_number: 0, }; + let doc = fastn_resolved_to_js::TDoc { + name: "foo", // Todo: Package name + definitions: Default::default(), + builtins: fastn_builtins::builtins(), + }; + + let output = fastn_resolved_to_js::get_all_asts( + &doc, + &[&c], + std::iter::IntoIterator::into_iter([fastn_builtins::builtins().get("ftd#text").unwrap()]), + ); + + let js_document_script = fastn_js::to_js(output.ast.as_slice(), "foo"); + let js_ftd_script = fastn_js::to_js( + fastn_resolved_to_js::default_bag_into_js_ast(&doc).as_slice(), + "foo", + ); + let js = format!("{js_ftd_script}\n{js_document_script}"); + let html = fastn_resolved_to_js::HtmlInput { + package: fastn_resolved_to_js::Package::new_name("foo"), // Todo + js, + css_files: vec![], + js_files: vec![], + doc: Box::new(doc), + has_rive_component: output.has_rive_components, + }; + + let html_str = html.to_test_html(); + + std::fs::write( + std::path::PathBuf::from("fastn-resolved-to-js/output.html"), + html_str, + ) + .unwrap(); + // this main should create a HTML file, and store it in current folder as index.html etc. } diff --git a/fastn-resolved-to-js/src/tdoc.rs b/fastn-resolved-to-js/src/tdoc.rs new file mode 100644 index 000000000..d5dbd5316 --- /dev/null +++ b/fastn-resolved-to-js/src/tdoc.rs @@ -0,0 +1,53 @@ +pub struct TDoc<'a> { + pub name: &'a str, + pub definitions: indexmap::IndexMap, + pub builtins: &'static indexmap::IndexMap, +} + +impl TDoc<'_> { + fn get(&self, name: &str) -> Option<&fastn_resolved::Definition> { + if let Some(definition) = self.definitions.get(name) { + return Some(definition); + } + + if let Some(definition) = self.builtins.get(name) { + return Some(definition); + } + + None + } +} + +impl<'a> fastn_resolved::tdoc::TDoc for TDoc<'a> { + fn get_opt_function(&self, name: &str) -> Option { + match self.get(name) { + Some(fastn_resolved::Definition::Function(f)) => Some(f.clone()), + _ => None, + } + } + + fn get_opt_record(&self, name: &str) -> Option { + match self.get(name) { + Some(fastn_resolved::Definition::Record(f)) => Some(f.clone()), + _ => None, + } + } + + fn name(&self) -> &str { + self.name + } + + fn get_opt_component(&self, name: &str) -> Option { + match self.get(name) { + Some(fastn_resolved::Definition::Component(f)) => Some(f.clone()), + _ => None, + } + } + + fn get_opt_web_component(&self, name: &str) -> Option { + match self.get(name) { + Some(fastn_resolved::Definition::WebComponent(f)) => Some(f.clone()), + _ => None, + } + } +} diff --git a/v0.5/Cargo.lock b/v0.5/Cargo.lock index 2f85dc619..344bfc124 100644 --- a/v0.5/Cargo.lock +++ b/v0.5/Cargo.lock @@ -236,6 +236,7 @@ dependencies = [ "fastn-builtins", "fastn-js", "fastn-resolved", + "indexmap", "indoc", "itertools", "once_cell", diff --git a/v0.5/fastn-compiler/src/js.rs b/v0.5/fastn-compiler/src/js.rs index b1fa44011..2c4780866 100644 --- a/v0.5/fastn-compiler/src/js.rs +++ b/v0.5/fastn-compiler/src/js.rs @@ -11,7 +11,7 @@ impl fastn_compiler::Compiler { // every symbol in self.symbol_used in the bag must be UR::R now let used_definitions = self.used_definitions(); let doc = fastn_compiler::TDoc { - name: "", // Todo: Package name + name: "foo", // Todo: Package name definitions: &used_definitions, builtins: fastn_builtins::builtins(), }; @@ -23,7 +23,7 @@ impl fastn_compiler::Compiler { resolved_content.as_slice(), used_definitions.into_iter().map(|(_, v)| v), ); - let js = fastn_js::to_js(output.ast.as_slice(), ""); + let js = fastn_js::to_js(output.ast.as_slice(), ""); // Todo: package name fastn_resolved_to_js::HtmlInput { package: Default::default(), // Todo diff --git a/v0.5/fastn-compiler/src/lib.rs b/v0.5/fastn-compiler/src/lib.rs index f1aa38771..89689861f 100644 --- a/v0.5/fastn-compiler/src/lib.rs +++ b/v0.5/fastn-compiler/src/lib.rs @@ -5,16 +5,18 @@ extern crate self as fastn_compiler; mod compiler; -mod js; +pub use compiler::compile; +pub(crate) use compiler::Compiler; + mod symbols; +pub use symbols::SymbolStore; + +mod js; mod tdoc; -pub use tdoc::TDoc; mod utils; +pub use tdoc::TDoc; -pub use compiler::compile; -pub(crate) use compiler::Compiler; pub use fastn_section::Result; -pub use symbols::SymbolStore; pub struct Output { #[expect(unused)]