2017-12-17 13:19:27 +01:00
|
|
|
use comrak;
|
|
|
|
use glob::glob;
|
|
|
|
use inflector::Inflector;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2017-12-23 22:02:46 +01:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2017-12-17 13:19:27 +01:00
|
|
|
pub struct List {
|
2017-12-23 22:02:46 +01:00
|
|
|
pub id: String,
|
2017-12-27 17:32:39 +01:00
|
|
|
pub index: i8,
|
2017-12-23 22:02:46 +01:00
|
|
|
pub data: String,
|
|
|
|
pub name: String,
|
2017-12-17 13:19:27 +01:00
|
|
|
path: PathBuf
|
|
|
|
}
|
|
|
|
|
|
|
|
impl List {
|
2017-12-23 22:03:25 +01:00
|
|
|
pub fn to_html(&self) -> String {
|
|
|
|
comrak::markdown_to_html(&self.data,
|
|
|
|
&comrak::ComrakOptions::default())
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:19:27 +01:00
|
|
|
pub fn load_all() -> Vec<Self> {
|
|
|
|
let mut lists : Vec<List> = vec![];
|
2017-12-27 17:32:39 +01:00
|
|
|
let mut index = 0;
|
2017-12-17 13:19:27 +01:00
|
|
|
for entry in glob("lists/*.list").unwrap().filter_map(Result::ok) {
|
|
|
|
let file_name = entry.file_name().unwrap().to_str().unwrap();
|
|
|
|
let name = match file_name.find('.') {
|
|
|
|
Some(index) => &file_name[0..index],
|
|
|
|
None => "unknown"
|
|
|
|
};
|
|
|
|
let mut data = String::new();
|
|
|
|
let mut file = File::open(&entry)
|
|
|
|
.expect(&format!("Cannot open list file {}", file_name));
|
|
|
|
file.read_to_string(&mut data)
|
|
|
|
.expect(&format!("Cannot read list file {}", file_name));
|
|
|
|
|
|
|
|
let list = List {
|
2017-12-23 22:03:25 +01:00
|
|
|
id: String::from(name),
|
2017-12-27 17:32:39 +01:00
|
|
|
index : index,
|
2017-12-17 13:19:27 +01:00
|
|
|
data: data,
|
|
|
|
name: String::from(name).to_title_case(),
|
|
|
|
path: entry.clone()
|
|
|
|
};
|
|
|
|
lists.push(list);
|
2017-12-27 17:32:39 +01:00
|
|
|
index += 1;
|
2017-12-17 13:19:27 +01:00
|
|
|
}
|
|
|
|
lists
|
|
|
|
}
|
|
|
|
}
|