Files
natter/src/intermediate/paragraph.rs
fluxcdbot 05c7ecde82
Some checks failed
format Build format has succeeded
clippy Build clippy has failed
rust-test Build rust-test has failed
build Build build has succeeded
CI: autofix rust code.
2026-07-11 22:20:00 +00:00

74 lines
2.1 KiB
Rust

use super::IObject;
use super::macros::intermediate;
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 {
#[allow(clippy::needless_lifetimes)]
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) => matches!(
&iregular_link.target,
super::LinkTarget::Image { src: _, alt: _ }
),
_ => false,
})
.count();
num_images == 1
&& self.children.iter().all(|c| match c {
IObject::RegularLink(iregular_link) => matches!(
&iregular_link.target,
super::LinkTarget::Image { src: _, alt: _ }
),
IObject::PlainText(iplain_text) => {
iplain_text.source.chars().all(|c| c.is_ascii_whitespace())
}
_ => false,
})
}
}