12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- // For using gulp
- const gulp = require('gulp');
- // For reading template parts
- const fs = require("fs");
- // For parsing codes, variables, loops etc. inside html templates
- const nunjucks = require('gulp-nunjucks');
- // For allowing custom loader
- const nunj = require('nunjucks');
- // For cleaning previous output
- //const del = require('del');
- const path = require('path');
- // Output directory
- const source_dir='src';
- const template_dir='src/html';
- const output_dir='public';
- var jsonInfo = JSON.parse(fs.readFileSync('src/info.json', 'utf8'));
- if ( jsonInfo.languages ) {
- jsonInfo.languages.forEach(function(lang) {
- if ( jsonInfo.default_langs.includes(lang.slug) ) {
- lang.default = 1
- }
- });
- }
- // Loader to include from relative path
- // @source https://github.com/mozilla/nunjucks/issues/1214#issuecomment-579534919
- class MyCustomLoader {
- constructor() {
- this.async = true
- }
- isRelative (filename) {
- console.log ('isRelative', filename)
- return true
- }
- resolve(parentName, filename) {
- console.log('resolve', parentName, filename)
- return filename
- }
- getSource (filePath, callback) {
- //console.log('getSource', filePath)
- try {
- const src = fs.readFileSync(filePath, 'utf8')
- const file = {
- src,
- path: filePath
- }
- callback(null, file)
- } catch (error) {
- callback(error)
- }
- }
- }
- // Runs when someone runs "gulp" on this directory
- gulp.task('default', async function(){
- var nunjucksenv = new nunj.Environment([new nunj.FileSystemLoader('views'),
- new MyCustomLoader()]);
- gulp.src(template_dir+'/index.html')
- .pipe(nunjucks.compile(
- {jsoninfo: jsonInfo}
- , {env: nunjucksenv}
- ))
- .pipe(gulp.dest('public'));
- });
|