Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w4/b2960e19784a42e2bebc3aa40e464b16 to your computer and use it in GitHub Desktop.
Save w4/b2960e19784a42e2bebc3aa40e464b16 to your computer and use it in GitHub Desktop.
From a5f4c545e231d76226497b938baf0b9d7747584b Mon Sep 17 00:00:00 2001
From: "jordan@doyle.la" <jordan@doyle.la>
Date: Fri, 7 Aug 2020 17:21:17 +0100
Subject: [PATCH] Use CSS styles for syntax highlighting
---
Cargo.lock | 6 +++---
Cargo.toml | 2 +-
src/highlight.rs | 26 +++++++++++++-------------
src/main.rs | 9 ++++++++-
templates/base.html | 2 ++
5 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 3832a1e..b4daa66 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -152,7 +152,7 @@ dependencies = [
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket 0.5.0-dev (git+https://github.com/SergioBenitez/Rocket?branch=master)",
"serde_derive 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
- "syntect 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syntect 4.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
@@ -1316,7 +1316,7 @@ dependencies = [
[[package]]
name = "syntect"
-version = "4.2.0"
+version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bincode 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1783,7 +1783,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee"
"checksum subtle 2.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "502d53007c02d7605a05df1c1a73ee436952781653da5d0bf57ad608f66932c1"
"checksum syn 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)" = "fb7f4c519df8c117855e19dd8cc851e89eb746fe7a73f0157e0d95fdec5369b0"
-"checksum syntect 4.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "83b43a6ca1829ccb0c933b615c9ea83ffc8793ae240cecbd15119b13d741161d"
+"checksum syntect 4.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b57a45fdcf4891bc79f635be5c559210a4cfa464891f969724944c713282eedb"
"checksum time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
"checksum time 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)" = "3a51cadc5b1eec673a685ff7c33192ff7b7603d0b75446fb354939ee615acb15"
"checksum time-macros 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9ae9b6e9f095bc105e183e3cd493d72579be3181ad4004fceb01adbe9eecab2d"
diff --git a/Cargo.toml b/Cargo.toml
index c8336d6..5afe479 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,7 +15,7 @@ askama = "0.9"
lazy_static = "1.4"
rand = { version = "0.7", features = ["nightly"] }
gpw = "0.1"
-syntect = "4.1"
+syntect = "4.3"
serde_derive = "1.0"
tokio = { version = "0.2", features = ["sync", "macros"] }
async-trait = "0.1"
diff --git a/src/highlight.rs b/src/highlight.rs
index 0dc3cba..f5d51bf 100644
--- a/src/highlight.rs
+++ b/src/highlight.rs
@@ -1,26 +1,26 @@
extern crate syntect;
-use syntect::easy::HighlightLines;
use syntect::highlighting::ThemeSet;
-use syntect::html::{styled_line_to_highlighted_html, IncludeBackground};
+use syntect::html::{ClassStyle, ClassedHTMLGenerator};
use syntect::parsing::SyntaxSet;
+lazy_static! {
+ pub static ref SS: SyntaxSet = SyntaxSet::load_defaults_nonewlines();
+ pub static ref TS: ThemeSet = ThemeSet::load_defaults();
+}
+
+
/// Takes the content of a paste and the extension passed in by the viewer and will return the content
/// highlighted in the appropriate format in HTML.
///
/// Returns `None` if the extension isn't supported.
pub fn highlight(content: &str, ext: &str) -> Option<String> {
- lazy_static! {
- static ref SS: SyntaxSet = SyntaxSet::load_defaults_newlines();
- static ref TS: ThemeSet = ThemeSet::load_defaults();
- }
-
let syntax = SS.find_syntax_by_extension(ext)?;
- let mut h = HighlightLines::new(syntax, &TS.themes["base16-ocean.dark"]);
- let regions = h.highlight(content, &SS);
- Some(styled_line_to_highlighted_html(
- &regions[..],
- IncludeBackground::No,
- ))
+ let mut html_generator =
+ ClassedHTMLGenerator::new_with_class_style(&syntax, &SS, ClassStyle::Spaced);
+ for line in content.lines() {
+ html_generator.parse_html_for_line(&line);
+ }
+ Some(html_generator.finalize())
}
diff --git a/src/main.rs b/src/main.rs
index 4522fd0..0706862 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -127,10 +127,17 @@ async fn show_paste(key: String, plaintext: IsPlaintextRequest) -> Result<Conten
}
}
+#[get("/highlight.css")]
+async fn highlight_css() -> Content<String> {
+ use syntect::html::{css_for_theme_with_class_style, ClassStyle};
+ let styles = css_for_theme_with_class_style(&highlight::TS.themes["base16-ocean.dark"], ClassStyle::Spaced);
+ Content(ContentType::CSS, styles)
+}
+
#[tokio::main]
async fn main() {
let result = rocket::ignite()
- .mount("/", routes![index, submit, submit_raw, show_paste])
+ .mount("/", routes![index, submit, submit_raw, show_paste, highlight_css])
.launch()
.await;
diff --git a/templates/base.html b/templates/base.html
index 31a1a17..0e77bb1 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -27,6 +27,8 @@
{% block styles %}
{% endblock styles %}
</style>
+
+ <link rel="stylesheet" type="text/css" href="/highlight.css">
</head>
<body>{% block content %}{% endblock content %}</body>
</html>
--
2.27.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment