From f1db883a4825bcb1fe5bae2e45d9f1f168b3e196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Haasler=20Garc=C3=ADa?= Date: Sun, 23 Aug 2015 18:05:07 +0000 Subject: [PATCH] Replace EJS with Nunjucks Replace layouts and adapt generator and tests. --- atom.ejs | 34 ---------------------- atom.xml | 41 ++++++++++++++++++++++++++ index.js | 8 ++--- lib/generator.js | 34 ++++++++++++---------- package.json | 12 ++++---- rss2.ejs | 28 ------------------ rss2.xml | 32 ++++++++++++++++++++ test/index.js | 76 +++++++++++++++++++++++++++++------------------- 8 files changed, 148 insertions(+), 117 deletions(-) delete mode 100644 atom.ejs create mode 100644 atom.xml delete mode 100644 rss2.ejs create mode 100644 rss2.xml diff --git a/atom.ejs b/atom.ejs deleted file mode 100644 index 70b5ce1..0000000 --- a/atom.ejs +++ /dev/null @@ -1,34 +0,0 @@ - - - <%-: config.title | cdata %> - <% if (config.subtitle){ %><%-: config.subtitle | cdata %><% } %> - - - <%= posts.first().updated.toISOString() %> - <%- url %> - <% if (config.author){ %> - - <%-: config.author | cdata %> - <% if (config.email){ %><%-: config.email | cdata %><% } %> - - <% } %> - Hexo - <% posts.each(function(post){ %> - - <%-: post.title | cdata %> - - <%- url + post.path %> - <%= post.date.toISOString() %> - <%= post.updated.toISOString() %> - <%-: post.content | cdata %> - - <% if (post.description){ %><%-: post.description | cdata %> - <% } else if (post.excerpt){ %><%-: post.excerpt | cdata %> - <% } else {%><%-: post.content.substring(0, 140) | cdata %><% } %> - - <% [].concat(post.tags.toArray(), post.categories.toArray()).forEach(function(category){ %> - - <% }) %> - - <% }) %> - diff --git a/atom.xml b/atom.xml new file mode 100644 index 0000000..73c19ec --- /dev/null +++ b/atom.xml @@ -0,0 +1,41 @@ + + + {{ config.title | e }} + {% if config.subtitle %}{{ config.subtitle | e }}{% endif %} + + + {{ posts.first().updated.toISOString() }} + {{ url }} + {% if config.author %} + + {{ config.author | e }} + {% if config.email %}{{ config.email | e }}{% endif %} + + {% endif %} + Hexo + {% for post in posts.toArray() %} + + {{ post.title | e }} + + {{ url + post.path }} + {{ post.date.toISOString() }} + {{ post.updated.toISOString() }} + {{ post.content | e }} + + {% if post.description %} + {{ post.description | e }} + {% elif post.excerpt %} + {{ post.excerpt | e }} + {% elif post.content %} + {{ post.content.substring(0, 140) | e }} + {% endif %} + + {% for category in post.categories.toArray() %} + + {% endfor %} + {% for tag in post.tags.toArray() %} + + {% endfor %} + + {% endfor %} + diff --git a/index.js b/index.js index 13a8101..4293a5e 100644 --- a/index.js +++ b/index.js @@ -9,20 +9,20 @@ var config = hexo.config.feed = assign({ var type = config.type.toLowerCase(); // Check feed type -if (type !== 'atom' && type !== 'rss2'){ +if (type !== 'atom' && type !== 'rss2') { config.type = 'atom'; } else { config.type = type; } // Set default feed path -if (!config.path){ +if (!config.path) { config.path = config.type + '.xml'; } // Add extension name if don't have -if (!pathFn.extname(config.path)){ +if (!pathFn.extname(config.path)) { config.path += '.xml'; } -hexo.extend.generator.register('feed', require('./lib/generator')); \ No newline at end of file +hexo.extend.generator.register('feed', require('./lib/generator')); diff --git a/lib/generator.js b/lib/generator.js index f901b3c..b04bac3 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -1,17 +1,23 @@ -var ejs = require('ejs'); +var nunjucks = require('nunjucks'); +var env = new nunjucks.Environment(); var pathFn = require('path'); var fs = require('fs'); -ejs.filters.cdata = function(str){ - return str ? '' : ''; -}; +nunjucks.configure({ + autoescape: false, + watch: false +}); + +env.addFilter('uriencode', function(str) { + return encodeURI(str); +}); -var atomTmplSrc = pathFn.join(__dirname, '../atom.ejs'); -var atomTmpl = ejs.compile(fs.readFileSync(atomTmplSrc, 'utf8')); -var rss2TmplSrc = pathFn.join(__dirname, '../rss2.ejs'); -var rss2Tmpl = ejs.compile(fs.readFileSync(rss2TmplSrc, 'utf8')); +var atomTmplSrc = pathFn.join(__dirname, '../atom.xml'); +var atomTmpl = nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env); +var rss2TmplSrc = pathFn.join(__dirname, '../rss2.xml'); +var rss2Tmpl = nunjucks.compile(fs.readFileSync(rss2TmplSrc, 'utf8'), env); -module.exports = function(locals){ +module.exports = function(locals) { var config = this.config; var feedConfig = config.feed; var template = feedConfig.type === 'rss2' ? rss2Tmpl : atomTmpl; @@ -22,18 +28,16 @@ module.exports = function(locals){ var url; // Sorry for that, it is hexo URL handling way. - if (config.root === '/'){ + if (config.root === '/') { url = config.url + config.root; - } - else - { + } else { url = config.url + '/'; } // And sorry for that too. url = url.replace(/([^:])\/\//g, '$1/'); - var xml = template({ + var xml = template.render({ config: config, url: url, posts: posts, @@ -44,4 +48,4 @@ module.exports = function(locals){ path: feedConfig.path, data: xml }; -}; \ No newline at end of file +}; diff --git a/package.json b/package.json index ee661dc..86118a1 100644 --- a/package.json +++ b/package.json @@ -23,20 +23,20 @@ "author": "Tommy Chen (http://zespia.tw)", "license": "MIT", "dependencies": { - "ejs": "^1.0.0", - "object-assign": "^3.0.0" + "nunjucks": "^1.3.4", + "object-assign": "^4.0.1" }, "devDependencies": { - "chai": "^1.9.1", + "chai": "^3.2.0", "cheerio": "^0.19.0", "coveralls": "^2.11.2", "gulp": "^3.8.9", - "gulp-istanbul": "^0.5.0", + "gulp-istanbul": "^0.10.0", "gulp-jshint": "^1.8.6", - "gulp-load-plugins": "^0.8.0", + "gulp-load-plugins": "^0.10.0", "gulp-mocha": "^2.0.0", "hexo": "^3.0.0", - "jshint-stylish": "^1.0.0", + "jshint-stylish": "^2.0.1", "mocha": "^2.0.1", "rimraf": "^2.2.8" } diff --git a/rss2.ejs b/rss2.ejs deleted file mode 100644 index 4c014f0..0000000 --- a/rss2.ejs +++ /dev/null @@ -1,28 +0,0 @@ - - - - <%-: config.title | cdata %> - <%- encodeURI(url) %> - - <%-: config.description | cdata %> - <%= posts.first().updated.toDate().toUTCString() %> - http://hexo.io/ - <% posts.each(function(post){ %> - - <%-: post.title | cdata %> - <%- encodeURI(url + post.path) %> - <%- encodeURI(url + post.path) %> - <%= post.date.toDate().toUTCString() %> - - <% if (post.description){ %><%-: post.description | cdata %> - <% } else if (post.excerpt){ %><%-: post.excerpt | cdata %> - <% } else {%><%-: post.content.substring(0, 140) | cdata %><% } %> - - <%-: post.content | cdata %> - <% if (post.comments){ %><%- encodeURI(url + post.path) %>#disqus_thread<% } %> - - <% }) %> - - diff --git a/rss2.xml b/rss2.xml new file mode 100644 index 0000000..9698c5f --- /dev/null +++ b/rss2.xml @@ -0,0 +1,32 @@ + + + + {{ config.title | e }} + {{ url | uriencode }} + + {{ config.description | e }} + {{ posts.first().updated.toDate().toUTCString() }} + http://hexo.io/ + {% for post in posts.toArray() %} + + {{ post.title | e }} + {{ (url + post.path) | uriencode }} + {{ (url + post.path) | uriencode }} + {{ post.date.toDate().toUTCString() }} + + {% if post.description %} + {{ post.description | e }} + {% elif post.excerpt %} + {{ post.excerpt | e }} + {% elif post.content %} + {{ post.content.substring(0, 140) | e }} + {% endif %} + + {{ post.content | e }} + {% if post.comments %}{{ (url + post.path) | uriencode }}#disqus_thread{% endif %} + + {% endfor %} + + diff --git a/test/index.js b/test/index.js index 12feb70..b499bed 100644 --- a/test/index.js +++ b/test/index.js @@ -1,44 +1,60 @@ var should = require('chai').should(); var Hexo = require('hexo'); -var ejs = require('ejs'); +var nunjucks = require('nunjucks'); +var env = new nunjucks.Environment(); var pathFn = require('path'); var fs = require('fs'); var assign = require('object-assign'); var cheerio = require('cheerio'); -ejs.filters.cdata = function(str){ - return str ? '' : ''; -}; +nunjucks.configure({ + autoescape: false, + watch: false +}); + +env.addFilter('uriencode', function(str) { + return encodeURI(str); +}); -var atomTmplSrc = pathFn.join(__dirname, '../atom.ejs'); -var atomTmpl = ejs.compile(fs.readFileSync(atomTmplSrc, 'utf8')); -var rss2TmplSrc = pathFn.join(__dirname, '../rss2.ejs'); -var rss2Tmpl = ejs.compile(fs.readFileSync(rss2TmplSrc, 'utf8')); +var atomTmplSrc = pathFn.join(__dirname, '../atom.xml'); +var atomTmpl = nunjucks.compile(fs.readFileSync(atomTmplSrc, 'utf8'), env); +var rss2TmplSrc = pathFn.join(__dirname, '../rss2.xml'); +var rss2Tmpl = nunjucks.compile(fs.readFileSync(rss2TmplSrc, 'utf8'), env); var urlConfig = { - url: 'http://localhost/', + url: 'http://localhost/', root: '/' }; -describe('Feed generator', function(){ - var hexo = new Hexo(__dirname, {silent: true}); +describe('Feed generator', function() { + var hexo = new Hexo(__dirname, { + silent: true + }); var Post = hexo.model('Post'); var generator = require('../lib/generator').bind(hexo); var posts; var locals; - before(function(){ - return Post.insert([ - {source: 'foo', slug: 'foo', date: 1e8}, - {source: 'bar', slug: 'bar', date: 1e8 + 1}, - {source: 'baz', slug: 'baz', date: 1e8 - 1} - ]).then(function(data){ + before(function() { + return Post.insert([{ + source: 'foo', + slug: 'foo', + date: 1e8 + }, { + source: 'bar', + slug: 'bar', + date: 1e8 + 1 + }, { + source: 'baz', + slug: 'baz', + date: 1e8 - 1 + }]).then(function(data) { posts = Post.sort('-date'); locals = hexo.locals.toObject(); }); }); - it('type = atom', function(){ + it('type = atom', function() { hexo.config.feed = { type: 'atom', path: 'atom.xml', @@ -48,7 +64,7 @@ describe('Feed generator', function(){ var result = generator(locals); result.path.should.eql('atom.xml'); - result.data.should.eql(atomTmpl({ + result.data.should.eql(atomTmpl.render({ config: hexo.config, url: urlConfig.url, posts: posts.limit(2), @@ -56,7 +72,7 @@ describe('Feed generator', function(){ })); }); - it('type = rss2', function(){ + it('type = rss2', function() { hexo.config.feed = { type: 'rss2', path: 'rss2.xml', @@ -66,7 +82,7 @@ describe('Feed generator', function(){ var result = generator(locals); result.path.should.eql('rss2.xml'); - result.data.should.eql(rss2Tmpl({ + result.data.should.eql(rss2Tmpl.render({ config: hexo.config, url: urlConfig.url, posts: posts.limit(2), @@ -74,7 +90,7 @@ describe('Feed generator', function(){ })); }); - it('limit = 0', function(){ + it('limit = 0', function() { hexo.config.feed = { type: 'atom', path: 'atom.xml', @@ -85,7 +101,7 @@ describe('Feed generator', function(){ var result = generator(locals); result.path.should.eql('atom.xml'); - result.data.should.eql(atomTmpl({ + result.data.should.eql(atomTmpl.render({ config: hexo.config, url: urlConfig.url, posts: posts, @@ -93,13 +109,13 @@ describe('Feed generator', function(){ })); }); - it('Relative URL handling', function(){ + it('Relative URL handling', function() { hexo.config.feed = { type: 'atom', path: 'atom.xml' }; - var checkURL = function(root, path, valid){ + var checkURL = function(root, path, valid) { hexo.config.url = root; hexo.config.path = path; @@ -114,12 +130,12 @@ describe('Feed generator', function(){ var GOOD = 'http://localhost/blog/'; - checkURL('http://localhost/blog', '/blog/', GOOD); - checkURL('http://localhost/blog', '/blog', GOOD); - checkURL('http://localhost/blog/', '/blog/', GOOD); - checkURL('http://localhost/blog/', '/blog', GOOD); + checkURL('http://localhost/blog', '/blog/', GOOD); + checkURL('http://localhost/blog', '/blog', GOOD); + checkURL('http://localhost/blog/', '/blog/', GOOD); + checkURL('http://localhost/blog/', '/blog', GOOD); checkURL('http://localhost/b/l/o/g', '/', 'http://localhost/b/l/o/g/'); }); -}); \ No newline at end of file +});