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

Use nunjucks-compatible syntax & operator #1068

Merged
merged 13 commits into from
Sep 1, 2019
Merged

Use nunjucks-compatible syntax & operator #1068

merged 13 commits into from
Sep 1, 2019

Conversation

stevenjoezhang
Copy link
Contributor

Swig-template supports || && or and
nunjucks only supports or and

mozilla/nunjucks#622

@1v9 1v9 added this to the v7.4.0 milestone Aug 5, 2019
@1v9
Copy link
Member

1v9 commented Aug 5, 2019

Maybe need a new label for code style change.

@ivan-nginx
Copy link
Member

Let's up this set on v8.x? I think no need on v7.

@ivan-nginx ivan-nginx modified the milestones: v7.4.0, v8.0.0 Aug 5, 2019
@ivan-nginx
Copy link
Member

ivan-nginx commented Aug 5, 2019

https://github.com/popomore/nunjucks-vs-swig

GitHub
Contribute to popomore/nunjucks-vs-swig development by creating an account on GitHub.

@stevenjoezhang
Copy link
Contributor Author

See also: hexojs/hexo#2903

@ivan-nginx
Copy link
Member

Need to rename *.swig on *njk also, I think.

@stevenjoezhang
Copy link
Contributor Author

stevenjoezhang commented Aug 6, 2019

Yes, we could do this after Hexo drops swig support. The syntax of these two template engines is similar, except for some minor differences:
json_encode -> dump

cd layout
find . -name "*.swig" -exec rename 's/\.swig$/.njk/' '{}' \;

@stevenjoezhang
Copy link
Contributor Author

@ivan-nginx @1v9 I tried to use hexo-renderer-njucks, but got a strange error:

ERROR Template render error: (/Users/StevenJoeZhang/blog/themes/next/layout/tag.njk) [Line 46, Column 4]
  unknown block tag: endfor
Template render error: (/Users/StevenJoeZhang/blog/themes/next/layout/tag.njk) [Line 46, Column 4]
  unknown block tag: endfor
    at Object._prettifyError (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/lib.js:36:11)
    at /Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:567:19
    at eval (eval at _compile (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:19:11)
    at Template.getExported (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:605:16)
    at eval (eval at _compile (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:18:5)
    at createTemplate (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:315:9)
    at handle (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:327:11)
    at /Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:339:9
    at next (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/lib.js:280:7)
    at Object.asyncIter (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/lib.js:286:3)
    at Environment.getTemplate (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:321:9)
    at eval (eval at _compile (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:16:5)
    at createTemplate (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:315:9)
    at handle (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:327:11)
    at /Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:339:9
    at next (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/lib.js:280:7)
    at Object.asyncIter (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/lib.js:286:3)
    at Environment.getTemplate (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:321:9)
    at Template.root [as rootRenderFunc] (eval at _compile (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:637:18), <anonymous>:9:5)
    at Template.render (/Users/StevenJoeZhang/blog/node_modules/nunjucks/src/environment.js:556:10)
    at /Users/StevenJoeZhang/blog/node_modules/hexo-renderer-njucks/index.js:22:27
    at Theme._View.View._compiled (/Users/StevenJoeZhang/blog/node_modules/hexo/lib/theme/view.js:123:48)
    at Theme._View.View.View.render (/Users/StevenJoeZhang/blog/node_modules/hexo/lib/theme/view.js:29:15)
    at /Users/StevenJoeZhang/blog/node_modules/hexo/lib/hexo/index.js:349:21
    at tryCatcher (/Users/StevenJoeZhang/blog/node_modules/bluebird/js/release/util.js:16:23)
    at /Users/StevenJoeZhang/blog/node_modules/bluebird/js/release/method.js:15:34
    at RouteStream._read (/Users/StevenJoeZhang/blog/node_modules/hexo/lib/hexo/router.js:123:3)
    at RouteStream.Readable.read (_stream_readable.js:474:10)

I have no idea why endfor tag is not parsed correctly. Migrate to nunjucks is not easy as changing the suffix name...

So, this PR works fine, but another PR is needed to handle the nunjucks syntax

@stevenjoezhang stevenjoezhang changed the title Replace && / || operator with and / or Use nunjucks-compatible syntax & operator Aug 6, 2019
@1v9
Copy link
Member

1v9 commented Aug 6, 2019

Hexo hasn't toally switched to nunjucks, but tag plugin used nunjucks to render.

@stevenjoezhang
Copy link
Contributor Author

Yes, so you have to install the plugin first: hexo-renderer-njucks

@1v9
Copy link
Member

1v9 commented Aug 6, 2019

These nunjucks plugins maintenance is not stable enough, so just wait hexojs/hexo#2903 finished and merged and more...

@stevenjoezhang
Copy link
Contributor Author

In fact none is in public Hexo directory. They are all 3rd-party plugin. And I choose the only one that still maintained...

@ivan-nginx
Copy link
Member

BTW, mb we can create own nunjucks plugin and make him internal in scripts dir?

@ivan-nginx
Copy link
Member

And I choose the only one that still maintained...

Which plugin u choose?

@stevenjoezhang
Copy link
Contributor Author

stevenjoezhang commented Aug 7, 2019

This one: https://github.com/AlynxZhou/hexo-renderer-nunjucks
In its README:

Both hexo-renderer-nunjucks and hexo-renderer-njk and hexo-renderer-njks are using Nunjucks version 2, and seems their author won't update them to Nunjucks 3. They take the best name in npm, but leave code unmaintained! So I write my own hexo renderer to render my theme ARIA.

GitHub
Yet another Nunjucks renderer for Hexo. Contribute to AlynxZhou/hexo-renderer-nunjucks development by creating an account on GitHub.

@stevenjoezhang
Copy link
Contributor Author

stevenjoezhang commented Aug 7, 2019

mb we can create own nunjucks plugin

yes, we can just put it in the scripts dir

@ivan-nginx
Copy link
Member

mb we can create own nunjucks plugin
yes, we can just put it in the scripts dir

So, we must find all possible plugins and create from them the best one.

@ivan-nginx
Copy link
Member

Also, we can no rename swig extension; we can just add swig to nunjucks renderer, that's all.

@stevenjoezhang
Copy link
Contributor Author

We can't migrate to njk unless we clearly understands the problem
#1068 (comment)

@1v9 1v9 modified the milestones: v7.4.0, v8.0.0 Aug 31, 2019
@stevenjoezhang stevenjoezhang modified the milestones: v8.0.0, v7.4.1 Aug 31, 2019
@stevenjoezhang stevenjoezhang merged commit abd45df into master Sep 1, 2019
@delete-merged-branch delete-merged-branch bot deleted the operator branch September 1, 2019 02:22
@ivan-nginx ivan-nginx mentioned this pull request Oct 2, 2019
33 tasks
@stevenjoezhang stevenjoezhang mentioned this pull request Oct 16, 2019
13 tasks
tongluyang pushed a commit to tongluyang/hexo-theme-next that referenced this pull request Nov 19, 2019
* Replace && with and

* Replace || with or

* Replace ! with not

* Use ES6 includes method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants