Skip to content

Commit

Permalink
feat: custom url handle parser
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed May 28, 2024
1 parent b03249c commit 8b16670
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/cangrebot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::marker::PhantomData;

use html2md::common::get_tag_attr;
use html2md::{parse_html_custom, TagHandler, TagHandlerFactory};
Expand Down Expand Up @@ -120,10 +121,15 @@ fn html_to_md(s: &str) -> String {
let mut custom_parser: HashMap<String, Box<dyn TagHandlerFactory>> = HashMap::new();
custom_parser
.entry("img".to_owned())
.or_insert(Box::new(ImgHandler));
.or_insert(Box::new(DefaultFactory::<ImgHandler>::default()));
custom_parser
.entry("a".to_owned())
.or_insert(Box::new(DefaultFactory::<LinkHandler>::default()));

parse_html_custom(s, &custom_parser)
}

#[derive(Default)]
struct ImgHandler;
impl TagHandler for ImgHandler {
fn handle(&mut self, tag: &html2md::Handle, printer: &mut html2md::StructuredPrinter) {
Expand All @@ -143,8 +149,44 @@ impl TagHandler for ImgHandler {
fn after_handle(&mut self, _printer: &mut html2md::StructuredPrinter) {}
}

impl TagHandlerFactory for ImgHandler {
#[derive(Default)]
struct LinkHandler {
start_pos: usize,
url: String
}
impl TagHandler for LinkHandler {

fn handle(&mut self, tag: &html2md::Handle, printer: &mut html2md::StructuredPrinter) {
self.start_pos = printer.data.len();

// try to extract a hyperlink
self.url = match tag.data {
html2md::NodeData::Element { ref attrs, .. } => {
let attrs = attrs.borrow();
let href = attrs.iter().find(|attr| attr.name.local.to_string() == "href");
match href {
Some(link) => link.value.to_string(),
None => "https://rustlang-es.org".to_owned()
}
}
_ => "https://rustlang-es.org".to_owned()
};
}

fn after_handle(&mut self, printer: &mut html2md::StructuredPrinter) {
// add braces around already present text, put an url afterwards
printer.insert_str(self.start_pos, "[");
printer.append_str(&format!("](<{}>)", self.url))
}
}

#[derive(Default)]
struct DefaultFactory<T> {
_marker: PhantomData<T>,
}
impl<T> TagHandlerFactory for DefaultFactory<T>
where T: 'static + Default + TagHandler {
fn instantiate(&self) -> Box<dyn TagHandler> {
Box::new(ImgHandler)
Box::new(T::default())
}
}

0 comments on commit 8b16670

Please sign in to comment.