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-08-03 20:23:32 +08:00
|
|
|
use utils;
|
|
|
|
|
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-07-17 00:20:36 +08:00
|
|
|
dest: PathBuf,
|
|
|
|
src: PathBuf,
|
2015-07-18 06:04:20 +08:00
|
|
|
indent_spaces: i32,
|
2015-07-17 00:20:36 +08:00
|
|
|
multilingual: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl BookConfig {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
BookConfig {
|
2015-07-19 06:08:38 +08:00
|
|
|
title: String::new(),
|
|
|
|
author: String::new(),
|
2015-07-17 00:20:36 +08:00
|
|
|
dest: PathBuf::from("book"),
|
|
|
|
src: PathBuf::from("src"),
|
2015-07-18 06:04:20 +08:00
|
|
|
indent_spaces: 4,
|
2015-07-17 00:20:36 +08:00
|
|
|
multilingual: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-03 07:37:13 +08:00
|
|
|
pub fn read_config(&mut self) -> &mut Self {
|
|
|
|
|
|
|
|
// If the file does not exist, return early
|
|
|
|
let mut config_file = match File::open(self.src.join("book.json")) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(_) => return self,
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut data = String::new();
|
|
|
|
config_file.read_to_string(&mut data).unwrap();
|
|
|
|
|
|
|
|
// Convert to JSON
|
|
|
|
let config = Json::from_str(&data).unwrap();
|
|
|
|
|
|
|
|
// Extract data
|
2015-08-03 20:23:32 +08:00
|
|
|
|
|
|
|
// Title & author
|
2015-08-03 07:37:13 +08:00
|
|
|
if let Some(a) = config.find_path(&["title"]) { self.title = a.to_string().replace("\"", "") }
|
2015-08-03 20:23:32 +08:00
|
|
|
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
|
|
|
|
if dest.is_relative() {
|
|
|
|
let dest = &self.src().parent().unwrap().join(&dest);
|
|
|
|
self.set_dest(dest);
|
|
|
|
}
|
|
|
|
}
|
2015-08-03 07:37:13 +08:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2015-07-19 06:08:38 +08:00
|
|
|
pub fn dest(&self) -> &Path {
|
|
|
|
&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-07-19 06:08:38 +08:00
|
|
|
pub fn src(&self) -> &Path {
|
|
|
|
&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
|
|
|
}
|