use super::*;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
pub enum ImageLayout {
Left,
Center,
Right,
}
#[derive(Clone, Default, Eq, PartialEq, Hash)]
pub struct ImageLink {
pub source: String,
pub description: Option<String>,
pub link: Option<String>,
pub force_caption: Option<bool>,
pub layout: Option<ImageLayout>,
pub size: Option<(usize, usize)>,
pub options: Option<CommandOptions>,
}
impl ImageLink {
#[inline]
pub fn into_node(self, range: MaybeRanged) -> ASTNode {
SmartLink::Image(box self).into_node(range)
}
#[inline]
pub fn set_src(&mut self, msg: impl Into<String>) {
self.source = msg.into();
}
#[inline]
pub fn set_alt(&mut self, msg: impl Into<String>) {
self.description = Some(msg.into());
}
#[inline]
pub fn set_link(&mut self, msg: impl Into<String>) {
self.link = Some(msg.into());
}
#[inline]
pub fn set_size(&mut self, width: usize, height: usize) {
self.size = Some((width, height));
}
#[inline]
pub fn set_layout(&mut self, layout: ImageLayout) {
self.layout = Some(layout);
}
pub fn set_options(&mut self, options: CommandOptions) -> Vec<NoteError> {
let mut options = options;
let mut errors = vec![];
options.kvs.extract_string("src").map(|f| self.set_src(f));
options.kvs.extract_string("source").map(|f| self.set_src(f));
self.parse_layout(&mut options, &mut errors);
options.kvs.extract_string("alt").map(|f| self.set_alt(f));
options.kvs.extract_string("caption").map(|f| self.set_alt(f));
options.kvs.extract_string("description").map(|f| self.set_alt(f));
options.kvs.extract_bool("force_caption").map(|f| self.force_caption = Some(f));
self.options = Some(options);
return errors;
}
fn parse_layout(&mut self, options: &mut CommandOptions, errors: &mut Vec<NoteError>) {
let value = match options.kvs.extract("layout") {
None => return,
Some(s) => s,
};
match value {
Value::Integer(i) if i.is_negative() => self.layout = Some(ImageLayout::Left),
Value::Integer(i) if i.is_zero() => self.layout = Some(ImageLayout::Center),
Value::Integer(i) if i.is_zero() => self.layout = Some(ImageLayout::Right),
Value::String(s) if s.to_ascii_lowercase().eq("left") => self.layout = Some(ImageLayout::Left),
Value::String(s) if s.to_ascii_lowercase().eq("center") => self.layout = Some(ImageLayout::Center),
Value::String(s) if s.to_ascii_lowercase().eq("right") => self.layout = Some(ImageLayout::Right),
_ => errors.push(NoteError::runtime_error(format!("Unknown layout option {}", value))),
}
}
}