Functional syntax highlighting for rust
This commit is contained in:
parent
731d7aa19c
commit
19e06488c4
7 changed files with 299 additions and 5 deletions
|
@ -1,9 +1,13 @@
|
||||||
use v6.e.PREVIEW;
|
use v6.e.PREVIEW;
|
||||||
|
|
||||||
use Pandoc;
|
use Pandoc;
|
||||||
use JSON::Class:auth<zef:vrurg>;
|
use Pygments;
|
||||||
use DB::Post;
|
use DB::Post;
|
||||||
|
|
||||||
|
|
||||||
|
use JSON::Class:auth<zef:vrurg>;
|
||||||
|
use File::Temp;
|
||||||
|
|
||||||
#| A plain markdown post
|
#| A plain markdown post
|
||||||
unit class MarkdownPost does Post is json(:pretty);
|
unit class MarkdownPost does Post is json(:pretty);
|
||||||
|
|
||||||
|
@ -20,8 +24,18 @@ method title(--> Str:D) {
|
||||||
|
|
||||||
# Simply provide our source file to pandoc
|
# Simply provide our source file to pandoc
|
||||||
method render-html(--> Str:D) {
|
method render-html(--> Str:D) {
|
||||||
|
# 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
|
markdown-to-html $!source
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# Return our summary, if we have one, otherwise extract the first paragraph of
|
# Return our summary, if we have one, otherwise extract the first paragraph of
|
||||||
# the markdown document
|
# the markdown document
|
||||||
|
|
|
@ -6,7 +6,7 @@ use JSON::Fast;
|
||||||
#| Run pandoc with the given arguments, dieing on failure
|
#| Run pandoc with the given arguments, dieing on failure
|
||||||
sub pandoc(*@args --> Str:D) {
|
sub pandoc(*@args --> Str:D) {
|
||||||
# Call into pandoc
|
# Call into pandoc
|
||||||
my $pandoc = run 'pandoc', @args, :out, :err;
|
my $pandoc = run 'pandoc', '--no-highlight', @args, :out, :err;
|
||||||
|
|
||||||
# Collect the output
|
# Collect the output
|
||||||
my $output = $pandoc.out.slurp: :close;
|
my $output = $pandoc.out.slurp: :close;
|
||||||
|
|
40
lib/Pygments.rakumod
Normal file
40
lib/Pygments.rakumod
Normal file
|
@ -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> <info-string> \h* \v
|
||||||
|
<code>
|
||||||
|
<&fence>
|
||||||
|
}
|
||||||
|
|
||||||
|
sub pygment(Str:D $code, Str:D $lang --> Str:D) {
|
||||||
|
my $pygments = run <pygmentize -f html -l>, $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<code>.Str;
|
||||||
|
my $lang = $match<info-string>.Str;
|
||||||
|
my $out = pygment $code, $lang;
|
||||||
|
## Mangle the html to meet our needs
|
||||||
|
# Delete the existing div and construct a <code></code> inside the <pre>
|
||||||
|
$out ~~ s:g/'<' \/? 'div' <-[>]>* '>'//;
|
||||||
|
$out ~~ s:g/'<pre>'/<pre><code class="{$lang}-code">/;
|
||||||
|
$out ~~ s:g/'</pre>'/<\/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
|
||||||
|
}
|
206
projects/Markdown/RustPosting.md
Normal file
206
projects/Markdown/RustPosting.md
Normal file
|
@ -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<T> {
|
||||||
|
gen_val: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
// impl of Val
|
||||||
|
impl Val {
|
||||||
|
fn value(&self) -> &f64 {
|
||||||
|
&self.val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> GenVal<T> {
|
||||||
|
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<u8> {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
|
@ -11,7 +11,6 @@ code {
|
||||||
/* Styling for fenced code blocks */
|
/* Styling for fenced code blocks */
|
||||||
pre > code {
|
pre > code {
|
||||||
display: block;
|
display: block;
|
||||||
white-space: pre-wrap;
|
|
||||||
padding: 1rem;
|
padding: 1rem;
|
||||||
border-radius: 0.55rem / 0.5rem;
|
border-radius: 0.55rem / 0.5rem;
|
||||||
word-wrap: normal;
|
word-wrap: normal;
|
||||||
|
|
|
@ -75,7 +75,7 @@ blockquote {
|
||||||
background-color: var(--bg-1);
|
background-color: var(--bg-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Colorization for code blocks */
|
/* Colorization for idris code blocks */
|
||||||
code {
|
code {
|
||||||
color: var(--code-fg-0);
|
color: var(--code-fg-0);
|
||||||
background-color: var(--code-bg-0);
|
background-color: var(--code-bg-0);
|
||||||
|
@ -102,3 +102,35 @@ code {
|
||||||
.hl-data {
|
.hl-data {
|
||||||
color: var(--code-red);
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -33,6 +33,9 @@ body, .post {
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: var(--box-gap);
|
gap: var(--box-gap);
|
||||||
}
|
}
|
||||||
|
.post {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
/* Style the site header */
|
/* Style the site header */
|
||||||
.site-header {
|
.site-header {
|
||||||
|
|
Loading…
Add table
Reference in a new issue