From bfb559bf53fb16f3c64d82af28e92cd3193ff858 Mon Sep 17 00:00:00 2001 From: Golmote Date: Sat, 11 Jun 2016 01:17:24 +0200 Subject: [PATCH] Add support for GraphQL --- components.js | 4 +++ components/prism-graphql.js | 24 +++++++++++++ components/prism-graphql.min.js | 1 + examples/prism-graphql.html | 34 +++++++++++++++++++ plugins/show-language/prism-show-language.js | 2 +- .../show-language/prism-show-language.min.js | 2 +- .../languages/graphql/attr-name_feature.test | 27 +++++++++++++++ tests/languages/graphql/boolean_feature.test | 13 +++++++ tests/languages/graphql/comment_feature.test | 13 +++++++ .../languages/graphql/directive_feature.test | 13 +++++++ tests/languages/graphql/keyword_feature.test | 24 +++++++++++++ tests/languages/graphql/number_feature.test | 23 +++++++++++++ tests/languages/graphql/string_feature.test | 15 ++++++++ tests/languages/graphql/variable_feature.test | 13 +++++++ 14 files changed, 206 insertions(+), 2 deletions(-) create mode 100644 components/prism-graphql.js create mode 100644 components/prism-graphql.min.js create mode 100644 examples/prism-graphql.html create mode 100644 tests/languages/graphql/attr-name_feature.test create mode 100644 tests/languages/graphql/boolean_feature.test create mode 100644 tests/languages/graphql/comment_feature.test create mode 100644 tests/languages/graphql/directive_feature.test create mode 100644 tests/languages/graphql/keyword_feature.test create mode 100644 tests/languages/graphql/number_feature.test create mode 100644 tests/languages/graphql/string_feature.test create mode 100644 tests/languages/graphql/variable_feature.test diff --git a/components.js b/components.js index 1a2a881b55..47622735a2 100644 --- a/components.js +++ b/components.js @@ -210,6 +210,10 @@ var components = { "require": "clike", "owner": "arnehormann" }, + "graphql": { + "title": "GraphQL", + "owner": "Golmote" + }, "groovy": { "title": "Groovy", "require": "clike", diff --git a/components/prism-graphql.js b/components/prism-graphql.js new file mode 100644 index 0000000000..6fbe93beb1 --- /dev/null +++ b/components/prism-graphql.js @@ -0,0 +1,24 @@ +Prism.languages.graphql = { + 'comment': /#.*/, + 'string': { + pattern: /"(?:\\.|[^\\"])*"/, + greedy: true + }, + 'number': /(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/, + 'boolean': /\b(?:true|false)\b/, + 'variable': /\$[a-z_]\w*/i, + 'directive': { + pattern: /@[a-z_]\w*/i, + alias: 'function' + }, + 'attr-name': /[a-z_]\w*(?=\s*:)/i, + 'keyword': [ + { + pattern: /(fragment\s+(?!on)[a-z_]\w*\s+|\.\.\.\s*)on\b/, + lookbehind: true + }, + /\b(?:query|fragment|mutation)\b/ + ], + 'operator': /!|=|\.{3}/, + 'punctuation': /[!(){}\[\]:=,]/ +}; \ No newline at end of file diff --git a/components/prism-graphql.min.js b/components/prism-graphql.min.js new file mode 100644 index 0000000000..2658c23946 --- /dev/null +++ b/components/prism-graphql.min.js @@ -0,0 +1 @@ +Prism.languages.graphql={comment:/#.*/,string:{pattern:/"(?:\\.|[^\\"])*"/,greedy:!0},number:/(?:\B-|\b)\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b/,"boolean":/\b(?:true|false)\b/,variable:/\$[a-z_]\w*/i,directive:{pattern:/@[a-z_]\w*/i,alias:"function"},"attr-name":/[a-z_]\w*(?=\s*:)/i,keyword:[{pattern:/(fragment\s+(?!on)[a-z_]\w*\s+|\.\.\.\s*)on\b/,lookbehind:!0},/\b(?:query|fragment|mutation)\b/],operator:/!|=|\.{3}/,punctuation:/[!(){}\[\]:=,]/}; \ No newline at end of file diff --git a/examples/prism-graphql.html b/examples/prism-graphql.html new file mode 100644 index 0000000000..8ad337c1f4 --- /dev/null +++ b/examples/prism-graphql.html @@ -0,0 +1,34 @@ +

GraphQL

+

To use this language, use the class "language-graphql".

+ +

Comments

+
# This is a comment
+ +

Strings

+
""
+"foo \"bar\" baz"
+ +

Numbers

+
0
+42
+3.14159
+-9e-5
+0.9E+7
+ +

Keywords

+
query withFragments {
+  user(id: 4) {
+    friends(first: 10) {
+      ...friendFields
+    }
+    mutualFriends(first: 10) {
+      ...friendFields
+    }
+  }
+}
+
+fragment friendFields on User {
+  id
+  name
+  profilePic(size: 50)
+}
\ No newline at end of file diff --git a/plugins/show-language/prism-show-language.js b/plugins/show-language/prism-show-language.js index ec33657bd4..a448bb7c62 100644 --- a/plugins/show-language/prism-show-language.js +++ b/plugins/show-language/prism-show-language.js @@ -5,7 +5,7 @@ if (typeof self === 'undefined' || !self.Prism || !self.document) { } // 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","asciidoc":"AsciiDoc","aspnet":"ASP.NET (C#)","autoit":"AutoIt","autohotkey":"AutoHotkey","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","http":"HTTP","inform7":"Inform 7","json":"JSON","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","parigp":"PARI/GP","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","protobuf":"Protocol Buffers","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","vim":"vim","wiki":"Wiki markup","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","asciidoc":"AsciiDoc","aspnet":"ASP.NET (C#)","autoit":"AutoIt","autohotkey":"AutoHotkey","basic":"BASIC","csharp":"C#","cpp":"C++","coffeescript":"CoffeeScript","css-extras":"CSS Extras","fsharp":"F#","glsl":"GLSL","graphql":"GraphQL","http":"HTTP","inform7":"Inform 7","json":"JSON","latex":"LaTeX","lolcode":"LOLCODE","matlab":"MATLAB","mel":"MEL","nasm":"NASM","nginx":"nginx","nsis":"NSIS","objectivec":"Objective-C","ocaml":"OCaml","parigp":"PARI/GP","php":"PHP","php-extras":"PHP Extras","powershell":"PowerShell","protobuf":"Protocol Buffers","jsx":"React JSX","rest":"reST (reStructuredText)","sas":"SAS","sass":"Sass (Sass)","scss":"Sass (Scss)","sql":"SQL","typescript":"TypeScript","vhdl":"VHDL","vim":"vim","wiki":"Wiki markup","yaml":"YAML"}/*]*/; Prism.hooks.add('before-highlight', function(env) { var pre = env.element.parentNode; if (!pre || !/pre/i.test(pre.nodeName)) { diff --git a/plugins/show-language/prism-show-language.min.js b/plugins/show-language/prism-show-language.min.js index 0123d6be79..6343379c06 100644 --- a/plugins/show-language/prism-show-language.min.js +++ b/plugins/show-language/prism-show-language.min.js @@ -1 +1 @@ -!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={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",asciidoc:"AsciiDoc",aspnet:"ASP.NET (C#)",autoit:"AutoIt",autohotkey:"AutoHotkey",basic:"BASIC",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",glsl:"GLSL",http:"HTTP",inform7:"Inform 7",json:"JSON",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",mel:"MEL",nasm:"NASM",nginx:"nginx",nsis:"NSIS",objectivec:"Objective-C",ocaml:"OCaml",parigp:"PARI/GP",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",protobuf:"Protocol Buffers",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",vim:"vim",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var a=s.element.parentNode;if(a&&/pre/i.test(a.nodeName)){var t,i,r=a.getAttribute("data-language")||e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1),l=a.previousSibling;l&&/\s*\bprism-show-language\b\s*/.test(l.className)&&l.firstChild&&/\s*\bprism-show-language-label\b\s*/.test(l.firstChild.className)?i=l.firstChild:(t=document.createElement("div"),i=document.createElement("div"),i.className="prism-show-language-label",t.className="prism-show-language",t.appendChild(i),a.parentNode.insertBefore(t,a)),i.innerHTML=r}})}}(); \ No newline at end of file +!function(){if("undefined"!=typeof self&&self.Prism&&self.document){var e={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",asciidoc:"AsciiDoc",aspnet:"ASP.NET (C#)",autoit:"AutoIt",autohotkey:"AutoHotkey",basic:"BASIC",csharp:"C#",cpp:"C++",coffeescript:"CoffeeScript","css-extras":"CSS Extras",fsharp:"F#",glsl:"GLSL",graphql:"GraphQL",http:"HTTP",inform7:"Inform 7",json:"JSON",latex:"LaTeX",lolcode:"LOLCODE",matlab:"MATLAB",mel:"MEL",nasm:"NASM",nginx:"nginx",nsis:"NSIS",objectivec:"Objective-C",ocaml:"OCaml",parigp:"PARI/GP",php:"PHP","php-extras":"PHP Extras",powershell:"PowerShell",protobuf:"Protocol Buffers",jsx:"React JSX",rest:"reST (reStructuredText)",sas:"SAS",sass:"Sass (Sass)",scss:"Sass (Scss)",sql:"SQL",typescript:"TypeScript",vhdl:"VHDL",vim:"vim",wiki:"Wiki markup",yaml:"YAML"};Prism.hooks.add("before-highlight",function(s){var a=s.element.parentNode;if(a&&/pre/i.test(a.nodeName)){var t,i,r=a.getAttribute("data-language")||e[s.language]||s.language.substring(0,1).toUpperCase()+s.language.substring(1),l=a.previousSibling;l&&/\s*\bprism-show-language\b\s*/.test(l.className)&&l.firstChild&&/\s*\bprism-show-language-label\b\s*/.test(l.firstChild.className)?i=l.firstChild:(t=document.createElement("div"),i=document.createElement("div"),i.className="prism-show-language-label",t.className="prism-show-language",t.appendChild(i),a.parentNode.insertBefore(t,a)),i.innerHTML=r}})}}(); \ No newline at end of file diff --git a/tests/languages/graphql/attr-name_feature.test b/tests/languages/graphql/attr-name_feature.test new file mode 100644 index 0000000000..7f976e6d50 --- /dev/null +++ b/tests/languages/graphql/attr-name_feature.test @@ -0,0 +1,27 @@ +{ + zuck: user(id: 4) { + name + } +} + +---------------------------------------------------- + +[ + ["punctuation", "{"], + ["attr-name", "zuck"], + ["punctuation", ":"], + " user", + ["punctuation", "("], + ["attr-name", "id"], + ["punctuation", ":"], + ["number", "4"], + ["punctuation", ")"], + ["punctuation", "{"], + "\r\n\t\tname\r\n\t", + ["punctuation", "}"], + ["punctuation", "}"] +] + +---------------------------------------------------- + +Checks for aliases, parameter names, etc. \ No newline at end of file diff --git a/tests/languages/graphql/boolean_feature.test b/tests/languages/graphql/boolean_feature.test new file mode 100644 index 0000000000..4019c444f8 --- /dev/null +++ b/tests/languages/graphql/boolean_feature.test @@ -0,0 +1,13 @@ +true +false + +---------------------------------------------------- + +[ + ["boolean", "true"], + ["boolean", "false"] +] + +---------------------------------------------------- + +Checks for booleans. \ No newline at end of file diff --git a/tests/languages/graphql/comment_feature.test b/tests/languages/graphql/comment_feature.test new file mode 100644 index 0000000000..054d6023d1 --- /dev/null +++ b/tests/languages/graphql/comment_feature.test @@ -0,0 +1,13 @@ +# +# foobar + +---------------------------------------------------- + +[ + ["comment", "#"], + ["comment", "# foobar"] +] + +---------------------------------------------------- + +Checks for comments. \ No newline at end of file diff --git a/tests/languages/graphql/directive_feature.test b/tests/languages/graphql/directive_feature.test new file mode 100644 index 0000000000..ca9b70048a --- /dev/null +++ b/tests/languages/graphql/directive_feature.test @@ -0,0 +1,13 @@ +@skip +@include + +---------------------------------------------------- + +[ + ["directive", "@skip"], + ["directive", "@include"] +] + +---------------------------------------------------- + +Checks for directives \ No newline at end of file diff --git a/tests/languages/graphql/keyword_feature.test b/tests/languages/graphql/keyword_feature.test new file mode 100644 index 0000000000..06d16f2abf --- /dev/null +++ b/tests/languages/graphql/keyword_feature.test @@ -0,0 +1,24 @@ +query +fragment +mutation +fragment foo on Bar +... on Foo + +---------------------------------------------------- + +[ + ["keyword", "query"], + ["keyword", "fragment"], + ["keyword", "mutation"], + ["keyword", "fragment"], + " foo ", + ["keyword", "on"], + " Bar\r\n", + ["operator", "..."], + ["keyword", "on"], + " Foo" +] + +---------------------------------------------------- + +Checks for keywords. \ No newline at end of file diff --git a/tests/languages/graphql/number_feature.test b/tests/languages/graphql/number_feature.test new file mode 100644 index 0000000000..c58cc24d91 --- /dev/null +++ b/tests/languages/graphql/number_feature.test @@ -0,0 +1,23 @@ +0 +42 +-5 +3.14159 +5e4 +6E-78 +0.3e+1 + +---------------------------------------------------- + +[ + ["number", "0"], + ["number", "42"], + ["number", "-5"], + ["number", "3.14159"], + ["number", "5e4"], + ["number", "6E-78"], + ["number", "0.3e+1"] +] + +---------------------------------------------------- + +Checks for numbers. \ No newline at end of file diff --git a/tests/languages/graphql/string_feature.test b/tests/languages/graphql/string_feature.test new file mode 100644 index 0000000000..a3d26be33a --- /dev/null +++ b/tests/languages/graphql/string_feature.test @@ -0,0 +1,15 @@ +"" +"foo bar" +"foo\"bar\\baz" + +---------------------------------------------------- + +[ + ["string", "\"\""], + ["string", "\"foo bar\""], + ["string", "\"foo\\\"bar\\\\baz\""] +] + +---------------------------------------------------- + +Checks for strings. \ No newline at end of file diff --git a/tests/languages/graphql/variable_feature.test b/tests/languages/graphql/variable_feature.test new file mode 100644 index 0000000000..642d173e8a --- /dev/null +++ b/tests/languages/graphql/variable_feature.test @@ -0,0 +1,13 @@ +$foo +$Foo_bar42 + +---------------------------------------------------- + +[ + ["variable", "$foo"], + ["variable", "$Foo_bar42"] +] + +---------------------------------------------------- + +Checks for variables. \ No newline at end of file