summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <j.wernick@eyeo.com>2021-06-23 22:25:56 +0200
committerJustin Wernick <j.wernick@eyeo.com>2021-06-23 22:25:56 +0200
commita7a313d047d792eaed02d837e0bca3bbfd95fb77 (patch)
treee3dd5671feb2b7ec36d7ac1c6bae54667ec15540
parent567623ffcc25f6f1ac0df045832cd085e970d6b2 (diff)
Fix syntax highlighting
The size is huge with syntax highlighting
-rw-r--r--Cargo.toml4
-rw-r--r--src/lib.rs38
-rw-r--r--src/links.rs5
3 files changed, 26 insertions, 21 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 4ae1cc1..502b4a1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,7 +15,7 @@ wasm-bindgen-test = "0.3.18"
seed = "0.8.0"
web-sys = { version = "0.3.47", features = ["Range"] }
syntect = { version = "4.5", default-features = false, features = ["default-fancy"], optional = true}
-lazy_static = "1.4.0"
+lazy_static = { version = "1.4", optional = true }
structform = "0.1.0"
derive_more = "0.99"
@@ -29,4 +29,4 @@ wasm-opt = ['-Os']
[features]
default = []
-highlighting = ["syntect"] \ No newline at end of file
+highlighting = ["syntect", "lazy_static"] \ No newline at end of file
diff --git a/src/lib.rs b/src/lib.rs
index adf9b3e..d20deb1 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -44,6 +44,7 @@ struct ShareLinksOptionsForm {
telegram: FormCheckboxInput<bool>,
}
+#[derive(Debug)]
pub enum Msg {
Input(ShareLinksOptionsFormField, String),
ClickCode,
@@ -83,10 +84,12 @@ fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
}
#[cfg(feature = "highlighting")]
-fn codify(node: &Node<Msg>) -> Node<Msg> {
+fn codify(links: &[Node<Msg>]) -> Node<Msg> {
use lazy_static::lazy_static;
use syntect::highlighting::{Theme, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet};
+ use syntect::easy::HighlightLines;
+ use syntect::html::{IncludeBackground, start_highlighted_html_snippet, append_highlighted_html_for_styled_line};
lazy_static! {
static ref SYNTAX_SET: SyntaxSet = SyntaxSet::load_defaults_newlines();
@@ -97,15 +100,20 @@ fn codify(node: &Node<Msg>) -> Node<Msg> {
ts.themes["base16-eighties.dark"].clone()
};
}
- Node::from_html(&syntect::html::highlighted_html_for_string(
- &node.to_string(),
- &SYNTAX_SET,
- &HTML_SYNTAX,
- &THEME,
- ))
- .into_iter()
- .next()
- .unwrap()
+
+ let mut highlighter = HighlightLines::new(&HTML_SYNTAX, &THEME);
+ let (mut output, bg) = start_highlighted_html_snippet(&THEME);
+
+ for link in links {
+ let line = format!("{}\n", link);
+ let regions = highlighter.highlight(&line, &SYNTAX_SET);
+ append_highlighted_html_for_styled_line(&regions[..], IncludeBackground::IfDifferent(bg), &mut output);
+ }
+ output.push_str("</pre>\n");
+ Node::from_html(&output)
+ .into_iter()
+ .next()
+ .unwrap()
}
#[cfg(not(feature = "highlighting"))]
@@ -134,15 +142,15 @@ fn view(model: &Model) -> Node<Msg> {
select![
attrs! {
At::Name => "template_engine",
- At::Value => model.form.template_engine.input
},
- TemplatingEngine::all_str().into_iter().map(|engine| {
+ TemplatingEngine::all().into_iter().map(|engine| {
+ let engine_str = engine.to_string();
option![
attrs! {
- At::Value => &engine,
- At::Selected => (model.form.template_engine.input == engine).as_at_value()
+ At::Value => &engine_str,
+ At::Selected => model.form.template_engine.value.as_ref().map_or(false, |e| e == &engine).as_at_value()
},
- &engine
+ &engine_str
]
}),
diff --git a/src/links.rs b/src/links.rs
index ae1547f..9e0a922 100644
--- a/src/links.rs
+++ b/src/links.rs
@@ -5,7 +5,7 @@ use std::str::FromStr;
pub const SMALL_SHARING_FONT: &'static str = "bold 11px Helvetica, Arial, sans-serif";
pub const LINK_REL: &'static str = "nofollow noopener noreferrer";
-#[derive(Debug, Display, Clone)]
+#[derive(Debug, Display, Clone, PartialEq, Eq)]
pub enum TemplatingEngine {
None,
Jekyll,
@@ -40,9 +40,6 @@ impl TemplatingEngine {
pub fn all() -> Vec<TemplatingEngine> {
vec![Self::None, Self::Jekyll]
}
- pub fn all_str() -> Vec<String> {
- Self::all().iter().map(ToString::to_string).collect()
- }
pub fn encode_url(&self, url: &str) -> String {
match self {