forked from adam-singer/widget.dart
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.dart
More file actions
executable file
·219 lines (181 loc) · 5.52 KB
/
build.dart
File metadata and controls
executable file
·219 lines (181 loc) · 5.52 KB
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
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env dart
import 'dart:async';
import 'dart:io';
import 'package:html5lib/dom.dart';
import 'package:html5lib/parser.dart';
import 'package:html5lib/dom_parsing.dart';
import 'package:bot/bot.dart';
final _whitespaceRegex = new RegExp(r'\s+');
void main(List<String> args) {
_log('** ARGS: $args');
final changes = _getChangedFiles(args);
if(!args.isEmpty && _onlyOutputFiles(changes)) {
_log(' ** Nothing interesting changed');
return;
}
_log('** CHANGES: $changes');
final input = 'web/index_source.html';
final output = 'web/index.html';
if(changes.contains(input)) {
_transform(input, output).then((bool value) {
if(value) {
_log('updated $output');
} else {
_log('no change to $output');
}
});
} else {
_log(" - skipping transform");
}
}
void _log(value) {
if(value != null) {
print(value);
/*
* NOTE: if you're having trouble debugging what's going on in build.dart
* You can un-comment this section. Changes to hidden files won't kick off the build
* so the file is named `.build.log`
final file = new File('.build.log');
final str = "$value\n";
file.writeAsStringSync(str, mode: FileMode.APPEND);
*/
}
}
bool _onlyOutputFiles(List<String> files) {
return files.every((value) => value.startsWith(r'web/out/'));
}
List<String> _getChangedFiles(List<String> args) {
return args.where((value) => value.contains('='))
.map((value) {
final indexOfEqu = value.indexOf('=');
return value.substring(indexOfEqu+1);
})
.toList();
}
Future<bool> _transform(String input, String output) {
_log('doing big page transform');
final file = new File(input);
assert(file.existsSync());
return file.readAsString()
.then((String contents) {
var parser = new HtmlParser(contents, generateSpans: true);
var document = parser.parse();
_tweakDocument(document);
return _updateIfChanged(output, document.outerHtml);
});
}
Future<bool> _updateIfChanged(String filePath, String newContent) {
final file = new File(filePath);
return file.exists()
.then((bool exists) {
if(exists) {
return file.readAsString()
.then((String content) => content != newContent);
} else {
return new Future.value(true);
}
}).then((bool shouldUpdate) {
if(shouldUpdate) {
return file.writeAsString(newContent)
.then((_) => true);
} else {
return new Future.value(false);
}
});
}
// TODO: crazy hack. Really need select recursive
// or propery query support...whichever
List<Element> _findElements(Element element, Func1<Element, bool> predicate, [List<Element> target]) {
if(target == null) {
target = new List<Element>();
}
if(predicate(element)) {
target.add(element);
}
element.children.forEach((e) => _findElements(e, predicate, target));
return target;
}
void _tweakDocument(Document document) {
document.queryAll('section')
.where((s) => s.attributes['class'] == 'component')
.forEach((s) {
_tweakComponentSection(s);
});
final sectionHeaders = _findElements(document.body, (e) {
return e.tagName == 'h1' || e.tagName == 'h2';
});
//
// TOC fun!
//
final tocUls = document.queryAll('div')
.where((e) {
return e.attributes['class'] != null &&
e.attributes['class'].contains('list-group');
});
assert(tocUls.length == 1);
final Element tocUl = $(tocUls).first;
sectionHeaders.forEach((h) {
final headerText = htmlSerializeEscape(h.innerHtml);
final headerId = headerText.toLowerCase().replaceAll(_whitespaceRegex, '_');
final link = new Element.tag('a')
..attributes['href'] = '#$headerId'
..attributes['class'] = 'level-${h.tagName} list-group-item'
..innerHtml = headerText;
tocUl.children.add(link);
h.attributes['id'] = headerId;
});
}
void _tweakComponentSection(Element element) {
// find demo section
final demoSections = element.children.where((e) {
return e.attributes['class'] == 'demo';
}).toList();
Element demoSection = null;
if(demoSections.length == 1) {
demoSection = demoSections[0];
}
// find code section
final codeSections = element.children.where((e) {
return e.attributes['class'] == 'code';
}).toList();
Element codeSection = null;
if(demoSections.length == 1) {
codeSection = codeSections[0];
}
// if we have both, take content of demo section, html encode and put it in demo
if(demoSection != null && codeSection != null) {
final demoMarkup = demoSection.innerHtml;
codeSection.innerHtml = _cleanUpCode(demoMarkup);
}
}
String _cleanUpCode(String input) {
input = htmlSerializeEscape(input);
//
// Get all non-empty lines
//
var lines = input.split('\n')
.where((String line) {
return line.trim().length > 0;
}).toList();
//
// Figure out the max number of spaces that prefix all lines
// and remove them so the sample output is indented just enough
//
final regex = new RegExp(r'^([ ]*).*$');
int minPrefix = null;
lines.forEach((String line) {
final match = regex.firstMatch(line);
assert(match.groupCount == 1);
final g = match.group(1);
if(minPrefix == null) {
minPrefix = g.length;
} else if(minPrefix > g.length) {
minPrefix = g.length;
}
});
for(var i = 0; i < lines.length; i++) {
lines[i] = lines[i].substring(minPrefix);
}
input = lines.join('\n');
return input;
}