Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace default swig engine with nunjucks #2903

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/extend/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { stripIndent } = require('hexo-util');
const { cyan } = require('chalk');
const nunjucks = require('nunjucks');
const { Environment } = require('hexo-renderer-nunjucks');
const Promise = require('bluebird');
const placeholder = '\uFFFC';
const rPlaceholder = /(?:<|&lt;)!--\uFFFC(\d+)--(?:>|&gt;)/g;
Expand Down Expand Up @@ -178,7 +178,7 @@ const formatNunjucksError = (err, input) => {

class Tag {
constructor() {
this.env = new nunjucks.Environment(null, {
this.env = new Environment(null, {
autoescape: false
});
}
Expand Down
9 changes: 1 addition & 8 deletions lib/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,6 @@ class Post {
return Promise.reject(new Error('No input file or string!')).asCallback(callback);
}

const isSwig = ext === 'swig';

// disable Nunjucks when the renderer spcify that.
const disableNunjucks = ext && ctx.render.renderer.get(ext) && !!ctx.render.renderer.get(ext).disableNunjucks;

Expand All @@ -258,12 +256,7 @@ class Post {
}).then(() => {
data.content = cacheObj.escapeContent(data.content);

if (isSwig) {
// Render with Nunjucks if this is a swig file
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure about this change. I cannot figure out if this removal is covered by unit tests.

return tag.render(data.content, data);
}

// Escape all Swig tags
// Escape all Nunjucks/Swig tags
if (!disableNunjucks) {
data.content = cacheObj.escapeAllSwigTags(data.content);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ module.exports = ctx => {

renderer.register('json', 'json', require('./json'), true);

renderer.register('swig', 'html', require('./swig'), true);
const nunjucks = require('hexo-renderer-nunjucks');
nunjucks.register(ctx);

const yaml = require('./yaml');

Expand Down
45 changes: 0 additions & 45 deletions lib/plugins/renderer/swig.js

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,16 @@
"hexo-fs": "^2.0.0",
"hexo-i18n": "^1.0.0",
"hexo-log": "^1.0.0",
"hexo-renderer-nunjucks": "^2.0.0",
"hexo-util": "^1.9.0",
"js-yaml": "^3.12.0",
"lodash": "^4.17.11",
"micromatch": "^4.0.2",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"nunjucks": "^3.1.3",
"pretty-hrtime": "^1.0.3",
"resolve": "^1.8.1",
"strip-ansi": "^6.0.0",
"swig-extras": "0.0.1",
"swig-templates": "^2.0.3",
"text-table": "^0.2.0",
"tildify": "^2.0.0",
"titlecase": "^1.1.2",
Expand Down
4 changes: 2 additions & 2 deletions test/scripts/console/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ describe('generate', () => {
// Add some source files
writeFile(join(hexo.theme_dir, 'source', 'a.txt'), 'a'),
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'b'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'c')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'c')
]);
await generate();

// Update source file
await Promise.all([
writeFile(join(hexo.theme_dir, 'source', 'b.txt'), 'bb'),
writeFile(join(hexo.theme_dir, 'source', 'c.swig'), 'cc')
writeFile(join(hexo.theme_dir, 'source', 'c.njk'), 'cc')
]);

// Generate again
Expand Down
8 changes: 4 additions & 4 deletions test/scripts/extend/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ describe('Renderer', () => {

r.isRenderableSync('yaml').should.be.false;

r.register('swig', 'html', () => {}, true);
r.register('njk', 'html', () => {}, true);

r.isRenderableSync('swig').should.be.true;
r.isRenderableSync('.swig').should.be.true;
r.isRenderableSync('layout.swig').should.be.true;
r.isRenderableSync('njk').should.be.true;
r.isRenderableSync('.njk').should.be.true;
r.isRenderableSync('layout.njk').should.be.true;
r.isRenderableSync('foo.html').should.be.false;
});

Expand Down
16 changes: 8 additions & 8 deletions test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('partial', () => {
const hexo = new Hexo(pathFn.join(__dirname, 'partial_test'), {silent: true});
const themeDir = pathFn.join(hexo.base_dir, 'themes', 'test');
const viewDir = pathFn.join(themeDir, 'layout') + pathFn.sep;
const viewName = 'article.swig';
const viewName = 'article.njk';

const ctx = {
site: hexo.locals,
Expand All @@ -32,7 +32,7 @@ describe('partial', () => {
fs.writeFile(hexo.config_path, 'theme: test')
]);
await hexo.init();
hexo.theme.setView('widget/tag.swig', 'tag widget');
hexo.theme.setView('widget/tag.njk', 'tag widget');
});

after(() => fs.rmdir(hexo.base_dir));
Expand All @@ -52,28 +52,28 @@ describe('partial', () => {
});

it('locals', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}).should.eql('bar');
});

it('cache', () => {
hexo.theme.setView('test.swig', '{{ foo }}');
hexo.theme.setView('test.njk', '{{ foo }}');

partial('test', {foo: 'bar'}, {cache: true}).should.eql('bar');
partial('test', {}, {cache: true}).should.eql('bar');
});

it('only', () => {
hexo.theme.setView('test.swig', '{{ foo }}{{ bar }}');
hexo.theme.setView('test.njk', '{{ foo }}{{ bar }}');

partial('test', {bar: 'bar'}, {only: true}).should.eql('bar');
});

it('a partial in another partial', () => {
hexo.theme.setView('partial/a.swig', '{{ partial("b") }}');
hexo.theme.setView('partial/b.swig', '{{ partial("c") }}');
hexo.theme.setView('partial/c.swig', 'c');
hexo.theme.setView('partial/a.njk', '{{ partial("b") }}');
hexo.theme.setView('partial/b.njk', '{{ partial("c") }}');
hexo.theme.setView('partial/c.njk', 'c');

partial('partial/a').should.eql('c');
});
Expand Down
22 changes: 11 additions & 11 deletions test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ describe('Hexo', () => {
});

it('_generate() - layout', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ config.title }}',
'{{ page.foo }}',
'{{ layout }}',
Expand All @@ -378,7 +378,7 @@ describe('Hexo', () => {
});

it('_generate() - layout array', () => {
hexo.theme.setView('baz.swig', 'baz');
hexo.theme.setView('baz.njk', 'baz');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand Down Expand Up @@ -418,7 +418,7 @@ describe('Hexo', () => {
it('_generate() - after_route_render filter', () => {
const hook = spy(result => result.replace('foo', 'bar'));
hexo.extend.filter.register('after_route_render', hook);
hexo.theme.setView('test.swig', 'foo');
hexo.theme.setView('test.njk', 'foo');
hexo.extend.generator.register('test', () => ({
path: 'test',
layout: 'test'
Expand All @@ -442,7 +442,7 @@ describe('Hexo', () => {
});

it('_generate() - validate locals', () => {
hexo.theme.setView('test.swig', [
hexo.theme.setView('test.njk', [
'{{ path }}',
'{{ url }}',
'{{ view_dir }}'
Expand All @@ -464,7 +464,7 @@ describe('Hexo', () => {
const path = 'bár';
hexo.config.url = 'http://fôo.com';

hexo.theme.setView('test.swig', '{{ url }}');
hexo.theme.setView('test.njk', '{{ url }}');

hexo.extend.generator.register('test', () => ({
path,
Expand All @@ -488,7 +488,7 @@ describe('Hexo', () => {
it('_generate() - reset cache for new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -508,7 +508,7 @@ describe('Hexo', () => {
it('_generate() - cache disabled and use new route', () => {
let count = 0;

hexo.theme.setView('test.swig', '{{ page.count() }}');
hexo.theme.setView('test.njk', '{{ page.count() }}');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -526,7 +526,7 @@ describe('Hexo', () => {
});

it('_generate() - cache disabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -535,12 +535,12 @@ describe('Hexo', () => {

return hexo._generate({cache: false})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '1'));
});

it('_generate() - cache enabled & update template', () => {
hexo.theme.setView('test.swig', '0');
hexo.theme.setView('test.njk', '0');

hexo.extend.generator.register('test', () => ({
path: 'test',
Expand All @@ -549,7 +549,7 @@ describe('Hexo', () => {

return hexo._generate({cache: true})
.then(() => checkStream(route.get('test'), '0'))
.then(() => hexo.theme.setView('test.swig', '1'))
.then(() => hexo.theme.setView('test.njk', '1'))
.then(() => checkStream(route.get('test'), '0')); // should return cached result
});

Expand Down
10 changes: 5 additions & 5 deletions test/scripts/hexo/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ describe('Post', () => {
});
});

it('render() - skip render phase if it\'s swig file', () => {
it('render() - skip render phase if it\'s nunjucks file', () => {
const content = [
'{% quote Hello World %}',
'quote content',
Expand All @@ -617,7 +617,7 @@ describe('Post', () => {

return post.render(null, {
content,
engine: 'swig'
engine: 'njk'
}).then(data => {
data.content.trim().should.eql([
'<blockquote><p>quote content</p>\n',
Expand All @@ -626,7 +626,7 @@ describe('Post', () => {
});
});

it('render() - escaping swig blocks with similar names', () => {
it('render() - escaping nunjucks blocks with similar names', () => {
const code = 'alert("Hello world")';
const highlighted = highlight(code);

Expand All @@ -651,7 +651,7 @@ describe('Post', () => {
});
});

it('render() - recover escaped swig blocks which is html escaped', () => {
it('render() - recover escaped nunjucks blocks which is html escaped', () => {
const content = '`{% raw %}{{ test }}{% endraw %}`';

return post.render(null, {
Expand All @@ -662,7 +662,7 @@ describe('Post', () => {
});
});

it('render() - recover escaped swig blocks which is html escaped before post_render', () => {
it('render() - recover escaped nunjucks blocks which is html escaped before post_render', () => {
const content = '`{% raw %}{{ test }}{% endraw %}`';

const filter = spy();
Expand Down
Loading