+74
| # Generator specification | ||
| * Required properties: | ||
| * template | templates -> Handlebars templates, {key: template} the key it's used to generate the file name. | ||
| * Optional properties: | ||
| * parser(data) -> It transform the canonical data, it returns the context for handlebars templates, the return could be object or array. | ||
| * helpers -> Handlebars helpers. | ||
| * partials -> Handlebars partials. | ||
| ##Simple definition: | ||
| ```javascript | ||
| var gen = { | ||
| template: {'readme.md' : '{{title}}'} | ||
| } | ||
| ``` | ||
| ##Multiple templates: | ||
| ```javascript | ||
| var gen = { | ||
| templates: [{'readme.md' : '{{title}}'}, {'Main.java' : '{{title}}'}] | ||
| } | ||
| ``` | ||
| ##Interpolating name: | ||
| The key is also a handlebars template it will use the context to generate the final name. | ||
| ```javascript | ||
| var gen = { | ||
| template: {'readme{{title}}.md' : '{{title}}'} | ||
| } | ||
| ``` | ||
| ##Defining a parser: | ||
| A parser could be used to modify or adapt the data, generally is used | ||
| to reduce logic in the template. | ||
| ```javascript | ||
| var gen = { | ||
| template: {'readme{{title}}.md' : '{{title}}'}, | ||
| parser: function(data){} | ||
| } | ||
| ``` | ||
| A parse could return and array, each element of the array will be passed to the template, this is useful to | ||
| generate multiple files. | ||
| ##Multiple templates using the same parser: | ||
| ```javascript | ||
| var gen = { | ||
| templates: [{'readme{{title}}.md' : '{{title}}', {'{{className}}Resource.java' : '{{title}}'], | ||
| parser: function(data){} | ||
| } | ||
| ``` | ||
| ##Multiple templates each one using different parser: | ||
| ```javascript | ||
| var gen = { | ||
| templates: [ | ||
| { | ||
| '{{name}}Resource.java': { | ||
| tmpl: 'hola {{title}}', | ||
| parser: function () { | ||
| return [{title: 'parse1', name: 'test'}] | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| '{{name}}.md': { | ||
| tmpl: 'readme {{title}}', | ||
| parser: function () { | ||
| return [{title: 'parse2', name: 'readme'}] | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| ``` |
+1
-2
| { | ||
| "env": { | ||
| "node": true, | ||
| "browser": true, | ||
| "mocha": true | ||
| "browser": true | ||
| }, | ||
@@ -7,0 +6,0 @@ "rules": { |
+51
-48
| 'use strict' | ||
| var Handlebars = require('handlebars') | ||
| var processParsers = function(tmpl, gen, data){ | ||
| var processParsers = function (tmpl, gen, data) { | ||
| var sdata = JSON.parse(JSON.stringify(data)) | ||
| var parsed | ||
| if(typeof tmpl === 'function'){ | ||
| if(typeof gen.parser === 'function'){ | ||
| parsed = gen.parser(sdata) | ||
| }else{ | ||
| parsed = sdata | ||
| } | ||
| }else if(typeof tmpl === 'object'){ | ||
| if(typeof tmpl.parser === 'function'){ | ||
| parsed = tmpl.parser(sdata) | ||
| }else{ | ||
| parsed = sdata | ||
| } | ||
| if (typeof tmpl.parser === 'function') { | ||
| parsed = tmpl.parser(sdata) | ||
| } else if (typeof gen.parser === 'function') { | ||
| parsed = gen.parser(sdata) | ||
| } else { | ||
| parsed = sdata | ||
| } | ||
| return parsed | ||
| } | ||
| var compileTemplate = function(tmpl){ | ||
| if(typeof tmpl === 'function'){ | ||
| var compileTemplate = function (tmpl) { | ||
| if (typeof tmpl === 'function') { | ||
| return tmpl | ||
| }else if(typeof tmpl === 'string') { | ||
| } else if (typeof tmpl === 'string') { | ||
| return Handlebars.compile(tmpl) | ||
| }else if(typeof tmpl === 'object'){ | ||
| tmpl.tmpl = compileTemplate(tmpl.tmpl) | ||
| return tmpl | ||
| } | ||
| } | ||
| var handleTemplates = function(tmpls){ | ||
| var handleObjectTemplate = function (tmpl) { | ||
| for (var key in tmpl) break | ||
| if (typeof tmpl !== 'object') { | ||
| throw new Error('generator.template should be an object') | ||
| } | ||
| if (typeof tmpl[key] === 'object') { | ||
| tmpl[key].tmpl = compileTemplate(tmpl[key].tmpl) | ||
| } else if (typeof tmpl[key] === 'string') { | ||
| tmpl[key] = {tmpl: compileTemplate(tmpl[key])} | ||
| } | ||
| return tmpl | ||
| } | ||
| var handleTemplates = function (tmpls) { | ||
| var templates = [] | ||
| if(tmpls === undefined){ | ||
| if (tmpls === undefined) { | ||
| return templates | ||
| } | ||
| if(Array.isArray(tmpls)){ | ||
| tmpls.forEach(function(def){ | ||
| templates.push(compileTemplate(def)) | ||
| if (Array.isArray(tmpls)) { | ||
| tmpls.forEach(function (def) { | ||
| for (var key in def) break | ||
| if (typeof def[key] === 'object') { | ||
| def[key].tmpl = compileTemplate(def[key].tmpl) | ||
| } else if (typeof def[key] === 'string') { | ||
| def[key] = {tmpl: compileTemplate(def[key])} | ||
| } | ||
| templates.push(def) | ||
| }) | ||
| }else{ | ||
| } else { | ||
| throw new Error('gen.templates must be array') | ||
@@ -50,20 +62,14 @@ } | ||
| var bindTemplate = function(tmplDef, data, options){ | ||
| var htmpl = tmplDef | ||
| if(typeof tmplDef === 'object'){ | ||
| htmpl = tmplDef.tmpl | ||
| } | ||
| var bindTemplate = function (key, tmplDef, data, options) { | ||
| var htmpl = tmplDef[key].tmpl | ||
| var result = {} | ||
| var key = Object.keys(data)[0] | ||
| result[key] = htmpl(data[key], options) | ||
| var nameTmpl = Handlebars.compile(key) | ||
| result[nameTmpl(data, options)] = htmpl(data, options) | ||
| return result | ||
| } | ||
| module.exports.process = function(data, gen) { | ||
| module.exports.process = function (data, gen) { | ||
| if (gen.handleRender && typeof gen.handleRender === 'function') { | ||
| var templates = [] | ||
| if(gen.template !== undefined){ | ||
| templates = templates.concat(compileTemplate(gen.template)) | ||
| if (gen.template !== undefined) { | ||
| templates = templates.concat(handleObjectTemplate(gen.template)) | ||
| } | ||
@@ -77,19 +83,16 @@ templates = templates.concat(handleTemplates(gen.templates)) | ||
| var results = [] | ||
| templates.forEach(function(template){ | ||
| var dataParsed = processParsers(template, gen, data) | ||
| templates.forEach(function (template) { | ||
| for (var key in template) break | ||
| var dataParsed = processParsers(template[key], gen, data) | ||
| if (Array.isArray(dataParsed)) { | ||
| dataParsed.forEach(function(dato){ | ||
| results.push(bindTemplate(template, dato, options)) | ||
| dataParsed.forEach(function (element) { | ||
| results.push(bindTemplate(key, template, element, options)) | ||
| }) | ||
| } else { | ||
| results.push(bindTemplate(template, dataParsed, options)) | ||
| results.push(bindTemplate(key, template, dataParsed, options)) | ||
| } | ||
| }) | ||
| return results | ||
| gen.handleRender(results) | ||
| } else { | ||
| throw new Error('generator.handleRender not defined') | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "data2code", | ||
| "version": "0.0.5", | ||
| "version": "0.0.6", | ||
| "description": "Codegenerator for data", | ||
@@ -5,0 +5,0 @@ "main": "lib/data2code.js", |
+5
-39
@@ -10,27 +10,6 @@ # data to code generator | ||
| ## Defining a Generator | ||
| [Generator spec](Generator.md) | ||
| Usually a generator it's responsible for certain path of the javascript object. | ||
| A generator is a simple object with the following properties: | ||
| * Required properties: | ||
| * template -> Handlebars template. | ||
| * parser(data) -> Function it receives RAML parsed data, returns parsed data | ||
| The parser must return and array of object each object must have name and content properties | ||
| Sample: | ||
| ```javascript | ||
| [ { name: "test.test", | ||
| model: { | ||
| title:data.title + " finos" | ||
| } | ||
| } | ||
| ] | ||
| ``` | ||
| * Optional properties: | ||
| * handleRender([str]) -> This function handles the render results, usually writes to disk. | ||
| * helpers -> Handlebars helpers. | ||
| * partials -> Handlebars partials. | ||
| ##Usage | ||
@@ -40,22 +19,9 @@ | ||
| var data2Code = require('data2Code'); | ||
| var raml = require('raml-parser'); | ||
| var simpleGen = {}; | ||
| simpleGen.template = '{{title}}'; | ||
| simpleGen.parser = function(){ | ||
| var gen = { | ||
| template: {'readme.md' : '{{title}}' | ||
| } | ||
| var handleRender = function (result) { | ||
| console.log(result); | ||
| } | ||
| Or to use multiple templates | ||
| var simpleGen = {} | ||
| simpleGen.templates = [{tmpl: "{title}", parser: function }] | ||
| raml.loadFile('myAPI.raml').then( function(data){ | ||
| data2Code.process(data, simpleGen); | ||
| data2Code.process(data, anotherGen); | ||
| }, function(error) { | ||
| console.log('Error parsing: ' + error); | ||
| }); | ||
| var results = data2Code.process(sampleData, gen) | ||
| ``` | ||
6838
19.38%8
14.29%110
2.8%26
-56.67%