This repository has been archived by the owner on Dec 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (64 loc) · 2.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
/**
* index.js
* ------------------------------------------------------
*
* Main module file that exports the tags funciton. Wooh!.
*
* ------------------------------------------------------
*/
var _ = require('underscore'),
names = require('./lib/tag-names');
// Validates attributes arguments when rendering a
// new tag. Basically just converts arrays, objects,
// etc to strings.
// @param attributes {string, array, or object} The attributes
// to be applied to the tag. If they are an array, they will
// will simply be joined with a whitespace. If it is an object,
// then the attributes will be combined with an equals sign, where
// the key is the name and the value is the value (ehm..).
//
function checkAttributes (attributes) {
attributes = attributes || "";
// Sanity checks...
if (_.isArray(attributes)) {
attributes = ' ' + attributes.join(' ');
} else if (_.isObject(attributes)) {
attributes = _.chain(attributes).map(function(val, key) {
return key + "=" + val;
}).reduce(function(m, n) {
return m + " " + n;
}, '').value();
} else if (_.isString(attributes) && attributes.length > 2) {
attributes = " " + attributes;
}
return attributes;
}
// Add a new tag name to the available methods.
//
// @param name {string} The name of the tag to create.
//
function addTag (name) {
tags[name] = function (context, attributes) {
context = context || "";
attributes = checkAttributes(attributes);
return '<' + (name + attributes) + '>' + context + '</' + name + '>';
};
return tags[name];
}
// The main tags object.
//
// @param tagName {string} Passed to internal addTag function
// to create a new tag method.
//
function tags (tagName) {
return addTag(tagName);
}
// This registers all of the default tags to
// the main export of the module.
//
_.each(names, function(tagName) {
addTag(tagName);
});
// Hooray!
module.exports = tags;