Patrix

A publishing engine

Code blocks

·

Patrix draws code blocks now. Paste code with the editor’s Code button and it is shown as written, in a monospaced box, with its syntax coloured. You don’t have to say what language it is: Patrix recognises the popular ones.

Recognised

Nothing below says what it is. Each was worked out from the code itself.

Go:

Go
package main

import (
	"fmt"
	"strings"
)

// shout says it louder.
func shout(s string) string {
	return strings.ToUpper(s) + "!"
}

func main() {
	for _, w := range []string{"posts", "pages", "people"} {
		fmt.Println(shout(w))
	}
}

Python:

Python
from dataclasses import dataclass

@dataclass
class Post:
    title: str
    words: int = 0

    def reading_time(self) -> int:
        """Minutes, at 230 words a minute."""
        return max(1, round(self.words / 230))

if __name__ == "__main__":
    print(Post("Code blocks", 460).reading_time())

A shell session:

Shell
$ curl -s https://patrix.cc/feed | grep -c "<item>"
$ for f in *.md; do
>   echo "importing $f" && patrix --import "$f" --type wxr
> done

JavaScript:

JavaScript
const posts = await fetch("/feed.json").then((r) => r.json());
for (const { title, url } of posts.items) {
  console.log(`${title} → ${url}`);
}

JSON:

JSON
{
  "name": "patrix",
  "blocks": ["code", "embed", "podcast"],
  "highlight": { "languages": 27, "styles": "theme" }
}

Swift:

Swift
import SwiftUI

struct PostRow: View {
    let title: String
    @State private var read = false

    var body: some View {
        Toggle(title, isOn: $read)
    }
}

Said

When there’s no telling, or the guess is wrong, say it yourself: the language you give wins. A short query like this one is marked SQL.

SQL
select title, published_at
from entries
where kind = 'post' and status = 'published'
order by published_at desc
limit 10;

And output is marked plain, so nothing in it is coloured.

ok   internal/patrix/extensions/codeblocks  0.25s
ok   internal/patrix/doc                     0.43s
PASS

How it works

Code blocks are Patrix’s first internal extension: a package built into the engine that adds a block type and a stylesheet, without touching the core. The code is stored exactly as typed. When the page is drawn it is read into tokens, and Patrix writes every tag around them itself, so nothing typed into a code block can ever reach a page as markup. Themes choose the colours.

Comments

Log in to leave a comment.