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
use crate::types::EntityMetadata;
use pulldown_cmark::{html, Event, HeadingLevel, Options, Parser, Tag};
use std::error::Error;
use std::fs::Metadata;
use std::io;
use std::path::{Path, PathBuf};
use tokio::fs::{create_dir_all, read_to_string, remove_dir_all, remove_file, ReadDir, read, write};
use walkdir::WalkDir;
pub fn get_files_in_dir_recursive(path: &Path) -> Vec<PathBuf> {
WalkDir::new(&path)
.into_iter()
.filter_map(|e| {
let entry = e.ok()?;
let metadata = entry.metadata().ok()?;
if metadata.is_dir() {
return None;
}
let path = entry.path().strip_prefix(&path).ok()?;
if path == Path::new("") {
return None;
}
Some(path.to_path_buf())
})
.collect::<Vec<_>>()
}
pub async fn get_entries_in_dir(
mut dir: ReadDir,
) -> Result<Vec<(String, Metadata, PathBuf)>, io::Error> {
let mut entries = Vec::new();
while let Some(entry) = dir.next_entry().await? {
let metadata = entry.metadata().await?;
let name = entry.file_name().to_string_lossy().to_string();
entries.push((name, metadata, entry.path()));
}
Ok(entries)
}
pub async fn remove_path(metadata: Metadata, path: PathBuf) -> Result<(), io::Error> {
if metadata.is_file() {
remove_file(path).await
} else {
remove_dir_all(path).await
}
}
pub async fn read_template(name: String, dir: &Path) -> Result<(String, String), io::Error> {
Ok((
name.strip_suffix(".hbs").unwrap_or(&name).to_string(),
read_to_string(dir.join(name))
.await?
.split("\n")
.map(|l| l.trim())
.collect::<Vec<&str>>()
.join("\n"),
))
}
pub async fn copy_file(input_path: PathBuf, output_path: PathBuf) -> Result<(), Box<dyn Error>> {
create_dir_all(output_path.parent().unwrap()).await?;
let contents = read(input_path).await?;
write(output_path, contents).await?;
Ok(())
}
pub fn md_to_html(md_str: &str) -> (Option<EntityMetadata>, String, String) {
let mut options = Options::empty();
options.insert(Options::ENABLE_FOOTNOTES);
options.insert(Options::ENABLE_HEADING_ATTRIBUTES);
options.insert(Options::ENABLE_STRIKETHROUGH);
let mut parser = Parser::new_ext(&md_str, options);
let mut inside_header = false;
let mut title = String::new();
let mut inside_metadata = false;
let mut metadata_str = String::new();
for event in parser {
match event {
Event::Start(Tag::Heading(HeadingLevel::H1, _, _)) => inside_header = true,
Event::Text(text) => {
if inside_header {
title = text.to_string();
break;
}
}
Event::Html(html_text) => {
if !inside_metadata {
if html_text.to_string().trim() == "<!--metadata" {
inside_metadata = true;
}
continue;
}
if html_text.to_string().trim() == "-->" {
inside_metadata = false;
continue;
}
metadata_str.push_str(html_text.to_string().as_ref());
}
_ => (),
};
}
let metadata: Option<EntityMetadata> = toml::from_str(metadata_str.as_ref()).ok();
parser = Parser::new_ext(&md_str, options);
let mut html_str = String::new();
html::push_html(&mut html_str, parser);
(metadata, title, html_str)
}