├── .eslintignore ├── .eslintrc ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── lib └── renderer.js ├── package.json └── test ├── .eslintrc ├── .mocharc.yml └── index.js /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | tmp/ -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo", 3 | "root": true 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | tmp/ 4 | *.log 5 | .idea/ 6 | .nyc_output/ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | sudo: false 4 | 5 | cache: 6 | apt: true 7 | directories: 8 | - node_modules 9 | 10 | node_js: 11 | - "10" 12 | - "12" 13 | - "node" 14 | 15 | script: 16 | - npm run eslint 17 | - npm run test-cov 18 | 19 | after_script: 20 | - npm install coveralls 21 | - nyc report --reporter=text-lcov | coveralls 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Tommy Chen 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATED! 2 | 3 | hexo-renderer-swig has been deprecated. 4 | 5 | The [swig](https://node-swig.github.io/swig-templates/) project seems inactive. We recommend migrate to other template engines. (e.g. [nunjucks](https://mozilla.github.io/nunjucks/)) 6 | 7 | Below references may help migrate from swig to nunjucks. 8 | 9 | > [JS Templating: Transitioning from swig to nunjucks](https://medium.com/engineers-optimizely/js-templating-transitioning-from-swig-to-nunjucks-ac0e94d1794b) 10 | > [hexo-renderer-nunjucks](https://github.com/hexojs/hexo-renderer-nunjucks) 11 | > [hexo-theme-next: Migrate to Nunjucks #1214](https://github.com/theme-next/hexo-theme-next/issues/1214) 12 | > [hexo-theme-next: Use nunjucks-compatible syntax & operator #1068](https://github.com/theme-next/hexo-theme-next/pull/1068/files) 13 | > [hexo-theme-next: Migrate to Nunjucks #1215](https://github.com/theme-next/hexo-theme-next/pull/1215/files) 14 | 15 | # hexo-renderer-swig 16 | 17 | [![Build Status](https://travis-ci.org/hexojs/hexo-renderer-swig.svg?branch=master)](https://travis-ci.org/hexojs/hexo-renderer-swig) 18 | [![NPM version](https://badge.fury.io/js/hexo-renderer-swig.svg)](https://www.npmjs.com/package/hexo-renderer-swig) 19 | [![Coverage Status](https://img.shields.io/coveralls/hexojs/hexo-renderer-swig.svg)](https://coveralls.io/r/hexojs/hexo-renderer-swig?branch=master) 20 | 21 | Add support for [Swig]. 22 | 23 | ## Installation 24 | 25 | ``` bash 26 | $ npm install hexo-renderer-swig --save 27 | ``` 28 | 29 | [Swig]: https://node-swig.github.io/swig-templates/ 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* global hexo */ 2 | 3 | 'use strict'; 4 | 5 | const renderer = require('./lib/renderer'); 6 | 7 | hexo.extend.renderer.register('swig', 'html', renderer, true); 8 | -------------------------------------------------------------------------------- /lib/renderer.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const swig = require('swig-templates'); 4 | const extras = require('swig-extras'); 5 | const forTag = require('swig-templates/lib/tags/for'); 6 | 7 | extras.useTag(swig, 'markdown'); 8 | extras.useTag(swig, 'switch'); 9 | extras.useTag(swig, 'case'); 10 | 11 | extras.useFilter(swig, 'batch'); 12 | extras.useFilter(swig, 'groupby'); 13 | extras.useFilter(swig, 'markdown'); 14 | extras.useFilter(swig, 'nl2br'); 15 | extras.useFilter(swig, 'pluck'); 16 | extras.useFilter(swig, 'split'); 17 | extras.useFilter(swig, 'trim'); 18 | extras.useFilter(swig, 'truncate'); 19 | 20 | swig.setDefaults({ 21 | cache: false, 22 | autoescape: false 23 | }); 24 | 25 | // Hack: Override for tag of Swig 26 | swig.setTag('for', forTag.parse, (...args) => { 27 | const compile = forTag.compile(...args).split('\n'); 28 | 29 | compile.splice(3, 0, ' if (!Array.isArray(__l) && typeof __l.toArray === "function") { __l = __l.toArray(); }'); 30 | 31 | return compile.join('\n'); 32 | }, forTag.ends, true); 33 | 34 | function swigRenderer({ text, path }, locals) { 35 | return swig.render(text, { 36 | locals, 37 | filename: path 38 | }); 39 | } 40 | 41 | swigRenderer.compile = ({ text, path }) => swig.compile(text, { filename: path }); 42 | 43 | module.exports = swigRenderer; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hexo-renderer-swig", 3 | "version": "2.0.0", 4 | "description": "Swig renderer plugin for Hexo", 5 | "main": "index.js", 6 | "scripts": { 7 | "eslint": "eslint .", 8 | "test": "mocha test/index.js", 9 | "test-cov": "nyc npm run test" 10 | }, 11 | "directories": { 12 | "lib": "./lib" 13 | }, 14 | "files": [ 15 | "lib/", 16 | "index.js" 17 | ], 18 | "repository": "hexojs/hexo-renderer-swig", 19 | "keywords": [ 20 | "hexo", 21 | "swig", 22 | "renderer" 23 | ], 24 | "author": "Tommy Chen (http://zespia.tw)", 25 | "license": "MIT", 26 | "dependencies": { 27 | "swig-templates": "^2.0.0", 28 | "swig-extras": "0.0.1" 29 | }, 30 | "devDependencies": { 31 | "chai": "^4.2.0", 32 | "eslint": "^7.3.1", 33 | "eslint-config-hexo": "^4.0.0", 34 | "mocha": "^8.0.1", 35 | "nyc": "^15.0.0" 36 | }, 37 | "engines": { 38 | "node": ">=8.6.0" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "hexo/test" 3 | } -------------------------------------------------------------------------------- /test/.mocharc.yml: -------------------------------------------------------------------------------- 1 | reporter: spec 2 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const should = require('chai').should(); // eslint-disable-line 4 | 5 | describe('Swig renderer', () => { 6 | const r = require('../lib/renderer'); 7 | 8 | it('normal', () => { 9 | const body = [ 10 | 'Hello {{ name }}!' 11 | ].join('\n'); 12 | 13 | r({text: body}, { 14 | name: 'world' 15 | }).should.eql('Hello world!'); 16 | }); 17 | 18 | it('override "for" tag', () => { 19 | const body = [ 20 | '{% for x in arr %}', 21 | '{{ x }}', 22 | '{% endfor %}' 23 | ].join(''); 24 | 25 | const data = { 26 | arr: { 27 | toArray: () => { 28 | return [1, 2, 3]; 29 | } 30 | } 31 | }; 32 | 33 | r({text: body}, data).should.eql('123'); 34 | }); 35 | 36 | it('compile', () => { 37 | const body = [ 38 | 'Hello {{ name }}!' 39 | ].join('\n'); 40 | 41 | const render = r.compile({ 42 | text: body 43 | }); 44 | 45 | render({ 46 | name: 'world' 47 | }).should.eql('Hello world!'); 48 | }); 49 | }); 50 | --------------------------------------------------------------------------------