-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
300 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Counter utility methods | ||
pub trait Counter { | ||
fn reset(&mut self); | ||
fn increment(&mut self) -> usize; | ||
fn decrement(&mut self) -> usize; | ||
} | ||
|
||
impl Counter for usize { | ||
fn reset(&mut self) { | ||
*self = 0; | ||
} | ||
|
||
fn increment(&mut self) -> usize { | ||
*self = self.checked_add(1).unwrap_or(*self); | ||
*self | ||
} | ||
|
||
fn decrement(&mut self) -> usize { | ||
*self = self.checked_sub(1).unwrap_or(*self); | ||
*self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::counter::Counter; | ||
use lol_html::html_content::ContentType; | ||
use lol_html::html_content::Element; | ||
use std::cell::RefCell; | ||
use std::rc::Rc; | ||
|
||
// Function to handle list elements and items | ||
#[inline] | ||
pub(crate) fn handle_list_or_item( | ||
element: &mut Element, | ||
list_type: Rc<RefCell<Option<String>>>, | ||
order_counter: Rc<RefCell<usize>>, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
match element.tag_name().as_str() { | ||
"ul" | "menu" => { | ||
*list_type.borrow_mut() = Some("ul".to_string()); | ||
order_counter.borrow_mut().reset(); // Reset the order counter for a new list | ||
} | ||
"ol" => { | ||
*list_type.borrow_mut() = Some("ol".to_string()); | ||
order_counter.borrow_mut().reset(); | ||
} | ||
"li" => { | ||
if list_type.borrow().as_deref() == Some("ol") { | ||
let order = order_counter.borrow_mut().increment(); | ||
element.before(&format!("\n{}. ", order), ContentType::Text); | ||
} else { | ||
element.before("\n* ", ContentType::Text); | ||
} | ||
} | ||
_ => (), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
pub(crate) mod counter; | ||
pub(crate) mod iframes; | ||
pub(crate) mod images; | ||
pub(crate) mod lists; | ||
pub(crate) mod quotes; | ||
pub(crate) mod styles; | ||
|
||
pub mod writer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use crate::rewriter::counter::Counter; | ||
use lol_html::html_content::{ContentType, Element, TextChunk}; | ||
use std::error::Error; | ||
use std::{cell::RefCell, rc::Rc}; | ||
|
||
// Function to handle <blockquote> elements | ||
pub(crate) fn rewrite_blockquote_element( | ||
el: &mut Element, | ||
quote_depth: Rc<RefCell<usize>>, | ||
) -> Result<(), Box<dyn Error + Send + Sync>> { | ||
quote_depth.borrow_mut().increment(); | ||
|
||
if let Some(end_tag_handlers) = el.end_tag_handlers() { | ||
end_tag_handlers.push(Box::new({ | ||
let quote_depth = quote_depth.clone(); | ||
move |_end| { | ||
quote_depth.borrow_mut().decrement(); | ||
Ok(()) | ||
} | ||
})); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
// Function to handle text within <blockquote> elements | ||
pub(crate) fn rewrite_blockquote_text( | ||
text_chunk: &mut TextChunk<'_>, | ||
quote_depth: Rc<RefCell<usize>>, | ||
) -> Result<(), Box<dyn Error + Send + Sync>> { | ||
let depth = *quote_depth.borrow(); | ||
let quote_prefix = "> ".repeat(depth); | ||
let lines: Vec<&str> = text_chunk.as_str().lines().collect(); | ||
let total_lines = lines.len(); | ||
|
||
let last = text_chunk.last_in_text_node(); | ||
|
||
let modified_text = lines | ||
.iter() | ||
.enumerate() | ||
.map(|(i, line)| { | ||
if i >= 1 && i == total_lines - 1 { | ||
format!("{}", line) | ||
} else { | ||
format!("{}{}", quote_prefix, line) | ||
} | ||
}) | ||
.collect::<Vec<_>>() | ||
.join(""); | ||
|
||
text_chunk.replace(&modified_text, ContentType::Html); | ||
|
||
if last { | ||
text_chunk.after("\n", ContentType::Text); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use lol_html::html_content::Element; | ||
|
||
/// Rewrite the initial elements that need extra styles. | ||
pub(crate) fn rewrite_style_element(el: &mut Element) -> Result<(), std::io::Error> { | ||
let tag_name = el.tag_name().to_ascii_lowercase(); | ||
let mark = match tag_name.as_str() { | ||
"b" | "strong" => "**", | ||
"i" | "em" => "*", | ||
"s" | "del" => "~~", | ||
"u" | "ins" => "__", | ||
_ => return Ok(()), // Return early if tag is not one of the specified | ||
}; | ||
|
||
// Apply the markup before the element's content | ||
el.before(mark, lol_html::html_content::ContentType::Text); | ||
|
||
// Apply the markup after the element's content | ||
el.after(mark, lol_html::html_content::ContentType::Text); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.