Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

aglio

danielgtaylor11.5kMIT2.3.0

An API Blueprint renderer with theme support

api, blueprint, protagonist, snowcrash, html, parse, markdown

readme

aglio

Dependency Status Build Status Coverage Status NPM version License Gitter

Introduction

An API Blueprint renderer that supports multiple themes and outputs static HTML that can be served by any web host. API Blueprint is a Markdown-based document format that lets you write API descriptions and documentation in a simple and straightforward way. Currently supported is API Blueprint format 1A.

Features

  • Fast parsing thanks to Protagonist
  • Asyncronous processing
  • Multiple templates/themes
  • Support for custom colors, templates, and theme engines
  • Include other documents in your blueprint
  • Commandline executable aglio -i service.apib -o api.html
  • Live-reloading preview server aglio -i service.apib --server
  • Node.js library require('aglio')
  • Excellent test coverage
  • Tested on BrowserStack

Example Output

Example output is generated from the example API Blueprint using the default Olio theme.

Including Files

It is possible to include other files in your blueprint by using a special include directive with a path to the included file relative to the current file's directory. Included files can be written in API Blueprint, Markdown or HTML (or JSON for response examples). Included files can include other files, so be careful of circular references.

<!-- include(filename.md) -->

For tools that do not support this include directive it will just render out as an HTML comment. API Blueprint may support its own mechanism of including files in the future, and this syntax was chosen to not interfere with the external documents proposal while allowing aglio users to include documents today.

Installation & Usage

There are three ways to use aglio: as an executable, in a docker container or as a library for Node.js.

Executable

Install aglio via NPM. You need Node.js installed and you may need to use sudo to install globally:

npm install -g aglio

Then, start generating HTML.

# Default theme
aglio -i input.apib -o output.html

# Use three-column layout
aglio -i input.apib --theme-template triple -o output.html

# Built-in color scheme
aglio --theme-variables slate -i input.apib -o output.html

# Customize a built-in style
aglio --theme-style default --theme-style ./my-style.less -i input.apib -o output.html

# Custom layout template
aglio --theme-template /path/to/template.jade -i input.apib -o output.html

# Custom theme engine
aglio -t my-engine -i input.apib -o output.html

# Run a live preview server on http://localhost:3000/
aglio -i input.apib -s

# Print output to terminal (useful for piping)
aglio -i input.apib -o -

# Disable condensing navigation links
aglio --no-theme-condense -i input.apib -o output.html

# Render full-width page instead of fixed max width
aglio --theme-full-width -i input.apib -o output.html

# Set an explicit file include path and read from stdin
aglio --include-path /path/to/includes -i - -o output.html

# Output verbose error information with stack traces
aglio -i input.apib -o output.html --verbose

With Docker

You can choose to use the provided Dockerfile to build yourself a repeatable and testable environment:

  1. Build the image with docker build -t aglio .
  2. Run aglio inside a container with docker run -t aglio You can use the -v switch to dynamically mount the folder that holds your API blueprint:
docker run -v $(pwd):/tmp -t aglio -i /tmp/input.apib -o /tmp/output.html

Node.js Library

You can also use aglio as a library. First, install and save it as a dependency:

npm install --save aglio

Then, convert some API Blueprint to HTML:

var aglio = require('aglio');

// Render a blueprint with a template by name
var blueprint = '# Some API Blueprint string';
var options = {
  themeVariables: 'default'
};

aglio.render(blueprint, options, function (err, html, warnings) {
    if (err) return console.log(err);
    if (warnings) console.log(warnings);

    console.log(html);
});

// Render a blueprint with a custom template file
options = {
  themeTemplate: '/path/to/my-template.jade'
};
aglio.render(blueprint, options, function (err, html, warnings) {
    if (err) return console.log(err);
    if (warnings) console.log(warnings);

    console.log(html);
});


// Pass custom locals along to the template, for example
// the following gives templates access to lodash and async
options = {
    themeTemplate: '/path/to/my-template.jade',
    locals: {
        _: require('lodash'),
        async: require('async')
    }
};
aglio.render(blueprint, options, function (err, html, warnings) {
   if (err) return console.log(err);
   if (warnings) console.log(warnings);

   console.log(html);
});

Reference

The following methods are available from the aglio library:

aglio.collectPathsSync (blueprint, includePath)

Get a list of paths that are included in the blueprint. This list can be watched for changes to do things like live reload. The blueprint's own path is not included.

var blueprint = '# GET /foo\n<-- include(example.json -->\n';
var watchPaths = aglio.collectPathsSync(blueprint, process.cwd())

aglio.render (blueprint, options, callback)

Render an API Blueprint string and pass the generated HTML to the callback. The options can either be an object of options or a simple layout name or file path string. Available options are:

Option Type Default Description
filterInput bool true Filter \r and \t from the input
includePath string process.cwd() Base directory for relative includes
locals object {} Extra locals to pass to templates
theme string 'default' Theme name to load for rendering

In addition, the default theme provides the following options:

Option Type Default Description
themeVariables string default Built-in color scheme or path to LESS or CSS
themeCondenseNav bool true Condense single-action navigation links
themeFullWidth bool false Use the full page width
themeTemplate string | Layout name or path to custom layout file
themeStyle string default Built-in style name or path to LESS or CSS
var blueprint = '...';
var options = {
    themeTemplate: 'default',
    locals: {
        myVariable: 125
    }
};

alio.render(blueprint, options, function (err, html, warnings) {
    if (err) return console.log(err);

    console.log(html);
});

aglio.renderFile (inputFile, outputFile, options, callback)

Render an API Blueprint file and save the HTML to another file. The input/output file arguments are file paths. The options behaves the same as above for aglio.render, except that the options.includePath defaults to the basename of the input filename.

aglio.renderFile('/tmp/input.apib', '/tmp/output.html', options, function (err, warnings) {
    if (err) return console.log(err);
    if (warnings) console.log(warnings);
});

Development

Pull requests are encouraged! Feel free to fork and hack away, especially on new themes. The build system in use is Grunt, so make sure you have it installed:

npm install -g grunt-cli

Then you can build the source and run the tests:

# Lint/compile the Coffeescript
grunt

# Run the test suite
grunt test

# Generate an HTML test coverage report
grunt coverage

# Render examples
grunt examples

Customizing Output

Aglio is split into two components: a base that contains logic for loading API Blueprint, handling commandline arguments, etc and a theme engine that handles turning the API Blueprint AST into HTML. The default theme engine that ships with Aglio is called olio. Templates are written in Jade, with support for inline Coffeescript, LESS and Stylus via filters. The default stylesheets are written in LESS.

While developing customizations, you may want to disable caching using the NOCACHE environment variable.

NOCACHE=1 aglio -i input.apib [customization options]

Custom Colors & Style

Aglio's default theme provides a way to easily override colors, fonts, padding, etc to match your company's style. This is done by providing your own LESS or CSS file(s) via the --theme-variables and --theme-style options. For example:

# Use my custom colors
aglio --theme-variables /path/to/my-colors.less -i input.apib -o output.html

The my-variables.less file might contain a custom HTTP PUT color specification:

/* HTTP PUT */
@put-color: #f0ad4e;
@put-background-color: #fcf8e3;
@put-text-color: contrast(@put-background-color);
@put-border-color: darken(spin(@put-background-color, -10), 5%);

See the default variables file for examples of which variables can be set.

The --theme-style option lets you override built-in styles with your own LESS or CSS definitions. It is processed after the variables have been defined, so the variables are available for your use. If you wish to modify a rule from an existing built-in style then you must copy the style. The order of loading roughly follows:

  1. Default variables
  2. Built-in or user-supplied variables
  3. Built-in or user-supplied style

Note that these options can be passed more than once, in which case they will be loaded in the order they were passed. This lets you, for example, load a variable preset like flatly and modify one of the colors with your own LESS file. Keep in mind that when you want to modify a built-in style you must explicitly list the style, e.g. --theme-style default --theme-style my-style.less.

Built-in Colors

  • cyborg
  • default
  • flatly
  • slate

Built-in Styles

  • default

Customizing Layout Templates

The --theme-template option allows you to provide a custom layout template that overrides the default. This is specified in the form of a Jade template file. See the default template file for an example.

The locals available to templates look like the following:

Name Description
api The API Blueprint AST from Protagonist
condenseNav If true, you should condense the nav if possible
date Date and time handling from Moment.js
fullWidth If true, you should consume the entire page width
highlight A function (code, lang) to highlight a piece of code
markdown A function to convert Markdown strings to HTML
slug A function to convert a string to a slug usable as an ID
hash A function to return an hash (currently MD5)

Built-in Layout Templates

  • default

Using Custom Themes

While Aglio ships with a default theme, you have the option of installing and using third-party theme engines. They may use any technology and are not limited to Jade and LESS. Consult the theme's documentation to see which options are available and how to use and customize the theme. Common usage between all themes:

# Install a custom theme engine globally
npm install -g aglio-theme-<NAME>

# Render using a custom theme engine
aglio -t <NAME> -i input.apib -o output.html

# Get a list of all options for a theme
aglio -t <NAME> --help

Writing a Theme Engine

Theme engines are simply Node.js modules that provide two public functions and follow a specific naming scheme (aglio-theme-NAME). Because they are their own npm package they can use whatever technologies the theme engine author wishes. The only hard requirement is to provide these two public functions:

getConfig()

Returns configuration information about the theme, such as the API Blueprint format that is supported and any options the theme provides.

render(input, options, done)

Render the given input API Blueprint AST with the given options. Calls done(err, html) when finished, either passing an error or the rendered HTML output as a string.

Example Theme

The following is a very simple example theme. Note: it only returns a very simple string instead of rending out the API Blueprint AST. Normally you would invoke a template engine and output the resulting HTML that is generated.

// Get the theme's configuration options
exports.getConfig = function () {
  return {
    // This is a list of all supported API Blueprint format versions
    formats: ['1A'],
    // This is a list of all options your theme accepts. See
    // here for more: https://github.com/bcoe/yargs#readme
    // Note: These get prefixed with `theme` when you access
    // them in the options object later!
    options: [
      {
        name: 'name',
        description: 'Your name',
        default: 'world'
      }
    ]
  };
}

// Asyncronously render out a string
exports.render = function (input, options, done) {
  // Normally you would use some template engine here.
  // To keep this code really simple, we just print
  // out a string and ignore the API Blueprint.
  done(null, 'Hello, ' + options.themeName + '!');
};

Example use:

# Install the theme globally
npm install -g aglio-theme-hello

# Render some output!
aglio -t hello -i example.apib -o -
=> 'Hello, world!'

# Pass in the custom theme option!
aglio -t hello --theme-name Daniel -i example.apib -o -
=> 'Hello, Daniel!'

You are free to use whatever template system (Jade, EJS, Nunjucks, etc) and any supporting libraries (e.g. for CSS) you like.

License

Copyright (c) 2016 Daniel G. Taylor

http://dgt.mit-license.org/

changelog

2.3.0

  • Switch to Drafter for API Blueprint parsing. #277
  • Fix case when no arguments are supplied after -o #262

2.2.1 - 2016-05-20

  • Bump protagonist version to 1.3.2 to support new features. #256
  • Update required default theme version to 1.6.3.

2.2.0 - 2015-10-26

  • Support for Node.js 3.x and 4.x. #183
  • Upgraded to Protagonist version 1.x, which has disabled the JSON Schema generation from MSON until some bugs can be worked out.

2.1.1 - 2015-09-14

  • Fix the default include path behavior when outputting a file to use the basename of the input file rather than process.cwd(). Also updates documentation to mention this behavior and option. #166

2.1.0 - 2015-09-11

  • Add a --include-path option to set the path for relative includes. #165

2.0.4 - 2015-08-14

  • Show theme name before loading when given --verbose option.
  • Update required default theme version to 1.2.0.

2.0.3 - 2015-08-12

  • Update required default theme version to 1.1.0.
  • Readme and example updates.

2.0.2 - 2015-08-04

  • Update required default theme version to support custom jade templates written for Aglio 1.x.
  • Add a --verbose output option for errors that displays pretty stack traces.
  • Provide more descriptive errors.

2.0.1 - 2015-08-03

  • Add backward-compatible support for the aglio binary to specify a custom layout via the -t commandline option. Example: aglio -i input.apib -t /path/to/my.jade -o output.html
  • Display default theme version when using aglio --version.

2.0.0 - 2015-07-16

This is a new major version of Aglio, and as such has some breaking changes. High-level changes in this release:

  • Use Drafter.js to support MSON via generated request/response bodies and schemas.
  • Add support for theme engines.
  • Use Olio as the new default theme engine.
  • API and resource group description headers are now included in the navigation bar.
  • Server mode can now serve static files, which is useful if your documentation contains images.
  • Fixes to how resource and action parameters are handled.

For more detailed information, see the beta releases below.

Effort was taken to prevent backward-incompatible changes. Here is a list of things that will break if you used them in 1.x.

Binary:

  • It is no longer possible to list templates (aglio -l). You may use npm list -g | grep aglio-theme to list all installed theme engine packages instead. Refer to individual theme documentation for possible theme engine options.

Library:

  • The aglio.getTemplates function has been removed.

Templates:

  • The multi-page layouts have been removed. Please open an issue if you would like to see them in the new theme engine.
  • The collapsed navigation layouts have been removed. This is now in the default theme and handled automatically based on browser window height.

The following are translated internally and will not break, but are suggested updates:

Binary:

  • The -t option is now shorthand for --theme instead of --template.
  • The --full-width and --condense-nav parameters are now --theme-full-width and --theme-condense-nav in the default theme engine.

Library:

  • Passing a string as the options to render and renderFile will still work if it is a known variant: default, flatly, slate, cyborg or one of the collapsed versions of those. If it is a path and the file exists, then it will use it as a custom themeLayout option. Otherwise it will set the theme engine name. :dizzy_face:
  • You should use options.theme instead of options.template.
  • You should use options.themeVariables = 'flatly' to set the color variation.
  • You should use options.themeTemplate = '/path/to/layout.jade' to set the layout template.

Thank you to all the contributors and testers for helping to make this an awesome release! :beers:

2.0.0-beta6 - 2015-07-14

2.0.0-beta5 - 2015-07-10

  • Fix an issue with included paths when using --server.
  • Update to olio theme 0.0.8.

2.0.0-beta4 - 2015-07-10

2.0.0-beta3 - 2015-07-09

  • Documentation updates.
  • Server mode now serves static files if found.
  • Add ability to output compiled API Blueprint file instead of HTML.
  • Update to olio theme 0.0.6.

2.0.0-beta2 - 2015-05-29

  • Live update fixes.
  • Example fixes.

2.0.0-beta1 - 2015-05-28

  • Implement theme engine support; depend on the default olio theme.
  • Switch to using drafter.js instead of protagonist directly.

1.18.0 - 2015-03-31

  • Upgrade to [Protagonist] 0.19.0, which adds support for Node.js 0.12.x and iojs 1.x. (#77)

1.17.1 - 2014-12-16

  • Switch to Remarkable Markdown parser, which is faster and supports the new CommonMark specification. GFM is supported.
  • Fix live reload no longer working with some configurations (#74)
  • Watch all included files for live reloading.

1.17.0 - 2014-12-16

1.16.2 - 2014-11-18

  • Update dependencies (chokidar, marked, protagonist, stylus)
  • Fixes rendering description when headers are not present (#66)
  • Fixes minor typo (#67)

1.16.1 - 2014-08-29

  • Fixes template js bug related to live reloading. (179ea7e)

1.16.0 - 2014-08-29

  • Fix long choice lists not wrapping (#35)
  • Fix long hostnames not wrapping (#55)
  • Add support for live reloading the preview server (#57)
  • Fix a bug when reading from stdin (#59)
  • Update dependencies (coffee-script)
  • Minor test fixes