Skip to content

Commit 81933ee

Browse files
committed
Merge pull request #813 from Golmote/prism-puppet
Add support for Puppet configuration
2 parents f5db346 + 0b4e1d3 commit 81933ee

17 files changed

+732
-0
lines changed

components.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@ var components = {
382382
"title": "Prolog",
383383
"owner": "Golmote"
384384
},
385+
"puppet": {
386+
"title": "Puppet",
387+
"owner": "Golmote"
388+
},
385389
"pure": {
386390
"title": "Pure",
387391
"owner": "Golmote"

components/prism-puppet.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
(function (Prism) {
2+
Prism.languages.puppet = {
3+
'heredoc': [
4+
// Matches the content of a quoted heredoc string (subject to interpolation)
5+
{
6+
pattern: /(@\("([^"\r\n\/):]+)"(?:\/[nrts$uL]*)?\).*(?:\r?\n|\r))(?:.*(?:\r?\n|\r))*?[ \t]*\|?[ \t]*-?[ \t]*\2/,
7+
lookbehind: true,
8+
alias: 'string',
9+
inside: {
10+
// Matches the end tag
11+
'punctuation': /(?=\S).*\S(?= *$)/
12+
// See interpolation below
13+
}
14+
},
15+
// Matches the content of an unquoted heredoc string (no interpolation)
16+
{
17+
pattern: /(@\(([^"\r\n\/):]+)(?:\/[nrts$uL]*)?\).*(?:\r?\n|\r))(?:.*(?:\r?\n|\r))*?[ \t]*\|?[ \t]*-?[ \t]*\2/,
18+
lookbehind: true,
19+
alias: 'string',
20+
inside: {
21+
// Matches the end tag
22+
'punctuation': /(?=\S).*\S(?= *$)/
23+
}
24+
},
25+
// Matches the start tag of heredoc strings
26+
{
27+
pattern: /@\("?(?:[^"\r\n\/):]+)"?(?:\/[nrts$uL]*)?\)/,
28+
alias: 'string',
29+
inside: {
30+
'punctuation': {
31+
pattern: /(\().+?(?=\))/,
32+
lookbehind: true
33+
}
34+
}
35+
}
36+
],
37+
'multiline-comment': {
38+
pattern: /(^|[^\\])\/\*[\s\S]*?\*\//,
39+
lookbehind: true,
40+
alias: 'comment'
41+
},
42+
'regex': {
43+
// Must be prefixed with the keyword "node" or a non-word char
44+
pattern: /((?:\bnode\s+|[^\s\w\\]\s*))\/(?:[^\/\\]|\\[\s\S])+\/(?:[imx]+\b|\B)/,
45+
lookbehind: true,
46+
inside: {
47+
// Extended regexes must have the x flag. They can contain single-line comments.
48+
'extended-regex': {
49+
pattern: /^\/(?:[^\/\\]|\\[\s\S])+\/[im]*x[im]*$/,
50+
inside: {
51+
'comment': /#.*/
52+
}
53+
}
54+
}
55+
},
56+
'comment': {
57+
pattern: /(^|[^\\])#.*/,
58+
lookbehind: true
59+
},
60+
'string': {
61+
// Allow for one nested level of double quotes inside interpolation
62+
pattern: /(["'])(?:\$\{(?:[^'"}]|(["'])(?:(?!\2)[^\\]|\\[\s\S])*\2)+\}|(?!\1)[^\\]|\\[\s\S])*\1/,
63+
inside: {
64+
'double-quoted': {
65+
pattern: /^"[\s\S]*"$/,
66+
inside: {
67+
// See interpolation below
68+
}
69+
}
70+
}
71+
},
72+
'variable': {
73+
pattern: /\$(?:::)?\w+(?:::\w+)*/,
74+
inside: {
75+
'punctuation': /::/
76+
}
77+
},
78+
'attr-name': /(?:\w+|\*)(?=\s*=>)/,
79+
'function': [
80+
{
81+
pattern: /(\.)(?!\d)\w+/,
82+
lookbehind: true
83+
},
84+
/\b(?:contain|debug|err|fail|include|info|notice|realize|require|tag|warning)\b|\b(?!\d)\w+(?=\()/
85+
],
86+
'number': /\b(?:0x[a-f\d]+|\d+(?:\.\d+)?(?:e-?\d+)?)\b/i,
87+
'boolean': /\b(?:true|false)\b/,
88+
// Includes words reserved for future use
89+
'keyword': /\b(?:application|attr|case|class|consumes|default|define|else|elsif|function|if|import|inherits|node|private|produces|type|undef|unless)\b/,
90+
'datatype': {
91+
pattern: /\b(?:Any|Array|Boolean|Callable|Catalogentry|Class|Collection|Data|Default|Enum|Float|Hash|Integer|NotUndef|Numeric|Optional|Pattern|Regexp|Resource|Runtime|Scalar|String|Struct|Tuple|Type|Undef|Variant)\b/,
92+
alias: 'symbol'
93+
},
94+
'operator': /=[=~>]?|![=~]?|<(?:<\|?|[=~|-])?|>[>=]?|->?|~>|\|>?>?|[*\/%+?]|\b(?:and|in|or)\b/,
95+
'punctuation': /[\[\]{}().,;]|:+/
96+
};
97+
98+
var interpolation = [
99+
{
100+
// Allow for one nested level of braces inside interpolation
101+
pattern: /(^|[^\\])\$\{(?:[^'"{}]|\{[^}]*\}|(["'])(?:(?!\2)[^\\]|\\[\s\S])*\2)+\}/,
102+
lookbehind: true,
103+
inside: {
104+
'short-variable': {
105+
// Negative look-ahead prevent wrong highlighting of functions
106+
pattern: /(^\$\{)(?!\w+\()(?:::)?\w+(?:::\w+)*/,
107+
lookbehind: true,
108+
alias: 'variable',
109+
inside: {
110+
'punctuation': /::/
111+
}
112+
},
113+
'delimiter': {
114+
pattern: /^\$/,
115+
alias: 'variable'
116+
},
117+
rest: Prism.util.clone(Prism.languages.puppet)
118+
}
119+
},
120+
{
121+
pattern: /(^|[^\\])\$(?:::)?\w+(?:::\w+)*/,
122+
lookbehind: true,
123+
alias: 'variable',
124+
inside: {
125+
'punctuation': /::/
126+
}
127+
}
128+
];
129+
Prism.languages.puppet['heredoc'][0].inside.interpolation = interpolation;
130+
Prism.languages.puppet['string'].inside['double-quoted'].inside.interpolation = interpolation;
131+
}(Prism));

components/prism-puppet.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/prism-puppet.html

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<h1>Puppet</h1>
2+
<p>To use this language, use the class "language-puppet".</p>
3+
4+
<h2>Comments</h2>
5+
<pre><code>#
6+
# Foobar
7+
/* Foo
8+
bar */</code></pre>
9+
10+
<h2>Strings and interpolation</h2>
11+
<pre><code>'foo \'bar\' baz'
12+
"$foo \"bar\" ${baz}"
13+
14+
@(FOOBAR) # Unquoted heredoc string
15+
Foo bar baz
16+
FOOBAR
17+
18+
@("BARBAZ"/$L) # Quoted heredoc string
19+
$foo bar ${baz}
20+
|-BARBAZ</code></pre>
21+
22+
<h2>Regular expressions</h2>
23+
<pre><code>if $host =~ /^www(\d+)\./ {}
24+
$foo = /foo
25+
bar # Extended regexes can include comments
26+
baz/x</code></pre>
27+
28+
<h2>Variables</h2>
29+
<pre><code>$foo
30+
$::foobar
31+
$foo::bar::baz</code></pre>
32+
33+
<h2>Functions</h2>
34+
<pre><code>require apache
35+
template('apache/vhost-default.conf.erb')
36+
[1,20,3].filter |$value| { $value < 10 }</code></pre>
37+
38+
<h2>All-in-one example</h2>
39+
<pre><code>file {'ntp.conf':
40+
path => '/etc/ntp.conf',
41+
ensure => file,
42+
content => template('ntp/ntp.conf'),
43+
owner => 'root',
44+
mode => '0644',
45+
}
46+
package {'ntp':
47+
ensure => installed,
48+
before => File['ntp.conf'],
49+
}
50+
service {'ntpd':
51+
ensure => running,
52+
subscribe => File['ntp.conf'],
53+
}
54+
Package['ntp'] -> File['ntp.conf'] ~> Service['ntpd']
55+
56+
$package_list = ['ntp', 'apache2', 'vim-nox', 'wget']
57+
$myhash = { key => { subkey => 'b' }}
58+
59+
include ntp
60+
require ntp
61+
class {'ntp':}
62+
63+
define apache::vhost ($port, $docroot, $servername = $title, $vhost_name = '*') {
64+
include apache
65+
include apache::params
66+
$vhost_dir = $apache::params::vhost_dir
67+
file { "${vhost_dir}/${servername}.conf":
68+
content => template('apache/vhost-default.conf.erb'),
69+
owner => 'www',
70+
group => 'www',
71+
mode => '644',
72+
require => Package['httpd'],
73+
notify => Service['httpd'],
74+
}
75+
}
76+
77+
apache::vhost {'homepages':
78+
port => 8081,
79+
docroot => '/var/www-testhost',
80+
}
81+
Apache::Vhost['homepages']
82+
83+
node 'www1.example.com' {
84+
include common
85+
include apache
86+
include squid
87+
}
88+
node /^www\d+$/ {
89+
include common
90+
}
91+
92+
# comment
93+
/* comment */
94+
95+
if $is_virtual {
96+
warning( 'Tried to include class ntp on virtual machine; this node may be misclassified.' )
97+
}
98+
elsif $operatingsystem == 'Darwin' {
99+
warning( 'This NTP module does not yet work on our Mac laptops.' )
100+
else {
101+
include ntp
102+
}
103+
104+
if $hostname =~ /^www(\d+)\./ {
105+
notify { "Welcome web server $1": }
106+
}
107+
108+
case $operatingsystem {
109+
'Solaris': { include role::solaris }
110+
'RedHat', 'CentOS': { include role::redhat }
111+
/^(Debian|Ubuntu)$/:{ include role::debian }
112+
default: { include role::generic }
113+
}
114+
$rootgroup = $osfamily ? {
115+
'Solaris' => 'wheel',
116+
/(Darwin|FreeBSD)/ => 'wheel',
117+
default => 'root',
118+
}
119+
120+
User <| groups == 'admin' |>
121+
Concat::Fragment <<| tag == "bacula-storage-dir-${bacula_director}" |>>
122+
123+
Exec <| title == 'update_migrations' |> {
124+
environment => 'RUBYLIB=/usr/lib/ruby/site_ruby/1.8/',
125+
}
126+
127+
@user {'deploy':
128+
uid => 2004,
129+
comment => 'Deployment User',
130+
group => www-data,
131+
groups => ["enterprise"],
132+
tag => [deploy, web],
133+
}
134+
135+
@@nagios_service { "check_zfs${hostname}":
136+
use => 'generic-service',
137+
host_name => "$fqdn",
138+
check_command => 'check_nrpe_1arg!check_zfs',
139+
service_description => "check_zfs${hostname}",
140+
target => '/etc/nagios3/conf.d/nagios_service.cfg',
141+
notify => Service[$nagios::params::nagios_service],
142+
}</code></pre>
143+
144+
<h2>Known failures</h2>
145+
<p>There are certain edge cases where Prism will fail.
146+
There are always such cases in every regex-based syntax highlighter.
147+
However, Prism dares to be open and honest about them.
148+
If a failure is listed here, it doesn’t mean it will never be fixed. This is more of a “known bugs” list, just with a certain type of bug.
149+
</p>
150+
151+
<h3>Comments, regular expressions or substrings that look like heredoc strings</h3>
152+
<pre><code>/* @(foo) */
153+
# @(foo)
154+
" @(foo) "
155+
$foo = /@(foo)/</code></pre>
156+
157+
<h3>Single-line comments or substrings that look like multi-line comments</h3>
158+
<pre><code># foo /* bar */ baz
159+
"foo /* bar */ baz"</code></pre>
160+
161+
<h3>Substrings that look like single-line comment</h3>
162+
<pre><code>"foo #bar baz"</code></pre>
163+
164+
<h3>More than one level of nested braces inside interpolation</h3>
165+
<pre><code>"Foobar ${foo({
166+
bar => {baz => 42}
167+
baz => 42
168+
})} <- broken"</code></pre>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
foo {
2+
bar => bar,
3+
* => {}
4+
}
5+
6+
----------------------------------------------------
7+
8+
[
9+
"foo ", ["punctuation", "{"],
10+
["attr-name", "bar"], ["operator", "=>"],
11+
" bar", ["punctuation", ","],
12+
["attr-name", "*"], ["operator", "=>"],
13+
["punctuation", "{"], ["punctuation", "}"],
14+
["punctuation", "}"]
15+
]
16+
17+
----------------------------------------------------
18+
19+
Checks for attributes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
true
2+
false
3+
4+
----------------------------------------------------
5+
6+
[
7+
["boolean", "true"],
8+
["boolean", "false"]
9+
]
10+
11+
----------------------------------------------------
12+
13+
Checks for booleans.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# Foobar
3+
/* Foo
4+
bar */
5+
6+
----------------------------------------------------
7+
8+
[
9+
["comment", "#"],
10+
["comment", "# Foobar"],
11+
["multiline-comment", "/* Foo\r\nbar */"]
12+
]
13+
14+
----------------------------------------------------
15+
16+
Checks for comments.

0 commit comments

Comments
 (0)