From 1a4ec92aff7e5f532652a5a38ac956b59ced3a61 Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 8 Sep 2020 20:43:58 +0200 Subject: [PATCH] Add SmartArrows Signed-off-by: trivernis --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 36 ++++++++++++++++++----- src/elements/mod.rs | 11 +++++++ src/elements/tokens.rs | 18 ++++++++++++ src/format/html/assets/style.css | 6 +++- src/format/html/to_html.rs | 17 +++++++++++ src/parser/inline.rs | 43 +++++++++++++++++++++++++++- src/references/configuration/keys.rs | 1 + src/references/configuration/mod.rs | 8 ++++++ 10 files changed, 133 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 74c7ca4..d405e56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1277,7 +1277,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "snekdown" -version = "0.28.0" +version = "0.29.0" dependencies = [ "asciimath-rs 0.5.7 (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 0543f84..e42eecd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "snekdown" -version = "0.28.0" +version = "0.29.0" authors = ["trivernis "] edition = "2018" license-file = "LICENSE" diff --git a/README.md b/README.md index 7a5a47b..e4b6f14 100644 --- a/README.md +++ b/README.md @@ -148,17 +148,31 @@ Example: ```toml # bibliography.bib.toml -[Meta] author = "Snek" published = "2020" test-key = ["test value", "test value 2"] -[imports] -ignored-imports = ["style.css"] # those files won't get imported -included-stylesheets = ["style2.css"] # stylesheets that should be included -included-configs = [] # other metadata files that should be included -included-bibliography = ["mybib.toml"] # bibliography that should be included -included-glossary = ["myglossary.toml"] #glossary that sould be included +# those files won't get imported +ignored-imports = ["style.css"] + +# stylesheets that should be included +included-stylesheets = ["style2.css"] + +# other metadata files that should be included +included-configs = [] + +# bibliography that should be included +included-bibliography = ["mybib.toml"] + +# glossary that sould be included +included-glossary = ["myglossary.toml"] + +# if external sources (images, stylesheets, MathJax) +# should be embedded into the document (default true) +embed-external = true + +# If SmartArrows should be used (default true) +smart-arrows = true ``` The `[Section]` keys are not relevant as the structure gets flattened before the values are read. @@ -287,6 +301,14 @@ $$$ The expression get's converted into MathML which is then converted by MathJax when loaded in the browser. +## Smart Arrows + +Snekdown automatically renders the sequences `-->`, `==>`, `<--`, `<==`, `<-->`, `<==>` as +their respective unicode arrows (similar to [markdown-it-smartarrows](https://github.com/adam-p/markdown-it-smartarrows)). +This behavior can be turned off by setting the config parameter `smart-arrows` to `false` +(the config needs to be imported before the arrows are used for that to work). + + ## Roadmap The end goal is to have a markup language with features similar to LaTeX. diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 49cb9fe..3aae97c 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -183,6 +183,7 @@ pub enum Inline { TemplateVar(Arc>), CharacterCode(CharacterCode), LineBreak, + Arrow(Arrow), } #[derive(Clone, Debug)] @@ -289,6 +290,16 @@ pub struct CharacterCode { pub(crate) code: String, } +#[derive(Clone, Debug)] +pub enum Arrow { + RightArrow, + LeftArrow, + LeftRightArrow, + BigRightArrow, + BigLeftArrow, + BigLeftRightArrow, +} + // implementations impl Document { diff --git a/src/elements/tokens.rs b/src/elements/tokens.rs index 4625be1..6084fad 100644 --- a/src/elements/tokens.rs +++ b/src/elements/tokens.rs @@ -81,6 +81,15 @@ pub(crate) const CHARACTER_STOP: char = SEMICOLON; pub(crate) const GLOSSARY_REF_START: char = TILDE; +// Arrows + +pub(crate) const A_RIGHT_ARROW: &'static [char] = &['-', '-', '>']; +pub(crate) const A_LEFT_ARROW: &'static [char] = &['<', '-', '-']; +pub(crate) const A_LEFT_RIGHT_ARROW: &'static [char] = &['<', '-', '-', '>']; +pub(crate) const A_BIG_RIGHT_ARROW: &'static [char] = &['=', '=', '>']; +pub(crate) const A_BIG_LEFT_ARROW: &'static [char] = &['<', '=', '=']; +pub(crate) const A_BIG_LEFT_RIGHT_ARROW: &'static [char] = &['<', '=', '=', '>']; + // groups pub(crate) const QUOTES: [char; 2] = [SINGLE_QUOTE, DOUBLE_QUOTE]; @@ -113,6 +122,15 @@ pub(crate) const INLINE_SPECIAL_CHARS: &'static [char] = &[ MATH, ]; +pub(crate) const INLINE_SPECIAL_SEQUENCES: &'static [&'static [char]] = &[ + A_BIG_LEFT_RIGHT_ARROW, + A_BIG_LEFT_ARROW, + A_BIG_RIGHT_ARROW, + A_RIGHT_ARROW, + A_LEFT_ARROW, + A_LEFT_RIGHT_ARROW, +]; + pub(crate) const LIST_SPECIAL_CHARS: [char; 14] = [ MINUS, PLUS, ASTERISK, O, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ]; diff --git a/src/format/html/assets/style.css b/src/format/html/assets/style.css index 4722cca..25b61ff 100644 --- a/src/format/html/assets/style.css +++ b/src/format/html/assets/style.css @@ -49,7 +49,7 @@ code { } code pre { - font-family: "Fira Code", monospace; + font-family: "Fira Code", "Mono", monospace; padding: 0.8em 0.2em; background-color: #EEE !important; border-radius: 0.25em; @@ -133,4 +133,8 @@ blockquote { text-decoration: none; color: inherit; border-bottom: 1px dotted #000; +} + +.arrow { + font-family: "Fira Code", "Mono", monospace; } \ No newline at end of file diff --git a/src/format/html/to_html.rs b/src/format/html/to_html.rs index de4493f..c7f90c0 100644 --- a/src/format/html/to_html.rs +++ b/src/format/html/to_html.rs @@ -65,6 +65,7 @@ impl ToHtml for Inline { Inline::LineBreak => writer.write("
".to_string()), Inline::CharacterCode(code) => code.to_html(writer), Inline::GlossaryReference(gloss) => gloss.lock().unwrap().to_html(writer), + Inline::Arrow(a) => a.to_html(writer), } } } @@ -687,3 +688,19 @@ impl ToHtml for GlossaryReference { Ok(()) } } + +impl ToHtml for Arrow { + fn to_html(&self, writer: &mut HTMLWriter) -> io::Result<()> { + writer.write("".to_string())?; + match self { + Arrow::RightArrow => writer.write("⟶".to_string()), + Arrow::LeftArrow => writer.write("⟵".to_string()), + Arrow::LeftRightArrow => writer.write("⟷".to_string()), + Arrow::BigRightArrow => writer.write("⟹".to_string()), + Arrow::BigLeftArrow => writer.write("⟸".to_string()), + Arrow::BigLeftRightArrow => writer.write("⟺".to_string()), + }?; + + writer.write("".to_string()) + } +} diff --git a/src/parser/inline.rs b/src/parser/inline.rs index 1b0012c..c7da338 100644 --- a/src/parser/inline.rs +++ b/src/parser/inline.rs @@ -3,7 +3,7 @@ use crate::elements::tokens::*; use crate::elements::BibReference; use crate::elements::*; use crate::parser::block::ParseBlock; -use crate::references::configuration::keys::BIB_REF_DISPLAY; +use crate::references::configuration::keys::{BIB_REF_DISPLAY, SMART_ARROWS}; use crate::references::glossary::GlossaryDisplay; use crate::references::glossary::GlossaryReference; use crate::references::templates::{GetTemplateVariables, Template, TemplateVariable}; @@ -36,6 +36,7 @@ pub(crate) trait ParseInline { fn parse_placeholder(&mut self) -> ParseResult>>; fn parse_template(&mut self) -> ParseResult