Files
natter/src/intermediate/paragraph.rs
Tom Alexander ae6f18d19c
Some checks failed
format Build format has succeeded
clippy Build clippy has failed
rust-test Build rust-test has succeeded
build Build build has succeeded
Center images when they are the only contents in a paragraph.
2025-02-22 13:15:58 -05:00

75 lines
2.1 KiB
Rust

use std::any::Any;
use super::macros::intermediate;
use super::IObject;
use crate::error::CustomError;
use organic::types::StandardProperties;
#[derive(Debug, Clone)]
pub(crate) struct IParagraph {
pub(crate) children: Vec<IObject>,
pub(crate) post_blank: organic::types::PostBlank,
}
intermediate!(
IParagraph,
&'orig organic::types::Paragraph<'parse>,
original,
intermediate_context,
{
let children = {
let mut ret = Vec::new();
for obj in original.children.iter() {
ret.push(IObject::new(intermediate_context.clone(), obj).await?);
}
ret
};
Ok(IParagraph {
children,
post_blank: original.get_post_blank(),
})
}
);
impl IParagraph {
pub(crate) async fn artificial<'orig, 'parse>(
_intermediate_context: crate::intermediate::IntermediateContext<'orig, 'parse>,
children: Vec<IObject>,
post_blank: organic::types::PostBlank,
) -> Result<IParagraph, CustomError> {
Ok(IParagraph {
children,
post_blank,
})
}
/// Checks if the paragraph contains nothing but a single image.
///
/// When this happens, we want to center the image.
pub(crate) fn is_single_image(&self) -> bool {
let num_images = self
.children
.iter()
.filter(|c| match c {
IObject::RegularLink(iregular_link) => match &iregular_link.target {
super::LinkTarget::Image { src, alt } => true,
_ => false,
},
_ => false,
})
.count();
num_images == 1
&& self.children.iter().all(|c| match c {
IObject::RegularLink(iregular_link) => match &iregular_link.target {
super::LinkTarget::Image { src, alt } => true,
_ => false,
},
IObject::PlainText(iplain_text) => {
iplain_text.source.chars().all(|c| c.is_ascii_whitespace())
}
_ => false,
})
}
}