lol-html
lol-html copied to clipboard
Question about mixing text and element handlers
Hey guys,
I have a case, where I need to put HTML <div>
element after or before <p>
element, based on how much words occured in previous <p>
elements.
I am now trying to handle it this way:
let mut words_count: usize = 0;
let mut previous_words_count: usize = 0;
let html = rewrite_str(
post.description.as_str(),
RewriteStrSettings {
element_content_handlers: vec![
text!("p", |t| {
previous_words_count = words_count.clone();
words_count += t.as_str().split(" ").count();
match html_elems
.iter()
.find(|x| previous_words_count < x.words_count && x.words_count <= words_count) {
None => (),
Some(x) => {
let platform_elem_string = x.by_platform(platform_str.as_str());
if !platform_elem_string.is_empty() {
t.after(platform_elem_string, ContentType::Html);
}
}
};
Ok(())
}),
],
..RewriteStrSettings::default()
}
).unwrap();
Sadly for t.after
and t.before
in given cases will insert these elements inside given element. I need to place them after or before <p>
.
Usage of elements! macro here will be advised, but then I cannot count words, right?
Any idea how I can resolve it? Is it even possible at this moment?
Thanks!