forked from ryhan/fixie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixie.js
executable file
·209 lines (181 loc) · 6.32 KB
/
fixie.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Fixie.js
* by Ryhan Hassan
*
* Automagically adds filler content
* whenever an element has class="fixie".
* Hope you find it useful :)
*/
var fixie = (
function () {
if (typeof window.getElementsByClassName != 'function') {
document.getElementsByClassName = function (cl) {
var retnode = [];
var myclass = new RegExp('\\b' + cl + '\\b');
var elem = this.getElementsByTagName('*');
for (var i = 0; i < elem.length; i++) {
var classes = elem[i].className;
if (myclass.test(classes)) retnode.push(elem[i]);
}
return retnode;
};
}
/*
* Spec
* Here are some functions you might find useful
*
* fixie_handler(element)
* fixie_handle_elements(elements)
*
* fixie_fetchWord();
* fixie_fetchPhrase();
* fixie_fetchSentence();
* fixie_fetchParagraph();
* fixie_fetchParagraphs();
*
*/
/*
* fixie_handler(element)
*
* Takes in an element and adds filler content.
* Returns false if tag is unrecognized.
*/
function fixie_handler(element) {
if (!/^\s*$/.test(element.innerHTML)){
var childs = element.children;
if(childs.length){
for(var fixie_i = 0; fixie_i < childs.length; fixie_i++){
fixie_handler(childs[fixie_i]);
}
}
return true;
}
switch (element.nodeName.toLowerCase()) {
case 'b':
case 'em':
case 'strong':
case 'button':
case 'th':
case 'td':
case 'title':
case 'tr':
element.innerHTML = fixie_fetchWord();
break;
case 'header':
case 'cite':
case 'caption':
case 'mark':
case 'q':
case 's':
case 'u':
case 'small':
case 'span':
case 'code':
case 'pre':
case 'li':
case 'dt':
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
element.innerHTML = fixie_fetchPhrase();
break;
case 'footer':
case 'aside':
case 'summary':
case 'blockquote':
case 'p':
element.innerHTML = fixie_fetchParagraph();
break;
case 'article':
case 'section':
element.innerHTML = fixie_fetchParagraphs()
break;
/* Special cases */
case 'a':
element.href = "http://ryhan.me/";
element.innerHTML = "www." + fixie_fetchWord() + fixie_capitalize(fixie_fetchWord()) + ".com";
break;
case 'img':
var width = element.getAttribute('width') || (element.getAttribute('width') = 250);
var height = element.getAttribute('height') || (element.getAttribute('height') = 100);
element.src = "http://placehold.it/" + width + "x" + height;
break;
default:
element.innerHTML = fixie_fetchSentence();
return false;
}
return true;
}
// Handle an array of elements
function fixie_handle_elements(elements){
for (var i = 0; i < elements.length; i++) {
fixie_handler(elements[i]);
}
}
// Begin generator
var fixie_wordlibrary = ["I", "8-bit", "ethical", "reprehenderit", "delectus", "non", "latte", "fixie", "mollit", "authentic", "1982", "moon", "helvetica", "dreamcatcher", "esse", "vinyl", "nulla", "Carles", "bushwick", "bronson", "clothesline", "fin", "frado", "jug", "kale", "organic", "local", "fresh", "tassel", "liberal", "art", "the", "of", "bennie", "chowder", "daisy", "gluten", "hog", "capitalism", "is", "vegan", "ut", "farm-to-table", "etsy", "incididunt", "sunt", "twee", "yr", "before", "gentrify", "whatever", "wes", "Anderson", "chillwave", "dubstep", "sriracha", "voluptate", "pour-over", "esse", "trust-fund", "Pinterest", "Instagram", "DSLR", "vintage", "dumpster", "totally", "selvage", "gluten-free", "brooklyn", "placeat", "delectus", "sint", "magna", "brony", "pony", "party", "beer", "shot", "narwhal", "salvia", "letterpress", "art", "party", "street-art", "seitan", "anime", "wayfarers", "non-ethical", "viral", "iphone", "anim", "polaroid", "gastropub", "city", 'classy', 'original', 'brew']
function fixie_capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function fixie_fetchWord() {
return fixie_wordlibrary[constrain(0, fixie_wordlibrary.length - 1 )];
}
function constrain(min, max){
return Math.round(Math.random() * (max - min) + min)
}
function fixie_fetch(min, max, func){
var fixie_length = constrain(min, max) ;
var result = [];
for (var fixie_i = 0; fixie_i < fixie_length ; fixie_i++) {
result.push(func());
}
return fixie_capitalize(result.join(' '));
}
function fixie_fetchPhrase() {
return fixie_fetch(3, 5, fixie_fetchWord);
}
function fixie_fetchSentence() {
return fixie_fetch(4, 9, fixie_fetchWord) + '.';
}
function fixie_fetchParagraph() {
return fixie_fetch(3, 7, fixie_fetchSentence);
}
function fixie_fetchParagraphs() {
var fixie_length = constrain(3, 7);
var fixie_str = "";
for (var fixie_i = 0; fixie_i < fixie_length - 1; fixie_i++) {
fixie_str += "<p>" + fixie_fetchParagraph() + "</p>";
}
return fixie_str;
}
// Handle all elements with class 'fixie'
fixie_handle_elements(document.getElementsByClassName('fixie'));
// Handle elements which match give css selectors
function init_str(selector_str) {
if (!document.querySelectorAll) {
return false;
}
try {
fixie_handle_elements(document.querySelectorAll(selector_str));
return true;
}
catch (err) {
return false;
}
}
return {
/* returns true if successful, false otherwise */
'init': function(strOrArr) {
if (typeof strOrArr === "object") {
return init_str(strOrArr.join(","));
}
else {
return init_str(strOrArr);
}
}
};
})();