2015-08-03 07:37:13 +08:00
|
|
|
extern crate rustc_serialize;
|
|
|
|
use self::rustc_serialize::json::Json;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io::Read;
|
2015-07-19 06:08:38 +08:00
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-17 00:20:36 +08:00
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
#[derive(Debug, Clone)]
|
2015-07-17 00:20:36 +08:00
|
|
|
pub struct BookConfig {
|
2015-07-29 06:57:47 +08:00
|
|
|
pub title: String,
|
|
|
|
pub author: String,
|
2015-08-12 04:55:51 +08:00
|
|
|
root: PathBuf,
|
2015-07-17 00:20:36 +08:00
|
|
|
dest: PathBuf,
|
|
|
|
src: PathBuf,
|
2015-08-06 04:35:26 +08:00
|
|
|
pub indent_spaces: i32,
|
2015-07-17 00:20:36 +08:00
|
|
|
multilingual: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl BookConfig {
|
2015-08-12 04:55:51 +08:00
|
|
|
pub fn new(root: &Path) -> Self {
|
2015-07-17 00:20:36 +08:00
|
|
|
BookConfig {
|
2015-07-19 06:08:38 +08:00
|
|
|
title: String::new(),
|
|
|
|
author: String::new(),
|
2015-08-12 04:55:51 +08:00
|
|
|
root: root.to_owned(),
|
2015-07-17 00:20:36 +08:00
|
|
|
dest: PathBuf::from("book"),
|
|
|
|
src: PathBuf::from("src"),
|
2015-08-11 22:13:41 +08:00
|
|
|
indent_spaces: 4, // indentation used for SUMMARY.md
|
2015-07-17 00:20:36 +08:00
|
|
|
multilingual: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-04 07:25:41 +08:00
|
|
|
pub fn read_config(&mut self, root: &Path) -> &mut Self {
|
2015-08-03 07:37:13 +08:00
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[fn]: read_config");
|
|
|
|
|
2015-08-03 07:37:13 +08:00
|
|
|
// If the file does not exist, return early
|
2015-08-04 07:25:41 +08:00
|
|
|
let mut config_file = match File::open(root.join("book.json")) {
|
2015-08-03 07:37:13 +08:00
|
|
|
Ok(f) => f,
|
2015-08-04 00:06:01 +08:00
|
|
|
Err(_) => {
|
2015-08-04 07:25:41 +08:00
|
|
|
debug!("[*]: Failed to open {:?}", root.join("book.json"));
|
2015-08-04 00:06:01 +08:00
|
|
|
return self
|
|
|
|
},
|
2015-08-03 07:37:13 +08:00
|
|
|
};
|
|
|
|
|
2015-08-04 00:06:01 +08:00
|
|
|
debug!("[*]: Reading config");
|
2015-08-03 07:37:13 +08:00
|
|
|
let mut data = String::new();
|
|
|
|
|
2015-08-12 04:55:51 +08:00
|
|
|
// Just return if an error occured.
|
|
|
|
// I would like to propagate the error, but I have to return `&self`
|
|
|
|
match config_file.read_to_string(&mut data) {
|
|
|
|
Err(_) => return self,
|
|
|
|
_ => {},
|
|
|
|
}
|
2015-08-03 20:23:32 +08:00
|
|
|
|
2015-08-12 04:55:51 +08:00
|
|
|
// Convert to JSON
|
|
|
|
if let Ok(config) = Json::from_str(&data) {
|
|
|
|
// Extract data
|
|
|
|
|
|
|
|
debug!("[*]: Extracting data from config");
|
|
|
|
// Title & author
|
|
|
|
if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") }
|
|
|
|
if let Some(a) = config.find_path(&["author"]) { self.author = a.to_string().replace("\"", "") }
|
|
|
|
|
|
|
|
// Destination
|
|
|
|
if let Some(a) = config.find_path(&["dest"]) {
|
|
|
|
let dest = PathBuf::from(&a.to_string().replace("\"", ""));
|
|
|
|
|
|
|
|
// If path is relative make it absolute from the parent directory of src
|
|
|
|
match dest.is_relative() {
|
|
|
|
true => {
|
|
|
|
let dest = self.get_root().join(&dest).to_owned();
|
|
|
|
self.set_dest(&dest);
|
|
|
|
},
|
|
|
|
false => { self.set_dest(&dest); },
|
|
|
|
}
|
2015-08-03 20:23:32 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-03 07:37:13 +08:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-08-12 04:55:51 +08:00
|
|
|
pub fn get_root(&self) -> &Path {
|
|
|
|
&self.root
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_root(&mut self, root: &Path) -> &mut Self {
|
|
|
|
self.root = root.to_owned();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:13:41 +08:00
|
|
|
pub fn get_dest(&self) -> &Path {
|
2015-07-19 06:08:38 +08:00
|
|
|
&self.dest
|
2015-07-17 00:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
pub fn set_dest(&mut self, dest: &Path) -> &mut Self {
|
|
|
|
self.dest = dest.to_owned();
|
2015-07-17 00:20:36 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-08-11 22:13:41 +08:00
|
|
|
pub fn get_src(&self) -> &Path {
|
2015-07-19 06:08:38 +08:00
|
|
|
&self.src
|
2015-07-17 00:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
pub fn set_src(&mut self, src: &Path) -> &mut Self {
|
|
|
|
self.src = src.to_owned();
|
|
|
|
self
|
|
|
|
}
|
2015-08-03 07:37:13 +08:00
|
|
|
|
2015-07-17 00:20:36 +08:00
|
|
|
}
|