Add html character escape codes

feature/epub-rendering
trivernis 4 years ago
parent b835f900eb
commit 4d27e054da

2
Cargo.lock generated

@ -1117,7 +1117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "snekdown" name = "snekdown"
version = "0.21.3" version = "0.22.0"
dependencies = [ dependencies = [
"asciimath-rs 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "asciimath-rs 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)",

@ -1,6 +1,6 @@
[package] [package]
name = "snekdown" name = "snekdown"
version = "0.21.3" version = "0.22.0"
authors = ["trivernis <trivernis@protonmail.com>"] authors = ["trivernis <trivernis@protonmail.com>"]
edition = "2018" edition = "2018"
license-file = "LICENSE" license-file = "LICENSE"

@ -173,6 +173,7 @@ pub enum Inline {
Math(Math), Math(Math),
BibReference(Arc<RwLock<BibReference>>), BibReference(Arc<RwLock<BibReference>>),
TemplateVar(Arc<RwLock<TemplateVariable>>), TemplateVar(Arc<RwLock<TemplateVariable>>),
CharacterCode(CharacterCode),
LineBreak, LineBreak,
} }
@ -268,6 +269,11 @@ pub struct MathBlock {
pub(crate) expression: Expression, pub(crate) expression: Expression,
} }
#[derive(Clone, Debug)]
pub struct CharacterCode {
pub(crate) code: String,
}
// implementations // implementations
impl Document { impl Document {

@ -33,6 +33,7 @@ pub(crate) const L_BRACE: char = '}';
pub(crate) const PERCENT: char = '%'; pub(crate) const PERCENT: char = '%';
pub(crate) const COMMA: char = ','; pub(crate) const COMMA: char = ',';
pub(crate) const MATH: char = '$'; pub(crate) const MATH: char = '$';
pub(crate) const AMPERSAND: char = '&';
// aliases // aliases
@ -75,6 +76,9 @@ pub(crate) const EMOJI: char = COLON;
pub(crate) const MATH_INLINE: &'static [char] = &[MATH, MATH]; pub(crate) const MATH_INLINE: &'static [char] = &[MATH, MATH];
pub(crate) const BOLD: [char; 2] = [ASTERISK, ASTERISK]; pub(crate) const BOLD: [char; 2] = [ASTERISK, ASTERISK];
pub(crate) const CHARACTER_START: char = AMPERSAND;
pub(crate) const CHARACTER_STOP: char = SEMICOLON;
// groups // groups
pub(crate) const QUOTES: [char; 2] = [SINGLE_QUOTE, DOUBLE_QUOTE]; pub(crate) const QUOTES: [char; 2] = [SINGLE_QUOTE, DOUBLE_QUOTE];

@ -68,6 +68,7 @@ impl ToHtml for Inline {
Inline::TemplateVar(var) => var.read().unwrap().to_html(), Inline::TemplateVar(var) => var.read().unwrap().to_html(),
Inline::Math(m) => m.to_html(), Inline::Math(m) => m.to_html(),
Inline::LineBreak => "<br>".to_string(), Inline::LineBreak => "<br>".to_string(),
Inline::CharacterCode(code) => code.to_html(),
} }
} }
} }
@ -608,3 +609,9 @@ impl ToHtml for TemplateVariable {
} }
} }
} }
impl ToHtml for CharacterCode {
fn to_html(&self) -> String {
format!("&{};", encode_minimal(self.code.as_str()))
}
}

@ -31,6 +31,7 @@ pub(crate) trait ParseInline {
fn parse_metadata_pair(&mut self) -> ParseResult<(String, MetadataValue)>; fn parse_metadata_pair(&mut self) -> ParseResult<(String, MetadataValue)>;
fn parse_placeholder(&mut self) -> ParseResult<Arc<RwLock<Placeholder>>>; fn parse_placeholder(&mut self) -> ParseResult<Arc<RwLock<Placeholder>>>;
fn parse_template(&mut self) -> ParseResult<Template>; fn parse_template(&mut self) -> ParseResult<Template>;
fn parse_character_code(&mut self) -> ParseResult<CharacterCode>;
} }
impl ParseInline for Parser { impl ParseInline for Parser {
@ -87,6 +88,8 @@ impl ParseInline for Parser {
Ok(Inline::BibReference(bibref)) Ok(Inline::BibReference(bibref))
} else if let Ok(math) = self.parse_math() { } else if let Ok(math) = self.parse_math() {
Ok(Inline::Math(math)) Ok(Inline::Math(math))
} else if let Ok(char_code) = self.parse_character_code() {
Ok(Inline::CharacterCode(char_code))
} else { } else {
Ok(Inline::Plain(self.parse_plain()?)) Ok(Inline::Plain(self.parse_plain()?))
} }
@ -507,4 +510,17 @@ impl ParseInline for Parser {
variables: vars, variables: vars,
}) })
} }
/// parses a character code &code; like a html character code
fn parse_character_code(&mut self) -> ParseResult<CharacterCode> {
let start_index = self.ctm.get_index();
self.ctm.assert_char(&CHARACTER_START, None)?;
self.ctm.seek_one()?;
let code =
self.ctm
.get_string_until_any_or_rewind(&[CHARACTER_STOP], &[LB], start_index)?;
self.ctm.seek_one()?;
Ok(CharacterCode { code })
}
} }

Loading…
Cancel
Save