├── .github └── FUNDING.yml ├── test ├── test_zen.coffee ├── test_zen.js ├── test.coffee └── test.js ├── index.coffee ├── package.json ├── index.js ├── LICENSE └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://gumroad.com/l/hGYGh 2 | -------------------------------------------------------------------------------- /test/test_zen.coffee: -------------------------------------------------------------------------------- 1 | dsl = require('./../index.js') 2 | zen = require 'zen-coding' 3 | 4 | dsl.parseKey = (k) -> zen k+'>{%s}' 5 | dsl.parseValue = (v,data) -> data[v] 6 | 7 | json = 8 | 'div#foo.flop>fieldset>div>ul': 9 | 'li.one>a[href="/"]': 'one' 10 | 'li.two>a[href="/"]': 'two' 11 | 12 | data = {'one':'hello', 'two': 'world'} 13 | 14 | console.log JSON.stringify json,null,2 15 | console.log dsl.parse json,data 16 | -------------------------------------------------------------------------------- /index.coffee: -------------------------------------------------------------------------------- 1 | module.exports = ( () -> 2 | 3 | opts: 4 | token: "%s" 5 | parseKey: (k) -> "<"+k+">%s" 6 | parseValue: (v,d) -> v 7 | 8 | parseDSL: (json,data,str) -> 9 | str = '' if not str? 10 | if typeof json is "object" 11 | for k,v of json 12 | nk = @.parseKey k 13 | nv = @.parse v, data, '' 14 | str += nk.replace /%s/, nv 15 | if typeof json is "string" 16 | str += @.parseValue json, data 17 | if not str? 18 | return json 19 | else return str 20 | 21 | parse: (json,data) -> 22 | @.parseDSL json,data 23 | 24 | )() 25 | -------------------------------------------------------------------------------- /test/test_zen.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.9.3 2 | (function() { 3 | var data, dsl, json, zen; 4 | 5 | dsl = require('./../index.js'); 6 | 7 | zen = require('zen-coding'); 8 | 9 | dsl.parseKey = function(k) { 10 | return zen(k + '>{%s}'); 11 | }; 12 | 13 | dsl.parseValue = function(v, data) { 14 | return data[v]; 15 | }; 16 | 17 | json = { 18 | 'div#foo.flop>fieldset>div>ul': { 19 | 'li.one>a[href="/"]': 'one', 20 | 'li.two>a[href="/"]': 'two' 21 | } 22 | }; 23 | 24 | data = { 25 | 'one': 'hello', 26 | 'two': 'world' 27 | }; 28 | 29 | console.log(JSON.stringify(json, null, 2)); 30 | 31 | console.log(dsl.parse(json, data)); 32 | 33 | }).call(this); 34 | -------------------------------------------------------------------------------- /test/test.coffee: -------------------------------------------------------------------------------- 1 | jsondsl = require('./../index.js').parse 2 | 3 | json = 4 | div: 'foo' 5 | 6 | console.log JSON.stringify json,null,2 7 | console.log jsondsl( json ) 8 | process.exit 1 if jsondsl json != "
foo
" 9 | 10 | json = 11 | div: 12 | div: 13 | div: 'foo' 14 | 15 | console.log JSON.stringify json,null,2 16 | console.log jsondsl( json ) 17 | process.exit 1 if jsondsl json != "
foo
" 18 | 19 | json = 20 | foo: 'bar' 21 | div: 22 | span: 23 | div: 'foo' 24 | 25 | console.log JSON.stringify json,null,2 26 | console.log jsondsl( json ) 27 | process.exit 1 if jsondsl json != "bar
foo
" 28 | 29 | json = 30 | div: 31 | div: 32 | div: 'foo' 33 | 34 | jsondsl = require('./../index.js') 35 | jsondsl.parseKey = (k) -> k+"[%s]" 36 | console.log jsondsl.parse json 37 | process.exit 1 if jsondsl.parse json != "div[div[div[foo]]]" 38 | 39 | console.log "tests ok" 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-dsl", 3 | "version": "1.0.3", 4 | "description": "easily create dsl's from json by evaluating json keys and values, json decode on steroids", 5 | "main": "index.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://coderofsalvation@github.com/coderofsalvation/json-dsl.git" 15 | }, 16 | "keywords": [ 17 | "dsl", 18 | "json", 19 | "minilanguage", 20 | "evaluate", 21 | "json", 22 | "keys", 23 | "traverse", 24 | "json", 25 | "json", 26 | "template", 27 | "json", 28 | "mapper", 29 | "json", 30 | "to", 31 | "html", 32 | "json", 33 | "to", 34 | "xml" 35 | ], 36 | "author": "Coder of Salvation", 37 | "license": "BSD-2-Clause", 38 | "bugs": { 39 | "url": "https://github.com/coderofsalvation/json-dsl/issues" 40 | }, 41 | "homepage": "https://github.com/coderofsalvation/json-dsl#readme" 42 | } 43 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.9.3 2 | (function() { 3 | module.exports = (function() { 4 | return { 5 | opts: { 6 | token: "%s" 7 | }, 8 | parseKey: function(k) { 9 | return "<" + k + ">%s"; 10 | }, 11 | parseValue: function(v, d) { 12 | return v; 13 | }, 14 | parseDSL: function(json, data, str) { 15 | var k, nk, nv, v; 16 | if (str == null) { 17 | str = ''; 18 | } 19 | if (typeof json === "object") { 20 | for (k in json) { 21 | v = json[k]; 22 | nk = this.parseKey(k); 23 | nv = this.parse(v, data, ''); 24 | str += nk.replace(/%s/, nv); 25 | } 26 | } 27 | if (typeof json === "string") { 28 | str += this.parseValue(json, data); 29 | } 30 | if (str == null) { 31 | return json; 32 | } else { 33 | return str; 34 | } 35 | }, 36 | parse: function(json, data) { 37 | return this.parseDSL(json, data); 38 | } 39 | }; 40 | })(); 41 | 42 | }).call(this); 43 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | // Generated by CoffeeScript 1.9.3 2 | (function() { 3 | var json, jsondsl; 4 | 5 | jsondsl = require('./../index.js').parse; 6 | 7 | json = { 8 | div: 'foo' 9 | }; 10 | 11 | console.log(JSON.stringify(json, null, 2)); 12 | 13 | console.log(jsondsl(json)); 14 | 15 | if (jsondsl(json !== "
foo
")) { 16 | process.exit(1); 17 | } 18 | 19 | json = { 20 | div: { 21 | div: { 22 | div: 'foo' 23 | } 24 | } 25 | }; 26 | 27 | console.log(JSON.stringify(json, null, 2)); 28 | 29 | console.log(jsondsl(json)); 30 | 31 | if (jsondsl(json !== "
foo
")) { 32 | process.exit(1); 33 | } 34 | 35 | json = { 36 | foo: 'bar', 37 | div: { 38 | span: { 39 | div: 'foo' 40 | } 41 | } 42 | }; 43 | 44 | console.log(JSON.stringify(json, null, 2)); 45 | 46 | console.log(jsondsl(json)); 47 | 48 | if (jsondsl(json !== "bar
foo
")) { 49 | process.exit(1); 50 | } 51 | 52 | json = { 53 | div: { 54 | div: { 55 | div: 'foo' 56 | } 57 | } 58 | }; 59 | 60 | jsondsl = require('./../index.js'); 61 | 62 | jsondsl.parseKey = function(k) { 63 | return k + "[%s]"; 64 | }; 65 | 66 | console.log(jsondsl.parse(json)); 67 | 68 | if (jsondsl.parse(json !== "div[div[div[foo]]]")) { 69 | process.exit(1); 70 | } 71 | 72 | console.log("tests ok"); 73 | 74 | }).call(this); 75 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Copyright 2015 Coder of Salvation / Leon van Kammen . All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are 5 | permitted provided that the following conditions are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright notice, this list of 8 | conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 | of conditions and the following disclaimer in the documentation and/or other materials 12 | provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY Coder of Salvation / Leon van Kammen AS IS'' AND ANY EXPRESS OR IMPLIED 15 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 16 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OR 17 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 | ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 | 24 | The views and conclusions contained in the software and documentation are those of the 25 | authors and should not be interpreted as representing official policies, either expressed 26 | or implied, of 27 | 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generate your own minilanguages fast using json as a startingpoint. 4 | No parsetrees, no lexical analyzers, just plain json. 5 | 6 | # Usage 7 | 8 | $ npm install json-dsl 9 | 10 | in your code 11 | 12 | jdsl = require('json-dsl').parse 13 | output = jdsl( yourjson ) 14 | 15 | # basics: xml 16 | 17 | By default nested json is converted into xml 18 | 19 | { 20 | "div": { 21 | "div": { 22 | "div": "foo" 23 | } 24 | } 25 | } 26 | 27 | gets converted into 28 | 29 |
foo
30 | 31 | # customize key evaluation 32 | 33 | lets evaluate the keys in a different way: 34 | 35 | jsondsl = require('json-dsl') 36 | jsondsl.parseKey = function(k) { 37 | return k + "[%s]"; 38 | }; 39 | output = jdsl( yourjson ) 40 | 41 | output: 42 | 43 | div[div[div[foo]]] 44 | 45 | # customize value evaluation 46 | 47 | lets take the previous example and lets add `parseValue` 48 | 49 | data = { foo: "bar" } 50 | jsondsl.parseValue = function(v,data) { 51 | return data[v] 52 | } 53 | output = jdsl( yourjson, data ) 54 | 55 | output: 56 | 57 | div[div[div[bar]]] 58 | 59 | # example: html template language 60 | 61 | > NOTE: the dsl below is a stripped down version of the [brown](https://npmjs.org/packages/brown) template engine, a hyperminimalistic template dsl which borrows from emmet and mustache. 62 | 63 | setup dsl: 64 | 65 | var jdsl = require('json-dsl'); 66 | var zen = require('zen-coding'); 67 | 68 | jdsl.parseKey = function(k) { 69 | return zen(k + '>{%s}'); 70 | }; 71 | 72 | jdsl.parseValue = function(v, data) { 73 | return data[v]; 74 | }; 75 | 76 | lets test it: 77 | 78 | var json = { 79 | 'div#foo.flop>fieldset>div>ul': { 80 | 'li.one>a[href="/"]': 'one', 81 | 'li.two>a[href="/"]': 'two' 82 | } 83 | }; 84 | 85 | var data = { 86 | 'one': 'hello', 87 | 'two': 'world' 88 | }; 89 | 90 | console.log(JSON.stringify(json, null, 2)); 91 | console.log(jdsl.parse(json, data)); 92 | 93 | outputs: 94 | 95 |
96 | 97 | --------------------------------------------------------------------------------