Basic syntax highlighting

This commit is contained in:
Nathan McCarty 2025-02-04 03:10:40 -05:00
parent 2c38910ccd
commit 37e92392ca
4 changed files with 87 additions and 3 deletions

View file

@ -5,13 +5,17 @@ use DB::BlogMeta;
unit class Config; unit class Config;
method generate-post(Str:D $title, Str:D $content, BlogMeta:D $meta) { method generate-post(Str:D $title, Str:D $content, BlogMeta:D $meta) {
my $head = my $head = head [
head [
meta :charset<utf-8>; meta :charset<utf-8>;
meta :name<viewport>, :content<width=device-width, initial-scale=1>; meta :name<viewport>, :content<width=device-width, initial-scale=1>;
title "{$meta.title} $title"; title "{$meta.title} $title";
# TODO: Add style sheets # TODO: Add style sheets
# Use Iosevka Alie as the monospace font
link :rel<stylesheet>,
:href<https://static.stranger.systems/fonts/IosevkaAlie/IosevkaAile.css>;
style %?RESOURCES<code.css>.slurp;
# TODO: Add description # TODO: Add description
# TODO: Add header links # TODO: Add header links
]; ];

View file

@ -60,11 +60,24 @@ sub pack(*@args) {
# Run a katla command, erroring on failure, returning the standard out, and # Run a katla command, erroring on failure, returning the standard out, and
# mangling the output accordingly # mangling the output accordingly
sub katla(*@args --> Str:D) { sub katla(*@args --> Str:D) {
# Run the requested katla command
my $katla = run 'katla', @args, :out, :err; my $katla = run 'katla', @args, :out, :err;
my $katla-out = $katla.out.slurp: :close; my $katla-out = $katla.out.slurp: :close;
my $katla-err = $katla.err.slurp: :close; my $katla-err = $katla.err.slurp: :close;
die "Katla exited with {$katla.exitcode}\nout: $katla-out\nerr: $katla-err" die "Katla exited with {$katla.exitcode}\nout: $katla-out\nerr: $katla-err"
unless $katla; unless $katla;
# TODO modify output # Modify the output to use our styling instead of katla's
# Remove the style block and line breaks
$katla-out ~~ s:g/'<style>' .* '</style>'//;
$katla-out ~~ s:g/'<br />'//;
# Use <pre> blocks for the code and inject our own class for idris code
$katla-out ~~ s:g/'<code class="' \w+ '">' \s*/<pre><code class="idris-code">/;
$katla-out ~~ s:g/\s* '</code>'/<\/code><\/pre>/;
# Replace the highlighting classes with our own
$katla-out ~~ s:g/'class="Idris' (\w+) '"'/class="hl-{$/[0].lc}"/;
# Clean up inappropiate escaping of some characters
$katla-out ~~ s:g/'\\*'/*/;
$katla-out ~~ s:g/'\\_'/_/;
$katla-out ~~ s:g/'\\\\'/\\/;
$katla-out $katla-out
} }

View file

@ -5,3 +5,27 @@ module Posts.HelloWorld
%default total %default total
``` ```
Here is an example function that prints to the standard out:
```idris
main : IO ()
main = do
putStrLn "Hello World"
putStrLn "Hello \n with new lines \n world"
putStrLn "And some other potential problem characters: * _"
```
And a function with some more stuff:
```idris
-- A regular comment
||| A doc comment
thingomizer : (a : String) -> IO ()
thingomizer a =
let xs = map (+ 1) [1, 2, 3, 4]
in do
putStrLn a
printLn xs
```

43
resources/code.css Normal file
View file

@ -0,0 +1,43 @@
/* TODO: Move this somewhere else */
:root {
color-scheme: light dark;
}
code {
font-family: "Iosevka Aile Web", monospace;
background-color: light-dark(#fbf3db, #103c48);
color: light-dark(#53676d, #adbcbc);
}
pre > code {
display: block;
padding: 1rem;
}
.hl-type {
color: light-dark(#ad8900, #dbb32d);
}
.hl-module {
font-style: italic;
}
.hl-function {
color: light-dark(#428b00, #84c747);
}
.hl-bound {
color: light-dark(#ca4898, #f275be);
}
.hl-keyword {
color: light-dark(#0072d4, #4695f7);
}
.hl-comment {
color: light-dark(#909995, #72898f);
}
.hl-data {
color: light-dark(#d2212d, #ed4a46)
}