From 0c81da7e92b0b1e2d9c7d543e43346f6c7ba16ae Mon Sep 17 00:00:00 2001 From: ArmorDarks Date: Mon, 6 Feb 2017 17:20:04 +0200 Subject: [PATCH] [task] add initial release of Gray Matter for Grunt task --- CHANGELOG.md | 5 ++++ tasks/grayMatter.coffee | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 tasks/grayMatter.coffee diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6eb3b48 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# Changelog + +## 1.0.0 + +Initial release \ No newline at end of file diff --git a/tasks/grayMatter.coffee b/tasks/grayMatter.coffee new file mode 100644 index 0000000..ec6d92a --- /dev/null +++ b/tasks/grayMatter.coffee @@ -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" \ No newline at end of file