Skip to content

Instantly share code, notes, and snippets.

@AWaselnuk
Last active August 29, 2015 14:13
Show Gist options
  • Save AWaselnuk/d276322ea09c976cae57 to your computer and use it in GitHub Desktop.
Save AWaselnuk/d276322ea09c976cae57 to your computer and use it in GitHub Desktop.
Converting a RAML file to a Handlebars template and then to an HTML file.
## parse a .raml file and then compile Handlebar templates
# Import libraries
fs = require('fs')
appRoot = require('app-root-path')
RAML = require('raml-parser')
Handlebars = require('handlebars')
saveTemplate = (pathToView, renderedTemplate) ->
console.log 'Saving template'
fs.writeFile pathToView, renderedTemplate, (error) ->
throw error if error
console.log('RAML precompiling complete!')
compileTemplate = (pathToTemplate, pathToView, RAMLdata) ->
console.log 'Compiling Template'
fs.readFile pathToTemplate, 'utf8', (error, template) ->
throw error if error
saveTemplate pathToView, Handlebars.compile(template)(RAMLdata)
# Parse RAML using this library
# https://github.com/raml-org/raml-js-parser
pathToRAML = "#{appRoot}/app/assets/raml/some_file.raml"
pathToTemplate = "#{appRoot}/app/assets/javascripts/templates/some_template.hbs"
pathToView = "#{appRoot}/app/views/api_resources/some_file.html.erb"
RAML.loadFile(pathToRAML).then(
(RAMLdata) -> compileTemplate(pathToTemplate, pathToView, RAMLdata)
, (error) -> console.error "RAML parsing error: #{error}"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment