Skip to content

Commit

Permalink
Replace EJS with Nunjucks
Browse files Browse the repository at this point in the history
Replace layouts and adapt generator and tests.
  • Loading branch information
ahaasler committed Aug 25, 2015
1 parent 565ddcc commit f1db883
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 117 deletions.
34 changes: 0 additions & 34 deletions atom.ejs

This file was deleted.

41 changes: 41 additions & 0 deletions atom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ config.title | e }}</title>
{% if config.subtitle %}<subtitle>{{ config.subtitle | e }}</subtitle>{% endif %}
<link href="{{ feed_url | uriencode }}" rel="self"/>
<link href="{{ url | uriencode }}"/>
<updated>{{ posts.first().updated.toISOString() }}</updated>
<id>{{ url }}</id>
{% if config.author %}
<author>
<name>{{ config.author | e }}</name>
{% if config.email %}<email>{{ config.email | e }}</email>{% endif %}
</author>
{% endif %}
<generator uri="http://hexo.io/">Hexo</generator>
{% for post in posts.toArray() %}
<entry>
<title>{{ post.title | e }}</title>
<link href="{{ (url + post.path) | uriencode }}"/>
<id>{{ url + post.path }}</id>
<published>{{ post.date.toISOString() }}</published>
<updated>{{ post.updated.toISOString() }}</updated>
<content type="html">{{ post.content | e }}</content>
<summary type="html">
{% if post.description %}
{{ post.description | e }}
{% elif post.excerpt %}
{{ post.excerpt | e }}
{% elif post.content %}
{{ post.content.substring(0, 140) | e }}
{% endif %}
</summary>
{% for category in post.categories.toArray() %}
<category term="{{ category.name }}" scheme="{{ (url + category.path) | uriencode }}"/>
{% endfor %}
{% for tag in post.tags.toArray() %}
<category term="{{ tag.name }}" scheme="{{ (url + tag.path) | uriencode }}"/>
{% endfor %}
</entry>
{% endfor %}
</feed>
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
hexo.extend.generator.register('feed', require('./lib/generator'));
34 changes: 19 additions & 15 deletions lib/generator.js
Original file line number Diff line number Diff line change
@@ -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 ? '<![CDATA[' + 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;
Expand All @@ -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,
Expand All @@ -44,4 +48,4 @@ module.exports = function(locals){
path: feedConfig.path,
data: xml
};
};
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
"author": "Tommy Chen <[email protected]> (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"
}
Expand Down
28 changes: 0 additions & 28 deletions rss2.ejs

This file was deleted.

32 changes: 32 additions & 0 deletions rss2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title>{{ config.title | e }}</title>
<link>{{ url | uriencode }}</link>
<atom:link href="{{ feed_url | uriencode }}" rel="self" type="application/rss+xml"/>
<description>{{ config.description | e }}</description>
<pubDate>{{ posts.first().updated.toDate().toUTCString() }}</pubDate>
<generator>http://hexo.io/</generator>
{% for post in posts.toArray() %}
<item>
<title>{{ post.title | e }}</title>
<link>{{ (url + post.path) | uriencode }}</link>
<guid>{{ (url + post.path) | uriencode }}</guid>
<pubDate>{{ post.date.toDate().toUTCString() }}</pubDate>
<description>
{% if post.description %}
{{ post.description | e }}
{% elif post.excerpt %}
{{ post.excerpt | e }}
{% elif post.content %}
{{ post.content.substring(0, 140) | e }}
{% endif %}
</description>
<content:encoded>{{ post.content | e }}</content:encoded>
{% if post.comments %}<comments>{{ (url + post.path) | uriencode }}#disqus_thread</comments>{% endif %}
</item>
{% endfor %}
</channel>
</rss>
Loading

0 comments on commit f1db883

Please sign in to comment.