From 4d27e054dad551258ba007c915fa84028cd7b72d Mon Sep 17 00:00:00 2001 From: trivernis Date: Thu, 13 Aug 2020 19:07:43 +0200 Subject: [PATCH] Add html character escape codes --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/elements/mod.rs | 6 ++++++ src/elements/tokens.rs | 4 ++++ src/format/html.rs | 7 +++++++ src/parser/inline.rs | 16 ++++++++++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad58059..c33b2b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1117,7 +1117,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "snekdown" -version = "0.21.3" +version = "0.22.0" dependencies = [ "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)", diff --git a/Cargo.toml b/Cargo.toml index 41e7831..bb70444 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.21.3" +version = "0.22.0" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/src/elements/mod.rs b/src/elements/mod.rs index b333f11..aa86622 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -173,6 +173,7 @@ pub enum Inline { Math(Math), BibReference(Arc>), TemplateVar(Arc>), + CharacterCode(CharacterCode), LineBreak, } @@ -268,6 +269,11 @@ pub struct MathBlock { pub(crate) expression: Expression, } +#[derive(Clone, Debug)] +pub struct CharacterCode { + pub(crate) code: String, +} + // implementations impl Document { diff --git a/src/elements/tokens.rs b/src/elements/tokens.rs index 1b717de..4572326 100644 --- a/src/elements/tokens.rs +++ b/src/elements/tokens.rs @@ -33,6 +33,7 @@ pub(crate) const L_BRACE: char = '}'; pub(crate) const PERCENT: char = '%'; pub(crate) const COMMA: char = ','; pub(crate) const MATH: char = '$'; +pub(crate) const AMPERSAND: char = '&'; // aliases @@ -75,6 +76,9 @@ pub(crate) const EMOJI: char = COLON; pub(crate) const MATH_INLINE: &'static [char] = &[MATH, MATH]; pub(crate) const BOLD: [char; 2] = [ASTERISK, ASTERISK]; +pub(crate) const CHARACTER_START: char = AMPERSAND; +pub(crate) const CHARACTER_STOP: char = SEMICOLON; + // groups pub(crate) const QUOTES: [char; 2] = [SINGLE_QUOTE, DOUBLE_QUOTE]; diff --git a/src/format/html.rs b/src/format/html.rs index 98acc6a..3b94a58 100644 --- a/src/format/html.rs +++ b/src/format/html.rs @@ -68,6 +68,7 @@ impl ToHtml for Inline { Inline::TemplateVar(var) => var.read().unwrap().to_html(), Inline::Math(m) => m.to_html(), Inline::LineBreak => "
".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())) + } +} diff --git a/src/parser/inline.rs b/src/parser/inline.rs index a47fdb2..58ca8ee 100644 --- a/src/parser/inline.rs +++ b/src/parser/inline.rs @@ -31,6 +31,7 @@ pub(crate) trait ParseInline { fn parse_metadata_pair(&mut self) -> ParseResult<(String, MetadataValue)>; fn parse_placeholder(&mut self) -> ParseResult>>; fn parse_template(&mut self) -> ParseResult