Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrockwell committed Feb 4, 2014
2 parents 4980121 + 6f4ffac commit c3a35dd
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 75 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@

### Wait.. What? Why?!

Because sometimes, templates either don't cut it, or you get sick of setting up templates. Behold: Tonto.js with support for 591 apache directives, without leaving the comfort of your Javascript couch.
Becuase sometimes you want to generate apache config files without leaving the comfort of javascript.

### The Tonto Name

The [Tonto (Dilzhę́’é) people](http://itcaonline.com/?page_id=1183) are one of the Western Apache groups from North America. Long ago, their enemies called them "foolish", "wild", "crazy", and "those who you don't understand" for speaking and doing things differently than their neighbors. Today they are know throughout art communities for their superior fine crafting.

### Use Case Examples

1. Server hosts can write a deployment script with Tonto.js to easily customize apache config files for new machines.
2. Wrap this library with your own to provide an easy interface for generating all of your project's apache configs.
1. Server hosts could write a deployment script with Tonto.js that easily customizes an apache config files for each new machine.
2. Wrap Tonto.js with your own library to provide an easy interface for generating your proprietary apache configurations with arguments.
3. Take your wife out for a nice steak dinner, then to a show that *she* wants to see. Tell her it was all made possible because you're smart and made time for her with Tonto.js.

## Installation

```shell
$ npm install tonto --save
```

## Using Tonto.js

```javascript
Expand Down
2 changes: 2 additions & 0 deletions lib/tonto.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*
* ## Installation
*
* You can install Tonto.js either by cloning the main repository with git @ https://github.com/FreeAllMedia/tonto, or by
*
* ```shell
* $ npm install tonto --save
* ```
Expand Down
93 changes: 23 additions & 70 deletions lib/tontoDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,91 +20,44 @@ module.exports = function TontoDirective(_name, _value, _directives) {

'use strict';

/* Dependencies */

var Renderer = require('./tontoDirectiveRenderer.js');

/* Public Interface */

/**
* Name of the directive in the generated config file, i.e. "VirtualHost", or "RewriteEngine"
* @property {string} name
*/
this.name = _name;
this.value = _value;
this.directives = _directives;
this.render = render;

/* Private Instance Variables */
/**
* Value used for the directive, such as "*:80", or "On"
* @property {string} value
*/
this.value = _value;

var self = this,
name = this.name,
value = this.value,
directives = this.directives;
/**
* If a TontoDirectiveCollection object is provided, the directive will render as an SGML-style block containing all rendered _directives.
* @property {TontoDirectiveCollection} directives
*/
this.directives = _directives;

var util = require('util');
/**
* Renders the directive to a valid apache config string.
* @method render
* @returns {string} Valid rendered apache config string.
*/
this.render = render;

/* Private Functions */

function render() {

var generatedString;

if (directives) {
generatedString = renderBlock();
} else {
generatedString = renderSolo(self);
}

return generatedString;

}

function renderBlock() {
var self = this;

var startTag = renderStartTag(),
content = renderBlockContent(),
endTag = renderEndTag();

// This looks weird, but we're really adding newline characters between each rendered sub-directive (which may contain it's own newlines),
// then splitting all of those directives by newline so that a tab can be added to the start of each.

content = addTabination(content);

return [startTag, content, endTag].join('\n');

}

function addTabination(content) {
content = content.split('\n');
content.forEach(addTab);
function addTab(contentLine, index) {
content[index] = '\t' + contentLine;
}
return content.join('\n');
}

function renderStartTag() {
return '<' + name + ' ' + value + '>';
}

function renderEndTag() {
return '</' + name + '>';
}

function renderBlockContent() {

var content = [];

directives.forEach(renderToContent);

function renderToContent(directive) {
content.push(directive.render());
}

return content.join('\n');

}

function renderSolo(directive) {
function render() {

return directive.name + ' ' + directive.value;
return Renderer.render(self);

}

Expand Down
84 changes: 84 additions & 0 deletions lib/tontoDirectiveRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* ```
* _____ ___ _ _ _____ ___
* |_ _/ _ \| \| |_ _/ _ \
* | || (_) | .` | | || (_) |
* |_| \___/|_|\_| |_| \___/.JS
* APACHE CONFIG FILE GENERATOR
* ```
* # TontoDirectiveRenderer.js
*
* Renders TontoDirectives into valid apache config strings.
*
* @class TontoDirectiveRenderer
* @constructor
*/
function TontoDirectiveRenderer() {}

module.exports = TontoDirectiveRenderer;

(function TontoDirectiveNamespace(TontoDirectiveRenderer) {

'use strict';

TontoDirectiveRenderer.render = render;

function render(directive) {

if (directive.directives) {
return renderBlock(directive);
} else {
return renderSolo(directive);
}
}

function renderBlock(directive) {

var startTag = renderStartTag(directive),
content = renderBlockContent(directive),
endTag = renderEndTag(directive);

content = addTabination(content);

return [startTag, content, endTag].join('\n');

}

function addTabination(content) {
content = content.split('\n');
content.forEach(addTab);
function addTab(contentLine, index) {
content[index] = '\t' + contentLine;
}
return content.join('\n');
}

function renderStartTag(directive) {
return '<' + directive.name + ' ' + directive.value + '>';
}

function renderEndTag(directive) {
return '</' + directive.name + '>';
}

function renderBlockContent(directive) {

var content = [];

directive.directives.forEach(renderToContent);

function renderToContent(directive) {
content.push(directive.render());
}

return content.join('\n');

}

function renderSolo(directive) {

return directive.name + ' ' + directive.value;

}

})(TontoDirectiveRenderer);
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
}
],
"scripts": {
"test": "mocha ./tests/*.js -w -R spec"
"test": "mocha -w ./tests/*.js -R spec"
},
"main": "./lib/tonto.js",
"dependencies": {
Expand All @@ -25,9 +25,10 @@
"grunt-complexity": "~0.1.4"
},
"config": {
"travis-cov": { "threshold": 100 },
"blanket": {
"pattern": [
"tonto/lib/*.js"
"/tonto/lib/"
]
}
},
Expand Down

0 comments on commit c3a35dd

Please sign in to comment.