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
//!
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::fs::to_child_str_path;
use crate::loader::load_data;
use crate::metadata::{Author, EntryKey as Key, Entry, Metadata};

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
}

/// generates an entry into file in the dst directory and returns its metadata.
pub fn make_entry(
    buf: &PathBuf,
    dst: &Path,
    reg: &Handlebars,
    cfg: &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);
    }

    // assign _path (a part of url) and slug
    let name = if e.has(Key::Slug) {
        e.get(Key::Slug).unwrap()
    } else {
        let stem = buf.file_stem().unwrap().to_string_lossy().into_owned();
        stem + ".html"
    };
    let path = dst.join(&name);

    e.add(Key::_Path, to_child_str_path(&path));
    e.add(Key::Slug, name);

    let mut file = fs::File::create(&path)?;
    let mut meta = &mut json!({
        "website": cfg.website.to_json(),
        "article": e.to_json(),
    });
    meta = merge_authors(meta, cfg);

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

/// generates a index file into dst directory.
pub fn make_index(
    dat: &mut Vec<Entry>,
    reg: &Handlebars,
    cfg: &Config,
) -> Result<(), Error>
{
    let dst_dir = cfg.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": cfg.website.to_json(),
        "headlines": json!(&dat),
    });
    meta = merge_authors(meta, cfg);

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

/// creates a file.
pub fn write_entry(s: &str, dst: &Path) -> Result<(), Error> {
    fs::write(dst, s)?;
    Ok(())
}