Skip to content

Commit

Permalink
added sway highlight.js (FuelLabs#129)
Browse files Browse the repository at this point in the history
* added sway highlight.js

* added higlightjs/sway and its build script

* added initial prism for sway

* added script for prism

* added prism-sway and instructions

* rename to all caps

* update build scripts

* added missing dots
  • Loading branch information
leviathanbeak authored Aug 2, 2021
1 parent cf8e9ce commit 919c18c
Show file tree
Hide file tree
Showing 9 changed files with 898 additions and 0 deletions.
321 changes: 321 additions & 0 deletions docs/theme/highlight.js

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions scripts/highlightjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Instructions

Update "sway.js" file.

Then run:

```sh
$ ./build.sh
```

If you are actively developing and testing and want to keep `highlight.js` directory add `keep` in the command

```sh
$ ./build.sh keep
```
21 changes: 21 additions & 0 deletions scripts/highlightjs/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash
project="highlight.js"
sway="sway.js"

if ! test -d ./${project}; then
git clone [email protected]:highlightjs/highlight.js.git
fi

cp ${sway} ${project}/src/languages
cd ${project}
npm install

rm -rf build
node tools/build.js sway
cp build/highlight.min.js ../../../docs/theme/highlight.js

# add "keep" in order to keep highlight.js repo
if [[ ${1} != "keep" ]]; then
cd ../
rm -rf ${project}
fi
175 changes: 175 additions & 0 deletions scripts/highlightjs/sway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
/*
Language: Sway
Author: Fuel [email protected]
Contributors: Elvis Dedic <[email protected]>,
Website: https://fuel.sh
Category: smart contracts, layer 2, blockchain,
*/

import * as regex from '../lib/regex.js';

/** @type LanguageFn */
export default function(hljs) {
const FUNCTION_INVOKE = {
className: "title.function.invoke",
relevance: 0,
begin: regex.concat(
/\b/,
/(?!let\b)/,
hljs.IDENT_RE,
regex.lookahead(/\s*\(/))
};
const NUMBER_SUFFIX = '([u](8|16|32|64))\?';

const KEYWORDS = [
"as",
"asm",
"contract",
"deref",
"enum",
"fn",
"impl",
"let",
"library",
"match",
"mut",
"predicate",
"ref",
"return",
"script",
"Self",
"self",
"str",
"struct",
"trait",
"use",
"where",
"while",
];
const LITERALS = [
"true",
"false",
];
const BUILTINS = [

];
const TYPES = [
"u8",
"u16",
"u32",
"u64",
];
return {
name: 'Sway',
aliases: [ 'sw' ],
keywords: {
$pattern: hljs.IDENT_RE + '!?',
type: TYPES,
keyword: KEYWORDS,
literal: LITERALS,
built_in: BUILTINS
},
illegal: '</',
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.COMMENT('/\\*', '\\*/', {
contains: [ 'self' ]
}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {
begin: /b?"/,
illegal: null
}),
{
className: 'string',
variants: [
{
begin: /b?r(#*)"(.|\n)*?"\1(?!#)/
},
{
begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/
}
]
},
{
className: 'symbol',
begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
},
{
className: 'number',
variants: [
{
begin: '\\b0b([01_]+)' + NUMBER_SUFFIX
},
{
begin: '\\b0o([0-7_]+)' + NUMBER_SUFFIX
},
{
begin: '\\b0x([A-Fa-f0-9_]+)' + NUMBER_SUFFIX
},
{
begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)' +
NUMBER_SUFFIX
}
],
relevance: 0
},
{
begin: [
/fn/,
/\s+/,
hljs.UNDERSCORE_IDENT_RE
],
className: {
1: "keyword",
3: "title.function"
}
},
{
begin: [
/let/, /\s+/,
/(?:mut\s+)?/,
hljs.UNDERSCORE_IDENT_RE
],
className: {
1: "keyword",
3: "keyword",
4: "variable"
}
},
{
begin: [
/type/,
/\s+/,
hljs.UNDERSCORE_IDENT_RE
],
className: {
1: "keyword",
3: "title.class"
}
},
{
begin: [
/(?:trait|enum|struct|impl|for|library)/,
/\s+/,
hljs.UNDERSCORE_IDENT_RE
],
className: {
1: "keyword",
3: "title.class"
}
},
{
begin: hljs.IDENT_RE + '::',
keywords: {
keyword: "Self",
built_in: BUILTINS
}
},
{
className: "punctuation",
begin: '->'
},
FUNCTION_INVOKE
]
};
}
19 changes: 19 additions & 0 deletions scripts/prism/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Instructions

Update "prism-sway.js" file.

Then run:

```sh
$ ./build.sh
```

If you are actively developing and testing and want to keep `prism` (repo) directory add `keep` in the command

```sh
$ ./build.sh keep
```

### Testing

If you want to test it in html, `keep` the "prism" folder, and open "test-suite.html" and start writing Sway.
22 changes: 22 additions & 0 deletions scripts/prism/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
project="prism"
components="components.json"
sway="prism-sway.js"

if ! test -d ./${project}; then
git clone [email protected]:PrismJS/prism.git
fi

cp ${sway} ${project}/components
cp ${components} ${project}
cd ${project}
npm ci

npm run build
cp components/prism-sway.min.js ../prism-sway.min.js

# add "keep" in order to keep highlight.js repo
if [[ ${1} != "keep" ]]; then
cd ../
rm -rf ${project}
fi
Loading

0 comments on commit 919c18c

Please sign in to comment.