1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-05-24 10:06:05 +02:00

Add input.smart_quotes option

This commit is contained in:
Elisabeth Henry 2016-10-18 00:33:42 +02:00
parent 8bfe644d45
commit 2818a699f7
3 changed files with 40 additions and 7 deletions

View File

@ -1,5 +1,5 @@
use error::{Error, Result, Source};
use cleaner::{Cleaner, French, Off, Default};
use cleaner::{Cleaner, CleanerParams, French, Off, Default};
use bookoptions::BookOptions;
use parser::Parser;
use token::Token;
@ -1052,12 +1052,15 @@ impl Book {
// Update the cleaner according to autoclean and lang options
fn update_cleaner(&mut self) {
let params = CleanerParams {
smart_quotes: self.options.get_bool("input.smart_quotes").unwrap(),
};
if self.options.get_bool("input.autoclean").unwrap() {
let lang = self.options.get_str("lang").unwrap().to_lowercase();
let cleaner: Box<Cleaner> = if lang.starts_with("fr") {
Box::new(French::new())
Box::new(French::new(params))
} else {
Box::new(Default)
Box::new(Default::new(params))
};
self.cleaner = cleaner;
} else {

View File

@ -88,6 +88,7 @@ resources.base_path.templates:path:. # {rs_tmpl}
# {input_opt}
input.autoclean:bool:true # {autoclean}
input.smart_quotes:bool:true # {smart_quotes}
input.yaml_blocks:bool:false # {yaml}
@ -222,6 +223,7 @@ html.crowbook_link:alias # {removed}
rs_tmpl = lformat!("Set base path but only for templates files. Useless if resources.base_path is set."),
autoclean = lformat!("Toggle cleaning of input markdown according to lang"),
smart_quotes = lformat!("If enabled, tries to replace vertical quotations marks to curly ones."),
yaml = lformat!("Enable inline YAML blocks to override options set in config file"),
tmp_dir = lformat!("Path where to create a temporary directory (default: uses result from Rust's std::env::temp_dir())"),
zip = lformat!("Command to use to zip files (for EPUB/ODT)"),

View File

@ -23,6 +23,11 @@ use crowbook_text_processing::clean::typographic_quotes;
use crowbook_text_processing::french::FrenchFormatter;
/// Contains cleaning parameters
pub struct CleanerParams {
pub smart_quotes: bool,
}
/// Trait for cleaning a string.
///
/// This trait must be called for text that is e.g. in a paragraph, a title,
@ -68,11 +73,28 @@ impl Cleaner for Off {}
/// false);
/// assert_eq!(&s, " A string with more whitespaces than needed ");
/// ```
pub struct Default;
pub struct Default {
params: CleanerParams,
}
impl Default {
/// New Default cleaner
pub fn new(params: CleanerParams) -> Default {
Default {
params: params,
}
}
}
impl Cleaner for Default {
/// Remove unnecessary whitespaces
fn clean<'a>(&self, input: Cow<'a, str>, _: bool) -> Cow<'a, str> {
typographic_quotes(remove_whitespaces(input))
let s = remove_whitespaces(input);
if self.params.smart_quotes {
typographic_quotes(s)
} else {
s
}
}
}
@ -92,12 +114,18 @@ impl Cleaner for Default {
/// ```
pub struct French {
formatter: FrenchFormatter,
params: CleanerParams,
}
impl French {
/// Creates a new french cleaner
pub fn new() -> French {
French { formatter: FrenchFormatter::new() }
pub fn new(params: CleanerParams) -> French {
let mut this = French {
formatter: FrenchFormatter::new(),
params: params,
};
this.formatter.typographic_quotes(this.params.smart_quotes);
this
}
}