Skip to content

Commit

Permalink
Added support for JQ language (#1896)
Browse files Browse the repository at this point in the history
This adds support for the JQ language.
  • Loading branch information
RunDevelopment committed Jun 8, 2019
1 parent 0853e69 commit 73d964b
Show file tree
Hide file tree
Showing 17 changed files with 380 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
Expand Up @@ -436,6 +436,10 @@
"require": "clike",
"owner": "thesave"
},
"jq": {
"title": "JQ",
"owner": "RunDevelopment"
},
"jsdoc": {
"title": "JSDoc",
"require": ["javascript", "javadoclike"],
Expand Down
67 changes: 67 additions & 0 deletions components/prism-jq.js
@@ -0,0 +1,67 @@
(function (Prism) {

var interpolation = /\\\((?:[^()]|\([^()]*\))*\)/.source;
var string = RegExp(/"(?:[^"\r\n\\]|\\[^\r\n(]|__)*"/.source.replace(/__/g, interpolation));
var stringInterpolation = {
'interpolation': {
pattern: RegExp(/((?:^|[^\\])(?:\\{2})*)/.source + interpolation),
lookbehind: true,
inside: {
'content': {
pattern: /^(\\\()[\s\S]+(?=\)$)/,
lookbehind: true,
inside: null // see below
},
'punctuation': /^\\\(|\)$/
}
}
};

var jq = Prism.languages.jq = {
'comment': /#.*/,
'property': {
pattern: RegExp(string.source + /(?=\s*:(?!:))/.source),
greedy: true,
inside: stringInterpolation
},
'string': {
pattern: string,
greedy: true,
inside: stringInterpolation
},

'function': {
pattern: /(\bdef\s+)[a-z_]\w+/i,
lookbehind: true
},

'variable': /\B\$\w+/,
'property-literal': {
pattern: /[a-z_]\w*(?=\s*:(?!:))/i,
alias: 'property'
},
'keyword': /\b(?:as|break|catch|def|elif|else|end|foreach|if|import|include|label|module|modulemeta|null|reduce|then|try|while)\b/,
'boolean': /\b(?:true|false)\b/,
'number': /(?:\b\d+\.|\B\.)?\d+(?:[eE][+-]?\d+)?\b/,

'operator': [
{
pattern: /\|=?/,
alias: 'pipe'
},
/\.\.|[!=<>]?=|\?\/\/|\/\/=?|[-+*/%]=?|[<>?]|\b(?:and|or|not)\b/
],
'c-style-function': {
pattern: /\b[a-z_]\w*(?=\s*\()/i,
alias: 'function'
},
'punctuation': /::|[()\[\]{},:;]|\.(?=\s*[\[\w$])/,
'dot': {
pattern: /\./,
alias: 'important'
}
}

stringInterpolation.interpolation.inside.content.inside = jq;

}(Prism));
1 change: 1 addition & 0 deletions components/prism-jq.min.js

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

11 changes: 11 additions & 0 deletions examples/prism-jq.html
@@ -0,0 +1,11 @@
<h2>Full example</h2>
<pre><code># comment
def some_method:
to_entries | sort_by(.foo) |
map(.foo) as $keys |
map(.bar) | transpose |
map(
[$keys, .] | transpose |
map({foo: .[0], bar: .[1], "foo-bar": "foo\("-" + "bar")"}) | from_entries
)
;</code></pre>
2 changes: 1 addition & 1 deletion plugins/show-language/prism-show-language.js
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","js":"JavaScript","abap":"ABAP","abnf":"Augmented Backus–Naur form","apacheconf":"Apache Configuration","apl":"APL","arff":"ARFF","asciidoc":"AsciiDoc","adoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Bash","basic":"BASIC","bnf":"Backus–Naur form","rbnf":"Routing Backus–Naur form","csharp":"C#","cs":"C#","dotnet":"C#","cpp":"C++","cil":"CIL","coffee":"CoffeeScript","cmake":"CMake","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","jinja2":"Django/Jinja2","dockerfile":"Docker","ebnf":"Extended Backus–Naur form","ejs":"EJS","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","gamemakerlanguage":"GameMaker Language","graphql":"GraphQL","hs":"Haskell","hcl":"HCL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","javadoc":"JavaDoc","javadoclike":"JavaDoc-like","javastacktrace":"Java stack trace","jsdoc":"JSDoc","js-extras":"JS Extras","json":"JSON","jsonp":"JSONP","json5":"JSON5","latex":"LaTeX","tex":"TeX","context":"ConTeXt","emacs":"Lisp","elisp":"Lisp","emacs-lisp":"Lisp","lolcode":"LOLCODE","md":"Markdown","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n1ql":"N1QL","n4js":"N4JS","n4jsd":"N4JS","nand2tetris-hdl":"Nand To Tetris HDL","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","phpdoc":"PHPDoc","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","py":"Python","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","rb":"Ruby","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","toml":"TOML","tt2":"Template Toolkit 2","ts":"TypeScript","t4-cs":"T4 Text Templates (C#)","t4":"T4 Text Templates (C#)","t4-vb":"T4 Text Templates (VB)","t4-templating":"T4 templating","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","vb":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML","yml":"YAML"}/*]*/;
var Languages = /*languages_placeholder[*/{"html":"HTML","xml":"XML","svg":"SVG","mathml":"MathML","css":"CSS","clike":"C-like","js":"JavaScript","abap":"ABAP","abnf":"Augmented Backus–Naur form","apacheconf":"Apache Configuration","apl":"APL","arff":"ARFF","asciidoc":"AsciiDoc","adoc":"AsciiDoc","asm6502":"6502 Assembly","aspnet":"ASP.NET (C#)","autohotkey":"AutoHotkey","autoit":"AutoIt","shell":"Bash","basic":"BASIC","bnf":"Backus–Naur form","rbnf":"Routing Backus–Naur form","csharp":"C#","cs":"C#","dotnet":"C#","cpp":"C++","cil":"CIL","coffee":"CoffeeScript","cmake":"CMake","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","jinja2":"Django/Jinja2","dockerfile":"Docker","ebnf":"Extended Backus–Naur form","ejs":"EJS","erb":"ERB","fsharp":"F#","gcode":"G-code","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","gamemakerlanguage":"GameMaker Language","graphql":"GraphQL","hs":"Haskell","hcl":"HCL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","javadoc":"JavaDoc","javadoclike":"JavaDoc-like","javastacktrace":"Java stack trace","jq":"JQ","jsdoc":"JSDoc","js-extras":"JS Extras","json":"JSON","jsonp":"JSONP","json5":"JSON5","latex":"LaTeX","tex":"TeX","context":"ConTeXt","emacs":"Lisp","elisp":"Lisp","emacs-lisp":"Lisp","lolcode":"LOLCODE","md":"Markdown","markup-templating":"Markup templating","matlab":"MATLAB","mel":"MEL","n1ql":"N1QL","n4js":"N4JS","n4jsd":"N4JS","nand2tetris-hdl":"Nand To Tetris HDL","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","opencl":"OpenCL","parigp":"PARI/GP","objectpascal":"Object Pascal","php":"PHP","phpdoc":"PHPDoc","php-extras":"PHP Extras","plsql":"PL/SQL","powershell":"PowerShell","properties":".properties","protobuf":"Protocol Buffers","py":"Python","q":"Q (kdb+ database)","jsx":"React JSX","tsx":"React TSX","renpy":"Ren'py","rest":"reST (reStructuredText)","rb":"Ruby","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","soy":"Soy (Closure Template)","tap":"TAP","toml":"TOML","tt2":"Template Toolkit 2","ts":"TypeScript","t4-cs":"T4 Text Templates (C#)","t4":"T4 Text Templates (C#)","t4-vb":"T4 Text Templates (VB)","t4-templating":"T4 templating","vbnet":"VB.Net","vhdl":"VHDL","vim":"vim","visual-basic":"Visual Basic","vb":"Visual Basic","wasm":"WebAssembly","wiki":"Wiki markup","xeoracube":"XeoraCube","xojo":"Xojo (REALbasic)","xquery":"XQuery","yaml":"YAML","yml":"YAML"}/*]*/;

Prism.plugins.toolbar.registerButton('show-language', function(env) {
var pre = env.element.parentNode;
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/jq/boolean_feature.test
@@ -0,0 +1,13 @@
true
false

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

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

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

Checks for booleans.
13 changes: 13 additions & 0 deletions tests/languages/jq/comment_feature.test
@@ -0,0 +1,13 @@
# foo
#

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

[
["comment", "# foo"],
["comment", "#"]
]

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

Checks for comments.
11 changes: 11 additions & 0 deletions tests/languages/jq/dot_feature.test
@@ -0,0 +1,11 @@
.

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

[
["dot", "."]
]

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

Checks for the dot.
31 changes: 31 additions & 0 deletions tests/languages/jq/function_feature.test
@@ -0,0 +1,31 @@
def foo:
def bar(x,y,z):
fooBar(x)

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

[
["keyword", "def"],
["function", "foo"],
["punctuation", ":"],

["keyword", "def"],
["function", "bar"],
["punctuation", "("],
"x",
["punctuation", ","],
"y",
["punctuation", ","],
"z",
["punctuation", ")"],
["punctuation", ":"],

["c-style-function", "fooBar"],
["punctuation", "("],
"x",
["punctuation", ")"]
]

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

Checks for functions.
47 changes: 47 additions & 0 deletions tests/languages/jq/keyword_feature.test
@@ -0,0 +1,47 @@
as
break
catch
def;
elif
else
end
foreach
if
import
include
label
module
modulemeta
null
reduce
then
try
while

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

[
["keyword", "as"],
["keyword", "break"],
["keyword", "catch"],
["keyword", "def"], ["punctuation", ";"],
["keyword", "elif"],
["keyword", "else"],
["keyword", "end"],
["keyword", "foreach"],
["keyword", "if"],
["keyword", "import"],
["keyword", "include"],
["keyword", "label"],
["keyword", "module"],
["keyword", "modulemeta"],
["keyword", "null"],
["keyword", "reduce"],
["keyword", "then"],
["keyword", "try"],
["keyword", "while"]
]

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

Checks for keywords.
46 changes: 46 additions & 0 deletions tests/languages/jq/operator_feature.test
@@ -0,0 +1,46 @@
..
== != >= <= < >
= ?
?// //=
+ - * / % | += -= *= /= %= |=
and or not

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

[
["operator", ".."],

["operator", "=="],
["operator", "!="],
["operator", ">="],
["operator", "<="],
["operator", "<"],
["operator", ">"],

["operator", "="],
["operator", "?"],

["operator", "?//"],
["operator", "//="],

["operator", "+"],
["operator", "-"],
["operator", "*"],
["operator", "/"],
["operator", "%"],
["operator", "|"],
["operator", "+="],
["operator", "-="],
["operator", "*="],
["operator", "/="],
["operator", "%="],
["operator", "|="],

["operator", "and"],
["operator", "or"],
["operator", "not"]
]

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

Checks for operators.
45 changes: 45 additions & 0 deletions tests/languages/jq/property_feature.test
@@ -0,0 +1,45 @@
{ foo: 1, "bar": 2, "foo\("Bar")": 3 }

# not a property
foo::bar

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

[
["punctuation", "{"],
["property-literal", "foo"],
["punctuation", ":"],
["number", "1"],
["punctuation", ","],
["property", [
"\"bar\""
]],
["punctuation", ":"],
["number", "2"],
["punctuation", ","],
["property", [
"\"foo",
["interpolation", [
["punctuation", "\\("],
["content", [
["string", [
"\"Bar\""
]]
]],
["punctuation", ")"]
]],
"\""
]],
["punctuation", ":"],
["number", "3"],
["punctuation", "}"],

["comment", "# not a property"],
"\r\nfoo",
["punctuation", "::"],
"bar"
]

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

Checks for properties.
33 changes: 33 additions & 0 deletions tests/languages/jq/punctuation_feature.test
@@ -0,0 +1,33 @@
() [] {}
, : ;
::
.foo
.[]

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

[
["punctuation", "("],
["punctuation", ")"],
["punctuation", "["],
["punctuation", "]"],
["punctuation", "{"],
["punctuation", "}"],

["punctuation", ","],
["punctuation", ":"],
["punctuation", ";"],

["punctuation", "::"],

["punctuation", "."],
"foo\r\n",

["punctuation", "."],
["punctuation", "["],
["punctuation", "]"]
]

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

Checks for punctuation.
42 changes: 42 additions & 0 deletions tests/languages/jq/string_feature.test
@@ -0,0 +1,42 @@
"foo"
"\""
"Aa\r\n\t\b\f\u03bc"
"inter\("pol" + "ation")"

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

[
["string", [
"\"foo\""
]],

["string", [
"\"\\\"\""
]],

["string", [
"\"Aa\\r\\n\\t\\b\\f\\u03bc\""
]],

["string", [
"\"inter",
["interpolation", [
["punctuation", "\\("],
["content", [
["string", [
"\"pol\""
]],
["operator", "+"],
["string", [
"\"ation\""
]]
]],
["punctuation", ")"]
]],
"\""
]]
]

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

Checks for strings and string interpolation.

0 comments on commit 73d964b

Please sign in to comment.