From 2ba69bac09be2144f9e20416fe5d23918ba8ea17 Mon Sep 17 00:00:00 2001 From: "Mr. Hyde" Date: Tue, 22 Sep 2015 06:27:51 +0200 Subject: [PATCH] [js] initial commit of v0.1.0 --- .editorconfig | 6 +--- .travis.yml | 4 +-- CHANGELOG | 8 +++++ LICENSE-MIT | 22 ++++++++++++ gruntfile.coffee | 54 ++++++++++++++++++++++++++++ package.json | 56 +++++++++++++++++++++++++++++ tasks/sitemap_xml.js | 78 ++++++++++++++++++++++++++++++++++++++++ test/fixtures/blog.html | 1 + test/fixtures/index.html | 1 + test/fixtures/news.html | 1 + test/fixtures/short.htm | 1 + 11 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 CHANGELOG create mode 100644 LICENSE-MIT create mode 100644 gruntfile.coffee create mode 100644 package.json create mode 100644 tasks/sitemap_xml.js create mode 100644 test/fixtures/blog.html create mode 100644 test/fixtures/index.html create mode 100644 test/fixtures/news.html create mode 100644 test/fixtures/short.htm diff --git a/.editorconfig b/.editorconfig index 7d3b177..d60f635 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,9 +12,5 @@ insert_final_newline = true indent_style = space indent_size = 2 -[*.{coffee,json,yml}] -indent_style = space -indent_size = 2 - [*.md] -trim_trailing_whitespace = false \ No newline at end of file +trim_trailing_whitespace = false diff --git a/.travis.yml b/.travis.yml index 4b3c616..ba509c8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: node_js node_js: - - '0.12' - - '4' + - '4.0' + - 'node' - 'iojs' before_install: - npm install -g grunt-cli diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..16ea82b --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,8 @@ +## v0.1.0 +* replaced `grunt.template.today()` with `moment()` to fit `W3C Datetime` standart +* allow to set custom time format #note: see [moment.js](http://momentjs.com/) +* [js-standard-style](https://github.com/feross/standard) +* code rewrited in ES6 and requres node v4.0.0 or higher +* general improvements to gruntfile (coffee, jit-grunt) +* upgraded `xmlbuilder` to 3.1.0 +* new `pretty` option #note: false by default diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..dd377c5 --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,22 @@ +Copyright (c) 2015 LotusTM + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation +files (the "Software"), to deal in the Software without +restriction, including without limitation the rights to use, +copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/gruntfile.coffee b/gruntfile.coffee new file mode 100644 index 0000000..5073d28 --- /dev/null +++ b/gruntfile.coffee @@ -0,0 +1,54 @@ +module.exports = (grunt) -> + 'use strict' + + # Track execution time + require('time-grunt') grunt + + # Load grunt tasks automatically + require('jit-grunt') grunt + + grunt.initConfig + pkg: grunt.file.readJSON 'package.json' + + path: + temp: 'temp' + test: 'test' + fixtures: '<%= path.test %>/fixtures' + + clean: + test: + '<%= path.temp %>' + + sitemap_xml: + default_options: + files: [ + cwd: '<%= path.fixtures %>' + src: '{,**/}*.html' + dest: '<%= path.temp %>/sitemap.xml' + ] + custom_options: + options: + root: 'true', + stripIndex: false, + fileName: 'map', + lastMod: grunt.template.today('yyyy-mm-dd'), + priority: '0.1', + changeFreq: 'monthly' + pretty: true + files: [ + cwd: '<%= path.fixtures %>' + src: '{,**/}*.{html,htm}' + dest: '<%= path.temp %>/map.xml' + ] + + grunt.loadTasks 'tasks' + + ### + A task for scss and js linting + ### + grunt.registerTask 'test', [ + 'clean' + 'sitemap_xml' + ] + + return diff --git a/package.json b/package.json new file mode 100644 index 0000000..e2377d7 --- /dev/null +++ b/package.json @@ -0,0 +1,56 @@ +{ + "name": "grunt-sitemap-xml", + "description": "Grunt plugin for generating XML sitemaps for search engine indexing", + "version": "0.1.0", + "homepage": "https://github.com/LotusTM/grunt-sitemap-xml", + "author": "LotusTM ", + "license": "MIT", + "repository": { + "type": "git", + "url": "git://github.com/LotusTM/grunt-sitemap-xml.git" + }, + "bugs": { + "url": "https://github.com/LotusTM/grunt-sitemap-xml/issues" + }, + "keywords": [ + "generator", + "google", + "grunt", + "gruntplugin", + "plugin", + "seo", + "sitemap", + "xml" + ], + "dependencies": { + "chalk": "^1.1.1", + "moment": "^2.10.6", + "xmlbuilder": "^3.1.0" + }, + "devDependencies": { + "babel-eslint": "^4.1.3", + "grunt": "^0.4.5", + "grunt-contrib-clean": "^0.6.0", + "grunt-contrib-jshint": "^0.11.3", + "jit-grunt": "^0.9.1", + "standard": "^5.3.1", + "time-grunt": "^1.2.1" + }, + "files": [ + "tasks", + "CHANGELOG.md", + "LICENSE-MIT" + ], + "directories": { + "test": "test" + }, + "scripts": { + "test": "standard && grunt test" + }, + "engines": { + "node": ">=4.0" + }, + "standard": { + "parser": "babel-eslint" + } +} diff --git a/tasks/sitemap_xml.js b/tasks/sitemap_xml.js new file mode 100644 index 0000000..9476e31 --- /dev/null +++ b/tasks/sitemap_xml.js @@ -0,0 +1,78 @@ +/* + * grunt-sitemap-xml + * https://github.com/LotusTM/grunt-sitemap-xml + * + * Copyright (c) 2015 LotusTM + * Licensed under the MIT license. + */ +'use strict' + +const chalk = require('chalk') +const moment = require('moment') +const builder = require('xmlbuilder') + +module.exports = function (grunt) { + // Read package.json + const pkg = this.config.data.pkg || grunt.file.readJSON('package.json') + + grunt.registerMultiTask('sitemap_xml', 'Grunt task for generating sitemap.xml for search engine indexing', function () { + let sitemap, root, url, message + + const options = this.options({ + siteRoot: pkg.homepage, + stripIndex: true, + fileName: 'sitemap', + lastMod: moment().format('YYYY-MM-DDTHH:mm:ssZ'), + priority: '0.5', + changeFreq: 'weekly', + pretty: false + }) + + // Resolve options.siteRoot, add '/' if needed + if (options.siteRoot) { + root = (options.siteRoot.slice(-1) === '/') ? options.siteRoot : options.siteRoot + '/' + } else { + grunt.fail.warn('Please set siteRoot variable in options or homepage property in package.json.') + } + + // Build XML string + let urlset = builder.create('urlset', { + version: '1.0', + encoding: 'UTF-8' + }) + + urlset.att('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9') + .att('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') + .att('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd') + + // Iterate over all specified file groups + this.files.forEach(file => { + file.src.forEach(filepath => { + // Strip index.html + filepath = (options.stripIndex) ? filepath.replace('index.html', '') : filepath + + // Create XML node for each entry + url = urlset.ele('url') + + url.ele('loc', root + filepath) + url.ele('lastmod', options.lastMod) + url.ele('changefreq', options.changeFreq) + url.ele('priority', options.priority) + + // for debug purpose + message = `loc: ${root + filepath}\nlastmod: ${options.lastMod}\nchangefreq: ${options.changeFreq}\npriority: ${options.priority}\n` + grunt.verbose.writeln(message) + }) + + // Format XML sitemap + sitemap = urlset.end({ + pretty: options.pretty + }) + + // Write the destination file. + grunt.file.write(file.dest, sitemap) + // Print a success message. + grunt.log.writeln(`File ${chalk.cyan(file.dest)} created.`) + }) + }) +} diff --git a/test/fixtures/blog.html b/test/fixtures/blog.html new file mode 100644 index 0000000..34e3a08 --- /dev/null +++ b/test/fixtures/blog.html @@ -0,0 +1 @@ +blog diff --git a/test/fixtures/index.html b/test/fixtures/index.html new file mode 100644 index 0000000..9015a7a --- /dev/null +++ b/test/fixtures/index.html @@ -0,0 +1 @@ +index diff --git a/test/fixtures/news.html b/test/fixtures/news.html new file mode 100644 index 0000000..7ee44d9 --- /dev/null +++ b/test/fixtures/news.html @@ -0,0 +1 @@ +news \ No newline at end of file diff --git a/test/fixtures/short.htm b/test/fixtures/short.htm new file mode 100644 index 0000000..eba61a7 --- /dev/null +++ b/test/fixtures/short.htm @@ -0,0 +1 @@ +short \ No newline at end of file