diff options
Diffstat (limited to 'src/directive/img.rs')
-rw-r--r-- | src/directive/img.rs | 201 |
1 files changed, 101 insertions, 100 deletions
diff --git a/src/directive/img.rs b/src/directive/img.rs index 3ee6501..8b74e5e 100644 --- a/src/directive/img.rs +++ b/src/directive/img.rs @@ -1,3 +1,4 @@ +use crate::directive::{DirectiveImplementation, Processed}; use crate::error::SiteError; use crate::page::PageMeta; use crate::site::Site; @@ -21,70 +22,67 @@ pub struct Img { width: Option<usize>, } -impl Img { - pub const REQUIRED: &'static [&'static str] = &[]; - pub const ALLOWED: &'static [&'static str] = &[ +impl DirectiveImplementation for Img { + const REQUIRED: &'static [&'static str] = &[]; + const ALLOWED: &'static [&'static str] = &[ "align", "alt", "class", "hspace", "id", "link", "size", "title", "vspace", ]; - pub const ALLOW_ANY_UNNAMED: bool = true; + const ALLOW_ANY_UNNAMED: bool = true; - fn new(src: String) -> Self { - Self { - src, - link: true, - align: None, - alt: None, - class: None, - height: None, - hspace: None, - id: None, - title: None, - vspace: None, - width: None, - } - } + fn from_parsed(p: &ParsedDirective) -> Self { + let unnamed = p.unnamed_args().pop().unwrap(); + let mut img = Img::new(unnamed.into()); + let args = p.args(); - fn link(&mut self, link: bool) { - self.link = link; - } + if let Some(link) = args.get("link") { + if *link == "no" { + img.link(false); + } + } - fn align(&mut self, align: String) { - self.align = Some(align); - } + if let Some(size) = args.get("size") { + if let Some((w, h)) = size.split_once('x') { + if let Ok(w) = w.parse() { + img.width(w); + } + if let Ok(h) = h.parse() { + img.height(h); + } + } + } - fn alt(&mut self, alt: String) { - self.alt = Some(alt); - } + if let Some(align) = args.get("align") { + img.align(align.to_string()); + } - fn class(&mut self, class: String) { - self.class = Some(class); - } + if let Some(alt) = args.get("alt") { + img.alt(alt.to_string()); + } - fn height(&mut self, h: usize) { - self.height = Some(h); - } + if let Some(class) = args.get("class") { + img.class(class.to_string()); + } - fn hspace(&mut self, hspace: String) { - self.hspace = Some(hspace); - } + if let Some(hspace) = args.get("hspace") { + img.hspace(hspace.to_string()); + } - fn id(&mut self, id: String) { - self.id = Some(id); - } + if let Some(id) = args.get("id") { + img.id(id.to_string()); + } - fn title(&mut self, title: String) { - self.title = Some(title); - } + if let Some(title) = args.get("title") { + img.title(title.to_string()); + } - fn vspace(&mut self, vspace: String) { - self.vspace = Some(vspace); - } + if let Some(vspace) = args.get("vspace") { + img.vspace(vspace.to_string()); + } - fn width(&mut self, w: usize) { - self.width = Some(w); + img } - pub fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> { + fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<Processed, SiteError> { trace!( "verify image exists: {} on {}", self.src, @@ -123,71 +121,74 @@ impl Img { img.push_str("</a>"); } - Ok(img) + Ok(Processed::Markdown(img)) } } -fn push_attr(s: &mut String, name: &str, value: &Option<String>) { - if let Some(v) = value { - s.push_str(&format!( - " {}=\"{}\"", - name, - encode_double_quoted_attribute(v) - )); +impl Img { + fn new(src: String) -> Self { + Self { + src, + link: true, + align: None, + alt: None, + class: None, + height: None, + hspace: None, + id: None, + title: None, + vspace: None, + width: None, + } } -} -impl From<&ParsedDirective> for Img { - fn from(p: &ParsedDirective) -> Self { - let unnamed = p.unnamed_args().pop().unwrap(); - let mut img = Img::new(unnamed.into()); - let args = p.args(); + fn link(&mut self, link: bool) { + self.link = link; + } - if let Some(link) = args.get("link") { - if *link == "no" { - img.link(false); - } - } + fn align(&mut self, align: String) { + self.align = Some(align); + } - if let Some(size) = args.get("size") { - if let Some((w, h)) = size.split_once('x') { - if let Ok(w) = w.parse() { - img.width(w); - } - if let Ok(h) = h.parse() { - img.height(h); - } - } - } + fn alt(&mut self, alt: String) { + self.alt = Some(alt); + } - if let Some(align) = args.get("align") { - img.align(align.to_string()); - } + fn class(&mut self, class: String) { + self.class = Some(class); + } - if let Some(alt) = args.get("alt") { - img.alt(alt.to_string()); - } + fn height(&mut self, h: usize) { + self.height = Some(h); + } - if let Some(class) = args.get("class") { - img.class(class.to_string()); - } + fn hspace(&mut self, hspace: String) { + self.hspace = Some(hspace); + } - if let Some(hspace) = args.get("hspace") { - img.hspace(hspace.to_string()); - } + fn id(&mut self, id: String) { + self.id = Some(id); + } - if let Some(id) = args.get("id") { - img.id(id.to_string()); - } + fn title(&mut self, title: String) { + self.title = Some(title); + } - if let Some(title) = args.get("title") { - img.title(title.to_string()); - } + fn vspace(&mut self, vspace: String) { + self.vspace = Some(vspace); + } - if let Some(vspace) = args.get("vspace") { - img.vspace(vspace.to_string()); - } + fn width(&mut self, w: usize) { + self.width = Some(w); + } +} - img +fn push_attr(s: &mut String, name: &str, value: &Option<String>) { + if let Some(v) = value { + s.push_str(&format!( + " {}=\"{}\"", + name, + encode_double_quoted_attribute(v) + )); } } |