Skip to content

Commit

Permalink
Fix some failing tests, minor tweaks to theme loading
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Feb 14, 2015
1 parent e73e862 commit fcdb001
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 47 deletions.
13 changes: 1 addition & 12 deletions src/bin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ parser = require('yargs')
.options('s', alias: 'server', describe: 'Start a local live preview server')
.options('h', alias: 'host', describe: 'Address to bind local preview server to', default: '127.0.0.1')
.options('p', alias: 'port', describe: 'Port for local preview server', default: 3000)
.options('l', alias: 'list', describe: 'List templates')
.epilog('See https://github.com/danielgtaylor/aglio#readme for more information')

# Console color settings for error/warnings
Expand Down Expand Up @@ -67,17 +66,7 @@ exports.run = (argv=parser.argv, done=->) ->
for entry in config.options
parser.options("theme-#{entry.name}", entry)

if argv.l
# List available templates
aglio.getTemplates (err, names) ->
if err
console.error err
return done err

console.log 'Templates:\n' + names.join('\n')

done()
else if argv.s
if argv.s
if not argv.i
parser.showHelp()
return done 'Invalid arguments'
Expand Down
17 changes: 9 additions & 8 deletions src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ includeReplace = (includePath, match, spaces, filename) ->
includeDirective = (includePath, input) ->
input.replace INCLUDE, includeReplace.bind(this, includePath)

# Get a list of available internal legacy templates
exports.getTemplates = (done) ->
done null, ['cyborg', 'default', 'flatly', 'slate']

# Get a list of all paths from included files. This *excludes* the
# input path itself.
exports.collectPathsSync = (input, includePath) ->
Expand All @@ -45,7 +41,7 @@ exports.collectPathsSync = (input, includePath) ->

# Get the theme module for a given theme name
exports.getTheme = (name) ->
name = 'olio' if name in ['cyborg', 'default', 'flatly', 'slate']
name = 'olio' if not name or name in ['cyborg', 'default', 'flatly', 'slate']
require "aglio-theme-#{name}"

# Render an API Blueprint string using a given template
Expand All @@ -56,8 +52,9 @@ exports.render = (input, options, done) ->
theme: options

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

# For backward compatibility
if options.template then options.theme = options.template
Expand All @@ -66,6 +63,10 @@ exports.render = (input, options, done) ->
console.log "Setting theme to olio and layout to #{options.theme}"
options.themeLayout = options.theme
options.theme = 'olio'
else if options.theme in ['cyborg', 'flatly', 'slate']
console.log "Setting theme to olio and colors to #{options.theme}"
options.themeColors = options.theme
options.theme = 'olio'

# Handle custom directive(s)
input = includeDirective options.includePath, input
Expand All @@ -80,10 +81,10 @@ exports.render = (input, options, done) ->

benchmark.start 'parse'
protagonist.parse filteredInput, (err, res) ->
benchmark.end 'parse'
if err
err.input = filteredInput
return done(err)
benchmark.end 'parse'

try
theme = exports.getTheme options.theme
Expand All @@ -92,8 +93,8 @@ exports.render = (input, options, done) ->

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

# Add filtered input to warnings since we have no
# error to return
Expand Down
38 changes: 11 additions & 27 deletions test/basic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,10 @@ root = path.dirname(__dirname)
blueprint = fs.readFileSync path.join(root, 'example.md'), 'utf-8'

describe 'API Blueprint Renderer', ->
it 'Should get a list of templates', (done) ->
aglio.getTemplates (err, templates) ->
if err then return done(err)

assert templates.length
done()

it 'Should handle template list error', (done) ->
sinon.stub fs, 'readdir', (name, callback) ->
callback 'error'

aglio.getTemplates (err, templates) ->
assert err
it 'Should load the default theme', ->
theme = aglio.getTheme 'default'

fs.readdir.restore()

done()
assert.ok theme

it 'Should get a list of included files', ->
sinon.stub fs, 'readFileSync', -> 'I am a test file'
Expand Down Expand Up @@ -74,7 +61,7 @@ describe 'API Blueprint Renderer', ->
aglio.render temp, 'default', done

it 'Should render a custom template by filename', (done) ->
template = path.join(root, 'templates', 'default.jade')
template = path.join(root, 'test', 'test.jade')
aglio.render '# Blueprint', template, (err, html) ->
if err then return done(err)

Expand Down Expand Up @@ -115,7 +102,9 @@ describe 'API Blueprint Renderer', ->
sinon.stub console, 'log'

aglio.renderFile path.join(root, 'example.md'), '-', 'default', (err) ->
if err then return done(err)
if err
console.log.restore()
return done(err)

assert console.log.called
console.log.restore()
Expand Down Expand Up @@ -179,13 +168,6 @@ describe 'API Blueprint Renderer', ->
done()

describe 'Executable', ->
it 'Should list templates', (done) ->
sinon.stub console, 'log'

bin.run l: true, ->
console.log.restore()
done()

it 'Should render a file', (done) ->
sinon.stub console, 'error'

Expand Down Expand Up @@ -221,6 +203,7 @@ describe 'Executable', ->

sinon.stub http, 'createServer', (handler) ->
listen: (port, host, cb) ->
console.log 'calling listen'
# Simulate requests
req =
url: '/favicon.ico'
Expand Down Expand Up @@ -253,8 +236,9 @@ describe 'Executable', ->
console.error.restore()
assert err

bin.run i: path.join(root, 'example.md'), s: true, p: 3000, h: 'localhost', ->
http.createServer.restore()
bin.run i: path.join(root, 'example.md'), s: true, p: 3000, h: 'localhost', (err) ->
assert.equal err, null
http.createServer.restore()

it 'Should handle errors', (done) ->
sinon.stub aglio, 'renderFile', (i, o, t, callback) ->
Expand Down
8 changes: 8 additions & 0 deletions test/test.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
doctype

html
head
meta(charset="utf-8")
title= self.api.name || 'API Documentation'
body
p Test layout page for a custom API

0 comments on commit fcdb001

Please sign in to comment.