UNPKG

grunt-pugpig-contents-xml

Version:
124 lines (97 loc) 3.62 kB
/* * grunt-pugpig-contents-xml * * Generate contents atom feed for PugPig. * * Copyright (c) 2014 Daniel Furze * Licensed under the MIT license */ 'use strict'; module.exports = function (grunt) { // Require dependencies var builder = require('xmlbuilder'), fs = require('fs'), superb = require('superb'), chalk = require('chalk'); var pkg = grunt.file.readJSON('./package.json'); grunt.registerMultiTask('pugpig_contents_xml', 'Generate content XML file for PugPig.', function () { // Merge task-specific and/or target-specific options with these defaults var options = this.options({ author: pkg.author.name, dest: 'tmp/', fileName: 'content', id: '123ABC', subtitle: 'Subtitle goes here', title: 'Title goes here' }); var root = require('xmlbuilder').create('root', {version: '1.0', encoding: 'UTF-8', standalone: true}, {pubID: null, sysID: null}, {allowSurrogateChars: false, skipNullAttributes: false, headless: false, ignoreDecorators: false, stringify: {}}); // Build XML string var feed = builder.create('feed', { version: '1.0', encoding: 'UTF-8' }); feed.att('xmlns', 'http://www.w3.org/2005/Atom'); feed.ele('id').txt(options.id); feed.ele('link').att({ 'rel': 'self', 'type': 'application/atom+xml', 'href': options.fileName + '.xml' }); feed.ele('title').att('type', 'text').txt(options.title); feed.ele('subtitle').att('type', 'text').txt(options.subtitle); feed.ele('updated').txt(grunt.template.today('yyyy-mm-dd\'T\'HH:MM:ss+00:00')); feed.ele('author').ele('name').txt(options.author); this.filesSrc.forEach(function(fullPath) { //@todo: Tidy up // Remove directory var filePath = fullPath.split('.')[0], // Get extension fileExt = fullPath.split('.')[1], // Split into parts filePathParts = filePath.split('/'), fileName = filePath.split('/')[filePathParts.length - 1], entryType = fileName.split('-')[0], entryName = fileName.split('-')[1]; // Create XML for each entry // @todo: Tidy up this hack! ... feed.raw('<entry>'); // var entry = builder.create('entry', { // headless: true // }); feed.ele('id').txt(options.id + ',' + entryType + ':/' + entryName); // ...and change these back on to entry feed.ele('link').att({ 'rel': 'alternate', 'type': 'text/html', 'href': fileName + '.' + fileExt }); feed.ele('title').att('type', 'text').txt(entryName + ' ' + entryType); feed.ele('summary').att('type', 'text').txt(entryName + ' ' + entryType + ' summary'); feed.ele('updated').txt(grunt.template.today('yyyy-mm-dd\'T\'HH:MM:ss+00:00')); feed.ele('published').txt(grunt.template.today('yyyy-mm-dd\'T\'HH:MM:ss+00:00')); // var entryString = feed.end({ // pretty: true, // indent: ' ', // newline: '\n' // }); // feed.ele(entryString); feed.raw('</entry>'); }); // Format XML string var xmlString = feed.end({ pretty: true, indent: ' ', newline: '\n' }); var fileName = options.fileName + '.xml'; // Write XML to file grunt.file.write(options.dest + fileName, xmlString); // Format and print a success message! var sup = superb(); sup = sup.charAt(0).toUpperCase() + sup.slice(1) + '!'; grunt.log.writeln(chalk.yellow(sup) + chalk.blue(' File "' + options.dest + fileName + '" created.')); }); };