From b52cbefa908bb90034400117000b6d0190d430e5 Mon Sep 17 00:00:00 2001 From: Elisabeth Henry Date: Tue, 20 Sep 2016 04:19:29 +0200 Subject: [PATCH] Add support for more metadata --- src/lib/book.rs | 11 +++++++---- src/lib/bookoptions.rs | 31 ++++++++++++++++++++++++++++--- src/lib/logger.rs | 4 ++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/lib/book.rs b/src/lib/book.rs index a5996af..d5760cc 100644 --- a/src/lib/book.rs +++ b/src/lib/book.rs @@ -658,10 +658,13 @@ impl Book { "tex" => escape_tex, _ => panic!("get mapbuilder called with invalid escape format") }; - let mut mapbuilder = MapBuilder::new() - .insert_str("author", f(self.options.get_str("author").unwrap())) - .insert_str("title", f(&self.options.get_str("title").unwrap())) - .insert_str("lang", self.options.get_str("lang").unwrap().to_owned()); + let mut mapbuilder = MapBuilder::new(); + for key in self.options.get_metadata() { + if let Ok(s) = self.options.get_str(key) { + mapbuilder = mapbuilder.insert_str(key, f(s)); + mapbuilder = mapbuilder.insert_bool(&format!("has_{}", key), true); + } + } let hash = lang::get_hash(self.options.get_str("lang").unwrap()); for (key, value) in hash.into_iter() { let key = format!("loc_{}", key.as_str().unwrap()); diff --git a/src/lib/bookoptions.rs b/src/lib/bookoptions.rs index 7dc5b92..ce78511 100644 --- a/src/lib/bookoptions.rs +++ b/src/lib/bookoptions.rs @@ -17,6 +17,12 @@ lang:str:en # Language of the book subject:str # Subject of the book (used for EPUB metadata) description:str # Description of the book (used for EPUB metadata) cover:path # Path to the cover of the book + +# Metadata (additional) +license:str # License of the book +version:str # Version of the book +date:str # Date the book was revised + # Output options output.epub:path # Output file name for EPUB rendering output.html:path # Output file name for HTML rendering @@ -99,6 +105,7 @@ pub struct BookOptions { valid_strings: Vec<&'static str>, valid_paths: Vec<&'static str>, valid_ints: Vec<&'static str>, + metadata: Vec<&'static str>, /// Source for errors (unnecessary copy :/) pub source: Source, @@ -118,17 +125,30 @@ impl BookOptions { valid_ints:vec!(), valid_strings:vec!(), valid_paths:vec!(), + metadata: vec!(), root: PathBuf::new(), source: Source::empty(), }; - - for (_, key, option_type, default_value) in Self::options_to_vec() { + + // Load default options and types from OPTIONS + let mut is_metadata = false; + for (comment, key, option_type, default_value) in Self::options_to_vec() { if key.is_none() { + if comment.contains("Metadata") || comment.contains("metadata") { + is_metadata = true; + } else { + is_metadata = false; + } continue; } let key = key.unwrap(); match option_type.unwrap() { - "str" => options.valid_strings.push(key), + "str" => { + if is_metadata { + options.metadata.push(key); + } + options.valid_strings.push(key); + }, "bool" => options.valid_bools.push(key), "int" => options.valid_ints.push(key), "char" => options.valid_chars.push(key), @@ -277,6 +297,11 @@ impl BookOptions { format!("could not parse '{}' as a valid YAML value", value))) } } + + /// Return the list of keys that are metadata + pub fn get_metadata(&self) -> &[&'static str] { + &self.metadata + } /// Gets an option pub fn get(&self, key: &str) -> Result<&BookOption> { diff --git a/src/lib/logger.rs b/src/lib/logger.rs index f9fa469..2d82d56 100644 --- a/src/lib/logger.rs +++ b/src/lib/logger.rs @@ -7,9 +7,9 @@ use std::io::Write; pub enum InfoLevel { /// Debug: the lowest level Debug = 0, - /// Info: won't be displayed by default + /// Warning: won't be displayed by default Warning, - /// Warning: the default level + /// Info: won't be displayed by default Info, /// Error Error