summaryrefslogtreecommitdiff
path: root/src/directive/table.rs
blob: 7610a6b520ae6f357323608fbd15b220abb1cafa (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
36
37
38
39
40
41
42
43
44
45
46
47
use crate::error::SiteError;
use crate::page::PageMeta;
use crate::site::Site;
use crate::wikitext::ParsedDirective;
use log::debug;

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

impl Table {
    pub const REQUIRED: &'static [&'static str] = &["data"];
    pub const ALLOWED: &'static [&'static str] = &[];
    pub const ALLOW_ANY_UNNAMED: bool = true;

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

    pub fn process(&self, _site: &Site, _meta: &mut PageMeta) -> Result<String, SiteError> {
        let mut table = String::new();
        let mut lines = self.data.trim().lines();
        if let Some(first) = lines.next() {
            let columns = first.split('|').count();
            debug!("table first: {:?}", first);
            debug!("table column count: {}", columns);
            table.push_str(&format!("|{}|\n", first));
            for _ in 0..columns {
                table.push_str("|-");
            }
            table.push_str("|\n");
        }
        for line in lines {
            table.push_str(&format!("|{}|\n", line));
        }
        debug!("table data: {}", self.data);
        debug!("table: {}", table);
        Ok(table)
    }
}

impl From<&ParsedDirective> for Table {
    fn from(p: &ParsedDirective) -> Self {
        Table::new(p.args().get("data").unwrap().to_string())
    }
}