https://github.com/karimsa/pennyworth
a natural language templating engine for alfred
https://github.com/karimsa/pennyworth
Last synced: about 1 year ago
JSON representation
a natural language templating engine for alfred
- Host: GitHub
- URL: https://github.com/karimsa/pennyworth
- Owner: karimsa
- Created: 2015-07-29T02:03:48.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2016-06-29T09:12:12.000Z (almost 10 years ago)
- Last Synced: 2025-03-11T21:03:58.207Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 39.1 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# pennyworth [](https://travis-ci.org/karimsa/pennyworth)
a natural language templating engine
[](https://nodei.co/npm/pennyworth/)
## usage
```javascript
// 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:
```javascript
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():
```javascript
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