Other structures, mixins and functions
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:
-
in English: titlecase on npm
-
in French: titlecase-french on npm
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 thatisSentenceStart()
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
outputsblabla
.
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. Ifobj
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 asgetSorP
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 onval
. 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:
-
util.numeral
for Numeral.js
-
-
getting values in parameters:
-
hasFlag(obj, flag)
checks ifobj[flag]
is true. It is just syntaxic sugar. -
getFlagValue(obj, flag)
does the same ashasFlag
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.
|