Skip to content

Commit

Permalink
doc: highlight schema files
Browse files Browse the repository at this point in the history
Adjust the highlighting component of mdbook to get syntax highlighting
for the Stef schema files. As a nice side effect, remove any unused
languages from the highlighter to reduce the file size.
  • Loading branch information
dnaka91 committed Nov 6, 2023
1 parent 827ae44 commit b6b595c
Show file tree
Hide file tree
Showing 27 changed files with 704 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/target
.lycheecache
1 change: 1 addition & 0 deletions book/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/book
/theme
1 change: 1 addition & 0 deletions book/highlight/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/node_modules
16 changes: 16 additions & 0 deletions book/highlight/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://biomejs.dev/schemas/1.3.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"indentStyle": "space",
"indentWidth": 2
}
}
17 changes: 17 additions & 0 deletions book/highlight/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "highlight",
"private": true,
"scripts": {
"esbuild": "esbuild src/*.{css,ts} --outdir=../theme --target=es2016 --global-name=hljs --legal-comments=none --bundle",
"watch": "pnpm run esbuild --watch",
"build": "pnpm run esbuild --minify",
"lint": "biome check --apply src/**/*.ts"
},
"dependencies": {
"highlight.js": "^11.9.0"
},
"devDependencies": {
"@biomejs/biome": "^1.3.3",
"esbuild": "^0.19.5"
}
}
80 changes: 80 additions & 0 deletions book/highlight/src/ayu-highlight.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
@import "./css/overrides.css";

/*
Based off of the Ayu theme
Original by Dempfi (https://github.com/dempfi/ayu)
*/

.hljs {
display: block;
overflow-x: auto;
background: #191f26;
color: #e6e1cf;
}

.hljs-comment,
.hljs-quote {
color: #5c6773;
font-style: italic;
}

.hljs-variable,
.hljs-template-variable,
.hljs-attribute,
.hljs-attr,
.hljs-regexp,
.hljs-link,
.hljs-selector-id,
.hljs-selector-class {
color: #ff7733;
}

.hljs-number,
.hljs-meta,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params {
color: #ffee99;
}

.hljs-string,
.hljs-bullet {
color: #b8cc52;
}

.hljs-title,
.hljs-built_in,
.hljs-section {
color: #ffb454;
}

.hljs-keyword,
.hljs-selector-tag,
.hljs-symbol {
color: #ff7733;
}

.hljs-name {
color: #36a3d9;
}

.hljs-tag {
color: #00568d;
}

.hljs-emphasis {
font-style: italic;
}

.hljs-strong {
font-weight: bold;
}

.hljs-addition {
color: #91b362;
}

.hljs-deletion {
color: #d96c75;
}
3 changes: 3 additions & 0 deletions book/highlight/src/css/overrides.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pre {
tab-size: 4;
}
84 changes: 84 additions & 0 deletions book/highlight/src/highlight.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
@import "./css/overrides.css";

/*
* An increased contrast highlighting scheme loosely based on the
* "Base16 Atelier Dune Light" theme by Bram de Haan
* (http://atelierbram.github.io/syntax-highlighting/atelier-schemes/dune)
* Original Base16 color scheme by Chris Kempson
* (https://github.com/chriskempson/base16)
*/

/* Comment */
.hljs-comment,
.hljs-quote {
color: #575757;
}

/* Red */
.hljs-variable,
.hljs-template-variable,
.hljs-attribute,
.hljs-tag,
.hljs-name,
.hljs-regexp,
.hljs-link,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class {
color: #d70025;
}

/* Orange */
.hljs-number,
.hljs-meta,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params {
color: #b21e00;
}

/* Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet {
color: #008200;
}

/* Blue */
.hljs-title,
.hljs-section {
color: #0030f2;
}

/* Purple */
.hljs-keyword,
.hljs-selector-tag {
color: #9d00ec;
}

.hljs {
display: block;
overflow-x: auto;
background: #f6f7f6;
color: #000;
}

.hljs-emphasis {
font-style: italic;
}

.hljs-strong {
font-weight: bold;
}

.hljs-addition {
color: #22863a;
background-color: #f0fff4;
}

.hljs-deletion {
color: #b31d28;
background-color: #ffeef0;
}
28 changes: 28 additions & 0 deletions book/highlight/src/highlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { HLJSOptions } from "highlight.js";
import hljs from "highlight.js/lib/core";
import bash from "highlight.js/lib/languages/bash";
import go from "highlight.js/lib/languages/go";
import kotlin from "highlight.js/lib/languages/kotlin";
import python from "highlight.js/lib/languages/python";
import rust from "highlight.js/lib/languages/rust";
import toml from "highlight.js/lib/languages/ini";
import typescript from "highlight.js/lib/languages/typescript";
import { stef } from "./languages/stef";

hljs.registerLanguage("bash", bash);
hljs.registerLanguage("go", go);
hljs.registerLanguage("kotlin", kotlin);
hljs.registerLanguage("python", python);
hljs.registerLanguage("rust", rust);
hljs.registerLanguage("toml", toml);
hljs.registerLanguage("typescript", typescript);

hljs.registerLanguage("stef", stef);

export function configure(options: Partial<HLJSOptions>) {
hljs.configure(options);
}

export function highlightBlock(element: HTMLElement) {
hljs.highlightElement(element);
}
120 changes: 120 additions & 0 deletions book/highlight/src/languages/stef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Language: Stef
Author: Dominik Nakamura <dnaka91@gmail.com>
Category: common, system
*/

import { HLJSApi, Language } from "highlight.js";

export function stef(hljs: HLJSApi): Language {
const KEYWORDS = ["mod", "struct", "enum", "const", "type", "use"];
const LITERALS = ["true", "false"];
const TYPES = [
"bool",
"u8",
"u16",
"u32",
"u64",
"u128",
"i8",
"i16",
"i32",
"i64",
"i128",
"f32",
"f64",
"string",
"bytes",
"box",
"vec",
"hash_map",
"hash_set",
"option",
"non_zero",
];
return {
name: "Stef",
keywords: {
type: TYPES,
keyword: KEYWORDS,
literal: LITERALS,
},
illegal: "</",
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.COMMENT("///", /\n/, {}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {
begin: /b?"/,
illegal: null,
}),
{
scope: "string",
variants: [
{ begin: /b?r(#*)"(.|\n)*?"\1(?!#)/ },
{ begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/ },
],
},
{
scope: "symbol",
match: /'[a-zA-Z_][a-zA-Z0-9_]*/,
},
{
scope: "number",
variants: [{ begin: /\b(\d+(\.[0-9]+)?)/ }],
relevance: 0,
},
{
scope: "meta",
begin: "#!?\\[",
end: "\\]",
contains: [
{
className: "string",
begin: /"/,
end: /"/,
},
],
},
{
match: [/(?:enum|struct|type)/, /\s+/, /[a-zA-Z0-9_]+/],
scope: {
1: "keyword",
3: "title.class",
},
},
{
match: ["const", /\s+/, /[a-zA-Z0-9_]+/],
scope: {
1: "keyword",
3: "variable.constant",
},
},
{
match: [/[a-z0-9_]+/, ":", /[a-zA-Z0-9_<>:,& ]+/, /@\d+/, /,?/],
scope: {
1: "attr",
2: "punctuation",
3: "type",
4: "symbol",
5: "punctuation",
},
relevance: 2,
},
{
match: [/[a-zA-Z0-9_<>,& ]+/, /@\d+/, /,?/],
scope: {
1: "type",
2: "symbol",
3: "punctuation",
},
relevance: 1,
},
{
match: `${hljs.IDENT_RE}::`,
keywords: {
type: TYPES,
},
},
],
};
}
2 changes: 2 additions & 0 deletions book/highlight/src/tomorrow-night.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "highlight.js/styles/tokyo-night-dark.css";
@import "./css/overrides.css";
2 changes: 1 addition & 1 deletion book/src/guide/creating.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ Special types
- Boxed strings: `box<string`.
- Boxed bytes: `box<bytes>`.

```rust,ignore
```stef
{{#include creating/basic.stef}}
```
2 changes: 1 addition & 1 deletion book/src/guide/examples.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Examples

```rust,ignore
```stef
{{#include examples/01.stef}}
```
4 changes: 2 additions & 2 deletions book/src/guide/generating.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ The file name is the same as the input schema file name, but with `.rs` as file

From that point on, the generated code can be used like regular Rust code. Extending the example a bit, let's say the schema file contained the following:

```rust,ignore
```stef
struct Sample {
value: u32 @1,
}
```

Then we could use the generated struct as follows:

```rust,ignore
```rust
// Include stef's `Encode` trait to get access to the `encode()` method.
use stef::Encode;

Expand Down
Loading

0 comments on commit b6b595c

Please sign in to comment.