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 | (function() {
|
36 |
|
37 | var page = require('webpage').create(),
|
38 | fs = require('fs'),
|
39 | system = require('system'),
|
40 |
|
41 | baseExamplesUrl = system.args[1],
|
42 | exampleDir = system.args[2],
|
43 |
|
44 | ignoreFiles = [
|
45 | 'index.html'
|
46 | ],
|
47 | intervalMillisecs = 25,
|
48 | renderMillisecs = 2000,
|
49 |
|
50 | curDir = fs.workingDirectory,
|
51 | exampleDirList = fs.list(exampleDir),
|
52 | pageindex = 0,
|
53 | fileName = '',
|
54 | htmlFiles = [],
|
55 | lenHtmlFiles = 0,
|
56 | loadInProgress = false;
|
57 |
|
58 |
|
59 | var util = {
|
60 | |
61 |
|
62 |
|
63 | baseName: function(path) {
|
64 | var parts = path.split(fs.separator);
|
65 | return parts[parts.length - 1];
|
66 | },
|
67 | |
68 |
|
69 |
|
70 | isHtmlFile: function(filename) {
|
71 | return (/\.html?$/).test(filename);
|
72 | },
|
73 | |
74 |
|
75 |
|
76 | appendSlash: function(str) {
|
77 | return ((/\/$/).test(str)) ? str : str + '/';
|
78 | },
|
79 | |
80 |
|
81 |
|
82 | buildUrl: function(baseurl, path) {
|
83 | var name = util.baseName(path),
|
84 | mode = 'raw';
|
85 | return util.appendSlash(baseurl) + name + '?mode=' + mode;
|
86 | },
|
87 | |
88 |
|
89 |
|
90 | logProgress: function() {
|
91 | var doneSymbol = '-',
|
92 | todoSymbol = ' ',
|
93 | currentSymbol = '>',
|
94 | barStrLeft = '[',
|
95 | barStrRight = ']',
|
96 | progresStep = 5,
|
97 | totalSteps = Math.round(100 / progresStep),
|
98 | ratio = (lenHtmlFiles === 0) ? 0 : (pageindex / lenHtmlFiles),
|
99 | percent = (ratio === 0) ? 0 : ratio * 100,
|
100 | normalizedNumDone = Math.floor(ratio * totalSteps),
|
101 | normalizedNumTodo = totalSteps - normalizedNumDone,
|
102 | progressLine = '',
|
103 | i = 0;
|
104 |
|
105 | progressLine += barStrLeft;
|
106 | for (; i < normalizedNumDone; i++) {
|
107 | progressLine += doneSymbol;
|
108 | }
|
109 | for (i = 0; i < normalizedNumTodo; i++) {
|
110 | progressLine += (i === 0) ? currentSymbol : todoSymbol;
|
111 | }
|
112 | progressLine += barStrRight;
|
113 |
|
114 |
|
115 | if (percent < 10) {
|
116 | progressLine += ' ';
|
117 | } else if (percent < 100) {
|
118 | progressLine += ' ';
|
119 | }
|
120 | progressLine += ' ' + percent.toFixed(1) + ' % done';
|
121 |
|
122 | if (fileName !== '') {
|
123 | progressLine += ', ' + util.baseName(fileName) + '';
|
124 | }
|
125 | console.log(progressLine);
|
126 | }
|
127 | };
|
128 |
|
129 |
|
130 |
|
131 | for (var i = 0; i < exampleDirList.length; i++) {
|
132 | var fullpath = exampleDir + fs.separator + exampleDirList[i];
|
133 | if (fs.isFile(fullpath) && util.isHtmlFile(fullpath) &&
|
134 | ignoreFiles.indexOf(util.baseName(fullpath)) === -1) {
|
135 |
|
136 | htmlFiles.push(fullpath);
|
137 | }
|
138 | }
|
139 | lenHtmlFiles = htmlFiles.length;
|
140 |
|
141 | console.log('Capturing ' + lenHtmlFiles + ' example screenshots.');
|
142 |
|
143 |
|
144 |
|
145 | var interval = setInterval(function() {
|
146 | if (!loadInProgress && pageindex < lenHtmlFiles) {
|
147 | util.logProgress();
|
148 | fileName = htmlFiles[pageindex];
|
149 | page.viewportSize = { width: 800, height: 600 };
|
150 | page.clipRect = {
|
151 | top: 0,
|
152 | left: 0,
|
153 | width: page.viewportSize.width,
|
154 | height: page.viewportSize.height
|
155 | };
|
156 | page.open(util.buildUrl(baseExamplesUrl, htmlFiles[pageindex]));
|
157 | }
|
158 | if (pageindex == lenHtmlFiles) {
|
159 | util.logProgress();
|
160 | console.log(lenHtmlFiles + ' screenshots captured.');
|
161 | phantom.exit();
|
162 | }
|
163 | }, intervalMillisecs);
|
164 |
|
165 |
|
166 | page.onLoadStarted = function() {
|
167 | loadInProgress = true;
|
168 | };
|
169 |
|
170 |
|
171 | page.onLoadFinished = function() {
|
172 | var dest = exampleDir + fs.separator + util.baseName(fileName) + '.png';
|
173 | window.setTimeout(function() {
|
174 | loadInProgress = false;
|
175 | page.render(dest);
|
176 | pageindex++;
|
177 | }, renderMillisecs);
|
178 | };
|
179 |
|
180 | })();
|