Last active
August 29, 2015 14:06
-
-
Save matori/432871e17f706772eee0 to your computer and use it in GitHub Desktop.
[for Metalsmith] Converting Jade content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Based on metalsmith-jade | |
* https://github.com/MaxBareiss/metalsmith-jade | |
* License MIT (c) 2014 Max Bareiss | |
* */ | |
/* | |
if you need output non-HTML file (like xml), | |
you have to add `ext` metadata key and extension-name string value (like 'xml') to that content. | |
Exmaple: | |
- title: 'My blog RSS feed' | |
- ext: 'xml' | |
and some metadata ... | |
*/ | |
/* | |
this jade converter have `:escape` filter | |
Example: | |
pre | |
code | |
:escape | |
<h1>Escaped Text</h1> | |
*/ | |
var basename = require('path').basename; | |
var join = require('path').join; | |
var dirname = require('path').dirname; | |
var extname = require('path').extname; | |
var jade = require('jade'); | |
var jade_runtime = require('../node_modules/jade/lib/runtime'); | |
var jade_filters = require('../node_modules/jade/lib/filters'); | |
var jade_inline = require('../node_modules/jade/lib/inline-tags'); | |
['ruby', 'rt', 'rb', 'rtc', 'var'].forEach(function (v, i) { | |
jade_inline.push(v); | |
}); | |
jade_filters.escape = function (text) { | |
return jade_runtime.escape(text); | |
}; | |
module.exports = function (options) { | |
options = options || {}; | |
return function (files, metalsmith, done) { | |
setImmediate(done); | |
Object.keys(files).forEach(function (file) { | |
//console.log('checking file: %s', file); | |
if (!isJade(file)) { | |
console.log('%s is not Jade file', file); | |
return; | |
} | |
var data = files[file]; | |
var dir = dirname(file); | |
var outputFile = basename(file, extname(file)) + '.'; | |
outputFile += data.ext ? data.ext.replace(/^\./, '') : 'html'; | |
if ('.' != dir) { | |
outputFile = dir + '/' + outputFile; | |
} | |
console.log('converting file: %s -> ' + outputFile, file); | |
options.filename = join(metalsmith.dir, metalsmith._src, file); | |
var str = jade.compile(data.contents.toString(), options)(options.locals); | |
data.contents = new Buffer(str); | |
delete files[file]; | |
files[outputFile] = data; | |
}); | |
}; | |
}; | |
function isJade(file) { | |
return /\.jade/.test(extname(file)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment