Skip to content

Commit

Permalink
Add support for HCL
Browse files Browse the repository at this point in the history
  • Loading branch information
outsideris committed Oct 24, 2018
1 parent bfbe446 commit f9013cb
Show file tree
Hide file tree
Showing 11 changed files with 240 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 @@ -330,6 +330,10 @@
"require": "clike",
"owner": "Golmote"
},
"hcl": {
"title": "HCL",
"owner": "outsideris"
},
"http": {
"title": "HTTP",
"peerDependencies": [
Expand Down
95 changes: 95 additions & 0 deletions components/prism-hcl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Prism.languages.hcl = {
'comment': [
{
pattern: /(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,
lookbehind: true
},
{
pattern: /(^|[^\\:])\/\/.*/,
lookbehind: true,
greedy: true
},
{
pattern: /#.*/
}
],
'keyword': [
{
pattern: /(resource|data)\s+(")?(\w+)(")?\s+(?=(")?([\w\-]+)(")?\s+{)/i,
inside: {
'type': [
{
pattern: /(resource|data)\s+(")?(\w+)(")?\s+/i,
lookbehind: true,
alias: 'variable'
}
]
}
},
{
pattern: /(provider|provisioner|variable|output|module|backend)\s+(")?([\w\-]+)(")?\s+(?={)/i,
inside: {
'type': [
{
pattern: /(provider|provisioner|variable|output|module|backend)\s+(")?([\w\-]+)(")?\s+/i,
lookbehind: true,
alias: 'variable'
}
]
}
},
{
pattern: /([\w\-]+)(?=\s+{)/i
}
],
'interpolation': [
{
pattern: /\${([\w\[\]\(\+\)\-\*\%\^\"\'\,\?\=\:\/\s]+\.?)+}/i,
inside: {
'type': [
{
pattern: /((terraform|var|self|count|module|path|data|local)\.)[\w\*]+/i,
lookbehind: true,
alias: 'variable'
}
],
'keyword': [
{
pattern: /(terraform|var|self|count|module|path|data|local)/i
}
],
'operator': [
{
pattern: /\${/
},
{
pattern: /}/
},
{
pattern: /\./
}
],
'function': [
{
pattern: /[\w]+(?=\()/i,
alias: 'variable'
}
],
'string': /".*"/i,
'punctuation': /[!"#%&'()*+,.\/;<=>@\[\\\]^`{|}~]/,
'number': /-?\d+\.?\d*/,

'others': [
{
pattern: /[\w\*]+/i,
alias: 'string'
}
],
},
}
],
'string': /(["'])(\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,
'boolean': /\b(?:true|false)\b/i,
'number': /-?\d+\.?\d*([kKmMgG]b?)?/,
'property': /([\w_-]+)(?=\s*(=)\s*)/i
};
1 change: 1 addition & 0 deletions components/prism-hcl.min.js

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

45 changes: 45 additions & 0 deletions examples/prism-hcl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<h2>Comments</h2>
<pre><code># Configure the AWS Provider
// Configure the AWS Provider
</code></pre>

<h2>Resources</h2>
<pre><code>resource "aws_instance" "web" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"

tags {
Name = "HelloWorld"
}
}</code></pre>

<h2>Provider</h2>
<pre><code>resource "aws_instance" "web" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"

tags {
Name = "HelloWorld"
}
}</code></pre>

<h2>Variables</h2>
<pre><code>variable "images" {
type = "map"

default = {
us-east-1 = "image-1234"
us-west-2 = "image-4567"
}
}</code></pre>

<h2>Outputs</h2>
<pre><code>output "address" {
value = "${aws_instance.db.public_dns}"
}</code></pre>

<h2>Modules</h2>
<pre><code>module "consul" {
source = "hashicorp/consul/aws"
servers = 5
}</code></pre>
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++","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","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++","coffeescript":"CoffeeScript","csp":"Content-Security-Policy","css-extras":"CSS Extras","django":"Django/Jinja2","erb":"ERB","fsharp":"F#","gedcom":"GEDCOM","glsl":"GLSL","gml":"GameMaker Language","graphql":"GraphQL","hcl":"HCL","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"}/*]*/;
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.

17 changes: 17 additions & 0 deletions tests/languages/hcl/comment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# foo bar
// foo bar
/*
* multi line
*/

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

[
["comment", "# foo bar"],
["comment", "// foo bar"],
["comment", "/*\r\n * multi line\r\n */"]
]

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

Checks for comments.
47 changes: 47 additions & 0 deletions tests/languages/hcl/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
resource "aws_db_instance" "main" {
data "terraform_remote_state" "main" {
output "dev_vpc_id" {
config {
terraform {
${data.aws_availability_zones.available.names[0]}"

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

[
["keyword",
["resource",
["type", " \"aws_db_instance\" "]]],
["string", "\"main\""],
" {\r\n",
["keyword",
["data",
["type", " \"terraform_remote_state\" "]]],
["string", "\"main\""],
" {\r\n",
["keyword",
["output",
["type", " \"dev_vpc_id\" "]]],
"{\r\n",
["keyword", "config"],
" {\r\n",
["keyword", "terraform"],
" {\r\n",
["interpolation",[
["operator", "${"],
["keyword", "data"],
["operator", "."],
["type", "aws_availability_zones"],
["operator", "."],
["others", "available"],
["operator", "."],
["others", "names"],
["punctuation", "["],
["number", "0"],
["punctuation", "]"],
["operator", "}"]
]], "\""
]

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

Checks for all keywords.
15 changes: 15 additions & 0 deletions tests/languages/hcl/number_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
42
1.02
4mb

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

[
["number", "42"],
["number", "1.02"],
["number", "4mb"]
]

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

Checks for numbers.
13 changes: 13 additions & 0 deletions tests/languages/hcl/string_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"double quoted\"' % string"
'single quoted"\' % string'

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

[
["string", "\"double quoted\\\"' % string\""],
["string", "'single quoted\"\\' % string'"]
]

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

Checks for strings.

0 comments on commit f9013cb

Please sign in to comment.