diff --git a/stdlib/src/web.rs b/stdlib/src/web.rs index 3edf64c6..b4d76f5d 100644 --- a/stdlib/src/web.rs +++ b/stdlib/src/web.rs @@ -92,6 +92,141 @@ use techscript_runtime::{ static SERVER_RUNNING: AtomicBool = AtomicBool::new(false); static PAGE_CONTENT: Mutex = Mutex::new(String::new()); +fn render_children(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + for child in &dsl.children { + html.push_str(&dsl_to_html( + &techscript_runtime::value::RuntimeValue::DslBlock(std::rc::Rc::new(child.clone())), + )); + } +} + +fn render_website(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + html.push_str(""); + for prop in &dsl.properties { + match prop.name.as_str() { + "title" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("{}", t)); + } + } + _ => {} + } + } + html.push_str(""); + html.push_str(""); + render_children(html, dsl); + html.push_str(""); +} + +fn render_page(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + html.push_str("
"); + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = dsl.args.first() { + html.push_str(&format!("

{}

", t)); + } + for prop in &dsl.properties { + if prop.name == "title" { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + } + render_children(html, dsl); + html.push_str("
"); +} + +fn render_hero(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + html.push_str("
"); + for prop in &dsl.properties { + match prop.name.as_str() { + "title" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + "subtitle" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + _ => {} + } + } + render_children(html, dsl); + html.push_str("
"); +} + +fn render_section(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + html.push_str("
"); + for prop in &dsl.properties { + match prop.name.as_str() { + "title" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + "id" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("", t)); + } + } + _ => {} + } + } + html.push_str("
"); + render_children(html, dsl); + html.push_str("
"); + html.push_str("
"); +} + +fn render_card(html: &mut String, dsl: &techscript_runtime::value::DslBlockValue) { + html.push_str("
"); + for prop in &dsl.properties { + match prop.name.as_str() { + "title" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + "text" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("

{}

", t)); + } + } + "image" => { + if let Some(techscript_runtime::value::RuntimeValue::Str(t)) = &prop.value { + html.push_str(&format!("\"card", t)); + } + } + _ => {} + } + } + html.push_str("
"); +} + /// Convert a DslBlockValue tree to HTML string. fn dsl_to_html(val: &RuntimeValue) -> String { match val { @@ -99,145 +234,19 @@ fn dsl_to_html(val: &RuntimeValue) -> String { let mut html = String::new(); match dsl.kind.as_str() { "website" => { - html.push_str(""); - for prop in &dsl.properties { - match prop.name.as_str() { - "title" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("{}", t)); - } - } - _ => {} - } - } - html.push_str(""); - html.push_str(""); - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } - html.push_str(""); + render_website(&mut html, dsl); } "page" => { - html.push_str("
"); - if let Some(RuntimeValue::Str(t)) = dsl.args.first() { - html.push_str(&format!("

{}

", t)); - } - for prop in &dsl.properties { - if prop.name == "title" { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - } - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } - html.push_str("
"); + render_page(&mut html, dsl); } "hero" => { - html.push_str("
"); - for prop in &dsl.properties { - match prop.name.as_str() { - "title" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - "subtitle" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - _ => {} - } - } - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } - html.push_str("
"); + render_hero(&mut html, dsl); } "section" => { - html.push_str("
"); - for prop in &dsl.properties { - match prop.name.as_str() { - "title" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - "id" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("", t)); - } - } - _ => {} - } - } - html.push_str("
"); - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } - html.push_str("
"); - html.push_str("
"); + render_section(&mut html, dsl); } "card" => { - html.push_str("
"); - for prop in &dsl.properties { - match prop.name.as_str() { - "title" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - "text" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!("

{}

", t)); - } - } - "image" => { - if let Some(RuntimeValue::Str(t)) = &prop.value { - html.push_str(&format!( - "\"card", - t - )); - } - } - _ => {} - } - } - html.push_str("
"); + render_card(&mut html, dsl); } "button" => { let label = dsl @@ -271,11 +280,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String { } "nav" => { html.push_str(""); } "header" => { @@ -287,11 +292,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String { } } } - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } + render_children(&mut html, dsl); html.push_str(""); } "footer" => { @@ -303,11 +304,7 @@ fn dsl_to_html(val: &RuntimeValue) -> String { } } } - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } + render_children(&mut html, dsl); html.push_str(""); } "input" => { @@ -325,29 +322,17 @@ fn dsl_to_html(val: &RuntimeValue) -> String { } "form" => { html.push_str("
"); - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } + render_children(&mut html, dsl); html.push_str("
"); } "main" => { html.push_str("
"); - for child in &dsl.children { - html.push_str(&dsl_to_html(&RuntimeValue::DslBlock(Rc::new( - child.clone(), - )))); - } + render_children(&mut html, dsl); html.push_str("
"); } "aside" => { html.push_str(""); } "start" => {