Skip to content

Commit

Permalink
Added support for HCL (#1594)
Browse files Browse the repository at this point in the history
Adds support for HCL ([HashiCorp configuration language](https://github.com/hashicorp/hcl)).
  • Loading branch information
outsideris authored and RunDevelopment committed Dec 27, 2018
1 parent 4f6f3c7 commit c939df8
Show file tree
Hide file tree
Showing 13 changed files with 459 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 @@ -337,6 +337,10 @@
"require": "clike",
"owner": "Golmote"
},
"hcl": {
"title": "HCL",
"owner": "outsideris"
},
"http": {
"title": "HTTP",
"peerDependencies": [
Expand Down
65 changes: 65 additions & 0 deletions components/prism-hcl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Prism.languages.hcl = {
'comment': /(?:\/\/|#).*|\/\*[\s\S]*?(?:\*\/|$)/,
'heredoc': {
pattern: /<<-?(\w+)[\s\S]*?^\s*\1/m,
greedy: true,
alias: 'string'
},
'keyword': [
{
pattern: /(?:resource|data)\s+(?:"(?:\\[\s\S]|[^\\"])*")(?=\s+"[\w-]+"\s+{)/i,
inside: {
'type': {
pattern: /(resource|data|\s+)(?:"(?:\\[\s\S]|[^\\"])*")/i,
lookbehind: true,
alias: 'variable'
}
}
},
{
pattern: /(?:provider|provisioner|variable|output|module|backend)\s+(?:[\w-]+|"(?:\\[\s\S]|[^\\"])*")\s+(?={)/i,
inside: {
'type': {
pattern: /(provider|provisioner|variable|output|module|backend)\s+(?:[\w-]+|"(?:\\[\s\S]|[^\\"])*")\s+/i,
lookbehind: true,
alias: 'variable'
}
}
},
{
pattern: /[\w-]+(?=\s+{)/
}
],
'property': [
/[\w-\.]+(?=\s*=(?!=))/,
/"(?:\\[\s\S]|[^\\"])+"(?=\s*[:=])/,
],
'string': {
pattern: /"(?:[^\\$"]|\\[\s\S]|\$(?:(?=")|\$+|[^"${])|\$\{(?:[^{}"]|"(?:[^\\"]|\\[\s\S])*")*\})*"/,
greedy: true,
inside: {
'interpolation': {
pattern: /(^|[^$])\$\{(?:[^{}"]|"(?:[^\\"]|\\[\s\S])*")*\}/,
lookbehind: true,
inside: {
'type': {
pattern: /(\b(?:terraform|var|self|count|module|path|data|local)\b\.)[\w\*]+/i,
lookbehind: true,
alias: 'variable'
},
'keyword': /\b(?:terraform|var|self|count|module|path|data|local)\b/i,
'function': /\w+(?=\()/,
'string': {
pattern: /"(?:\\[\s\S]|[^\\"])*"/,
greedy: true,
},
'number': /\b0x[\da-f]+|\d+\.?\d*(?:e[+-]?\d+)?/i,
'punctuation': /[!\$#%&'()*+,.\/;<=>@\[\\\]^`{|}~?:]/,
}
},
}
},
'number': /\b0x[\da-f]+|\d+\.?\d*(?:e[+-]?\d+)?/i,
'boolean': /\b(?:true|false)\b/i,
'punctuation': /[=\[\]{}]/,
};
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.

42 changes: 42 additions & 0 deletions examples/prism-hcl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<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>provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-1"
}</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++","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","javastacktrace":"Java stack trace","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"}/*]*/;
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","hcl":"HCL","http":"HTTP","hpkp":"HTTP Public-Key-Pins","hsts":"HTTP Strict-Transport-Security","ichigojam":"IchigoJam","inform7":"Inform 7","javastacktrace":"Java stack trace","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.

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.
206 changes: 206 additions & 0 deletions tests/languages/hcl/interpolation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"${data.aws_availability_zones.available.names[0]}"
"${aws_db_subnet_group.default.id}"
"${var.something ? 1 : 0}"
"${var.num ? 1.02 : 0}"
"${length(var.hostnames)}"
"${format("web-%03d", count.index + 1)}"
"${file("templates/web_init.tpl")}"
"${replace(var.sub_domain, ".", "\\\\\\\\.")}"
"${filepath.foo}"

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

[
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "data"],
["punctuation", "."],
["type", "aws_availability_zones"],
["punctuation", "."],
"available",
["punctuation", "."],
"names",
["punctuation", "["],
["number", "0"],
["punctuation", "]"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
"aws_db_subnet_group",
["punctuation", "."],
"default",
["punctuation", "."],
"id",
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "var"],
["punctuation", "."],
["type", "something"],
["punctuation", "?"],
["number", "1"],
["punctuation", ":"],
["number", "0"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["keyword", "var"],
["punctuation", "."],
["type", "num"],
["punctuation", "?"],
["number", "1.02"],
["punctuation", ":"],
["number", "0"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "length"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "hostnames"],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "format"],
["punctuation", "("],
["string", "\"web-%03d\""],
["punctuation", ","],
["keyword", "count"],
["punctuation", "."],
["type", "index"],
["punctuation", "+"],
["number", "1"],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "file"],
["punctuation", "("],
["string", "\"templates/web_init.tpl\""],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
["function", "replace"],
["punctuation", "("],
["keyword", "var"],
["punctuation", "."],
["type", "sub_domain"],
["punctuation", ","],
["string", "\".\""],
["punctuation", ","],
["string", "\"\\\\\\\\\\\\\\\\.\""],
["punctuation", ")"],
["punctuation", "}"]
]
],
"\""
]
],
["string",
[
"\"",
[
"interpolation",
[
["punctuation", "$"],
["punctuation", "{"],
"filepath",
["punctuation", "."],
"foo",
["punctuation", "}"]
]
],
"\""
]
]
]

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

Checks for all interpolation.
32 changes: 32 additions & 0 deletions tests/languages/hcl/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
resource "aws_db_instance" "main" {
data "terraform_remote_state" "main" {
output "dev_vpc_id" {
config {
terraform {

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

[
["keyword",
["resource ",
["type", "\"aws_db_instance\""]]],
["string", ["\"main\""]],
["punctuation", "{"],
["keyword",
["data ",
["type", "\"terraform_remote_state\""]]],
["string", ["\"main\""]],
["punctuation", "{"],
["keyword",
["output",
["type", " \"dev_vpc_id\" "]]],
["punctuation", "{"],
["keyword", "config"],
["punctuation", "{"],
["keyword", "terraform"],
["punctuation", "{"]
]

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

Checks for all keywords.
Loading

0 comments on commit c939df8

Please sign in to comment.