Gulp module for RosaeNLG

Helps to compile rosaeNLG templates in js files for client side in browser rendering. It can be used independantly of gulp.

Example of project using Gulp for RosaeNLG: See RosaeNLG Boilerplate or rosaenlg-browser-poc technical demo module.

Installation

npm install gulp-rosaenlg

Usage

compileTemplates

compileTemplates will compile and package one or multiple RosaeNLG templates for browser side rendering in a stream and returns that stream:

  • sourcesAndNames (array, mandatory): each entry must have 2 properties, source for the filename of the template and name for the name of the generated function

  • language (string, mandatory): any supported rosaeNLG language (all templates must have the same language)

  • dest (string, mandatory): the destination js filename

  • holderName (string, mandatory): the name of the package that will hold all the functions

  • tinyify (boolean, optional): put true if you want to tinyify the output

const gulpRosaeNLG = require('gulp-rosaenlg');
const fs = require('fs');

const tmpFile = 'tmp.js';

let os = gulpRosaeNLG.compileTemplates([{source: 'test/test.pug', name:'test'}], 'en_US', tmpFile, 'templates_holder');

os.on('finish', function() {
  console.log('DONE');
  const compiledString = fs.readFileSync(tmpFile, 'utf-8');
  console.log(`done: ${compiledString.length}`);
  // fs.unlinkSync(tmpFile);
});

renderTemplateInFile

renderTemplateInFile renders a RosaeNLG template into a file:

  • template (string, mandatory): the filename of the template to render

  • dest (string, mandatory): the filename that will contain the output

  • options (mandatory): the parameters that will be given to renderFile; typically put language and most often some data.

packageTemplateJson

packageTemplateJson packages (and optionally compiles) templates into a single object, generally for JSON serialization. One single PackagedTemplateParams param:

  • templateId: string; will be just kept as is (not used during the packaging process)

  • entryTemplate: string; the main template; do not put the full path

  • folderWithTemplates: string; the folder containing all the templates (including the entryTemplate)

  • compileInfo: mandatory as at some point compilation will occur

    • activate: boolean; if set, the template will be compiled, and included in the output

    • compileDebug: boolean; activate Pug debug

    • language: language

  • autotest: all fields will just be copied as is in the output (not used during packaging)

    • activate: boolean

    • input: object that is a valid input to render the template

    • expected: string[]; strings that should be in the rendered template

don’t forget to put language in autotest section too.

Extract from the boilerplate project:

const gulpRosaeNLG = require('gulp-rosaenlg');
const fs = require('fs');

function doPackage(cb) {

  const packageObj = gulpRosaeNLG.packageTemplateJson({
    templateId: 'phones',
    entryTemplate: 'phoneForJson.pug',
    folderWithTemplates: 'templates',
    compileInfo: {
      language: 'fr_FR'
    },
    autotest: {
      activate: true,
      input: {
        language: 'fr_FR',
        phone: {
          "name": "OnePlus 5T",
          "colors": ["Black", "Red", "White"],
          "displaySize": 6,
          "screenRatio": 80.43,
          "battery": 3300,
          "bluetooth": 5
        }
      },
      expected: ['phone', 'battery']
    }
  });

  fs.writeFileSync('dist/phone_package.json', JSON.stringify(packageObj), 'utf8');
  cb();
}

exports.all = doPackage;