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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Blades  Copyright (C) 2021 Maroš Grego
//
// This file is part of Blades. This program comes with ABSOLUTELY NO WARRANTY;
// This is free software, and you are welcome to redistribute it under the
// conditions of the GNU General Public License version 3.0.
//
// You should have received a copy of the GNU General Public License
// along with Blades.  If not, see <http://www.gnu.org/licenses/>

use crate::config::{Config, ASSET_SRC_DIR};
use crate::error::Result;
use crate::page::{Page, Pages};
use crate::taxonomies::{Classification, TaxonList};
use crate::types::{DateTime, MutSet};

use std::fs::{copy, create_dir_all, read_dir, remove_dir_all, remove_file, File};
use std::io::{BufRead, BufReader, BufWriter, ErrorKind, Write};
use std::path::{is_separator, Path, PathBuf};

use ramhorns::{Content, Template};

#[inline]
pub(crate) fn render<P, C>(template: &Template, path: P, content: &C, rendered: &MutSet) -> Result
where
    C: Content,
    P: Into<PathBuf>,
{
    let path = path.into();
    template.render_to_file(&path, content)?;
    if let Some(path) = rendered.lock().replace(path) {
        println!("Warning: more paths render to {}", path.to_string_lossy());
    }
    Ok(())
}

#[derive(Content)]
struct Meta<'p, 'r>(
    #[ramhorns(rename = "date")] DateTime,
    #[ramhorns(rename = "pages")] Pages<'p, 'r>,
    #[ramhorns(rename = "taxons")] TaxonList<'p, 'r>,
    #[ramhorns(rename = "site")] &'r Config<'p>,
);

impl<'p> Meta<'p, '_> {
    #[inline]
    fn render(&self, name: &str, template: &str, path: &Path, rendered: &MutSet) -> Result {
        render(&Template::new(template)?, path.join(name), self, rendered)
    }
}

/// Render sitemap, Atom and RSS feeds if enabled in the config.
pub fn render_meta<'p>(
    pages: &[Page<'p>],
    taxons: &Classification<'p, '_>,
    config: &Config<'p>,
    rendered: &MutSet,
) -> Result {
    let pages = Pages::new(pages, 0..pages.len(), 0, &config.url);
    let meta = Meta(DateTime::now(), pages, TaxonList(taxons), config);
    let path = Path::new(config.output_dir.as_ref());

    if config.sitemap {
        let sitemap = include_str!("templates/sitemap.xml");
        meta.render("sitemap.xml", sitemap, path, rendered)?;
    }
    if config.rss {
        let rss = include_str!("templates/rss.xml");
        meta.render("rss.xml", rss, path, rendered)?;
    }
    if config.atom {
        let atom = include_str!("templates/atom.xml");
        meta.render("atom.xml", atom, path, rendered)?;
    }
    Ok(())
}

fn copy_dir(src: &mut PathBuf, dest: &mut PathBuf) -> Result {
    let iter = match read_dir(&src) {
        Ok(iter) => iter,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(()),
        Err(e) => return Err(e.into()),
    };
    create_dir_all(&dest)?;
    for entry in iter.filter_map(Result::ok) {
        let file_type = entry.file_type()?;
        let file_name = entry.file_name();
        src.push(&file_name);
        dest.push(&file_name);
        if file_type.is_file() {
            copy(&src, &dest)?;
        } else if file_type.is_dir() {
            copy_dir(src, dest)?;
        }
        src.pop();
        dest.pop();
    }
    Ok(())
}

/// Place assets located in the `assets` directory or in the `assets` subdirectory of the theme,
/// if used, into a dedicated subdirectory of the output directory specified in the config
/// (defaults to `assets`, too).
pub fn colocate_assets(config: &Config) -> Result {
    let mut output = Path::new(config.output_dir.as_ref()).join(config.assets.as_ref());
    match remove_dir_all(&output) {
        Ok(_) => Ok(()),
        Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
        Err(e) => Err(e),
    }?;
    let mut src = PathBuf::with_capacity(64);
    if !config.theme.is_empty() {
        src.push(config.theme_dir.as_ref());
        src.push(config.theme.as_ref());
        src.push(ASSET_SRC_DIR);
        copy_dir(&mut src, &mut output)?;
        src.clear();
    }
    src.push(ASSET_SRC_DIR);
    copy_dir(&mut src, &mut output)
}

/// Delete all the pages that were present in the previous render, but not the current one.
/// Then, write all the paths that were rendered to the file `filelist`
pub fn cleanup(rendered: MutSet, filelist: &str) -> Result {
    let rendered = rendered.into_inner();
    if let Ok(f) = File::open(filelist) {
        BufReader::new(f).lines().try_for_each(|filename| {
            let filename = filename?;
            if !rendered.contains(Path::new(&filename)) {
                // Every directory has its index rendered
                if let Some(dir) = filename.strip_suffix("index.html") {
                    if dir.ends_with(is_separator) {
                        return match remove_dir_all(dir) {
                            Ok(_) => Ok(()),
                            Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
                            Err(e) => Err(e),
                        };
                    }
                }
                match remove_file(&filename) {
                    Ok(_) => Ok(()),
                    Err(e) if e.kind() == ErrorKind::NotFound => Ok(()),
                    Err(e) => Err(e),
                }
            } else {
                Ok(())
            }
        })?;
    };

    let f = File::create(filelist)?;
    let mut f = BufWriter::new(f);
    for path in rendered {
        // It was already checked that the paths contain valid UTF-8
        writeln!(&mut f, "{}", path.into_os_string().into_string().unwrap())?;
    }

    Ok(())
}