From 19e06488c42433b5ba15bed9c9c83ca4342e7657 Mon Sep 17 00:00:00 2001 From: Nathan McCarty Date: Fri, 7 Feb 2025 05:48:05 -0500 Subject: [PATCH] Functional syntax highlighting for rust --- lib/DB/MarkdownPost.rakumod | 18 ++- lib/Pandoc.rakumod | 2 +- lib/Pygments.rakumod | 40 ++++++ projects/Markdown/RustPosting.md | 206 +++++++++++++++++++++++++++++++ resources/code.css | 1 - resources/colors.css | 34 ++++- resources/main.css | 3 + 7 files changed, 299 insertions(+), 5 deletions(-) create mode 100644 lib/Pygments.rakumod create mode 100644 projects/Markdown/RustPosting.md diff --git a/lib/DB/MarkdownPost.rakumod b/lib/DB/MarkdownPost.rakumod index 0f189d5..bbddc8e 100644 --- a/lib/DB/MarkdownPost.rakumod +++ b/lib/DB/MarkdownPost.rakumod @@ -1,9 +1,13 @@ use v6.e.PREVIEW; use Pandoc; -use JSON::Class:auth; +use Pygments; use DB::Post; + +use JSON::Class:auth; +use File::Temp; + #| A plain markdown post unit class MarkdownPost does Post is json(:pretty); @@ -20,7 +24,17 @@ method title(--> Str:D) { # Simply provide our source file to pandoc method render-html(--> Str:D) { - markdown-to-html $!source + # Test to see if this posts contains any fenced code blocks, if so, + # pygmentize it through a temporary file + my $contents = $!source.slurp; + if $contents ~~ /'```'/ { + my $output = highlight-code $contents; + my ($filename, $filehandle) = tempfile; + $filehandle.spurt: $output, :close; + markdown-to-html $filename.IO + } else { + markdown-to-html $!source + } } # Return our summary, if we have one, otherwise extract the first paragraph of diff --git a/lib/Pandoc.rakumod b/lib/Pandoc.rakumod index 713b376..2b0c91f 100644 --- a/lib/Pandoc.rakumod +++ b/lib/Pandoc.rakumod @@ -6,7 +6,7 @@ use JSON::Fast; #| Run pandoc with the given arguments, dieing on failure sub pandoc(*@args --> Str:D) { # Call into pandoc - my $pandoc = run 'pandoc', @args, :out, :err; + my $pandoc = run 'pandoc', '--no-highlight', @args, :out, :err; # Collect the output my $output = $pandoc.out.slurp: :close; diff --git a/lib/Pygments.rakumod b/lib/Pygments.rakumod new file mode 100644 index 0000000..527012f --- /dev/null +++ b/lib/Pygments.rakumod @@ -0,0 +1,40 @@ +#| Interaction with pygments +unit module Pygments; + +my token fence { '```' } +my token info-string { \w+ } +# TODO: Be more precise about this so we can handle backticks in code blocks +my token code { <-[`]>+ } +my token code-block { + <&fence> \h* \v + + <&fence> +} + +sub pygment(Str:D $code, Str:D $lang --> Str:D) { + my $pygments = run , $lang, :out, :in; + $pygments.in.spurt: $code, :close; + $pygments.out.slurp: :close; +} + +sub highlight-code(Str:D $input --> Str:D) is export { + my $text = $input; + # TODO: Figure out a way to exclude idris code so we can process both in the + # same file + while $text ~~ &code-block { + my $match = $/; + # Extract the match and have pygments colorize the code + my $code = $match.Str; + my $lang = $match.Str; + my $out = pygment $code, $lang; + ## Mangle the html to meet our needs + # Delete the existing div and construct a inside the
+       $out ~~ s:g/'<' \/? 'div' <-[>]>* '>'//;
+       $out ~~ s:g/'
'/
/;
+       $out ~~ s:g/'
'/<\/code><\/pre>/; + # Rename the classes to match our needs + $out ~~ s:g/'span class="' (\w+) '"'/span class="hl-{$/[0].lc}"/; + $text = $match.replace-with: $out; + } + $text +} diff --git a/projects/Markdown/RustPosting.md b/projects/Markdown/RustPosting.md new file mode 100644 index 0000000..e673f02 --- /dev/null +++ b/projects/Markdown/RustPosting.md @@ -0,0 +1,206 @@ +# Rustposting + +Some example code with some potential problem characters: + +```rust +let newline_string = "hello \n world"; +let thing = *newline_string; +``` + +Here is some example rust code: + +```rust +fn main() { + // Statements here are executed when the compiled binary is called. + + // Print text to the console + println!("Hello World!"); +} +``` + +And a slightly less trivial example: + +```rust +fn main() { + // Variables can be type annotated. + let logical: bool = true; + + let a_float: f64 = 1.0; // Regular annotation + let an_integer = 5i32; // Suffix annotation + + // Or a default will be used. + let default_float = 3.0; // + let default_integer = 7; // + + // A type can also be inferred from context. + let mut inferred_type = 12; // Type i64 is inferred from another line. + inferred_type = 4294967296i64; + + // A mutable variable's value can be changed. + let mut mutable = 12; // Mutable + mutable = 21; + + // Error! The type of a variable can't be changed. + mutable = true; + + // Variables can be overwritten with shadowing. + let mutable = true; + + /* Compound types - Array and Tuple */ + + // Array signature consists of Type T and length as [T; length]. + let my_array: [i32; 5] = [1, 2, 3, 4, 5]; + + // Tuple is a collection of values of different types + // and is constructed using parentheses (). + let my_tuple = (5u32, 1u8, true, -5.04f32); +} +``` + +Toss in some type definitions to + +```rust +#[derive(Debug)] +struct Person { + name: String, + age: u8, +} + +// A unit struct +struct Unit; + +// A tuple struct +struct Pair(i32, f32); + +enum WebEvent { + // An variant may either be + PageLoad, + PageUnload, + // like tuple structs, + KeyPress(char), + Paste(String), + // or c-like structures. + Click { x: i64, y: i64 }, +} + +struct Point { + x: f64, + y: f64, +} + +// Implementation block, all associated functions & methods go in here +impl Point { + // This is an "associated function" because this function is associated with + // a particular type, that is, Point. + // + // Associated functions don't need to be called with an instance. + // These functions are generally used like constructors. + fn origin() -> Point { + Point { x: 0.0, y: 0.0 } + } + + // Another associated function, taking two arguments: + fn new(x: f64, y: f64) -> Point { + Point { x: x, y: y } + } +} + +``` + + +Modules and imports + +```rust +#![allow(unused_variables)] + +use deeply::nested::function as other_function; +use std::fs::File; + +fn function() { + println!("called function()"); +} + +mod deeply { + pub mod nested { + pub fn function() { + println!("called deeply::nested::function()"); + } + } +} + +struct Val { + val: f64, +} + +struct GenVal { + gen_val: T, +} + +// impl of Val +impl Val { + fn value(&self) -> &f64 { + &self.val + } +} + +impl GenVal { + fn value(&self) -> &T { + &self.gen_val + } +} + +let a = Box::new(5i32); + +macro_rules! say_hello { + () => { + // The macro will expand into the contents of this block. + println!("Hello!") + }; +} + +macro_rules! calculate { + (eval $e:expr) => { + { + let val: usize = $e; // Force types to be unsigned integers + println!("{} = {}", stringify!{$e}, val); + } + }; +} + +fn give_adult(drink: Option<&str>) { + // Specify a course of action for each case. + match drink { + Some("lemonade") => println!("Yuck! Too sugary."), + Some(inner) => println!("{}? How nice.", inner), + None => println!("No drink? Oh well."), + } +} + +impl Person { + + // Gets the area code of the phone number of the person's job, if it exists. + fn work_phone_area_code(&self) -> Option { + // It would take a lot more code - try writing it yourself and see which + // is easier. + self.job?.phone_number?.area_code + } +} + +#[cfg(target_family = "unix")] +#[link(name = "m")] +extern { + // this is a foreign function + // that computes the square root of a single precision complex number + fn csqrtf(z: Complex) -> Complex; + + fn ccosf(z: Complex) -> Complex; +} + +fn main() { + let raw_p: *const u32 = &10; + + unsafe { + assert!(*raw_p == 10); + } +} +``` diff --git a/resources/code.css b/resources/code.css index b9d9c48..18edce7 100644 --- a/resources/code.css +++ b/resources/code.css @@ -11,7 +11,6 @@ code { /* Styling for fenced code blocks */ pre > code { display: block; - white-space: pre-wrap; padding: 1rem; border-radius: 0.55rem / 0.5rem; word-wrap: normal; diff --git a/resources/colors.css b/resources/colors.css index bfd642f..5974f21 100644 --- a/resources/colors.css +++ b/resources/colors.css @@ -75,7 +75,7 @@ blockquote { background-color: var(--bg-1); } -/* Colorization for code blocks */ +/* Colorization for idris code blocks */ code { color: var(--code-fg-0); background-color: var(--code-bg-0); @@ -102,3 +102,35 @@ code { .hl-data { color: var(--code-red); } + +/* Colorization for pygments code blocks */ +.hl-kd, .hl-k, .hl-kc, .hl-bp { + color: var(--code-green); +} +.hl-n, .hl-nn { + color: var(--code-violet); +} +.hl-s, .hl-se { + color: var(--code-cyan); +} +.hl-nf, .hl-fm { + color: var(--code-blue); +} +.hl-c1, .hl-cm { + color: var(--code-dim-0); +} +.hl-mf, .hl-mi { + color: var(--code-magenta); +} +.hl-kt, .hl-nb, .hl-nc { + color: var(--code-orange); +} +.hl-cp { + color: var(--code-red); +} +.hl-se { + font-style: italic; +} +.hl-fm, .hl-k, .hl-o, .hl-kp { + font-weight: bold; +} diff --git a/resources/main.css b/resources/main.css index a23b136..4b54d5e 100644 --- a/resources/main.css +++ b/resources/main.css @@ -33,6 +33,9 @@ body, .post { align-items: center; gap: var(--box-gap); } +.post { + width: 100%; +} /* Style the site header */ .site-header {