Skip to content

Commit

Permalink
Html is generating
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Nov 24, 2024
1 parent cf50b0c commit 3c0024e
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fastn-resolved-to-js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
output.html
1 change: 1 addition & 0 deletions fastn-resolved-to-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ indoc.workspace = true
serde.workspace = true
once_cell.workspace = true
sha2.workspace = true
indexmap.workspace = true
90 changes: 69 additions & 21 deletions fastn-resolved-to-js/src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ const EMPTY_HTML_BODY: &str = "<body></body><style id=\"styles\"></style>";

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
Expand All @@ -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#"
<script src="../fastn-js/prism/prism.js"></script>
<script src="../fastn-js/prism/prism-line-highlight.js"></script>
<script src="../fastn-js/prism/prism-line-numbers.js"></script>
<script src="../fastn-js/prism/prism-rust.js"></script>
<script src="../fastn-js/prism/prism-json.js"></script>
<script src="../fastn-js/prism/prism-python.js"></script>
<script src="../fastn-js/prism/prism-markdown.js"></script>
<script src="../fastn-js/prism/prism-bash.js"></script>
<script src="../fastn-js/prism/prism-sql.js"></script>
<script src="../fastn-js/prism/prism-javascript.js"></script>
<link rel="stylesheet" href="../fastn-js/prism/prism-line-highlight.css">
<link rel="stylesheet" href="../fastn-js/prism/prism-line-numbers.css">
<script>{}</script>
"#,
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#"
<script src="{}"></script>
<script src="{}"></script>
<script src="{}"></script>
<link rel="stylesheet" href="{}">
{}
"#,
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()
)
}

Expand Down Expand Up @@ -182,6 +220,16 @@ pub struct Package {
favicon: Option<Favicon>,
}

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,
Expand Down
3 changes: 3 additions & 0 deletions fastn-resolved-to-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -342,6 +344,7 @@ pub fn default_bag_into_js_ast(doc: &dyn fastn_resolved::tdoc::TDoc) -> Vec<fast
ftd_asts
}

#[derive(Debug)]
pub struct AstOutput {
pub ast: Vec<fastn_js::Ast>,
pub has_rive_components: bool,
Expand Down
44 changes: 42 additions & 2 deletions fastn-resolved-to-js/src/main.rs
Original file line number Diff line number Diff line change
@@ -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![],
Expand All @@ -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.
}
53 changes: 53 additions & 0 deletions fastn-resolved-to-js/src/tdoc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pub struct TDoc<'a> {
pub name: &'a str,
pub definitions: indexmap::IndexMap<String, fastn_resolved::Definition>,
pub builtins: &'static indexmap::IndexMap<String, fastn_resolved::Definition>,
}

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<fastn_resolved::Function> {
match self.get(name) {
Some(fastn_resolved::Definition::Function(f)) => Some(f.clone()),
_ => None,
}
}

fn get_opt_record(&self, name: &str) -> Option<fastn_resolved::Record> {
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<fastn_resolved::ComponentDefinition> {
match self.get(name) {
Some(fastn_resolved::Definition::Component(f)) => Some(f.clone()),
_ => None,
}
}

fn get_opt_web_component(&self, name: &str) -> Option<fastn_resolved::WebComponentDefinition> {
match self.get(name) {
Some(fastn_resolved::Definition::WebComponent(f)) => Some(f.clone()),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions v0.5/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions v0.5/fastn-compiler/src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
};
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions v0.5/fastn-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 3c0024e

Please sign in to comment.