diff --git a/fastn-runtime/src/html.rs b/fastn-runtime/src/html.rs index 8d02b7968..fec7e31ea 100644 --- a/fastn-runtime/src/html.rs +++ b/fastn-runtime/src/html.rs @@ -1,6 +1,6 @@ use fastn_runtime::extensions::*; -pub struct HtmlInput { +pub struct HtmlData { pub package: Package, pub js: String, pub css_files: Vec<String>, @@ -10,7 +10,38 @@ pub struct HtmlInput { } const EMPTY_HTML_BODY: &str = "<body></body><style id=\"styles\"></style>"; -impl HtmlInput { +impl HtmlData { + fn from_cd(o: fastn_resolved::CompiledDocument) -> fastn_runtime::HtmlData { + let doc = fastn_runtime::TDoc { + name: "foo", // Todo: Package name + definitions: o.definitions, + builtins: fastn_builtins::builtins(), + }; + + let output = fastn_runtime::get_all_asts( + &doc, + &o.content, + 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_runtime::default_bag_into_js_ast(&doc).as_slice(), + "foo", + ); + let js = format!("{js_ftd_script}\n{js_document_script}"); + fastn_runtime::HtmlData { + package: fastn_runtime::Package::new_name("foo"), // Todo + js, + css_files: vec![], + js_files: vec![], + doc: Box::new(doc), + has_rive_component: output.has_rive_components, + } + } + pub fn to_html(&self) -> String { self.to_html_(false) } diff --git a/fastn-runtime/src/lib.rs b/fastn-runtime/src/lib.rs index 3daa78bce..276dcc4a3 100644 --- a/fastn-runtime/src/lib.rs +++ b/fastn-runtime/src/lib.rs @@ -13,7 +13,7 @@ mod value; use element::Element; use extensions::*; -pub use html::{Favicon, HtmlInput, Package}; +pub use html::{Favicon, HtmlData, Package}; pub use resolver::ResolverData; pub use value::Value; @@ -153,7 +153,7 @@ impl fastn_runtime::extensions::ComponentDefinitionExt for fastn_resolved::Compo } pub fn from_tree( - tree: &[&fastn_resolved::ComponentInvocation], + tree: &[fastn_resolved::ComponentInvocation], doc: &dyn fastn_resolved::tdoc::TDoc, has_rive_components: &mut bool, ) -> fastn_js::Ast { @@ -349,7 +349,7 @@ pub struct AstOutput { } pub fn get_all_asts<'a, T: Iterator<Item = &'a fastn_resolved::Definition>>( doc: &dyn fastn_resolved::tdoc::TDoc, - tree: &[&fastn_resolved::ComponentInvocation], + tree: &[fastn_resolved::ComponentInvocation], used_definitions: T, ) -> AstOutput { // Check if the document tree uses Rive, if so add the Rive script. diff --git a/fastn-runtime/src/main.rs b/fastn-runtime/src/main.rs index b54e74492..84aa80f27 100644 --- a/fastn-runtime/src/main.rs +++ b/fastn-runtime/src/main.rs @@ -16,35 +16,6 @@ fn main() { line_number: 0, }; - let doc = fastn_runtime::TDoc { - name: "foo", // Todo: Package name - definitions: Default::default(), - builtins: fastn_builtins::builtins(), - }; - - let output = fastn_runtime::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_runtime::default_bag_into_js_ast(&doc).as_slice(), - "foo", - ); - let js = format!("{js_ftd_script}\n{js_document_script}"); - let html = fastn_runtime::HtmlInput { - package: fastn_runtime::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("../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/ftd/src/js/mod.rs b/ftd/src/js/mod.rs index 42379e14f..1eacb1099 100644 --- a/ftd/src/js/mod.rs +++ b/ftd/src/js/mod.rs @@ -91,7 +91,7 @@ pub fn document_into_js_ast(document: ftd::interpreter::Document) -> JSAstData { // Check if document tree has rive. This is used to add rive script. let mut has_rive_components = false; let mut document_asts = vec![fastn_runtime::from_tree( - &document.tree.as_slice().iter().collect::<Vec<_>>(), + document.tree.as_slice(), &doc, &mut has_rive_components, )];