Skip to content

Commit

Permalink
add view & list to see all shortened URLs. Uses mustache.js!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lehnardt committed Oct 11, 2009
1 parent 1eb5a3e commit ca066f5
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lists/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function(head, req) {
// !json templates.index
// !code vendor/mustache.js/mustache.js

provides("html", function() {
var row;
while(row = getRow()) {
send(Mustache.to_html(templates.index.row, {
target: row.value,
id: row.key
}));
}
send(templates.index.tail);
});
}
8 changes: 8 additions & 0 deletions templates/index/head.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>head</title>
</head>
<body>

1 change: 1 addition & 0 deletions templates/index/row.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<li><a href="{{target}}">{{target}} ({{id}})</a></li>
3 changes: 3 additions & 0 deletions templates/index/tail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
</ul>
</body>
</html>
132 changes: 132 additions & 0 deletions vendor/mustache.js/mustache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
Shamless port of http://github.com/defunkt/mustache
by Jan Lehnardt <jan@apache.org>
Thanks @defunkt for the awesome code
TBD: MIT, see LICENSE
ChangeLog:
- 04.10.2009: Ininitial port at http://devhouseberlin.de/
*/

var Mustache = {
name: "mustache.js",
version: "0.1",
debug: true,
stack: " ",
context: {},
to_html: function(template, view) {
return this.render(template, view);
},

render: function(template, view) {
this.stack = this.stack + " ";
// fail fast
if(template.indexOf("{{") == -1) {
return template;
}
this.context = context = this.merge((this.context || {}), view);
var html = this.render_section(template);
// restore context, recursion might have messed it up
this.context = context;
return this.render_tags(html);
},

render_partial: function(name) {
var evil_name = eval(name)
switch(typeof evil_name) {
case "string":
return this.to_html(evil_name, "");
case "object":
var tpl = name + "_template";
return this.to_html(eval(tpl), evil_name);
default:
throw("Unknown partial type.");
}
},

merge: function(a, b) {
for(var name in b) {
if(b.hasOwnProperty(name)) {
a[name] = b[name];
}
}
return a;
},

render_section: function(template) {
if(template.indexOf("{{#") == -1) {
return template;
}
var that = this;
return template.replace(/\{\{\#(.+)\}\}\s*([\s\S]+)\{\{\/\1\}\}\s*/mg,
function(match, name, content) {
var value = that.find(name);
if(that.is_array(value)) {
return value.map(function(row) {
return that.render(content, row);
}).join('');
} else if(value) {
return that.render(content);
} else {
return "";
}
}
);
},

is_array: function(a) {
return (a &&
typeof a === 'object' &&
a.constructor === Array);
},

render_tags: function(template) {
// values
var that = this;
return template.replace(/\{\{(!|<|\{)?([^\/#]+?)\1?\}\}+/mg,
function (match, operator, name) {
switch(operator) {
case "!": // ignore comments
return match;
case "<": // render partial
return that.render_partial(name);
case '{': // the triple mustache is unescaped
return that.find(name);
default: // escape the value
return that.escape(that.find(name));
}
}, this);
},

escape: function(s) {
return s.toString().replace(/[&"<>\\]/g, function(s) {
switch(s) {
case "&": return "&amp;";
case "\\": return "\\\\";;
case '"': return '\"';;
case "<": return "&lt;";
case ">": return "&gt;";
default: return s;
}
});
},

find: function(name) {
name = this.trim(name);
// print(this.stack + "find(" + name + ")");
var context = this.context;
if(typeof context[name] === "function") {
return context[name].apply(context);
}
if(context[name] !== undefined) {
return context[name];
}
throw("Can't find " + name + " in " + context);
},

trim: function(s) {
return s.replace(/^\s*|\s*$/g, '');
},
};
5 changes: 5 additions & 0 deletions views/shorts/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function(doc) {
if(!doc.target) { return; }

emit(doc._id, doc.target);
}

0 comments on commit ca066f5

Please sign in to comment.