Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattt Thompson committed Nov 12, 2008
0 parents commit 09b7b3f
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vCard.js is a simple Javascript parser for vCards.
Marvel at its elegance. Or don't. It's damn useful either way.
58 changes: 58 additions & 0 deletions demo/css/reset.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* --------------------------------------------------------------
reset.css
* Resets default browser CSS.
Based on work by Eric Meyer:
* meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/
-------------------------------------------------------------- */

html, body, div, span, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, code,
del, dfn, em, img, q, dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td, .reset {
margin: 0;
padding: 0;
border: 0;
font-weight: inherit;
font-style: inherit;
font-size: 100%;
font-family: inherit;
vertical-align: baseline;
}

.reset{
list-style:none;
display:block;
}


body { line-height: 1.5; background: #fff; margin:1.5em 0; }

/* Tables still need 'cellspacing="0"' in the markup. */
table { border-collapse: separate; border-spacing: 0; }
caption, th, td { text-align: left; font-weight:400; }

/* Remove possible quote marks (") from <q>, <blockquote>. */
blockquote:before, blockquote:after, q:before, q:after { content: ""; }
blockquote, q { quotes: "" ""; }

a img { border: none; }

/* Clearing floats without extra markup
Based on How To Clear Floats Without Structural Markup by PiE
[http://www.positioniseverything.net/easyclearing.html] */

.clear { display: inline-block; }
.clear:after, .container:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
* html .clear { height: 1%; }
.clear { display: block; }
74 changes: 74 additions & 0 deletions demo/css/screen.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* ________________
| | Default CSS styling for vCard.js
| vCard.js CSS | Created by Mattt Thompson, 2008
| | Released under the MIT License
̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅̅
*/


body{
font-family:Helvetica, Arial, sans-serif;
}

div.vcard{
border:1px #ccc solid;
width:25em;
padding:20px;
margin:0 auto;
}

.fn{
font-size:2.0em;
font-weight:bold;
}


.fn, .org{
display:block;
}


.vcard span.street-address{
display:block;
}

.vcard span.locality:after{
content:', ';
}

.vcard span.region:after{
content:' ';
}

.vcard span.country-name{
display:block;
}

.vcard address, .vcard span.tel, .vcard span.email, .vcard span.url{
display:block;
clear:left;
}

.vcard span.type{
display:block;
float:left;
text-align:right;
width:3em;
margin-right:1em;
margin-left:20px;
color:#ccc;

}

.content{
float:left;
}

img{float:right; margin:0 0 10px 10px; max-width:100px; max-height:100px;}

hr{
clear:both;
margin:10px 0;
border:none;
border-top:1px #ccc solid;
}
22 changes: 22 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>vCard.js Demo</title>
<script src="../vcard.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="screen" charset="utf-8">
<link rel="stylesheet" href="css/screen.css" type="text/css" media="screen" charset="utf-8">
</head>

<body>
<div id="container" style="margin:10px auto;"></div>
<script src="sample.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var hcard = document.createElement("div");
hcard.innerHTML = vCard.initialize($SAMPLE).to_html();
document.getElementById('container').appendChild(hcard);
</script>
</body>
</html>
1 change: 1 addition & 0 deletions demo/sample.js

Large diffs are not rendered by default.

159 changes: 159 additions & 0 deletions vcard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// ____________
// | | A Javascript parser for vCards
// | vCard.js | Created by Mattt Thompson, 2008
// | | Released under the MIT License
// ̅̅̅̅̅̅̅̅̅̅̅̅

// Adding Javascript 1.6 Compatibility
if(!Array.prototype.forEach){Array.prototype.forEach=function(d,c){c=c||this;for(var b=0,a=this.length;b<a;b++){d.call(c,this[b],b,this)}}}if(typeof Prototype!="undefined"||!Array.prototype.map){Array.prototype.map=function(d,c){c=c||this;var e=[];for(var b=0,a=this.length;b<a;b++){e.push(d.call(c,this[b],b,this))}return e}}if(typeof Prototype!="undefined"||!Array.prototype.filter){Array.prototype.filter=function(d,c){c=c||this;var e=[];for(var b=0,a=this.length;b<a;b++){if(d.call(c,this[b],b,this)){e.push(this[b])}}return e}}["forEach","map","filter","slice","concat"].forEach(function(a){if(!Array[a]){Array[a]=function(b){return this.prototype[a].apply(b,Array.prototype.slice.call(arguments,1))}}});Date.ISO8601PartMap={Year:1,Month:3,Date:5,Hours:7,Minutes:8,Seconds:9};Date.matchISO8601=function(a){return a.match(/^(\d{4})(-?(\d{2}))?(-?(\d{2}))?(T(\d{2}):?(\d{2})(:?(\d{2}))?)?(Z?(([+\-])(\d{2}):?(\d{2})))?$/)};Date.parseISO8601=function(e){var b=this.matchISO8601(e);if(b){var a=new Date,c,d=0;for(var f in this.ISO8601PartMap){if(part=b[this.ISO8601PartMap[f]]){a["set"+f]((f=="Month")?parseInt(part)-1:parseInt(part))}else{a["set"+f]((f=="Date")?1:0)}}if(b[11]){d=(parseInt(b[14])*60)+parseInt(b[15]);d*=((parseInt[13]=="-")?1:-1)}d-=a.getTimezoneOffset();a.setTime(a.getTime()+(d*60*1000));return a}};

vCard = {
initialize: function(_input){
var vc = {};
this.parse(_input, vc);

vc.prototype = vCard.Base;
return vCard.extend(vc, vCard.SingletonMethods);
},
parse: function(_input, fields) {
var regexps = {
simple: /^(version|fn|title|org)\:(.+)$/i,
complex: /^([^\:\;]+);([^\:]+)\:(.+)$/,
key: /item\d{1,2}\./,
properties: /((type=)?(.+);?)+/
}

var lines = _input.split('\n');
for (n in lines) {
line = lines[n];

if(regexps['simple'].test(line))
{
results = line.match(regexps['simple']);
key = results[1].toLowerCase();
value = results[2];

fields[key] = value;
}

else if(regexps['complex'].test(line))
{
results = line.match(regexps['complex']);
key = results[1].replace(regexps['key'], '').toLowerCase();

properties = results[2].split(';');
properties = Array.filter(properties, function(p) { return ! p.match(/[a-z]+=[a-z]+/) });
properties = Array.map(properties, function(p) { return p.replace(/type=/g, '') });

type = properties.pop() || 'default';
type = type.toLowerCase();

value = results[3];
value = /;/.test(value) ? [value.split(';')] : value;

fields[key] = fields[key] || {};
fields[key][type] = fields[key][type] || [];
fields[key][type] = fields[key][type].concat(value);
}
}
},
SingletonMethods: {
to_html: function() {
var output = '<div class="vcard">';

if(this.photo)
{
output += '<img class="photo" src="data:image/png;base64,' + this.photo['base64'][0] + '" />';
}

output += '<span class="fn">' + this.fn + '</span>'; // Required



if(this.title)
{
output += '<span class="title">' + this.title + '</span>';
}

if(this.org)
{
output += '<span class="org">' + this.org + '</span>';
}

output += '<hr/>'

for(type in this.adr)
{
for(n in this.adr[type])
{
value = this.adr[type][n];

output += '<address class="adr">';
output += '<span class="type">' + type + '</span>';
output += '<div class="content">';

adr_fields = ['post-office-box', 'extended-address', 'street-address',
'locality', 'region', 'postal-code', 'country-name' ]
for(field in adr_fields)
{
if(value[field])
{
output += '<span class="' + adr_fields[field] + '">';
output += value[field];
output += '</span>';
}
}

output += '</div>';
output += '</address>';
}
}

for(type in this.tel)
{
for(n in this.tel[type])
{
value = this.tel[type][n];
output += '<span class="tel">';
output += '<span class="type">' + type + '</span>';
output += '<span class="value">' + value + '</span>';
output += '</span>';
}
}

for(type in this.email)
{
for(n in this.email[type])
{
value = this.email[type][n];
output += '<span class="email">';
output += '<span class="type">' + type + '</span>';
output += '<a class="value" href="mailto:' + value + '">' + value + '</a>';
output += '</span>';
}
}

for(type in this.url)
{
for(n in this.url[type])
{
value = this.url[type][n];
output += '<span class="url">';
output += '<span class="type">' + type + '</span>';
output += '<a class="value" href="' + value + '">' + value + '</a>';
output += '</span>';
}
}

output += '</div>';
output = output.replace(/\\n/g, '<br/>');
return output;
}
},
extend : function(dest, source) {
for (var prop in source) dest[prop] = source[prop];
return dest;
},

Base: {}
}

0 comments on commit 09b7b3f

Please sign in to comment.