Skip to content

Commit

Permalink
Add TOML support (#1488)
Browse files Browse the repository at this point in the history
Add support for [Tom's Obvious, Minimal Language](https://github.com/toml-lang/toml).

Some simplifications:

1. The `.`s of dotted keys are not highlighted. This is because the `.` could be inside
quotes and checking that isn't easy.
2. There is no distinction between table `[foo]` and array `[[foo]]`. `foo` will be
highlighted as `table` in both cases.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Dec 2, 2018
1 parent 2577b6e commit 5b6ad70
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions components.json
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@
"peerDependencies": "css",
"owner": "Golmote"
},
"toml": {
"title": "TOML",
"owner": "RunDevelopment"
},
"tt2": {
"title": "Template Toolkit 2",
"require": ["clike", "markup-templating"],
Expand Down
43 changes: 43 additions & 0 deletions components/prism-toml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(function (Prism) {

// pattern: /(?:[\w-]+|'[^'\n\r]*'|"(?:\.|[^\\"\r\n])*")/
var key = "(?:[\\w-]+|'[^'\n\r]*'|\"(?:\\.|[^\\\\\"\r\n])*\")";

Prism.languages.toml = {
'comment': {
pattern: /#.*/,
greedy: true
},
'table': {
pattern: RegExp("(\\[\\s*)" + key + "(?:\\s*\\.\\s*" + key + ")*(?=\\s*\\])"),
lookbehind: true,
greedy: true,
alias: 'class-name'
},
'key': {
pattern: RegExp("(^\\s*|[{,]\\s*)" + key + "(?:\\s*\\.\\s*" + key + ")*(?=\\s*=)", "m"),
lookbehind: true,
greedy: true,
alias: 'property'
},
'string': {
pattern: /"""(?:\\[\s\S]|[^\\])*?"""|'''[\s\S]*?'''|'[^'\n\r]*'|"(?:\\.|[^\\"\r\n])*"/,
greedy: true
},
'date': [
{
// Offset Date-Time, Local Date-Time, Local Date
pattern: /\d{4}-\d{2}-\d{2}(?:[T\s]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?)?/i,
alias: 'number'
},
{
// Local Time
pattern: /\d{2}:\d{2}:\d{2}(?:\.\d+)?/i,
alias: 'number'
}
],
'number': /(?:\b0(?:x[\da-zA-Z]+(?:_[\da-zA-Z]+)*|o[0-7]+(?:_[0-7]+)*|b[10]+(?:_[10]+)*))\b|[-+]?\d+(?:_\d+)*(?:\.\d+(?:_\d+)*)?(?:[eE][+-]?\d+(?:_\d+)*)?\b|[-+]?(?:inf|nan)\b/,
'boolean': /\b(?:true|false)\b/,
'punctuation': /[.,=[\]{}]/
};
}(Prism));
1 change: 1 addition & 0 deletions components/prism-toml.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ if (!Prism.plugins.toolbar) {
}

// The languages map is built automatically with gulp
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","javascript":"JavaScript","abap":"ABAP","actionscript":"ActionScript","apacheconf":"Apache Configuration","apl":"APL","applescript":"AppleScript","arff":"ARFF","asciidoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Shell","basic":"BASIC","csharp":"C#","cpp":"C++","cil":"CIL","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","json":"JSON","jsonp":"JSONP","latex":"LaTeX","livescript":"LiveScript","lolcode":"LOLCODE","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n4js":"N4JS","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","toml":"TOML","tt2":"Template Toolkit 2","typescript":"TypeScript","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML"}/*]*/;
Prism.plugins.toolbar.registerButton('show-language', function(env) {
var pre = env.element.parentNode;
if (!pre || !/pre/i.test(pre.nodeName)) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/languages/toml/boolean_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
true
false

----------------------------------------------------

[
["boolean", "true"],
["boolean", "false"]
]

----------------------------------------------------

Checks for booleans.
11 changes: 11 additions & 0 deletions tests/languages/toml/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# I'm a comment, you're not.

----------------------------------------------------

[
["comment", "# I'm a comment, you're not."]
]

----------------------------------------------------

Checks for comments.
33 changes: 33 additions & 0 deletions tests/languages/toml/date_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
1979-05-27T07:32:00Z
1979-05-27T00:32:00-07:00
1979-05-27T00:32:00.999999-07:00
1979-05-27 07:32:00Z

1979-05-27T07:32:00
1979-05-27T00:32:00.999999

1979-05-27

07:32:00
00:32:00.999999

----------------------------------------------------

[
["date", "1979-05-27T07:32:00Z"],
["date", "1979-05-27T00:32:00-07:00"],
["date", "1979-05-27T00:32:00.999999-07:00"],
["date", "1979-05-27 07:32:00Z"],

["date", "1979-05-27T07:32:00"],
["date", "1979-05-27T00:32:00.999999"],

["date", "1979-05-27"],

["date", "07:32:00"],
["date", "00:32:00.999999"]
]

----------------------------------------------------

Checks for dates.
42 changes: 42 additions & 0 deletions tests/languages/toml/key_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
abc = "abc"
"abc" = "abc"
"abc".'a"b"c'.abc = "abc"
a . b . c = "abc"

a = { b = "b", c = "c" }

----------------------------------------------------

[
["key", "abc"],
["punctuation", "="],
["string", "\"abc\""],

["key", "\"abc\""],
["punctuation", "="],
["string", "\"abc\""],

["key", "\"abc\".'a\"b\"c'.abc"],
["punctuation", "="],
["string", "\"abc\""],

["key", "a . b . c"],
["punctuation", "="],
["string", "\"abc\""],

["key", "a"],
["punctuation", "="],
["punctuation", "{"],
["key", "b"],
["punctuation", "="],
["string", "\"b\""],
["punctuation", ","],
["key", "c"],
["punctuation", "="],
["string", "\"c\""],
["punctuation", "}"]
]

----------------------------------------------------

Checks for keys.
79 changes: 79 additions & 0 deletions tests/languages/toml/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
42
0
+0
-0
+99
-17

1_000
5_349_221

0xDEADBEEF
0xdeadbeef
0xdead_beef

0o0123_4567
0o755

0b1101_0110

+1.0
3.1415
-0.01
5e+22
1e6
1e1_000
-2E-2
6.626e-34
9_224_617.445_991_228_313

inf
+inf
-inf
nan
+nan
-nan

----------------------------------------------------

[
["number", "42"],
["number", "0"],
["number", "+0"],
["number", "-0"],
["number", "+99"],
["number", "-17"],

["number", "1_000"],
["number", "5_349_221"],

["number", "0xDEADBEEF"],
["number", "0xdeadbeef"],
["number", "0xdead_beef"],

["number", "0o0123_4567"],
["number", "0o755"],

["number", "0b1101_0110"],

["number", "+1.0"],
["number", "3.1415"],
["number", "-0.01"],
["number", "5e+22"],
["number", "1e6"],
["number", "1e1_000"],
["number", "-2E-2"],
["number", "6.626e-34"],
["number", "9_224_617.445_991_228_313"],

["number", "inf"],
["number", "+inf"],
["number", "-inf"],
["number", "nan"],
["number", "+nan"],
["number", "-nan"]
]

----------------------------------------------------

Checks for numbers.
29 changes: 29 additions & 0 deletions tests/languages/toml/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
""
"abc"
"\""
''
'#'
'"abc"'
"""
abc
"""
'''
abc
'''

----------------------------------------------------

[
["string", "\"\""],
["string", "\"abc\""],
["string", "\"\\\"\""],
["string", "''"],
["string", "'#'"],
["string", "'\"abc\"'"],
["string", "\"\"\"\n\tabc\n\t\"\"\""],
["string", "'''\n\tabc\n\t'''"]
]

----------------------------------------------------

Checks for strings.
20 changes: 20 additions & 0 deletions tests/languages/toml/table_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[table]
[[array]]

----------------------------------------------------

[
["punctuation", "["],
["table", "table"],
["punctuation", "]"],

["punctuation", "["],
["punctuation", "["],
["table", "array"],
["punctuation", "]"],
["punctuation", "]"]
]

----------------------------------------------------

Checks for tables.

0 comments on commit 5b6ad70

Please sign in to comment.