Skip to content

Commit

Permalink
Merge pull request #765 from fastn-stack/ftd_document
Browse files Browse the repository at this point in the history
`ftd.document`
  • Loading branch information
Arpita-Jaiswal committed Mar 21, 2023
2 parents 121cd4f + 6bc1fc1 commit 175ddd9
Show file tree
Hide file tree
Showing 33 changed files with 2,119 additions and 75 deletions.
117 changes: 60 additions & 57 deletions fastn-core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,68 +455,71 @@ pub fn replace_markers_2022(
base_url: &str,
) -> String {
ftd::html1::utils::trim_all_lines(
s.replace("__ftd_doc_title__", "")
.replace("__ftd_data__", html_ui.variables.as_str())
.replace(
"__ftd_canonical_url__",
config.package.generate_canonical_url(main_id).as_str(),
)
.replace(
"__favicon_html_tag__",
resolve_favicon(
config.root.as_str(),
config.package.name.as_str(),
&config.package.favicon,
)
.unwrap_or_default()
.as_str(),
)
.replace("__ftd_external_children__", "{}")
.replace(
"__ftd__",
format!("{}{}", html_ui.html.as_str(), font_style).as_str(),
)
.replace(
"__ftd_js__",
format!("{}{}", ftd_js, fastn_2022_js()).as_str(),
s.replace(
"__ftd_doc_title__",
html_ui.html_data.title.unwrap_or_default().as_str(),
)
.replace("__ftd_data__", html_ui.variables.as_str())
.replace(
"__ftd_canonical_url__",
config.package.generate_canonical_url(main_id).as_str(),
)
.replace(
"__favicon_html_tag__",
resolve_favicon(
config.root.as_str(),
config.package.name.as_str(),
&config.package.favicon,
)
.replace(
"__extra_js__",
get_extra_js(
config.ftd_external_js.as_slice(),
config.ftd_inline_js.as_slice(),
html_ui.js.as_str(),
)
.as_str(),
.unwrap_or_default()
.as_str(),
)
.replace("__ftd_external_children__", "{}")
.replace(
"__ftd__",
format!("{}{}", html_ui.html.as_str(), font_style).as_str(),
)
.replace(
"__ftd_js__",
format!("{}{}", ftd_js, fastn_2022_js()).as_str(),
)
.replace(
"__extra_js__",
get_extra_js(
config.ftd_external_js.as_slice(),
config.ftd_inline_js.as_slice(),
html_ui.js.as_str(),
)
.replace(
"__extra_css__",
get_extra_css(
config.ftd_external_css.as_slice(),
config.ftd_inline_css.as_slice(),
html_ui.css.as_str(),
)
.as_str(),
.as_str(),
)
.replace(
"__extra_css__",
get_extra_css(
config.ftd_external_css.as_slice(),
config.ftd_inline_css.as_slice(),
html_ui.css.as_str(),
)
.replace(
"__ftd_functions__",
format!(
"{}\n{}\n{}\n{}\n{}\n{}\n{}",
html_ui.functions.as_str(),
html_ui.dependencies.as_str(),
html_ui.variable_dependencies.as_str(),
html_ui.dummy_html.as_str(),
html_ui.raw_html.as_str(),
html_ui.mutable_variable,
html_ui.immutable_variable
)
.as_str(),
.as_str(),
)
.replace(
"__ftd_functions__",
format!(
"{}\n{}\n{}\n{}\n{}\n{}\n{}",
html_ui.functions.as_str(),
html_ui.dependencies.as_str(),
html_ui.variable_dependencies.as_str(),
html_ui.dummy_html.as_str(),
html_ui.raw_html.as_str(),
html_ui.mutable_variable,
html_ui.immutable_variable
)
.replace("__ftd_body_events__", html_ui.outer_events.as_str())
.replace("__ftd_css__", ftd::css())
.replace("__ftd_element_css__", "")
.replace("__base_url__", base_url)
.as_str(),
)
.replace("__ftd_body_events__", html_ui.outer_events.as_str())
.replace("__ftd_css__", ftd::css())
.replace("__ftd_element_css__", "")
.replace("__base_url__", base_url)
.as_str(),
)
}

Expand Down
76 changes: 74 additions & 2 deletions ftd/src/executor/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub enum Element {
Row(Row),
Column(Column),
Document(Document),
Text(Text),
Integer(Text),
Boolean(Text),
Expand All @@ -14,7 +15,7 @@ pub enum Element {
IterativeElement(IterativeElement),
CheckBox(CheckBox),
WebComponent(WebComponent),
Null,
Null { line_number: usize },
}

impl Element {
Expand All @@ -31,7 +32,8 @@ impl Element {
Element::Iframe(i) => Some(&i.common),
Element::TextInput(i) => Some(&i.common),
Element::CheckBox(c) => Some(&c.common),
Element::Null => None,
Element::Document(_) => None,
Element::Null { .. } => None,
Element::RawElement(_) => None,
Element::WebComponent(_) => None,
Element::IterativeElement(i) => i.element.get_common(),
Expand All @@ -42,10 +44,36 @@ impl Element {
match self {
Element::Row(r) => Some(&mut r.container.children),
Element::Column(c) => Some(&mut c.container.children),
Element::Document(d) => Some(&mut d.children),
Element::RawElement(r) => Some(&mut r.children),
_ => None,
}
}

pub(crate) fn is_document(&self) -> bool {
matches!(self, Element::Document(_))
}

pub(crate) fn line_number(&self) -> usize {
match self {
Element::Row(r) => r.common.line_number,
Element::Column(c) => c.common.line_number,
Element::Document(d) => d.line_number,
Element::Text(t) => t.common.line_number,
Element::Integer(i) => i.common.line_number,
Element::Boolean(b) => b.common.line_number,
Element::Decimal(d) => d.common.line_number,
Element::Image(i) => i.common.line_number,
Element::Code(c) => c.common.line_number,
Element::Iframe(i) => i.common.line_number,
Element::TextInput(t) => t.common.line_number,
Element::RawElement(r) => r.line_number,
Element::IterativeElement(i) => i.iteration.line_number,
Element::CheckBox(c) => c.common.line_number,
Element::WebComponent(w) => w.line_number,
Element::Null { line_number } => *line_number,
}
}
}

#[derive(serde::Deserialize, Debug, Default, PartialEq, Clone, serde::Serialize)]
Expand Down Expand Up @@ -83,6 +111,18 @@ pub struct Column {
pub common: Common,
}

#[derive(serde::Deserialize, Debug, Default, PartialEq, Clone, serde::Serialize)]
pub struct HTMLData {
pub title: ftd::executor::Value<Option<String>>,
}

#[derive(serde::Deserialize, Debug, Default, PartialEq, Clone, serde::Serialize)]
pub struct Document {
pub data: HTMLData,
pub children: Vec<Element>,
pub line_number: usize,
}

#[derive(serde::Deserialize, Debug, PartialEq, Default, Clone, serde::Serialize)]
pub struct Text {
pub text: ftd::executor::Value<Rendered>,
Expand Down Expand Up @@ -891,6 +931,38 @@ pub fn column_from_properties(
Ok(Column { container, common })
}

pub fn document_from_properties(
properties: &[ftd::interpreter2::Property],
arguments: &[ftd::interpreter2::Argument],
doc: &mut ftd::executor::TDoc,
line_number: usize,
children: Vec<Element>,
) -> ftd::executor::Result<Document> {
Ok(Document {
data: html_data_from_properties(properties, arguments, doc, line_number)?,
children,
line_number,
})
}

#[allow(clippy::too_many_arguments)]
pub fn html_data_from_properties(
properties: &[ftd::interpreter2::Property],
arguments: &[ftd::interpreter2::Argument],
doc: &mut ftd::executor::TDoc,
line_number: usize,
) -> ftd::executor::Result<HTMLData> {
Ok(HTMLData {
title: ftd::executor::value::optional_string(
"title",
properties,
arguments,
doc,
line_number,
)?,
})
}

#[allow(clippy::too_many_arguments)]
pub fn common_from_properties(
properties: &[ftd::interpreter2::Property],
Expand Down
80 changes: 78 additions & 2 deletions ftd/src/executor/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct RT {
pub aliases: ftd::Map<String>,
pub bag: ftd::Map<ftd::interpreter2::Thing>,
pub main: ftd::executor::Column,
pub html_data: ftd::executor::HTMLData,
pub dummy_instructions: ftd::VecMap<ftd::executor::DummyElement>,
pub element_constructor: ftd::Map<ftd::executor::ElementConstructor>,
pub js: std::collections::HashSet<String>,
Expand All @@ -31,6 +32,7 @@ impl Default for RT {
aliases: Default::default(),
bag: Default::default(),
main: Default::default(),
html_data: Default::default(),
dummy_instructions: ftd::VecMap::new(),
element_constructor: Default::default(),
js: Default::default(),
Expand Down Expand Up @@ -58,14 +60,36 @@ impl<'a> ExecuteDoc<'a> {
css: &mut css,
}
.execute()?;

let (html_data, children) = match execute_doc.first() {
Some(first) if first.is_document() => {
if execute_doc.len().ne(&1) {
return ftd::executor::utils::parse_error(
"ftd.document can't have siblings.",
document.name.as_str(),
first.line_number(),
);
}

match first {
ftd::executor::Element::Document(d) => {
(d.data.to_owned(), d.children.to_owned())
}
_ => unreachable!(),
}
}
_ => (ftd::executor::HTMLData::default(), execute_doc),
};

let mut main = ftd::executor::element::default_column();
main.container.children.extend(execute_doc);
main.container.children.extend(children);

Ok(RT {
name: document.name.to_string(),
aliases: document.aliases,
bag: document.data,
main,
html_data,
dummy_instructions,
element_constructor,
js,
Expand Down Expand Up @@ -383,7 +407,9 @@ impl<'a> ExecuteDoc<'a> {
ExecuteDoc::insert_element(
&mut elements,
container.as_slice(),
ftd::executor::Element::Null,
ftd::executor::Element::Null {
line_number: instruction.line_number,
},
);
break;
}
Expand Down Expand Up @@ -481,6 +507,7 @@ impl<'a> ExecuteDoc<'a> {
current = match &mut current[*i] {
ftd::executor::Element::Row(r) => &mut r.container.children,
ftd::executor::Element::Column(r) => &mut r.container.children,
ftd::executor::Element::Document(r) => &mut r.children,
t => unreachable!("{:?}", t),
};
}
Expand Down Expand Up @@ -753,6 +780,55 @@ impl<'a> ExecuteDoc<'a> {
inherited_variables,
)?)
}
"ftd#document" => {
doc.insert_local_variables(
component_definition.name.as_str(),
instruction.properties.as_slice(),
component_definition
.arguments
.iter()
.cloned()
.filter(|k| k.name.eq("colors") || k.name.eq("types"))
.collect_vec()
.as_slice(),
local_container,
instruction.line_number,
inherited_variables,
false,
)?;

if !instruction.events.is_empty() {
return ftd::executor::utils::parse_error(
"Events are not expected for ftd.document type",
doc.name,
instruction.events.first().unwrap().line_number,
);
}

if instruction.condition.is_some() {
return ftd::executor::utils::parse_error(
"Condition is not expected for ftd.document type",
doc.name,
instruction.condition.clone().unwrap().line_number,
);
}

if local_container.len().ne(&1) || local_container.first().unwrap().ne(&0) {
return ftd::executor::utils::parse_error(
"ftd.document can occur only once and must be the root",
doc.name,
instruction.line_number,
);
}

ftd::executor::Element::Document(ftd::executor::element::document_from_properties(
instruction.properties.as_slice(),
component_definition.arguments.as_slice(),
doc,
instruction.line_number,
vec![],
)?)
}
"ftd#image" => {
ftd::executor::Element::Image(ftd::executor::element::image_from_properties(
instruction.properties.as_slice(),
Expand Down
4 changes: 2 additions & 2 deletions ftd/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub type FieldWithValue = (ftd::interpreter2::Field, Option<ftd::ast::VariableVa

pub use dummy::{DummyElement, ElementConstructor};
pub use element::{
CheckBox, Code, Column, Common, Container, Element, Event, Iframe, Image, ImageSrc,
IterativeElement, RawElement, Row, Text, TextInput, WebComponent,
CheckBox, Code, Column, Common, Container, Document, Element, Event, HTMLData, Iframe, Image,
ImageSrc, IterativeElement, RawElement, Row, Text, TextInput, WebComponent,
};
pub use main::{ExecuteDoc, RT};
pub use styles::{
Expand Down
Loading

0 comments on commit 175ddd9

Please sign in to comment.