Skip to content

Commit

Permalink
some more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
amitu committed Nov 24, 2024
1 parent 9ca8c7d commit 4cb1cdb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
35 changes: 33 additions & 2 deletions fastn-runtime/src/html.rs
Original file line number Diff line number Diff line change
@@ -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>,
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions fastn-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
29 changes: 0 additions & 29 deletions fastn-runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion ftd/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)];
Expand Down

0 comments on commit 4cb1cdb

Please sign in to comment.