Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose a render function which accepts a preparsed blueprint as input #196

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ alio.render(blueprint, options, function (err, html, warnings) {
});
```

#### aglio.renderBlueprint (blueprint, options, callback)
Render a preparsed API Blueprint. Analogous to `render` except for the first argument, which is expected to be a blueprint object.

```javascript
aglio.renderBlueprint(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.

Expand Down
56 changes: 34 additions & 22 deletions src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ exports.getTheme = (name) ->
name = 'olio' if not name or name in LEGACY_TEMPLATES
require "aglio-theme-#{name}"

# Render an API Blueprint string using a given template
exports.render = (input, options, done) ->
# Render an API Blueprint object using a given template
exports.renderBlueprint = (input, options, done) ->
# Support a template name as the options argument
if typeof options is 'string' or options instanceof String
options =
theme: options

# Defaults
options.filterInput ?= true
options.includePath ?= process.cwd()
options.theme ?= 'default'

# For backward compatibility
Expand All @@ -79,6 +77,36 @@ exports.render = (input, options, done) ->
options.themeVariables = variables
options.theme = 'olio'

try
theme = exports.getTheme options.theme
catch err
return done(errMsg 'Error getting theme', err)

# Setup default options if needed
for option in theme.getConfig().options or []
# Convert `foo-bar` into `themeFooBar`
words = (f[0].toUpperCase() + f.slice(1) for f in option.name.split('-'))
name = "theme#{words.join('')}"
options[name] ?= option.default

benchmark.start 'render-total'
theme.render input, options, (err, html) ->
benchmark.end 'render-total'
if err then return done(err)

done null, html, null

# Render an API Blueprint string using a given template
exports.render = (input, options, done) ->
# Support a template name as the options argument
if typeof options is 'string' or options instanceof String
options =
theme: options

# Defaults
options.filterInput ?= true
options.includePath ?= process.cwd()

# Handle custom directive(s)
input = includeDirective options.includePath, input

Expand All @@ -97,28 +125,12 @@ exports.render = (input, options, done) ->
err.input = input
return done(errMsg 'Error parsing input', err)

try
theme = exports.getTheme options.theme
catch err
return done(errMsg 'Error getting theme', err)

# Setup default options if needed
for option in theme.getConfig().options or []
# Convert `foo-bar` into `themeFooBar`
words = (f[0].toUpperCase() + f.slice(1) for f in option.name.split('-'))
name = "theme#{words.join('')}"
options[name] ?= option.default

benchmark.start 'render-total'
theme.render res.ast, options, (err, html) ->
benchmark.end 'render-total'
if err then return done(err)

exports.renderBlueprint res.ast, options, (err, html) ->
# Add filtered input to warnings since we have no
# error to return
res.warnings.input = filteredInput

done null, html, res.warnings
done err, html, res.warnings

# Render from/to files
exports.renderFile = (inputFile, outputFile, options, done) ->
Expand Down