metalsmith-jstransformer
Advanced tools
Comparing version 0.0.3 to 0.1.0
74
index.js
@@ -1,49 +0,47 @@ | ||
var minimatch = require('minimatch'); | ||
var jstransformer = require('jstransformer'); | ||
var path = require('path'); | ||
var merge = require('merge'); | ||
var jstransformer = require('jstransformer') | ||
var transformers = {} | ||
module.exports = function (opts) { | ||
var transformers = {}; | ||
// Load all the required JSTransformers. | ||
for (var i in opts || {}) { | ||
var name = opts[i]; | ||
transformers[name] = jstransformer(require('jstransformer-' + name)); | ||
/** | ||
* Get the transformer from the given name. | ||
*/ | ||
function getTransformer (name) { | ||
if (transformers[name]) { | ||
return transformers[name] | ||
} | ||
transformers[name] = jstransformer(require('jstransformer-' + name)) | ||
return transformers[name] | ||
} | ||
module.exports = function (opts) { | ||
return function (files, metalsmith, done) { | ||
for (var transform in transformers) { | ||
// Loop through every file. | ||
for (var file in files) { | ||
// Get all the transforming extensions. | ||
var extensions = file.split('.') | ||
// Only process if there are more than 2 extensions. | ||
if (extensions.length > 2) { | ||
var transforms = extensions.splice(2) | ||
// Loop through backwards through each extension. | ||
for (var i = transforms.length - 1; i >= 0; i--) { | ||
// Retrieve the transform name. | ||
var transformName = transforms[i] | ||
var transformer = getTransformer(transformName) | ||
// Find all files that match the given transformer. | ||
var transformFiles = minimatch.match(Object.keys(files), "*." + transform, { | ||
matchBase: true | ||
}); | ||
for (var i in transformFiles) { | ||
var filename = transformFiles[i]; | ||
var data = files[filename]; | ||
var dir = path.dirname(filename); | ||
var locals = merge(data, metalsmith.metadata()); | ||
// Construct the new file name. | ||
var name = path.basename(filename, path.extname(filename)); | ||
// Default to an .html file extension if none exists. | ||
if (path.extname(name) === '') { | ||
name = path.basename(filename, path.extname(filename)) + '.html'; | ||
// Process the content of the file with the transformer. | ||
var input = files[file].contents.toString() | ||
// TODO: Figure out what to do for options. | ||
var output = transformer.render(input, files[file], files[file]) | ||
files[file].contents = new Buffer(output.body) | ||
} | ||
// Process the file contents using the transformer. | ||
var result = transformers[transform].render(data.contents.toString('utf-8'), locals); | ||
data.contents = new Buffer(result.body); | ||
// Replace the file with the newly processed one. | ||
delete files[filename]; | ||
var finalPath = path.join(dir, name); | ||
files[finalPath] = data; | ||
// Now that content is updated, rename without the extensions. | ||
var data = files[file] | ||
delete files[file] | ||
var finalPath = extensions[0] + '.' + extensions[1] | ||
files[finalPath] = data | ||
} | ||
} | ||
done(); | ||
done() | ||
} | ||
}; | ||
} |
{ | ||
"name": "metalsmith-jstransformer", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Metalsmith JSTransformer Plugin", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "standard index.js test/index.js; mocha" | ||
}, | ||
@@ -27,13 +27,14 @@ "repository": { | ||
"dependencies": { | ||
"jstransformer": "^0.0.2", | ||
"merge": "^1.2.0", | ||
"minimatch": "^2.0.4" | ||
"jstransformer": "^0.0.2" | ||
}, | ||
"devDependencies": { | ||
"assert-dir-equal": "*", | ||
"jstransformer-coffee-script": "*", | ||
"jstransformer-jade": "*", | ||
"jstransformer-styl": "^0.1.0", | ||
"jstransformer-styl": "*", | ||
"jstransformer-uglify-js": "git://github.com/jstransformers/jstransformer-uglify-js.git#master", | ||
"metalsmith": "^1.7.0", | ||
"mocha": "*" | ||
"mocha": "*", | ||
"standard": "^4.3.3" | ||
} | ||
} |
@@ -1,8 +0,7 @@ | ||
# Metalsmith JSTransformer Plugin | ||
# Metalsmith JSTransformer Plugin [![NPM version](https://img.shields.io/npm/v/metalsmith-jstransformer.svg)](https://www.npmjs.org/package/metalsmith-jstransformer) | ||
[![Build Status](https://img.shields.io/travis/RobLoach/metalsmith-jstransformer/master.svg)](https://travis-ci.org/RobLoach/metalsmith-jstransformer) | ||
[![Dependency Status](https://david-dm.org/RobLoach/metalsmith-jstransformer.png)](https://david-dm.org/RobLoach/metalsmith-jstransformer) | ||
[![NPM version](https://img.shields.io/npm/v/metalsmith-jstransformer.svg)](https://www.npmjs.org/package/metalsmith-jstransformer) | ||
[Metalsmith](http://metalsmith.io) plugin to process files with [JSTransformer](https://github.com/jstransformers/jstransformer), using [any available transformer](https://www.npmjs.com/browse/keyword/jstransformer). | ||
[Metalsmith](http://metalsmith.io) plugin to process files with any [JSTransformer](http://github.com/jstransformers). | ||
@@ -15,9 +14,14 @@ ## Installation | ||
If you haven't checked out [Metalsmith](http://metalsmith.io) before, head over | ||
to their website and check out the documentation. | ||
Create files that you would like to act on with JSTransformers with file extensions representing the transformer to use, in the format `example.html.<transformer>`. For example, if you would like to process with Jade, you would name it `example.html.jade`. | ||
First, create a file that you would like to act on with JSTransformers, with | ||
the file name of "example.html.<pluginname>". In the following Jade example, it | ||
would be `example.html.jade`. | ||
Use multiple transformers by appending additional file extension transformer names at the end. For example, to [HTML-Minifier](https://github.com/jstransformers/jstransformer-html-minifier) our Jade example above, you would use the filename `example.html.html-minifier.jade`. | ||
``` | ||
example.html.html-minifier.jade | ||
example.html.html-minifier | ||
example.html | ||
``` | ||
Jade -> HTML-Minifier -> example.html | ||
### CLI | ||
@@ -31,5 +35,3 @@ | ||
"plugins": { | ||
"metalsmith-jstransformer": [ | ||
"jade" | ||
] | ||
"metalsmith-jstransformer": {} | ||
} | ||
@@ -47,13 +49,7 @@ } | ||
metalsmith.use(jstransformer([ | ||
"jade" | ||
])); | ||
metalsmith.use(jstransformer()); | ||
``` | ||
## Options | ||
## License | ||
An array of strings representing which JSTransformers to use. Example: | ||
```javascript | ||
["jade", "scss"] | ||
``` | ||
MIT |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5507
1
5
42
8
53
- Removedmerge@^1.2.0
- Removedminimatch@^2.0.4
- Removedbalanced-match@1.0.2(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedmerge@1.2.1(transitive)
- Removedminimatch@2.0.10(transitive)