Figured out how to make using the enum a lot less annoying!
This commit is contained in:
parent
cb2f93c4d0
commit
6a4170b4c8
2 changed files with 28 additions and 59 deletions
|
@ -1,46 +0,0 @@
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum Item {
|
||||
Ignore,
|
||||
Text(String),
|
||||
//text, links, formatting are all markdown
|
||||
//arguably, for better control it will be best to turn markdown into its own set of items
|
||||
Image(String),
|
||||
Gif(String), //can't detect gif from image, has to be handled on front-end
|
||||
Svg(String),// wont' support for a while I think.
|
||||
Video(Video),
|
||||
Audio(Audio),
|
||||
Source(String),
|
||||
BoldedText(Vec<Item>),
|
||||
EmphasisText(Vec<Item>),
|
||||
UnorderedList(Vec<Item>),
|
||||
OrderedList(Vec<Item>),
|
||||
ListItem(Vec<Item>),
|
||||
Paragraph(Vec<Item>),//gotta replace this with specific items, needlessly flexible
|
||||
Link(Link),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Link{
|
||||
pub href: String,
|
||||
pub children: Vec<Item>
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Video{
|
||||
pub children: Vec<Item>
|
||||
//might have to do fancy things to detect autoplay...
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct Audio{
|
||||
pub children: Vec<Item>
|
||||
//might have to do fancy things to detect autoplay...
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub enum ContainerTag{
|
||||
P,
|
||||
Div,
|
||||
Button,//arguably redundant
|
||||
Table,
|
||||
|
||||
}
|
41
src/lib.rs
41
src/lib.rs
|
@ -1,11 +1,33 @@
|
|||
use scraper::{ElementRef, Html, Node};
|
||||
pub mod elements;
|
||||
use elements::*;
|
||||
|
||||
/*
|
||||
The goal here is to flatten the DOM as much as possible.
|
||||
paragraphs with fancy formatting are turned into markdown, same with
|
||||
*/
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Item {
|
||||
Ignore,
|
||||
Text(String),
|
||||
//text, links, formatting are all markdown
|
||||
//arguably, for better control it will be best to turn markdown into its own set of items
|
||||
Image(String),
|
||||
Gif(String), //can't detect gif from image, has to be handled on front-end
|
||||
Svg(String),// wont' support for a while I think.
|
||||
Video(Vec<Item>),
|
||||
Audio(Vec<Item>),
|
||||
Source(String),
|
||||
BoldedText(Vec<Item>),
|
||||
EmphasisText(Vec<Item>),
|
||||
UnorderedList(Vec<Item>),
|
||||
OrderedList(Vec<Item>),
|
||||
ListItem(Vec<Item>),
|
||||
Paragraph(Vec<Item>),//gotta replace this with specific items, needlessly flexible
|
||||
Link(String,Vec<Item>),
|
||||
}
|
||||
|
||||
|
||||
pub fn itemize_content(content: &str) -> Vec<Item> {
|
||||
let frag = Html::parse_fragment(content);
|
||||
frag.root_element().children().map(|e|{
|
||||
|
@ -37,12 +59,9 @@ fn parse_items(n: ego_tree::NodeRef<'_,Node>) -> Item{
|
|||
Some(link) => {link}
|
||||
None => {""}
|
||||
};
|
||||
return Item::Link(
|
||||
Link{
|
||||
href: href.to_owned(),
|
||||
children: get_children(&el)
|
||||
}
|
||||
)
|
||||
return Item::Link(href.to_owned(),get_children(&el))
|
||||
|
||||
|
||||
}
|
||||
"img" => {
|
||||
match el.attr("src") {
|
||||
|
@ -61,11 +80,7 @@ fn parse_items(n: ego_tree::NodeRef<'_,Node>) -> Item{
|
|||
}
|
||||
}
|
||||
"video" => {
|
||||
return Item::Video(
|
||||
Video{
|
||||
children: get_children(&el)
|
||||
}
|
||||
)
|
||||
return Item::Video(get_children(&el))
|
||||
}
|
||||
"ol" => {
|
||||
return Item::OrderedList(get_children(&el))
|
||||
|
|
Loading…
Reference in a new issue