Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: scripts/convert_book.dart

Issue 24596006: Fix the book build script, and regenerate the book (add to*Case note) (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | scripts/pubspec.lock » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env dart 1 #!/usr/bin/env dart
2 2
3 import 'dart:io'; 3 import 'dart:io';
4 import 'dart:async'; 4 import 'package:path/path.dart' as path;
5 5
6 Directory outputDir; 6 Directory outputDir;
7 7
8 String frontMatter(title) { 8 String frontMatter(title) {
9 final yaml = """ 9 final yaml = """
10 --- 10 ---
11 # WARNING: GENERATED FILE. DO NOT EDIT. 11 # WARNING: GENERATED FILE. DO NOT EDIT.
12 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book 12 # WANT TO CONTRIBUTE? SEE https://github.com/dart-lang/dart-up-and-running-book
13 layout: book 13 layout: book
14 title: "${title} from Dart: Up and Running" 14 title: "${title} from Dart: Up and Running"
15 description: "Read ${title} of Dart: Up and Running, published by O'Reilly." 15 description: "Read ${title} of Dart: Up and Running, published by O'Reilly."
16 --- 16 ---
17 """; 17 """;
18 return yaml; 18 return yaml;
19 } 19 }
20 20
21 convertFile(String fileName) { 21 convertFile(String fileName) {
22 print(fileName); 22 print(fileName);
23 var file = new File(fileName); 23 var file = new File(fileName);
24 var contents = file.readAsStringSync(); 24 var contents = file.readAsStringSync();
25 var title = new RegExp(r'<title>(.*)</title>').firstMatch(contents)[1]; 25 var title = new RegExp(r'<title>(.*)</title>').firstMatch(contents)[1];
26 var start = contents.indexOf(r'<div class="navheader">'); 26 var start = contents.indexOf(r'<div class="navheader">');
27 var end = contents.lastIndexOf(r'</body></html>'); 27 var end = contents.lastIndexOf(r'</body></html>');
28 var body = contents.substring(start, end); 28 var body = contents.substring(start, end);
29 var filenameOnly = new Path(fileName).filename; 29 var filenameOnly = path.basename(fileName);
30 30
31 writeFile(filenameOnly, title, body); 31 writeFile(filenameOnly, title, body);
32 } 32 }
33 33
34 writeFile(String fileName, String title, String body) { 34 writeFile(String fileName, String title, String body) {
35 var out = new File('${outputDir.path}/${fileName}'); 35 var out = new File('${outputDir.path}/${fileName}');
36 out.writeAsStringSync(frontMatter(title)); 36 out.writeAsStringSync(frontMatter(title));
37 out.writeAsStringSync(body, mode: FileMode.APPEND, encoding: Encoding.UTF_8); 37 out.writeAsStringSync(body, mode: FileMode.APPEND);
38 } 38 }
39 39
40 checkDir(Directory dir) { 40 checkDir(Directory dir) {
41 if (!dir.existsSync()) { 41 if (!dir.existsSync()) {
42 print("Directory '${dir.path}' does not exist"); 42 print("Directory '${dir.path}' does not exist");
43 exit(1); 43 exit(1);
44 } 44 }
45 } 45 }
46 46
47 main() { 47 main() {
48 var args = new Options().arguments; 48 var args = new Options().arguments;
49 if (args.length != 2) { 49 if (args.length != 2) {
50 print("Usage: convert_book.dart dir_of_html output_dir"); 50 print("Usage: convert_book.dart dir_of_html output_dir");
51 exit(1); 51 exit(1);
52 } 52 }
53 53
54 var inputDir = new Directory(args[0]); 54 var inputDir = new Directory(args[0]);
55 outputDir = new Directory(args[1]); 55 outputDir = new Directory(args[1]);
56 checkDir(inputDir); 56 checkDir(inputDir);
57 checkDir(outputDir); 57 checkDir(outputDir);
58 58
59 List<FileSystemEntity> filenames = inputDir.listSync(); 59 List<FileSystemEntity> filenames = inputDir.listSync();
60 filenames 60 filenames
61 .map((f) => f.path) 61 .map((f) => f.path)
62 .where((name) => name.endsWith(".html")) 62 .where((name) => name.endsWith(".html"))
63 .forEach((name) => convertFile(name)); 63 .forEach((name) => convertFile(name));
64 } 64 }
OLDNEW
« no previous file with comments | « no previous file | scripts/pubspec.lock » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698