forked from flutter/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.dart
115 lines (93 loc) · 3.29 KB
/
extract.dart
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
import 'dart:io';
import 'package:path/path.dart' as p;
/// Extract Dart snippets from the markdown documentation.
void main(List<String> args) {
// Validate our cwd.
if (!new File('pubspec.yaml').existsSync()) {
print('This tool must be run from the project root.');
exit(1);
}
// Remove any previously generated files.
clean();
// Traverse all markdown files in the repository.
int extractCount = 0;
Iterable<FileSystemEntity> files = Directory.current
.listSync(recursive: true)
.where((FileSystemEntity entity) => entity is File && entity.path.endsWith('.md') && !entity.path.contains('README.md'));
files.forEach((FileSystemEntity file) => extractCount += _processFile(file));
print('\n$extractCount code snippets extracted.');
}
int _processFile(File file) {
String name = p.basename(file.path);
print(name);
// Look for ```dart sections.
String source = file.readAsStringSync();
List<String> lines = source.split('\n');
int index = 1;
int count = 0;
String lastComment;
while (index < lines.length) {
// Look for ```dart sections.
if (lines[index].trim().startsWith('```dart') && lastComment?.trim() != 'skip') {
int startIndex = index + 1;
index++;
while (index < lines.length && !lines[index].trim().startsWith('```')) {
index++;
}
_extractSnippet(name, ++count, startIndex, lines.sublist(startIndex, index),
includeSource: lastComment);
} else if (lines[index].trim().startsWith('<!--')) {
// Look for <!-- comment sections.
int startIndex = index;
while (!lines[index].trim().endsWith('-->')) {
index++;
}
lastComment = lines.sublist(startIndex, index + 1).join('\n');
lastComment = lastComment.substring(4);
if (lines[startIndex].trim() == '<!--') {
// remove the first \n
lastComment = lastComment.substring(1);
}
lastComment = lastComment.substring(0, lastComment.length - 3);
} else {
lastComment = null;
}
index++;
}
return count;
}
void _extractSnippet(String filename, int snippet, int startLine, List<String> lines,
{String includeSource}) {
bool hasImport = lines.any((String line) => line.trim().startsWith('import '));
String path = 'example/${filename.replaceAll('-', '_').replaceAll('.', '_')}_'
'$snippet.dart';
String source = '// Extracted from $filename, line $startLine.\n';
if (!hasImport) {
source += "import 'package:flutter/material.dart';\n\n";
}
if (includeSource != null) {
source += "$includeSource\n";
}
source += '${lines.join('\n')}\n';
source = _removeMarkup(source);
new File(path).writeAsStringSync(source);
print(' ${lines.length} line snippet ==> $path');
}
String _removeMarkup(String source) {
List<String> tags = ['strike', 'highlight', 'note', 'red'];
tags.forEach((String tag) {
source = source.replaceAll('\[\[$tag\]\]', '');
source = source.replaceAll('\[\[/$tag\]\]', '');
});
return source;
}
void clean() {
var exampleDir = new Directory('example');
if (!exampleDir.existsSync()) {
exampleDir.createSync();
}
Iterable<File> files = exampleDir
.listSync()
.where((FileSystemEntity entity) => entity is File && entity.path.endsWith('.dart'));
files.forEach((File file) => file.deleteSync());
}