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

Side by Side Diff: dart/site/try/extract_theme.dart

Issue 125123002: try.dartlang.org version 5. (Closed) Base URL: /Users/ahe/Dart/all@master
Patch Set: Created 6 years, 11 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:io';
6
7 StringBuffer themes = new StringBuffer();
8
9 void main() {
10 print('part of trydart.themes;\n');
11 new Options().arguments.forEach(extractTheme);
12 print('''
13 /// List of known themes. The default is the first theme.
14 const List<Theme> THEMES = const <Theme> [
15 const Theme(),
16 $themes];''');
17 }
18
19 final DECORATION_PATTERN = new RegExp(r'^ *<([a-z][^ ]+)[ ]');
20
21 String attr(String name, String line) {
22 var match = new RegExp('$name'r'="([^"]*)"').firstMatch(line);
23 if (match == null) return null;
24 return match[1];
25 }
26
27 void extractTheme(String filename) {
28 bool openedTheme = false;
29 for (String line in new File(filename).readAsLinesSync()) {
30 if (line.startsWith('<colorTheme')) {
31 openTheme(line, filename);
32 openedTheme = true;
33 } else if (line.startsWith('</colorTheme>')) {
34 if (!openedTheme) throw 'Theme not found in $filename';
35 closeTheme();
36 openedTheme = false;
37 } else if (DECORATION_PATTERN.hasMatch(line)) {
38 if (!openedTheme) throw 'Theme not found in $filename';
39 printDecoration(line);
40 }
41 }
42 }
43
44 openTheme(String line, String filename) {
45 var name = attr('name', line);
46 var author = attr('author', line);
47 if (name == null) name = 'Untitled';
48 if (name == 'Default') name = 'Dart Editor';
49 var declaration = name.replaceAll(new RegExp('[^a-zA-Z0-9_]'), '_');
50 themes.write(' const ${declaration}Theme(),\n');
51 print('/// $name theme extracted from');
52 print('/// $filename.');
53 if (author != null) {
54 print('/// Author: $author.');
55 }
56 print("""
57 class ${declaration}Theme extends Theme {
58 const ${declaration}Theme();
59
60 String get name => '$name';
61 """);
62 }
63
64 closeTheme() {
65 print('}\n');
66 }
67
68 printDecoration(String line) {
69 String name = DECORATION_PATTERN.firstMatch(line)[1];
70 if (name == 'class') name = 'className';
71 if (name == 'enum') name = 'enumName';
72 StringBuffer properties = new StringBuffer();
73 var color = attr('color', line);
74 if (color != null) {
75 properties.write("color: '$color'");
76 }
77 var bold = attr('bold', line) == 'true';
78 if (bold) {
79 if (!properties.isEmpty) properties.write(', ');
80 properties.write('bold: true');
81 }
82 print(' Decoration get $name => const Decoration($properties);');
83 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698