summaryrefslogtreecommitdiff
path: root/src/directive/img.rs
blob: dea2625cd76d8a2a952678f3d437d6ed9e42e58f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::error::SiteError;
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;
use log::trace;
use std::path::Path;

#[derive(Debug, Eq, PartialEq)]
pub struct Img {
    src: String,
}

impl Img {
    pub const REQUIRED: &'static [&'static str] = &[];
    pub const ALLOWED: &'static [&'static str] = &["alt", "class", "link"];
    pub const ALLOW_ANY_UNNAMED: bool = true;

    pub fn new(src: String) -> Self {
        Self { src }
    }

    pub fn process(&self, site: &Site, meta: &mut PageMeta) -> Result<String, SiteError> {
        trace!("verify image exists: {}", self.src);
        let src = site.resolve(meta.path(), Path::new(&self.src))?;
        trace!("img src={:?}", src.display());
        Ok(format!("![]({})", self.src))
    }
}

impl From<ParsedDirective> for Img {
    fn from(p: ParsedDirective) -> Self {
        let unnamed = p.unnamed_args().pop().unwrap();
        Img::new(unnamed.into())
    }
}