1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
use crate::parser_helpers::{BlockChildType, CellType, DataCell, DelimitedDisplayType};
use super::element_text::ElementText;
use super::helpers::*;
use std::str::Lines;
#[derive(Debug, Default)]
pub struct TagInfo {
pub name: String,
pub indent: usize,
pub is_self_closing: bool,
pub in_props: bool,
}
#[derive(Debug)]
pub struct Emitter {
pub self_closing_tags: Vec<&'static str>,
}
impl Emitter {
pub fn new(self_closing_tags: Vec<&'static str>) -> Emitter {
Emitter { self_closing_tags }
}
pub fn emit_json(&self, data_cell: &DataCell) -> String {
let mut output = String::new();
/* let json_tree: DataCell = serde_json::from_str(json).unwrap(); */
match &data_cell.cell_type {
CellType::Root(root) => {
root.children.iter().for_each(|child| {
output.push_str(&self.emit_json(child));
});
}
CellType::Element(el) => {
output.push_str(&format!("<{} ", el.name));
el.props.iter().for_each(|prop| {
output.push_str(&Self::handle_prop_line(&format!(
"{} {}",
prop.key, prop.value
)));
});
if self.self_closing_tags.contains(&el.name.as_str()) {
output.push_str(" />");
} else {
output.push_str(" >");
el.children.iter().for_each(|child| {
output.push_str(&self.emit_json(child));
});
if el.children.is_empty() {
output.push_str("\"\"");
}
output.push_str(&format!("</{}>", el.name));
}
}
CellType::Block(block) => {
let mut text_block = String::new();
let mut sub_text_block = String::new();
block
.children
.iter()
.enumerate()
.for_each(|(i, child)| match child {
BlockChildType::Text(text) => {
// let mut prev_child = None;
// if i > 0 {
// prev_child = block.children.iter().nth(i - 1);
// }
// let mut prev_is_block = false;
// if let Some(prev_child) = prev_child {
// if let BlockChildType::Delimited(dl) = prev_child {
// prev_is_block = dl.display_type == DelimitedDisplayType::BLOCK
// }
// }
// if prev_is_block {
// text_block.push_str(&format!("\"#<{}>r#\"", "p"));
// }
if !text.content.trim().is_empty() {
sub_text_block.push_str(&format!("\"#<{}>r#\"", "span"));
sub_text_block.push_str(&text.content);
sub_text_block.push_str(&format!("\"#</{}>r#\"", "span"));
} else {
sub_text_block.push_str(&text.content);
}
// if prev_is_block {
// text_block.push_str(&format!("\"#</{}>r#\"", "p"));
// }
}
BlockChildType::Delimited(dl) => {
if let Some(wrap_with) = &dl.wrapped_with {
if !sub_text_block.trim().is_empty() {
text_block
.push_str(&format!("\"#<{} class=\"text\">r#\"", "span"));
text_block.push_str(&sub_text_block);
text_block.push_str(&format!("\"#</{}>r#\"", "span"));
}
sub_text_block = "".to_string();
text_block.push_str(&format!("\"#<{}>r#\"", wrap_with));
text_block.push_str(&dl.delimeter);
text_block.push_str(&dl.terminal);
if dl.delimeter == "_|" {
text_block.push_str("|_");
} else {
text_block.push_str(&dl.delimeter);
}
text_block.push_str(&format!("\"#</{}>r#\"", wrap_with));
} else {
sub_text_block.push_str(&dl.delimeter);
sub_text_block.push_str(&dl.terminal);
sub_text_block.push_str(&dl.delimeter);
}
}
});
if !sub_text_block.trim().is_empty() {
text_block.push_str(&format!("\"#<{} class=\"text\">r#\"", "span"));
text_block.push_str(&sub_text_block);
text_block.push_str(&format!("\"#</{}>r#\"", "span"));
}
let text_el = ElementText::new(&text_block);
output.push_str(&format!("r#\"{}\"#", text_el.handle_delimeters()));
}
_ => {}
}
output
}
fn handle_prop_line(line: &str) -> String {
let mut prop_line = line.trim().to_string();
// add quotes to prop value if it's a string
let prop_value = prop_line.split_once(" ");
if let Some((prop_key, prop_value)) = prop_value {
let is_number = prop_value.trim().parse::<f32>();
let is_bool = prop_value.trim() == "false" || prop_value.trim() == "true";
let is_vec = prop_value.trim().starts_with("vec![");
if is_number.is_err() && !is_bool && !is_vec {
prop_line = match prop_key {
"src" => format!(" {}=\"/{}\"", prop_key.trim(), prop_value.trim()),
_ => format!(" {}=\"{}\"", prop_key.trim(), prop_value.trim()),
}
} else {
prop_line = format!(" {}={}", prop_key.trim(), prop_value.trim())
}
}
prop_line
}
}