forked from adactio/Pattern-Primer
-
Notifications
You must be signed in to change notification settings - Fork 10
/
pattern-primer.js
executable file
·134 lines (113 loc) · 3.48 KB
/
pattern-primer.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
#!/usr/bin/env node
/*jslint nomen: true */
'use strict';
var settings = {
webserverport : '8080',
wwwroot: 'public',
pattern_path: 'public/patterns',
sourcehtmlfile: 'source.html',
tofile_outputpath: 'docs'
},
util = require('util'),
connect = require('connect'),
primer = function (serverResponse, tofile, tofileCallback) {
tofile = tofile || false;
var fs = require('fs'),
patternFolder = './' + settings.pattern_path,
simpleEscaper = function (text) {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/"/g, '"').replace(/'/g, ''');
},
outputPatterns = function (patterns) {
fs.readFile(settings.sourcehtmlfile, 'utf-8', function (err, content) {
if (err !== null) {
util.puts('There was an error when trying to read file:', 'source.html');
return;
}
var i,
l,
file;
for (i = 0, l = patterns.length; i < l; i += 1) {
file = patterns[i];
content += '<div class="pattern"><div class="display">';
content += file.content;
content += '</div><div class="source"><textarea rows="6" cols="30">';
content += simpleEscaper(file.content);
content += '</textarea>';
if (!tofile) {
content += '<p><a href="patterns/' + file.filename + '">' + file.filename + '</a></p>';
}
content += '</div></div>';
}
content += '</body></html>';
if (tofile) {
tofileCallback(content);
} else {
serverResponse.end(content);
}
});
},
handleFiles = function (files) {
var i,
l,
file,
patterns = [];
// This was asyncronous, but we need the file names, which we can't get from the callback of 'readFile'
for (i = 0, l = files.length; i < l; i += 1) {
file = {
filename : files[i]
};
file.content = fs.readFileSync(patternFolder + '/' + file.filename, 'utf-8');
patterns.push(file);
}
outputPatterns(patterns);
},
beginProcess = function () {
fs.readdir(patternFolder, function (err, contents) {
if (err !== null && err.code === 'ENOENT') {
util.puts('Cannot find patterns folder:', patternFolder);
return;
}
var files = [],
i,
l;
for (i = 0, l = contents.length; i < l; i += 1) {
if (contents[i].substr(-5) === '.html') {
files.push(contents[i]);
}
}
handleFiles(files);
});
};
beginProcess();
},
server = connect.createServer(
connect.static(__dirname + '/' + settings.wwwroot),
function (req, resp) {
if (req.url !== '/') {
resp.writeHead(404, {
'Content-Length': 0,
'Content-Type': 'text/plain'
});
resp.end();
return;
}
primer(resp);
}
);
if (process.argv[2] === '--tofile') {
primer(null, true, function (content) {
var fs = require('fs');
fs.writeFile('./' + settings.tofile_outputpath + '/index.html', content, 'utf-8', function (err) {
if (err !== null && err.code === 'ENOENT') {
util.puts('Cannot find patterns folder:', settings.tofile_outputpath);
return;
}
fs.createReadStream('./' + settings.wwwroot + '/global.css').pipe(fs.createWriteStream('./' + settings.tofile_outputpath + '/global.css'));
util.puts('Stand-alone output can now be found in "' + settings.tofile_outputpath + '/"');
});
});
} else {
server.listen(settings.webserverport);
util.puts('You can now visit http://localhost:' + settings.webserverport + '/ to see your patterns.');
util.puts('To kill this server, just press Ctrl + C');
}