forked from h5bp/html5please
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
182 lines (149 loc) · 4.89 KB
/
app.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
// Native modules
var fs = require('fs');
var request = require('request');
// Third-party modules
var Backbone = require('backbone');
var Handlebars = require('handlebars');
var Markdown = require('node-markdown').Markdown;
var async = require('async');
//polyfill or no
Handlebars.registerHelper('polyfillaction', function(tags) {
if(tags && tags.indexOf('polyfill') > -1) {
return 'with <b class=polyfill>polyfill</b>';
}
if(tags && tags.indexOf('fallback') > -1) {
return 'with <b class=fallback>fallback</b>';
}
return false;
});
Handlebars.registerHelper('featuretag', function(feature) {
var tag = /^<(.*)>/.exec(feature);
if(tag) {
return "<" + tag[1] + ">";
} else {
return feature;
}
});
Handlebars.registerHelper('testurl', function(url) {
return /caniuse/.exec(url) != null? 'View browser share %': 'Learn more';
});
// File paths
var paths = {
output: './index.html',
template: './template.html',
posts: './posts',
githuburl: 'https://github.com/h5bp/html5please/blob/master/posts/',
caniuseurl: 'http://caniuse.com/',
};
// Tag Array
var featuretags = [];
// Ensures Backbone doesn't try to make a DOM element
Backbone.View.prototype._ensureElement = function() {};
exports.Feature = Backbone.Model.extend({
initialize: function() {
if (this.has("contents")) {
var i, len, parts, key, val, posttags;
var obj = { contents: '' };
var docs = this.get('contents').split('\n\n');
var lines = docs[0].split('\n');
if(this.has('editfrag')) {
obj.editurl = paths.githuburl + this.get('editfrag') + '.md';
obj.slug = this.get('editfrag');
}
for (i = 0, len = lines.length; i < len; i++) {
parts = lines[i].trim().split(':');
if (parts.length < 2) {
console.error(lines);
throw new Error('Invalid key: val ... ' + obj.slug);
}
key = parts[0];
val = parts.slice(1).join(':').trim();
if(key == 'tags') {
posttags = val.split(' ');
//if it is not whitespace separated it must be comma separated
if(posttags.length === 1) {
posttags.join(' ').split(',');
}
posttags = posttags.map(function(tag) {
tag = tag.trim();
//catch use of tags like polyfill, gtie9
tag = /([^,]*)/.exec(tag)[1];
return tag;
});
val = posttags.join(' ');
}
if(key == 'kind') {
obj.moreurl = paths.caniuseurl + obj.slug;
}
obj[key] = (key === 'polyfillurls') ? "" + Markdown(val) : "" + val.trim();
}
obj.contents = "" + Markdown(docs.slice(1).join("\n\n"));
// Update the model to use the metadata and contents
this.set(obj);
}
}
});
exports.Features = Backbone.Collection.extend({
model: exports.Feature,
comparator : function(item){
return item.get('feature').replace(/<|>|@|(<)/g,'').toLowerCase();
},
sync: function(method, model, options) {
var data = [];
var files = fs.readdirSync(paths.posts);
// Slice off the file extension for each
files.forEach(function(file, i) {
// skip dotfiles
if (file.indexOf('.') == 0) return;
data.push({
contents: fs.readFileSync(paths.posts + "/" + file).toString(),
editfrag: file.slice(0, -3),
id: i
});
});
// Call success with the fetched data
options.success(data);
}
});
exports.Markup = Backbone.View.extend({
initialize: function() {
// Read in the template and compile via handlebars to a reusable
// property that can be accessed in render.
var source = fs.readFileSync(paths.template).toString();
this.template = Handlebars.compile(source);
},
// Triggered only when argument checkurls is passed to CLI
updateurls: function(updateUrlCallback) {
console.log('updating caniuse data');
var collectionlength = this.collection.length;
var eventcount = 0;
var callback = callback;
async.forEachSeries(
this.collection.toArray(),
function(feature, callback) {
var req = request({
method: 'GET',
uri: feature.get('moreurl'),
headers: {
'User-Agent': 'html5please'
}
}, function(error, response) {
if(response.statusCode == 404) {
console.log('url not found: ', feature.get('moreurl'));
feature.unset('moreurl', {silent: true});
}
callback();
});
},
function() {
updateUrlCallback();
});
},
render: function() {
console.log("rendering to the file…");
// Render the template to the output file
var html = this.template({ featuretags: featuretags, features: this.collection.toJSON() });
fs.writeFileSync(paths.output, html);
console.log("Your index page is now ready")
}
});