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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
//!
use std::fs;
use std::io::{Error, ErrorKind, Write};
use std::path::{Path, PathBuf};

use handlebars::Handlebars;
use serde_json::Value;

use crate::config::Config;
use crate::loader::load_data;
use crate::metadata::{Author, EntryKey as Key, Entry, Metadata};

/// copies file under dst directory.
pub fn move_entry(buf: &PathBuf, dst: &Path) -> Result<(), Error> {
    let to = dst.join(buf.file_name().unwrap());
    fs::copy(buf.to_str().unwrap(), to)?;
    Ok(())
}

fn merge_authors<'a>(meta: &'a mut Value, c: &Config) -> &'a mut Value {
    *meta.pointer_mut("/website/metadata/authors").unwrap() =
        json!(c.website.metadata.as_ref().map_or(
            // TODO: handle email in format like: "name <email>"
            c.authors.as_ref().map_or(vec![], |a| {
                a.iter().map(|n| Author::new(n)).collect()
            }),
            |m| {
                m.authors
                    .as_ref()
                    .map_or(vec![], |v| v.iter().map(Author::from).collect())
            }
        ));
    meta
}

/// puts an entry into file in the dst directory and returns its metadata.
pub fn save_entry(
    buf: &PathBuf,
    reg: &Handlebars,
    c: &Config,
) -> Result<Entry, Error>
{
    let mut e = load_data(&fs::read_to_string(buf)?);
    if !e.has(Key::Content) {
        let empty = Entry::new();
        return Ok(empty);
    }

    let stem = buf.file_stem().unwrap().to_string_lossy().into_owned();
    let name = stem + ".html";
    let path = Path::new(&c.build.get_target_dir()).join(&name);

    e.add(Key::Slug, name);
    let mut file = fs::File::create(path)?;

    let mut meta = &mut json!({
        "website": c.website.to_json(),
        "article": e.to_json(),
    });
    meta = merge_authors(meta, c);

    let result = reg
        .render("layout", meta)
        .map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
    file.write_all(result.as_bytes())?;
    Ok(e)
}

/// creates a index file into dst directory.
pub fn make_index(
    dat: &mut Vec<Entry>,
    reg: &Handlebars,
    c: &Config,
) -> Result<(), Error>
{
    let dst_dir = c.build.get_target_dir();
    let path = Path::new(&dst_dir).join("index.html");
    let mut file = fs::File::create(path)?;

    let mut meta = &mut json!({
        "website": c.website.to_json(),
        "content": "",
    });
    meta = merge_authors(meta, c);

    let mut result: String = "".to_string();
    for d in dat {
        result = result +
            &reg.render("headline", &json!({"article": &d.to_json()}))
                .map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;
    }

    *meta.pointer_mut("/content").unwrap() = json!(
        "<div class=\"content\"><ul>".to_string() + &result + "</ul></div>"
    );
    result = reg
        .render("layout", meta)
        .map_err(|e| Error::new(ErrorKind::InvalidInput, e))?;

    file.write_all(result.as_bytes())?;
    Ok(())
}