pennyworth

1.0.1 • Public • Published

pennyworth Build Status

a natural language templating engine

NPM

usage

// build a template from a template string
var template = pennyworth.template('my $adjective template.');
 
// the template is a function that can be called with data at
// any time and multiple times
template('my beautiful template').then(function (compiled) {
    // it will always return a promise to allow support for
    // asynchronous filters
 
    console.log(compiled); // { adjective : 'beautiful' }
});

spec

variables

template: hello, $who.

  • for hello, alfred => $who = alfred
  • for hello, alfred. how are you? => $who = alfred how are you
  • for hello, how are you? => $who = how are you

template: hello, $who. how are you?

  • for hello, alfred. => $who = alfred
  • for hello, alferd. how are you? => $who = alfred
  • for hello, how are you? => $who = ``

variable filters

template: i am $x years old.

  • for i am 18 years old. => $x = "18"

template: i am $x:int years old.

  • for i am 18 years old => $x = 18

Supported filters: string, int, float, word.

Adding a new filter:

pennyworth.filter('my-new-filter', function ( input ) {
    return String(input) + '!';
});
 
var template = pennyworth.template('hello, $who:my-new-filter.');
template('hello, alfred.').who === 'alfred!'; // true

For asynchronous filters, grab a callback using this.async():

pennyworth.filter('async', function ( input ) {
    let done = this.async();
    done( error , filtered );
});

named entities

pennyworth uses the Stanford NER for named entity filters.

Supported filters (from the 7 class model): Location, Person, Organization, Money, Percent, Date, Time

To use any, just use the entity type as the filter name:

For instance, for the input string My name is Alfred Pennyworth not Wayne Mansion., if you use the filter 'string': My name is $name:string then the resolved name will be "Alfred Pennyworth not Wayne Mansion". But if you use the filter 'person', the output will be "Alfred Pennyworth".

expander

to expand a set of arguments, use curly braces:

  • {hey, hi, hello}, there will expand to:
  • hey, there.
  • hi, there.
  • hello, there.

conditional

to make a part of the expansion conditional, use a question mark:

  • i am $age {years?} expands to:
  • i am $age
  • i am $age years

Package Sidebar

Install

npm i pennyworth

Weekly Downloads

2

Version

1.0.1

License

GPL-3.0

Last publish

Collaborators

  • karimsa