use scraper::{ElementRef, Html, Node}; use iced::widget::markdown; /* The goal here is to flatten the DOM as much as possible. paragraphs with fancy formatting are turned into markdown, same with */ //Supported content #[derive(Debug,Clone)] pub enum Content { Markdown(String), MarkdownParsed(Vec), Image(String), Audio(String), Video(String), Ignore } pub fn parse_content(c: &str) -> Vec{ process_content(&itemize_content(c)).into_iter().map(|i| { match i { Content::Markdown(s) => { Content::MarkdownParsed(markdown::parse(&s).collect()) //this is super lazy but it works.... } _ => {i} } }).collect() } fn markdownify_child(item: &Item) -> String { let mut result = "".to_owned(); match markdown_content(&item) { Content::Markdown(s) => { result = result + &s; }, _ => {} } result } fn process_children(children: &Vec) -> String { let mut result = "".to_owned(); for c in children{ result = result + &markdownify_child(c); } result } fn has_image(children: &Vec) -> bool { let mut result = false; for c in children { match c { Item::Image(_) => { return true; } _ => {} } } result } fn has_video(children: &Vec) -> bool{ let mut result = false; for c in children { match c { Item::Video(_) => { return true; } _ => {} } } false } fn markdown_content(item: &Item) -> Content { let mut markdown = String::new(); match item { Item::Title(n,children) => { markdown = markdown + &"#".repeat(*n) + " " +&process_children(children); }, Item::BoldedText(children) => { markdown = format!("**{}**",process_children(children)); }, Item::EmphasisText(children) => { markdown = format!("*{}*",process_children(children)); } Item::Text(s) => { markdown = markdown + s; }, Item::Link(href, children) => { markdown = markdown + &format!("[{}]({})",process_children(children),href); } Item::Paragraph(children) => { markdown = markdown + &process_children(children); } Item::UnorderedList(children) => { markdown = markdown + &process_children(children); } Item::OrderedList(children) => { markdown = markdown + &process_children(children); } Item::ListItem(children) => { markdown = "\n- ".to_owned() + &process_children(children); } _ => {} } Content::Markdown(markdown) } fn get_media_source(children: &Vec) -> Option { for c in children { match c { Item::Source(src) => { return Some(src.to_owned()); } _ => {} } } None } fn media_content(item: &Item) -> Content{ match item { Item::Audio(children) => { match get_media_source(children) { Some(s) => { return Content::Audio(s); } None => { return Content::Markdown("