# widl-template

> Use WIDL as input to Handlebars templates for code generation, documentation, etc

Latest version **0.6.3** (published 2021-07-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install widl-template
pnpm add widl-template
yarn add widl-template
bun add widl-template
```

Provides the command `widl-template`.

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.3 |
| Published | 2021-07-05 |
| First published | 2021-06-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 63.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | jsoverson@gmail.com |
| Maintainers | jsoverson |

## Links

- npm: https://www.npmjs.com/package/widl-template
- npm.io page: https://npm.io/package/widl-template

## Dependencies (5)

- [debug](https://npm.io/package/debug.md) ^4.3.1
- [yargs](https://npm.io/package/yargs.md) ^17.0.1
- [@wapc/widl](https://npm.io/package/@wapc/widl.md) 0.0.9
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.7
- [change-case-all](https://npm.io/package/change-case-all.md) ^1.0.14

## Recent versions

- 0.6.3 (latest) — 2021-07-05
- 0.6.2 — 2021-06-28
- 0.6.0 — 2021-06-28
- 0.5.0 — 2021-06-23
- 0.4.0 — 2021-06-15
- 0.3.0 — 2021-06-13
- 0.2.1 — 2021-06-11
- 0.2.0 — 2021-06-11
- 0.1.0 — 2021-06-10
- 0.0.2 — 2021-06-09
- 0.0.1 — 2021-06-09

## README

# widl-template: WIDL Handlebars Templater

This library and CLI utility allows you to pass serialized [WIDL]() to a Handlebars template for code generation, automatic documentation, linking, etc.

## Installation

```shell
$ npm install widl-template
```

## Usage

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);
```

## Data format

Use the [WIDL validator's](https://jsoverson.github.io/widl-validator/) AST view to visualize the structure of the WIDL data.

### Notes: Minor AST changes

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.

## Partials

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');
```

## Helpers

This library includes two helpers to help templating from the command line:

### `isKind`

A conditional block that tests the kind of WIDL node

```hbs
{{#isKind 'TypeDefinition'}}
  # Type:
  {{name.value}}
{{/isKind}}
```

### `join`

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)
```

### `camelCase`

### `capitalCase`

### `constantCase`

### `dotCase`

### `headerCase`

### `noCase`

### `paramCase`

### `pascalCase`

### `pathCase`

### `sentenceCase`

### `snakeCase`

Case-related helpers that expose functions from [change-case-all](https://www.npmjs.com/package/change-case-all) e.g.

```hbs
{{pascalCase context}}
```

### `upperCase`

### `lowerCase`

Uppercase & lowercase helpers that transform an entire string

```hbs
{{upperCase context}}
```

### `import` block

Import another `widl` file

```hbs
{{#import 'other/file.widl'}}
  # Hello from
  {{namespace.name.value}}
{{/import}}
```

### `dirname`

Exposes Node.js's [path.dirname](https://nodejs.org/api/path.html#path_path_dirname_path).

```hbs
{{dirname value}}
```

### `basename`

Exposes Node.js's [path.basename](https://nodejs.org/api/path.html#path_path_dirname_path).

```hbs
{{basename value}}
```

### `replace`

Simple string replacement helper.

```hbs
{{replace original '.js' ''}}
```

### `switch`/`case`/`default`

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}}
```

### `eachWithName`

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}}
```

### `codegen-type`

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}}
```

---
_Source: https://npm.io/package/widl-template · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
