Permalink
Browse files

[puppet mode] Add

  • Loading branch information...
1 parent 712f28c commit 91d93fdb988457b7021d49007c25a31f3bbdf81c @nextrevision nextrevision committed with marijnh Feb 8, 2014
Showing with 328 additions and 0 deletions.
  1. +1 −0 doc/compress.html
  2. +1 −0 mode/index.html
  3. +1 −0 mode/meta.js
  4. +121 −0 mode/puppet/index.html
  5. +204 −0 mode/puppet/puppet.js
View
1 doc/compress.html
@@ -126,6 +126,7 @@
<option value="http://codemirror.net/mode/pig/pig.js">pig.js</option>
<option value="http://codemirror.net/mode/properties/properties.js">properties.js</option>
<option value="http://codemirror.net/mode/python/python.js">python.js</option>
+ <option value="http://codemirror.net/mode/puppet/puppet.js">puppet.js</option>
<option value="http://codemirror.net/mode/q/q.js">q.js</option>
<option value="http://codemirror.net/mode/r/r.js">r.js</option>
<option value="http://codemirror.net/mode/rpm/changes/changes.js">rpm/changes.js</option>
View
1 mode/index.html
@@ -77,6 +77,7 @@
<li><a href="php/index.html">PHP</a></li>
<li><a href="pig/index.html">Pig Latin</a></li>
<li><a href="properties/index.html">Properties files</a></li>
+ <li><a href="puppet/index.html">Puppet</a></li>
<li><a href="python/index.html">Python</a></li>
<li><a href="q/index.html">Q</a></li>
<li><a href="r/index.html">R</a></li>
View
1 mode/meta.js
@@ -57,6 +57,7 @@ CodeMirror.modeInfo = [
{name: 'Plain Text', mime: 'text/plain', mode: 'null'},
{name: 'Properties files', mime: 'text/x-properties', mode: 'properties'},
{name: 'Python', mime: 'text/x-python', mode: 'python'},
+ {name: 'Puppet', mime: 'text/x-puppet', mode: 'puppet'},
{name: 'Cython', mime: 'text/x-cython', mode: 'python'},
{name: 'R', mime: 'text/x-rsrc', mode: 'r'},
{name: 'reStructuredText', mime: 'text/x-rst', mode: 'rst'},
View
121 mode/puppet/index.html
@@ -0,0 +1,121 @@
+<!doctype html>
+
+<title>CodeMirror: Puppet mode</title>
+<meta charset="utf-8"/>
+<link rel=stylesheet href="../../doc/docs.css">
+
+<link rel="stylesheet" href="../../lib/codemirror.css">
+<script src="../../lib/codemirror.js"></script>
+<script src="../../addon/edit/matchbrackets.js"></script>
+<script src="puppet.js"></script>
+<style>
+ .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
+ .cm-s-default span.cm-arrow { color: red; }
+ </style>
+<div id=nav>
+ <a href="http://codemirror.net"><img id=logo src="../../doc/logo.png"></a>
+
+ <ul>
+ <li><a href="../../index.html">Home</a>
+ <li><a href="../../doc/manual.html">Manual</a>
+ <li><a href="https://github.com/marijnh/codemirror">Code</a>
+ </ul>
+ <ul>
+ <li><a href="../index.html">Language modes</a>
+ <li><a class=active href="#">Puppet</a>
+ </ul>
+</div>
+
+<article>
+<h2>Puppet mode</h2>
+<form><textarea id="code" name="code">
+# == Class: automysqlbackup
+#
+# Puppet module to install AutoMySQLBackup for periodic MySQL backups.
+#
+# class { 'automysqlbackup':
+# backup_dir => '/mnt/backups',
+# }
+#
+
+class automysqlbackup (
+ $bin_dir = $automysqlbackup::params::bin_dir,
+ $etc_dir = $automysqlbackup::params::etc_dir,
+ $backup_dir = $automysqlbackup::params::backup_dir,
+ $install_multicore = undef,
+ $config = {},
+ $config_defaults = {},
+) inherits automysqlbackup::params {
+
+# Ensure valid paths are assigned
+ validate_absolute_path($bin_dir)
+ validate_absolute_path($etc_dir)
+ validate_absolute_path($backup_dir)
+
+# Create a subdirectory in /etc for config files
+ file { $etc_dir:
+ ensure => directory,
+ owner => 'root',
+ group => 'root',
+ mode => '0750',
+ }
+
+# Create an example backup file, useful for reference
+ file { "${etc_dir}/automysqlbackup.conf.example":
+ ensure => file,
+ owner => 'root',
+ group => 'root',
+ mode => '0660',
+ source => 'puppet:///modules/automysqlbackup/automysqlbackup.conf',
+ }
+
+# Add files from the developer
+ file { "${etc_dir}/AMB_README":
+ ensure => file,
+ source => 'puppet:///modules/automysqlbackup/AMB_README',
+ }
+ file { "${etc_dir}/AMB_LICENSE":
+ ensure => file,
+ source => 'puppet:///modules/automysqlbackup/AMB_LICENSE',
+ }
+
+# Install the actual binary file
+ file { "${bin_dir}/automysqlbackup":
+ ensure => file,
+ owner => 'root',
+ group => 'root',
+ mode => '0755',
+ source => 'puppet:///modules/automysqlbackup/automysqlbackup',
+ }
+
+# Create the base backup directory
+ file { $backup_dir:
+ ensure => directory,
+ owner => 'root',
+ group => 'root',
+ mode => '0755',
+ }
+
+# If you'd like to keep your config in hiera and pass it to this class
+ if !empty($config) {
+ create_resources('automysqlbackup::backup', $config, $config_defaults)
+ }
+
+# If using RedHat family, must have the RPMforge repo's enabled
+ if $install_multicore {
+ package { ['pigz', 'pbzip2']: ensure => installed }
+ }
+
+}
+</textarea></form>
+ <script>
+ var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
+ mode: "text/x-puppet",
+ matchBrackets: true,
+ indentUnit: 4
+ });
+ </script>
+
+ <p><strong>MIME types defined:</strong> <code>text/x-puppet</code>.</p>
+
+ </article>
View
204 mode/puppet/puppet.js
@@ -0,0 +1,204 @@
+CodeMirror.defineMode("puppet", function () {
+ // Stores the words from the define method
+ var words = {};
+ // Taken, mostly, from the Puppet official variable standards regex
+ var variable_regex = /({)?([a-z][a-z0-9_]*)?((::[a-z][a-z0-9_]*)*::)?[a-zA-Z0-9_]+(})?/;
+
+ // Takes a string of words separated by spaces and adds them as
+ // keys with the value of the first argument 'style'
+ function define(style, string) {
+ var split = string.split(' ');
+ for (var i = 0; i < split.length; i++) {
+ words[split[i]] = style;
+ }
+ }
+
+ // Takes commonly known puppet types/words and classifies them to a style
+ define('keyword', 'class define site node include import inherits');
+ define('keyword', 'case if else in and elsif default or');
+ define('atom', 'false true running present absent file directory undef');
+ define('builtin', 'action augeas burst chain computer cron destination dport exec ' +
+ 'file filebucket group host icmp iniface interface jump k5login limit log_level ' +
+ 'log_prefix macauthorization mailalias maillist mcx mount nagios_command ' +
+ 'nagios_contact nagios_contactgroup nagios_host nagios_hostdependency ' +
+ 'nagios_hostescalation nagios_hostextinfo nagios_hostgroup nagios_service ' +
+ 'nagios_servicedependency nagios_serviceescalation nagios_serviceextinfo ' +
+ 'nagios_servicegroup nagios_timeperiod name notify outiface package proto reject ' +
+ 'resources router schedule scheduled_task selboolean selmodule service source ' +
+ 'sport ssh_authorized_key sshkey stage state table tidy todest toports tosource ' +
+ 'user vlan yumrepo zfs zone zpool');
+
+ // After finding a start of a string ('|") this function attempts to find the end;
+ // If a variable is encountered along the way, we display it differently when it
+ // is encapsulated in a double-quoted string.
+ function tokenString(stream, state) {
+ var current, prev, found_var = false;
+ while (!stream.eol() && (current = stream.next()) != state.pending) {
+ if (current === '$' && prev != '\\' && state.pending == '"') {
+ found_var = true;
+ break;
+ }
+ prev = current;
+ }
+ if (found_var) {
+ stream.backUp(1);
+ }
+ if (current == state.pending) {
+ state.continueString = false;
+ } else {
+ state.continueString = true;
+ }
+ return "string";
+ }
+
+ // Main function
+ function tokenize(stream, state) {
+ // Matches one whole word
+ var word = stream.match(/[\w]+/, false);
+ // Matches attributes (i.e. ensure => present ; 'ensure' would be matched)
+ var attribute = stream.match(/(\s+)?\w+\s+=>.*/, false);
+ // Matches non-builtin resource declarations
+ // (i.e. "apache::vhost {" or "mycustomclasss {" would be matched)
+ var resource = stream.match(/(\s+)?[\w:_]+(\s+)?{/, false);
+ // Matches virtual and exported resources (i.e. @@user { ; and the like)
+ var special_resource = stream.match(/(\s+)?[@]{1,2}[\w:_]+(\s+)?{/, false);
+
+ // Finally advance the stream
+ var ch = stream.next();
+
+ // Have we found a variable?
+ if (ch === '$') {
+ if (stream.match(variable_regex)) {
+ // If so, and its in a string, assign it a different color
+ return state.continueString ? 'variable-2' : 'variable';
+ }
+ // Otherwise return an invalid variable
+ return "error";
+ }
+ // Should we still be looking for the end of a string?
+ if (state.continueString) {
+ // If so, go through the loop again
+ stream.backUp(1);
+ return tokenString(stream, state);
+ }
+ // Are we in a definition (class, node, define)?
+ if (state.inDefinition) {
+ // If so, return def (i.e. for 'class myclass {' ; 'myclass' would be matched)
+ if (stream.match(/(\s+)?[\w:_]+(\s+)?/)) {
+ return 'def';
+ }
+ // Match the rest it the next time around
+ stream.match(/\s+{/);
+ state.inDefinition = false;
+ }
+ // Are we in an 'include' statement?
+ if (state.inInclude) {
+ // Match and return the included class
+ stream.match(/(\s+)?\S+(\s+)?/);
+ state.inInclude = false;
+ return 'def';
+ }
+ // Do we just have a function on our hands?
+ // In 'ensure_resource("myclass")', 'ensure_resource' is matched
+ if (stream.match(/(\s+)?\w+\(/)) {
+ stream.backUp(1);
+ return 'def';
+ }
+ // Have we matched the prior attribute regex?
+ if (attribute) {
+ stream.match(/(\s+)?\w+/);
+ return 'tag';
+ }
+ // Do we have Puppet specific words?
+ if (word && words.hasOwnProperty(word)) {
+ // Negates the initial next()
+ stream.backUp(1);
+ // Acutally move the stream
+ stream.match(/[\w]+/);
+ // We want to process these words differently
+ // do to the importance they have in Puppet
+ if (stream.match(/\s+\S+\s+{/, false)) {
+ state.inDefinition = true;
+ }
+ if (word == 'include') {
+ state.inInclude = true;
+ }
+ // Returns their value as state in the prior define methods
+ return words[word];
+ }
+ // Is there a match on a reference?
+ if (/(\s+)?[A-Z]/.test(word)) {
+ // Negate the next()
+ stream.backUp(1);
+ // Match the full reference
+ stream.match(/(\s+)?[A-Z][\w:_]+/);
+ return 'def';
+ }
+ // Have we matched the prior resource regex?
+ if (resource) {
+ stream.match(/(\s+)?[\w:_]+/);
+ return 'def';
+ }
+ // Have we matched the prior special_resource regex?
+ if (special_resource) {
+ stream.match(/(\s+)?[@]{1,2}/);
+ return 'special';
+ }
+ // Match all the comments. All of them.
+ if (ch == "#") {
+ stream.skipToEnd();
+ return "comment";
+ }
+ // Have we found a string?
+ if (ch == "'" || ch == '"') {
+ // Store the type (single or double)
+ state.pending = ch;
+ // Perform the looping function to find the end
+ return tokenString(stream, state);
+ }
+ // Match all the brackets
+ if (ch == '{' || ch == '}') {
+ return 'bracket';
+ }
+ // Match characters that we are going to assume
+ // are trying to be regex
+ if (ch == '/') {
+ stream.match(/.*\//);
+ return 'variable-3';
+ }
+ // Match all the numbers
+ if (ch.match(/[0-9]/)) {
+ stream.eatWhile(/[0-9]+/);
+ return 'number';
+ }
+ // Match the '=' and '=>' operators
+ if (ch == '=') {
+ if (stream.peek() == '>') {
+ stream.next();
+ }
+ return "operator";
+ }
+ // Keep advancing through all the rest
+ stream.eatWhile(/[\w-]/);
+ // Return a blank line for everything else
+ return null;
+ }
+ // Start it all
+ return {
+ startState: function () {
+ var state = {};
+ state.inDefinition = false;
+ state.inInclude = false;
+ state.continueString = false;
+ state.pending = false;
+ return state;
+ },
+ token: function (stream, state) {
+ // Strip the spaces, but regex will account for them eitherway
+ if (stream.eatSpace()) return null;
+ // Go through the main process
+ return tokenize(stream, state);
+ }
+ };
+});
+CodeMirror.defineMIME("text/x-puppet", "puppet");

0 comments on commit 91d93fd

Please sign in to comment.