mdBook/src/book/bookconfig.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

extern crate rustc_serialize;
use self::rustc_serialize::json::Json;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
2015-07-17 00:20:36 +08:00
#[derive(Debug, Clone)]
2015-07-17 00:20:36 +08:00
pub struct BookConfig {
pub title: String,
pub author: String,
2015-07-17 00:20:36 +08:00
dest: PathBuf,
src: PathBuf,
pub indent_spaces: i32,
2015-07-17 00:20:36 +08:00
multilingual: bool,
}
impl BookConfig {
pub fn new() -> Self {
BookConfig {
title: String::new(),
author: String::new(),
2015-07-17 00:20:36 +08:00
dest: PathBuf::from("book"),
src: PathBuf::from("src"),
indent_spaces: 4, // indentation used for SUMMARY.md
2015-07-17 00:20:36 +08:00
multilingual: false,
}
}
pub fn read_config(&mut self, root: &Path) -> &mut Self {
debug!("[fn]: read_config");
// If the file does not exist, return early
let mut config_file = match File::open(root.join("book.json")) {
Ok(f) => f,
Err(_) => {
debug!("[*]: Failed to open {:?}", root.join("book.json"));
return self
},
};
debug!("[*]: Reading config");
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
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
if dest.is_relative() {
let dest = &self.get_src().parent().unwrap().join(&dest);
self.set_dest(dest);
}
}
self
}
pub fn get_dest(&self) -> &Path {
&self.dest
2015-07-17 00:20:36 +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
}
pub fn get_src(&self) -> &Path {
&self.src
2015-07-17 00:20:36 +08:00
}
pub fn set_src(&mut self, src: &Path) -> &mut Self {
self.src = src.to_owned();
self
}
2015-07-17 00:20:36 +08:00
}