Other structures, mixins and functions

This is the documentation for 3.1.1 version, which is not the latest version. Consider upgrading to 4.3.0.

Structures

titlecase will capitalize the first letter of the main words in its content (which is surprisingly more subtle than just uppercasing each word).

It uses these libs:

Only available in English and French.

There are plenty of options for each of these libs, but they are not supported by RosaeNLG yet (you get the default output).

Mixins

  • isSentenceStart() will return a true if you are at the very beginning of a sentence (i.e. no word has been output yet). Use the \#[+flushBuffer] mixin just before to be sure that isSentenceStart() returns a valid value.

  • toHtmlList(obj) will generate an HTML structure of the js object, with <ul> and <li> etc. This is useful for outputting the data that was used to generate a text, in the same document.

  • uppercaseWords(str) will output all words of the string with the first letter uppercase.

  • eatSpace will 'eat' the spaces around him. bla \#[+eatSpace()] bla outputs blabla.

Functions

  • gender and number of an object:

    • getRefGender(obj) gives the current gender of an object (according to the last triggered representant). It should not be used that often.

    • getRefNumber(obj) give the current number of an object.

  • getSorP(table, obj): table is an array with 2 elements. If obj is singular, the first item of the array will be output, if plural, it will be the second. It can be useful in referring expressions: \#{getSorP(['it', 'they'], DIAMOND)}

  • getMFN(table, obj): same as getSorP but for masculine/feminine/neutral. You don’t need to provide a neutral if it does not exist in the target language. getMorF(table, obj) is an alias.

  • valueToSorP(val) will return 'S' (singular) or 'P' (plural) depending on val. Rules are language specific: in English 0 degrees, while in French 0 degré. Useful in referring expressions when the number can vary: setRefNumber(DIAMONDS, valueToSorP(DIAMONDS.numberOfDiamonds));

  • logParams(params) will put in console the stringified param object.

  • access to embedded libs:

  • getting values in parameters:

    • hasFlag(obj, flag) checks if obj[flag] is true. It is just syntaxic sugar.

    • getFlagValue(obj, flag) does the same as hasFlag excepts it returns a value, not a boolean. Is also just syntaxic sugar.

Include

Standard Pug include are supported: include somepug or include somepug.pug will include the somepug.pug file. See Pug includes.

RosaeNLG has an additional feature to include plain JavaScript files. It is just syntaxic sugar and a strict equivalent to including a big pug file:

-
  function someJsCode() {
    ...
  }
  ...

Main benefit are:

  • ability to lint the JavaScript code properly

  • ability to test the code independantly

To include a plain JavaScript file, just include somejsfile.js. The .js file extension must be present.

Same rules as with js code included in Pug files apply. Typically, it is not possible to use require.
Always use / as the path separator. Windows \ is not supported by Pug.

pug_mixins map

You may want to call mixins dynamically, knowing their name only at runtime. The global map pug_mixins references all mixins by their name.