grunt-pugpig-editions-xml
Version:
Generate editions XML file for PugPig publications.
103 lines (84 loc) • 3.16 kB
JavaScript
/*
* grunt-pugpig-editions-xml
*
*
* Copyright (c) 2014 Daniel Furze
* Licensed under the MIT license
*
* @todo: multiple entries
*
*/
;
module.exports = function (grunt) {
// Require dependencies
var builder = require('xmlbuilder'),
fs = require('fs'),
superb = require('superb'),
chalk = require('chalk');
grunt.registerMultiTask('pugpig_editions_xml', 'Generate editions XML file for PugPig.', function () {
// Merge task-specific and/or target-specific options with these defaults
var options = this.options({
fileName: 'editions'
});
// Build XML string
var editionsXml = builder.create('feed', {
version: '1.0',
encoding: 'UTF-8'
});
editionsXml.att('xmlns', 'http://www.w3.org/2005/Atom');
editionsXml.att('xmlns:dcterms', 'http://purl.org/dc/terms/');
editionsXml.att('xmlns:opds', 'http://opds-spec.org/2010/catalog');
editionsXml.att('xmlns:app', 'http://www.w3.org/2007/app');
editionsXml.ele('id').txt(options.id);
editionsXml.ele('link').att({
'rel': 'self',
'type': 'application/atom+xml;profile=opds-catalog;kind=acquisition',
'href': options.selfLink
});
editionsXml.ele('title').txt(options.title);
editionsXml.ele('updated').txt(grunt.template.today('yyyy-mm-dd\'T\'HH:MM:ss\'Z\''));
editionsXml.ele('author').ele('name').txt(options.author);
//@todo: clean up this hack!
editionsXml.raw('<entry>');
// var entry = builder.create('entry', {
// headless: true
// });
editionsXml.ele('title').txt(options.title + ' ' + options.editionName); // ...and move these back on to entry
editionsXml.ele('id').txt(options.id + '.' + options.editionName);
editionsXml.ele('updated').txt(grunt.template.today('yyyy-mm-dd\'T\'HH:MM:ss\'Z\''));
editionsXml.ele('dcterms:issued').txt(grunt.template.today('yyyy-mm-dd'));
editionsXml.ele('author').ele('name').txt(options.author);
editionsXml.ele('summary').att('type', 'text').txt(options.summaryText);
editionsXml.ele('link').att({
'rel': 'http://opds-spec.org/image',
'type': 'image/jpg',
'href': options.coverSrc
});
editionsXml.ele('link').att({
'rel': 'http://opds-spec.org/acquisition',
'type': 'application/pugpigpkg+xml',
'href': options.issuesSrc
});
editionsXml.ele('link').att({
'rel': 'alternate',
'type': 'application/pugpigpkg+xml',
'href': options.issuesSrc
});
// var entryString = feed.end({
// pretty: true,
// indent: ' ',
// newline: '\n'
// });
// feed.ele(entryString);
editionsXml.raw('</entry>');
// Format XML string
var xmlString = editionsXml.end({ pretty: true, indent: ' ', newline: '\n' }),
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.'));
});
};