aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2022-08-30 16:54:57 +0200
committermetamuffin <metamuffin@disroot.org>2022-08-30 16:54:57 +0200
commit802efbca25cb92d8567761361b6513fd57e05578 (patch)
tree62eb0139143cc343d83d92091eee8efaf0888001
parented6186a764701e702032156c9632ac19305652be (diff)
downloadmetamuffin-blog-802efbca25cb92d8567761361b6513fd57e05578.tar
metamuffin-blog-802efbca25cb92d8567761361b6513fd57e05578.tar.bz2
metamuffin-blog-802efbca25cb92d8567761361b6513fd57e05578.tar.zst
basic syntax highlighting
-rw-r--r--code/Cargo.lock17
-rw-r--r--code/Cargo.toml11
-rw-r--r--code/src/main.rs1
-rw-r--r--code/src/markdown.rs10
-rw-r--r--code/src/syntax_highlight/grammar.rs6
-rw-r--r--code/src/syntax_highlight/mod.rs29
-rw-r--r--code/src/syntax_highlight/theme.rs6
-rw-r--r--content/articles/2022-08-29-blog-test.md8
-rw-r--r--content/style.css4
9 files changed, 79 insertions, 13 deletions
diff --git a/code/Cargo.lock b/code/Cargo.lock
index a2766ae..0bf0ca0 100644
--- a/code/Cargo.lock
+++ b/code/Cargo.lock
@@ -49,6 +49,7 @@ dependencies = [
"iso8601",
"laby",
"markdown",
+ "synoptic",
]
[[package]]
@@ -324,6 +325,16 @@ dependencies = [
]
[[package]]
+name = "synoptic"
+version = "1.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e61e0e7c523515593cf1f78e7f3a1d6cd919b79329319b46244f39f4c59b971a"
+dependencies = [
+ "regex",
+ "unicode-width",
+]
+
+[[package]]
name = "termcolor"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -345,6 +356,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4f5b37a154999a8f3f98cc23a628d850e154479cd94decf3414696e12e31aaf"
[[package]]
+name = "unicode-width"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
+
+[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/code/Cargo.toml b/code/Cargo.toml
index 8fd54fb..3547fd4 100644
--- a/code/Cargo.toml
+++ b/code/Cargo.toml
@@ -4,8 +4,9 @@ version = "0.1.0"
edition = "2021"
[dependencies]
-laby = "0.2.4"
-clap = { version = "3.2.17", features = ["derive"] }
-anyhow = "1.0.62"
-markdown = "0.3.0"
-iso8601 = "0.5.0"
+laby = "0.2.4" # html generation macros
+clap = { version = "3.2.17", features = ["derive"] } # bloated argument parser
+anyhow = "1.0.62" # error stuff
+markdown = "0.3.0" # markdown parser
+iso8601 = "0.5.0" # date parsing
+synoptic = "1.2.0" # syntax highlighting
diff --git a/code/src/main.rs b/code/src/main.rs
index 0bd0169..c20d137 100644
--- a/code/src/main.rs
+++ b/code/src/main.rs
@@ -1,6 +1,7 @@
pub mod atom;
pub mod html;
pub mod markdown;
+pub mod syntax_highlight;
use atom::generate_atom;
use clap::{Parser, Subcommand};
diff --git a/code/src/markdown.rs b/code/src/markdown.rs
index 5d98f83..22934c5 100644
--- a/code/src/markdown.rs
+++ b/code/src/markdown.rs
@@ -1,5 +1,6 @@
use markdown::{Block, ListItem, Span};
+use crate::syntax_highlight::syntax_highlight;
pub fn span_to_html(ss: Vec<Span>) -> String {
let mut out = String::new();
@@ -28,8 +29,12 @@ pub fn blocks_to_html(blocks: Vec<Block>) -> String {
}
Block::Paragraph(p) => format!("<p>{}</p>", span_to_html(p)),
Block::Blockquote(q) => format!("<quote>{}</quote>", blocks_to_html(q)),
- Block::CodeBlock(_syntax, content) => {
- format!("<pre>{}</pre>", escape(&content)) // TODO syntax highlighting
+ Block::CodeBlock(syntax, content) => {
+ if let Some(s) = &syntax {
+ format!("<pre>{}</pre>", syntax_highlight(s, &content))
+ } else {
+ format!("<pre>{}</pre>", escape(&content))
+ }
}
Block::OrderedList(els, _) => format!(
"<ol>{}</ol>",
@@ -74,4 +79,3 @@ pub fn escape(text: &str) -> String {
.replace("'", "&#8217;")
.replace("\"", "&quot;")
}
-
diff --git a/code/src/syntax_highlight/grammar.rs b/code/src/syntax_highlight/grammar.rs
new file mode 100644
index 0000000..95417ab
--- /dev/null
+++ b/code/src/syntax_highlight/grammar.rs
@@ -0,0 +1,6 @@
+pub fn grammar_for(syntax: &str) -> &'static [(&'static [&'static str], &'static str)] {
+ match syntax {
+ "rs" => &[(&["fn", "pub", "async"], "keyword")],
+ _ => unreachable!(),
+ }
+}
diff --git a/code/src/syntax_highlight/mod.rs b/code/src/syntax_highlight/mod.rs
new file mode 100644
index 0000000..688b010
--- /dev/null
+++ b/code/src/syntax_highlight/mod.rs
@@ -0,0 +1,29 @@
+pub mod grammar;
+pub mod theme;
+
+use crate::{markdown::escape, syntax_highlight::theme::theme};
+use grammar::grammar_for;
+use synoptic::{Highlighter, Token};
+
+pub fn syntax_highlight(lang: &str, source: &str) -> String {
+ let mut h = Highlighter::new();
+ for (regex, kind) in grammar_for(lang) {
+ h.join(regex, kind).unwrap();
+ }
+ let highlighting = h.run(source);
+ let mut out = String::new();
+
+ for (_c, row) in highlighting.iter().enumerate() {
+ eprintln!("{row:?}");
+ for tok in row {
+ match tok {
+ Token::Start(kind) => out += &format!("<span style=\"color:{}\">", theme(kind)),
+ Token::Text(text) => out += &escape(text),
+ Token::End(_kind) => out += "</span>",
+ }
+ }
+ out += "\n"
+ }
+ eprintln!("{out:?}");
+ out
+}
diff --git a/code/src/syntax_highlight/theme.rs b/code/src/syntax_highlight/theme.rs
new file mode 100644
index 0000000..40434ad
--- /dev/null
+++ b/code/src/syntax_highlight/theme.rs
@@ -0,0 +1,6 @@
+pub fn theme(kind: &str) -> &'static str {
+ match kind {
+ "keyword" => "#9999ff",
+ _ => "#ff00ff",
+ }
+}
diff --git a/content/articles/2022-08-29-blog-test.md b/content/articles/2022-08-29-blog-test.md
index ea4d166..2b65444 100644
--- a/content/articles/2022-08-29-blog-test.md
+++ b/content/articles/2022-08-29-blog-test.md
@@ -10,9 +10,11 @@ _italic_ **bold** `code` [Link](https://metamuffin.org/)
Look at this code here.
-```
-code
-block
+```rs
+fn main() { 1 + 1 }
+pub async fn useless(s: Box<dyn Write>) -> impl Future<usize> {
+ todo!()
+}
```
As you can see, its pointless i.e. no points
diff --git a/content/style.css b/content/style.css
index dbfd2ea..a6f41a2 100644
--- a/content/style.css
+++ b/content/style.css
@@ -20,10 +20,10 @@ h1 {font-size: xx-large }
h2 {font-size: x-large }
h3 {font-size: large }
-p,h1,h2,h3,h4,h5,h6,span,li { font-family: "Ubuntu", sans-serif; }
+p,h1,h2,h3,h4,h5,h6,li { font-family: "Ubuntu", sans-serif; }
h1,h2 { color: #b575ff }
h3 { color: #ff83fd }
-p,span,li { color: white; margin-left: 2em }
+p,li { color: white; margin-left: 2em }
a { color: #82a8ff; font-style: italic; text-decoration: underline }
hr { border: 1px solid grey }