New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

brown

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brown - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

brown.js

2

brown.min.js

@@ -1,1 +0,1 @@

require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module){(function(){module.exports=function(){return{opts:{token:"%s"},parseKey:function(k){return"<"+k+">%s</"+k+">"},parseValue:function(v){return v},parseDSL:function(json,data,str){var k,nk,nv,v;if(null==str&&(str=""),"object"==typeof json)for(k in json)v=json[k],nk=this.parseKey(k),nv=this.parse(v,data,""),str+=nk.replace(/%s/,nv);return"string"==typeof json&&(str+=this.parseValue(json,data)),null==str?json:str},parse:function(json,data){return this.parseDSL(json,data)}}}()}).call(this)},{}],2:[function(require,module){function frag(s){var arr=s.split("+"),ret=[];return _.each(arr,function(v,i){if(/\{\d+\}/.test(v))if(~v.indexOf("*")){v=v.split("*");for(var ss=_getPH(v[0]),sss="",m=0;m<v[1];m++)sss+=ss;ret[i]=sss}else ret[i]=_getPH(v);else ret[i]=_getFrag(v)}),ret}function _attrs(str){if(!str)return"";var arr,sid,clas=[],o={},s=[];return arr=str.match(/(\#[\w\-\d]+)|(\.[\w\-\d]+)|(\[[^\]]+\])/g),arr&&_.each(arr,function(me){"["===me.charAt(0)?s.push(me.replace(/\[|\]/g,"")):"."===me.charAt(0)&&void 0===o[me]?(clas.push(me.slice(1)),o[me]=!0):sid=sid||me.slice(1)}),sid&&s.push('id="'+sid+'"'),clas.length&&s.push('class="'+clas.join(" ")+'"'),s.join(" ")}function _tag(str,ph){if(!str)return"";if(/\<[^\>]+\>/.test(str))return str;if(/[\+\*\>\{]/.test(str))return _getFrag(str);var s,tag=str.match(/^[^\W]+/),attrs=_attrs(str);return attrs=attrs?" "+attrs:"",ph=ph||"&nbsp;",tag||(tag="div"),s="<"+tag+attrs+(/img|input|br|hr/i.test(tag)?" />":">"+ph+"</"+tag+">")}function _sibling(str){if(!str)return"";var arr=str.split("+"),s="";return _.each(arr,function(v){s+=_tag(v)}),s}function _repeat(str){if(!str)return"";for(var arr=str.split("*"),s="",i=0;i<(arr[1]||0);i++)s+=_tag(arr[0]);return s}function _stack(str){if(!str)return"";var arr=str.split(">"),s="&nbsp;";return _.each(arr,function(v){s=s.replace(/\&nbsp;/g,_tag(v))}),s}function _bracket(str,Zen){if(!str)return"";if(!/\([^\(\)]+\)/.test(str))return str;var arr=str.match(/\([^\(\)]+\)/g);return _.each(arr,function(f){var key="{"+Zen.fragIndex+"}";void 0===Zen.frags[f]&&(Zen.frags[key]=f.replace(/\(|\)/g,"")),str=str.split(f).join(key),Zen.fragIndex++}),/\([^\(\)]+\)/.test(str)?_bracket(str,Zen):str}function _getStack(str,Zen){if(!str)return"";if(str.indexOf(">")<0)return str;var reg=/[^\>\+]+\>[^\>]+$/,last=str.match(reg);if(last){var key="{"+Zen.fragIndex+"}",f=last[0];void 0===Zen.frags[f]&&(Zen.frags[key]=f),str=str.replace(reg,key),Zen.fragIndex++}return~str.indexOf(">")?_getStack(str,Zen):str}function _getFrag(str){return~str.indexOf(">")?_stack(str):~str.indexOf("+")?_sibling(str):~str.indexOf("*")?_repeat(str):str.indexOf("{")<0?_tag(str):str}function _getPH(str){var arr=str.split(/\{|\}/g),ret=[];return _.each(arr,function(v,i){if(v)if(isNaN(v))ret[i]=v;else{var ph=Zen.frags[v];ret[i]=ph?_getPH(ph):"{"+v+"}"}else ret[i]=""}),ret.join("")}var Zen={},_={each:function(obj,iteratee){var k,results,v;results=[];for(k in obj)v=obj[k],results.push(iteratee(v,k));return results}};module.exports=function(s){Zen.frags={},Zen.fragIndex=0;var _s;return _s=_bracket(s,Zen),_s=_getStack(_s,Zen),_.each(Zen.frags,function(r,k){/\{\d+\}/.test(k)&&(Zen.frags[k.replace(/\{|\}/g,"")]=_getFrag(r))}),frag(_s).join("").replace(/(\&nbsp;)|(\{\s+\})/g,"")}},{}],brown:[function(require,module,exports){(function(){var zen;zen=require("zen-coding"),module.exports=function(){return this.escapes=["___","___"],this.jdsl=require("json-dsl"),this.jdsl.parseKey=function(k){return zen(k+">{%s}")},this.jdsl.parseValue=function(v,data){var i,item,items,itemstr,len,result;if(result="",items=!1,v.match(/->/))for(items=data[v.split("->")[0]],console.dir(items),v=v.split("->")[1],i=0,len=items.length;len>i;i++)item=items[i],itemstr=v.replace(/(\{\{)(.*?)(\}\})/g,function($0,$1,$2){return $2=$2.length>1&&"."===$2[0]?this.applyFiltersAndEval($2,item,data):this.applyFiltersAndEval($2,item,data)}),v.match(/>/)&&(itemstr=zen(itemstr)),result+=itemstr;else result=v,result=result.replace(/(\{\{)(.*?)(\}\})/g,function($0,$1,$2){return $2=this.applyFiltersAndEval("."+$2,data,data)});return result=result.replace(/___/g,""),result=result.replace(/ </g,"<").replace(/> /g,">")},this.applyFiltersAndEval=function($2,item,data){var filters,is_array;return filters=this.getFilters($2).filters,$2=this.getFilters($2).str,is_array=!1,"."===$2?(is_array=!0,$2=item):($2="item"+$2,$2=String(eval($2)).replace(/undefined/,"")),$2=this.applyFilters($2,filters,data),is_array&&($2=this.escapes[0]+$2+escapes[1]),$2},this.getFilters=function(str){var filters;return filters=[],str.match(/|/)&&(filters=str.split("|"),str=filters.shift()),{str:str,filters:filters}},this.applyFilters=function(str,filters,data){var filter,i,len;if(filters.length)for(i=0,len=filters.length;len>i;i++)filter=filters[i],str=eval("data."+filter+'("'+str+'")');return str},this.parse=function(json,data){return this.jdsl.parse(json,data)},this}()}).call(this)},{"json-dsl":1,"zen-coding":2}]},{},[]);
require=function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){!function(root,factory){"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?module.exports=factory():root.micromustache=factory()}(this,function(){function render(template,view){return"string"!=typeof template?template:(("object"!=typeof view||null===view)&&(view={}),template.replace(/\{?\{\{\s*(.*?)\s*\}\}\}?/g,function(match,varName){var value=view[varName]||view[varName.split(":")[0]];switch(typeof value){case"string":case"number":case"boolean":return value;case"function":return value.apply(view,varName.split(":").slice(1));default:return""}}))}function compile(template){return function(view){return render(template,view)}}return{render:render,to_html:render,compile:compile}})},{}],brown:[function(require,module){(function(){module.exports=function(){var k,merge,v;merge=this.merge=function(source,obj,clone){var prop,v;if(null===source||null==source)return source;for(prop in obj)v=obj[prop],null!==source[prop]&&"object"==typeof source[prop]&&"object"==typeof obj[prop]?this.merge(source[prop],obj[prop]):source[prop]=clone?this.clone:obj[prop];return source},this.micromustache=require("micromustache"),this.micromustache._render=this.micromustache.render,this.micromustache.render=function(str,data){return this._render(str,merge(data,this))}.bind(this.micromustache),this.render=function(input,data){return this.micromustache.render(input,data)};for(k in this)v=this[k],"function"==typeof v&&(this[k]=v.bind(this));return this}.apply({})}).call(this)},{micromustache:1}]},{},[]);
#!/usr/bin/env node
brown = require( __dirname+"/.")
var data = {}
if( process.argv.length <3 ){
console.log( "Usage: brown <string|jsonstring|jsonfile> [jsonstring|jsonfile]" );
console.log( "\nexamples:\n\t$ brown 'foo {{foo}}' '{\"foo\":\"world\"}'");
console.log( "\t$ brown html.json data.json\n\t$ brown html.json '{\"foo\":\"world\"}'");
process.exit()
}
obj = {}
obj[process.argv[2]] = ''
console.log( brown.parse( obj,{} ) );
function get(input){
var obj = input;
try {
obj = require('fs').readFileSync( input ).toString()
}catch(e){ }
if( obj[0] == "{" )
obj = JSON.parse(obj);
return obj
}
var input = get(process.argv[2]);
var data = get(process.argv[3] || '');
console.dir(input);
console.dir(data);
console.log( brown.render( input, data ) );

@@ -1,90 +0,41 @@

// Generated by CoffeeScript 1.9.3
// Generated by CoffeeScript 1.10.0
(function() {
var zen;
zen = require('zen-coding');
module.exports = (function() {
this.escapes = ["___", "___"];
this.jdsl = require('json-dsl');
this.jdsl.parseKey = function(k) {
return zen(k + '>{%s}');
};
this.jdsl.parseValue = function(v, data) {
var i, item, items, itemstr, len, result;
result = '';
items = false;
if (v.match(/->/)) {
items = data[v.split('->')[0]];
v = v.split('->')[1];
for (i = 0, len = items.length; i < len; i++) {
item = items[i];
itemstr = v.replace(/(\{\{)(.*?)(\}\})/g, function($0, $1, $2) {
if ($2.length > 1 && $2[0] === '.') {
return $2 = this.applyFiltersAndEval($2, item, data);
} else {
return $2 = this.applyFiltersAndEval($2, item, data);
}
});
if (v.match(/>/)) {
itemstr = zen(itemstr);
var k, merge, v;
merge = this.merge = function(source, obj, clone) {
var prop, v;
if (source === null || (source == null)) {
return source;
}
for (prop in obj) {
v = obj[prop];
if (source[prop] !== null && typeof source[prop] === 'object' && typeof obj[prop] === 'object') {
this.merge(source[prop], obj[prop]);
} else {
if (clone) {
source[prop] = this.clone;
} else {
source[prop] = obj[prop];
}
result += itemstr;
}
} else {
result = v;
result = result.replace(/(\{\{)(.*?)(\}\})/g, function($0, $1, $2) {
return $2 = this.applyFiltersAndEval("." + $2, data, data);
});
}
result = result.replace(/___/g, '');
result = result.replace(/ </g, '<').replace(/> /g, '>');
return result;
return source;
};
this.applyFiltersAndEval = function($2, item, data) {
var filters, is_array;
filters = this.getFilters($2).filters;
$2 = this.getFilters($2).str;
is_array = false;
if ($2 === '.') {
is_array = true;
$2 = item;
} else {
$2 = "item" + $2;
$2 = String(eval($2)).replace(/undefined/, '');
}
$2 = this.applyFilters($2, filters, data);
if (is_array) {
$2 = this.escapes[0] + $2 + escapes[1];
}
return $2;
this.micromustache = require('micromustache');
this.micromustache._render = this.micromustache.render;
this.micromustache.render = (function(str, data) {
return this._render(str, merge(data, this));
}).bind(this.micromustache);
this.render = function(input, data) {
return this.micromustache.render(input, data);
};
this.getFilters = function(str) {
var filters;
filters = [];
if (str.match(/|/)) {
filters = str.split('|');
str = filters.shift();
for (k in this) {
v = this[k];
if (typeof v === 'function') {
this[k] = v.bind(this);
}
return {
str: str,
filters: filters
};
};
this.applyFilters = function(str, filters, data) {
var filter, i, len;
if (filters.length) {
for (i = 0, len = filters.length; i < len; i++) {
filter = filters[i];
str = eval('data.' + filter + '("' + str + '")');
}
}
return str;
};
this.parse = function(json, data) {
return this.jdsl.parse(json, data);
};
}
return this;
})();
}).apply({});
}).call(this);
{
"name": "brown",
"version": "1.1.1",
"description": "hyperfast and minimalistic json2html template engine for node/coffeescript/browser, a mashup between emmet/mustache/json-dsl/smarty",
"version": "1.1.2",
"description": "Ultra-extendable Mustache-ish template engine on steroids in 846 gzipped kilobytes",
"main": "index.js",

@@ -14,3 +14,3 @@ "scripts": {

},
"bin":{
"bin": {
"brown": "cli.js"

@@ -40,5 +40,6 @@ },

"dependencies": {
"json-dsl": "^1.0.2",
"zen-coding": "coderofsalvation/zen-coding"
"micromustache": "git+https://github.com/coderofsalvation/micromustache.git"
},
"devDependencies": {
}
}
<h1>ϐrown</h1>
JSON 2 html/xml template engine on steroids.
It borrows from JADE, but is jsonfriendly and has syntactical testosterone.
Jade-ish + Mustache-ish template engine on steroids in 846 gzipped kilobytes

@@ -14,153 +13,84 @@ # Usage

then
## Simple
brown = require('brown').parse
brown.render( "hello {{foo}}", { foo: "world" } );
json =
ul:
li:
'a[href="{{url}}"]': "click me"
data = { url: "http://turtlesarecool.com" }
console.log brown json,data
outputs:
### outputs
hello world
<ul><li><a href="http://turtlesarecool.com"></li></ul>
## Functions
# Syntactical testosterone
Create a fullfledged template engine by adding functions:
json =
'div#foo.flop>fieldset>div>span':
ul: 'items->li>a[href="{{url}}"]>{ {{.label}} }'
div:
'span>b.foo': '{{foo}}'
'span>b.bar': '{{func()}}'
'span>b.pip': '{{foo|upper|important}}'
brown.micromustache.encode = function(key) {
var html = this[key] || '';
return String(html).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};
data =
items: [{label:'one', url:"/"},{label:'two',url:'/two'}]
foo: "hello world"
func: () -> @.foo + " foobar"
upper: (str) -> str.toUpperCase()
important: (str) -> "!! "+str
> NOTE: instead of coffeescript, see the javascript example [here](https://github.com/coderofsalvation/brown/blob/master/test/test.js)
json = {
div:
'a href="{{href}}" onclick="{{encode:label}}"': "{{label}}"
};
### outputs
brown.render( json, {href="/", label:"my \"label\""} )
<div id="foo" class="flop">
<fieldset>
<div>
<span>
<ul>
<li>
<a href="/"> one </a>
</li>
<li>
<a href="/two"> two </a>
</li>
</ul>
</span>
</div>
</fieldset>
</div>
outputs:
<div>
<span>
<b class="foo">hello world</b>
</span>
<span>
<b class="bar">hello world foobar</b>
</span>
<span>
<b class="pipe">!! HELLO WORLD</b>
</span>
<ul>
<li>
<a href="/" onclick="my &quot;label&quot;">my "label"</a>
</li>
</ul>
</div>
# Philosophy
> Need more? See [if/foreach/filter/etc examples here](https://gist.github.com/coderofsalvation/93610d527c7b8534567f) on how to extend brown with [micromustache](https://www.npmjs.com/package/micromustache) functions.
ϐrown wants to be very fast an minimal, therefore uses [json-dsl](https://npmjs.com/packages/json-dsl).
ϐrown combines ideas from smarty, mustache and emmet/zencoding.
ϐrown focuses on json and json-portability instead of text (smarty/mustache) or introducing a new syntax which require lexicalscanner/parsertree-bloat (jade,coffeecup,haml etc).
## Generate JADE-ish xml/html from JADE-ish json
# Features
ϐrown can be monkeypatched, to automatically produce xml-trees from json (like JADE):
* basic emmet syntax ('a>b' becomes '<a><b></b></a>') [overview here](http://docs.emmet.io/cheat-sheet/)
* mustache variable substitution using {{variablename}}
* filter chaining using '|'
* functions using {{functionname()}}
* iteration over objects using '->' and {{.keyname}}
* iteration over arrays using '->' and {{.}}
var json = {
ul: {
li: {
'a href="{{foo}}": "Click me"
}
}
}
brown.render json,{ foo: "/" }
With these basics you can literally do anything.
Need more? just use functions and filters.
outputs:
# nested templates
<ul>
<li>
<a href="/">Click me </a>
</li>
</ul>
Want master templates?
Easy, you can call brown inside brown.
How?
Simple, by just monkeypatching ϐrown with [json-dsl](https://npmjs.org/package/json-dsl). See [coffeescript](test/jadeish.coffee) / [JS](test/jadeish.js) examples.
data =
master:
html:
head: ''
body:
'div#main': '{{content()}}'
page:
'ul#menu':
div: "{{foo}}"
data:
foo: "hello world"
content: () -> brown @.page, @.data
## Commandline util
brown data.master, data
Use as a commandline generator (install using `npm install -g` ) :
> NOTE: instead of coffeescript, see the javascript example [here](https://github.com/coderofsalvation/brown/blob/master/test/mastertemplate.js)
$ brown
Usage: brown <string|jsonstring|jsonfile> [jsonstring|jsonfile]
### output
examples:
$ brown 'foo {{foo}}' '{"foo":"world"}'
$ brown html.json data.json
$ brown html.json '{"foo":"world"}'
<html><head></head><body><div id="main"><ul id="menu"><div>hello world</div></ul></div></body></html></body></head></html>
# Goals / Philosophy
# traversing arrays/objects
* lightweight and extendable
* mustache without the noise (using [micromustache](https://www.npmjs.com/package/micromustache))
Arrays here you go
With these basics you can literally do anything.
> Need more? See [more examples here](https://gist.github.com/coderofsalvation/93610d527c7b8534567f) on how to extend brown with [micromustache](https://www.npmjs.com/package/micromustache) functions.
div:
ul: 'arr->li>{ {{.}} }'
ol: 'arr->{{.|upper}}'
And for objects just reference the keys like so: `{{.keyname}}`
For more info see the info above or the [tests](https://github.com/coderofsalvation/brown/blob/master/test/test.js)
# global functions and filters
Easy, just use extend (`npm install extend`).
global =
somefilter: (str) -> str.toUpperCase()
projectname: () -> return "myprojectname"
unique =
welcome: "hello"
json =
div:
span: '{{welcome}} {{projectname()|somefilter}}'
console.log brown json, extend global, unique
### output
<div><span>hello MYPROJECTNAME</span></div>
# Commandline util
$ brown 'a>b'
will output: `<a><b></b></b>`
# Roadmap
* stability and peace
* prevent javascript event injection as text by using 'onclick' e.g. in json

@@ -1,42 +0,33 @@

// Generated by CoffeeScript 1.9.3
// Generated by CoffeeScript 1.10.0
(function() {
var brown, data, json;
var brown, data, json, str;
brown = require('./../index.coffee').parse;
brown = require('../index.coffee');
json = {
div: {
'span>b.foo': '{{foo}}',
'span>b.bar': '{{func()}}',
'span>b.pipe': '{{foo|upper|important}}',
'div#foo.flop>fieldset>div>span': {
ul: 'items->li>a[href="{{.url}}"]>{ {{.label|upper}} }'
},
'div': {
ul: 'arr->li>{ {{.}} }',
ol: 'arr->{{.|upper}}'
}
}
console.log(brown.render("hello {{foo}}", {
foo: "world"
}));
brown.micromustache.encode = function(key) {
var html;
html = this[key] || '';
return String(html).replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
};
data = {
items: [
{
label: 'one',
url: "/"
}, {
label: 'two',
url: '/two'
href: "/",
label: "my \"label\""
};
str = {
'a href="{{href}}" onclick="{{encode:label}}"': "{{label}}"
};
console.log(brown.render(str, data));
json = {
ul: {
li: {
'a href="{{href}}" onclick="{{encode:label}}"': "{{label}}"
}
],
arr: [1, 2, "three"],
foo: "hello world",
func: function() {
return this.foo + " foobar";
},
upper: function(str) {
return str.toUpperCase();
},
important: function(str) {
return "!! " + str;
}

@@ -47,20 +38,28 @@ };

console.log(brown(json, data));
console.log(brown.render(json, {
href: "/",
label: "my \"label\""
}));
brown.micromustache.zen = require('../node_modules/zen-coding');
json = {
div: "items->li{{.}}"
div: {
'{{zen:span>b.foo}}': '{{foo}}',
'{{zen:span>b.bar}}': '{{func}}',
'{{zen:div#foo.flop>fieldset>div>span}}': "{{foo}}"
}
};
data = {
items: [
{
label: 'one',
url: "/"
}, {
label: 'two',
url: '/two'
}
]
foo: "hello world",
func: function() {
return this.foo + " foobar";
}
};
console.log(JSON.stringify(json, null, 2));
console.log(brown.render(json, data));
}).call(this);

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc