mirror of
https://github.com/lise-henry/crowbook
synced 2024-09-25 16:10:43 +02:00
Escape nb spaces with html entities when html.escape_nb_spaces is true
This commit is contained in:
parent
d3b4cbbf27
commit
4d1f005247
@ -52,7 +52,7 @@ html.highlight_code:bool:true # Provides syntax highlighting for code bloc
|
||||
html.highlight.js:tpl # Set another highlight.js version than the bundled one
|
||||
html.highlight.css:tpl # Set another highlight.js CSS theme than the default one
|
||||
html.side_notes:bool:false # Display footnotes as side notes in HTML/Epub (experimental)
|
||||
|
||||
html.escape_nb_spaces:bool:true # Replace unicode non breaking spaces with HTML entities and CSS
|
||||
|
||||
# Standalone HTML options
|
||||
html_single.one_chapter:bool:false # Display only one chapter at a time (with a button to display all)
|
||||
|
@ -24,28 +24,22 @@ const NB_CHAR_NARROW:char = '\u{202F}'; // narrow non breaking space
|
||||
const NB_CHAR_EM:char = '\u{2002}'; // demi em space
|
||||
|
||||
|
||||
/// Escape non breaking spaces for HTML, so they are visible.
|
||||
#[doc(hidden)]
|
||||
/// Escape non breaking spaces for HTML, so there is no problem for displaying them if the font or browser
|
||||
/// doesn't know what to do with them
|
||||
pub fn escape_nb_spaces<'a, S: Into<Cow<'a, str>>>(input: S) -> Cow<'a, str> {
|
||||
let input = input.into();
|
||||
if input.contains(|c| match c {
|
||||
if let Some(first) = input.chars().position(|c| match c {
|
||||
NB_CHAR | NB_CHAR_NARROW | NB_CHAR_EM => true,
|
||||
_ => false
|
||||
}) {
|
||||
let mut output = String::with_capacity(input.len());
|
||||
for c in input.chars() {
|
||||
let mut chars = input.chars().collect::<Vec<_>>();
|
||||
let rest = chars.split_off(first);
|
||||
let mut output = chars.into_iter().collect::<String>();
|
||||
for c in rest {
|
||||
match c {
|
||||
NB_CHAR_NARROW
|
||||
| NB_CHAR_EM
|
||||
| NB_CHAR
|
||||
=> output.push_str(&format!("<span style = \"background-color: {}\">{}</span>",
|
||||
match c {
|
||||
NB_CHAR => "#ffff66",
|
||||
NB_CHAR_NARROW => "#9999ff",
|
||||
NB_CHAR_EM => "#ff9999",
|
||||
_ => unreachable!()
|
||||
},
|
||||
c)),
|
||||
NB_CHAR_NARROW => output.push_str(r#"<span class = "nnbsp"> </span>"#),
|
||||
NB_CHAR_EM => output.push_str(r#"<span class = "ensp"> </span>"#),
|
||||
NB_CHAR => output.push_str(r#"<span class = "nbsp"> </span>"#),
|
||||
_ => output.push(c),
|
||||
}
|
||||
}
|
||||
|
@ -390,7 +390,8 @@ impl<'a> HtmlRenderer<'a> {
|
||||
} else {
|
||||
content
|
||||
};
|
||||
if this.as_ref().book.options.get_bool("proofread.nb_spaces").unwrap() {
|
||||
|
||||
if this.as_ref().book.options.get_bool("html.escape_nb_spaces").unwrap() {
|
||||
content = escape_nb_spaces(content);
|
||||
}
|
||||
Ok(content.into_owned())
|
||||
|
@ -279,8 +279,11 @@ impl<'a> HtmlDirRenderer<'a> {
|
||||
let template_css = try!(compile_str(try!(self.html.book.get_template("html.css")).as_ref(),
|
||||
&self.html.book.source,
|
||||
"could not compile template 'html.css"));
|
||||
let data = try!(self.html.book.get_metadata(|s| Ok(s.to_owned())))
|
||||
.build();
|
||||
let mut data = try!(self.html.book.get_metadata(|s| Ok(s.to_owned())));
|
||||
if self.html.book.options.get_bool("proofread.nb_spaces").unwrap() {
|
||||
data = data.insert_bool("display_spaces", true);
|
||||
}
|
||||
let data = data.build();
|
||||
let mut res:Vec<u8> = vec!();
|
||||
template_css.render_data(&mut res, &data);
|
||||
let css = String::from_utf8_lossy(&res);
|
||||
|
@ -163,9 +163,12 @@ impl<'a> HtmlSingleRenderer<'a> {
|
||||
let template_css = try!(compile_str(try!(self.html.book.get_template("html.css")).as_ref(),
|
||||
&self.html.book.source,
|
||||
"could not compile template 'html.css'"));
|
||||
let data = try!(self.html.book.get_metadata(|s| self.render_vec(&try!(Parser::new().parse_inline(s)))))
|
||||
.insert_bool(self.html.book.options.get_str("lang").unwrap(), true)
|
||||
.build();
|
||||
let mut data = try!(self.html.book.get_metadata(|s| self.render_vec(&try!(Parser::new().parse_inline(s)))))
|
||||
.insert_bool(self.html.book.options.get_str("lang").unwrap(), true);
|
||||
if self.html.book.options.get_bool("proofread.nb_spaces").unwrap() {
|
||||
data = data.insert_bool("display_spaces", true);
|
||||
}
|
||||
let data = data.build();
|
||||
let mut res:Vec<u8> = vec!();
|
||||
template_css.render_data(&mut res, &data);
|
||||
let css = String::from_utf8_lossy(&res);
|
||||
|
@ -174,4 +174,21 @@ p.first-para {
|
||||
text-indent: 0;
|
||||
}
|
||||
|
||||
/* Use this for escape narrow space so it is non-breaking */
|
||||
.nnbsp {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
{{#display_spaces}}
|
||||
.nnbsp {
|
||||
background-color: #9999ff;
|
||||
}
|
||||
|
||||
.ensp {
|
||||
background-color: #ff9999;
|
||||
}
|
||||
|
||||
.nbsp {
|
||||
background-color: #ffff66;
|
||||
}
|
||||
{{/display_spaces}}
|
Loading…
Reference in New Issue
Block a user