-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[task] add initial release of Gray Matter for Grunt task
- Loading branch information
1 parent
8000511
commit 0c81da7
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Changelog | ||
|
||
## 1.0.0 | ||
|
||
Initial release |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
matter = require('gray-matter') | ||
{ set } = require('lodash') | ||
{ cyan } = require('chalk') | ||
|
||
module.exports = (grunt) -> | ||
|
||
grunt.registerMultiTask 'grayMatter', 'Extract data from specified files with Gray Matter', () -> | ||
options = @options( | ||
baseDir: '' | ||
preprocessPath: undefined | ||
preprocessMatterData: undefined | ||
preprocessData: undefined | ||
replacer: null | ||
space: 2 | ||
parser: undefined | ||
eval: false | ||
lang: undefined | ||
delims: undefined | ||
) | ||
|
||
if not @files.length | ||
grunt.log.error('No files specified.') | ||
return | ||
|
||
data = {} | ||
filedest = null | ||
processedFiles = [] | ||
|
||
@files.forEach (file) => | ||
filedest = file.orig.dest | ||
|
||
if not file.src.length | ||
grunt.log.error("No source files specified for #{cyan(filedest)}.") | ||
return | ||
|
||
file.src.forEach (src) => | ||
matterData = matter.read(src, options).data | ||
path = src.replace(options.baseDir, '') | ||
|
||
if typeof options.preprocessPath == 'function' | ||
path = options.preprocessPath.call(file, path, src) | ||
|
||
if typeof options.preprocessMatterData == 'function' | ||
matterData = options.preprocessMatterData.call(file, matterData, path, src) | ||
|
||
set(data, path, matterData) | ||
processedFiles.push(src) | ||
|
||
if typeof options.preprocessData == 'function' | ||
data = options.preprocessData.call(@, data) | ||
|
||
grunt.file.write(filedest, JSON.stringify(data, options.replacer, options.space)) | ||
|
||
grunt.log.ok "#{cyan(processedFiles.length)} files processed" | ||
grunt.verbose.ok "#{processedFiles.map((file) => "\nProcessed: #{cyan(file)}")}" | ||
grunt.verbose.ok "File #{cyan(filedest)} created" |