Use WIDL as input to Handlebars templates for code generation, documentation, etc
npm install widl-templateThis library and CLI utility allows you to pass serialized [WIDL]() to a Handlebars template for code generation, automatic documentation, linking, etc.
``shell`
$ npm install widl-template
On the command line:
``
$ widl-template
In JavaScript
`js
const { render } = require('widl-template');
const widlSrc = // string of WIDL
const templateSrc = // string of Handlebars template source
const renderedTemplate = render(widlSrc, templateSrc);
console.log(renderedTemplate);
`
Use the WIDL validator's AST view to visualize the structure of the WIDL data.
This utility uplevels the first namespace and interface it finds to the tree's root so you can more easily access them in your templates.
Register all .hbs files in a directory as partials by using the registerPartials() function or by passing a directory to the CLI via -p or --partials
`js
import { registerPartials } from 'widl-template';
await registerPartials('partialsdir');
`
This library includes two helpers to help templating from the command line:
A conditional block that tests the kind of WIDL node
`hbs`
{{#isKind 'TypeDefinition'}}
# Type:
{{name.value}}
{{/isKind}}
The join maps over ever element with the passed block and joins them with the supplied separator.
`hbs`
({{#join parameters ', '}}{{name}}:{{type}}{{/join}})
Given parameters of [{name: 'someName', type:'someValue'},{name: 'someName2', type:'someValue2'}], the join above would output:
``
(someName: someValue, someName2: someValue2)
Case-related helpers that expose functions from change-case-all e.g.
`hbs`
{{pascalCase context}}
Uppercase & lowercase helpers that transform an entire string
`hbs`
{{upperCase context}}
Import another widl file
`hbs`
{{#import 'other/file.widl'}}
# Hello from
{{namespace.name.value}}
{{/import}}
Exposes Node.js's path.dirname.
`hbs`
{{dirname value}}
Exposes Node.js's path.basename.
`hbs`
{{basename value}}
Simple string replacement helper.
`hbs`
{{replace original '.js' ''}}
An implementation of switch/case statements as handlebars helpers.
`hbs`
{{#switch kind}}
{{#case 'A'}}
First block
{{/case}}
{{#case 'B'}}
Second block
{{/case}}
{{#default}}
Default block
{{/default}}
{{/switch}}
A block that iterates over every object in a passed list that has a name property equal to the passed name.
Used to iterate over fields or definitions to find a specific name
`hbs`
{{#eachWithName definitions 'MyType'}}
Some description specific to MyType
{{/eachWithName}}
This is a partial code generator that turns a WIDL type node (i.e. from a field or argument, not a TypeDefinition) back into a WIDL string.
```
{{#each fields}}
{{name.value}} : {{codegen-type type}}
{{/each}}