1
0
mirror of https://github.com/lise-henry/crowbook synced 2024-11-10 05:01:40 +01:00

Make Cleaner trait use Cow

This commit is contained in:
Elisabeth Henry 2016-09-19 21:26:50 +02:00
parent c1f39d1b20
commit 474992b09e
4 changed files with 22 additions and 19 deletions

@ -561,9 +561,8 @@ impl Book {
/// Either clean a string or does nothing,
/// according to book `lang` and `autoclean` options
pub fn clean(&self, mut text:String, tex: bool) -> String {
self.cleaner.clean(&mut text, tex);
text
pub fn clean<'s, S: Into<Cow<'s, str>>>(&self, text: S, tex: bool) -> Cow<'s, str> {
self.cleaner.clean(text.into(), tex)
}

@ -17,6 +17,7 @@
//! This module contains the `Cleaner` traits and various implementations of it.
use std::borrow::Cow;
/// Custom function because we don't really want to touch \t or \n
fn is_whitespace(c: char) -> bool {
@ -34,7 +35,9 @@ pub trait Cleaner: Sync {
///
/// * `str`: the string that must be cleaned
/// * `latex`: a bool specifying whether output is Latex code or not
fn clean(&self, _str: &mut String, _latex: bool) {}
fn clean<'a>(&self, str: Cow<'a, str>, _latex: bool) -> Cow<'a, str> {
str
}
}
/// Cleaner implementation that does nothing
@ -69,7 +72,7 @@ impl Cleaner for Off {}
pub struct Default;
impl Cleaner for Default {
/// Remove unnecessary whitespaces
fn clean(&self, s: &mut String, _: bool) {
fn clean<'a>(&self, s: Cow<'a, str>, _: bool) -> Cow<'a, str> {
if s.contains(is_whitespace) { // if not, no need to do anything
let mut new_s = String::with_capacity(s.len());
let mut previous_space = false;
@ -87,7 +90,9 @@ impl Cleaner for Default {
}
}
*s = new_s
Cow::Owned(new_s)
} else {
s
}
}
}
@ -110,14 +115,16 @@ pub struct French;
impl Cleaner for French {
/// Puts non breaking spaces before/after `:`, `;`, `?`, `!`, `«`, `»`, `—`
fn clean(&self, s: &mut String, latex: bool) {
fn clean<'a>(&self, s: Cow<'a, str>, latex: bool) -> Cow<'a, str> {
fn is_trouble(c: char) -> bool {
match c {
'?'|'!'|';'|':'|'»'|'«'|'—' => true,
_ => false
}
}
if !s.contains(is_trouble) { // if not, no need to do anything
return Default.clean(s, latex);
}
let nb_char = if latex {
'~'
} else {
@ -134,11 +141,7 @@ impl Cleaner for French {
'\u{2002}' // demi em space
};
if !s.contains(is_trouble) { // if not, no need to do anything
return;
}
Default.clean(s, latex); // first pass with default impl
let s = Default.clean(s, latex); // first pass with default impl
let mut new_s = String::with_capacity(s.len());
{
let mut chars = s.chars();
@ -182,7 +185,7 @@ impl Cleaner for French {
}
}
*s = new_s
Cow::Owned(new_s)
}
}

@ -222,7 +222,7 @@ impl<'a> HtmlRenderer<'a> {
let content = if this.as_ref().verbatim {
escape_html(text.as_ref())
} else {
escape_html(this.as_ref().book.clean(text.clone(), false))
escape_html(this.as_ref().book.clean(text.as_ref(), false))
};
if this.as_ref().first_letter {
this.as_mut().first_letter = false;

@ -27,6 +27,7 @@ use renderer::Renderer;
use std::iter::Iterator;
use std::fs::File;
use std::io::Read;
use std::borrow::Cow;
use mustache;
@ -182,9 +183,9 @@ impl<'a> Renderer for LatexRenderer<'a> {
match *token {
Token::Str(ref text) => {
let content = if self.escape {
self.book.clean(escape_tex(text.as_ref()).into_owned(), true)
self.book.clean(escape_tex(text.as_ref()), true)
} else {
text.clone()
Cow::Borrowed(text.as_ref())
};
if self.first_letter {
self.first_letter = false;
@ -216,10 +217,10 @@ impl<'a> Renderer for LatexRenderer<'a> {
Ok(format!("{}{}{}", initial, first_word, rest))
}
} else {
Ok(content)
Ok(content.into_owned())
}
} else {
Ok(content)
Ok(content.into_owned())
}
}
Token::Paragraph(ref vec) => {