OLD | NEW |
---|---|
1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 # Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 # for details. All rights reserved. Use of this source code is governed by a | 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. | 3 # BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #!/usr/bin/env python | 5 #!/usr/bin/env python |
6 # | 6 # |
7 | 7 |
8 """Rewrites HTML files, converting Dart script sections into JavaScript. | 8 """Rewrites HTML files, converting Dart script sections into JavaScript. |
9 | 9 |
10 Process HTML files, and internally changes script sections that use Dart code | 10 Process HTML files, and internally changes script sections that use Dart code |
(...skipping 20 matching lines...) Expand all Loading... | |
31 DOM_IMPORT_MATCHER = re.compile( | 31 DOM_IMPORT_MATCHER = re.compile( |
32 r"^#import\(['\"]dart\:dom['\"].*\);", re.MULTILINE) | 32 r"^#import\(['\"]dart\:dom['\"].*\);", re.MULTILINE) |
33 HTML_IMPORT_MATCHER = re.compile( | 33 HTML_IMPORT_MATCHER = re.compile( |
34 r"^#import\(['\"]dart\:html['\"].*\);", re.MULTILINE) | 34 r"^#import\(['\"]dart\:html['\"].*\);", re.MULTILINE) |
35 | 35 |
36 FROG_NOT_FOUND_ERROR = ( | 36 FROG_NOT_FOUND_ERROR = ( |
37 """Couldn't find compiler: please run the following commands: | 37 """Couldn't find compiler: please run the following commands: |
38 $ cd %s/frog | 38 $ cd %s/frog |
39 $ ./tools/build.py -m release""") | 39 $ ./tools/build.py -m release""") |
40 | 40 |
41 ENTRY_POINT_DOM = """ | 41 ENTRY_POINT = """ |
42 #library('entry'); | 42 #library('entry'); |
43 #import('dart:dom'); | |
44 #import('%s', prefix: 'original'); | 43 #import('%s', prefix: 'original'); |
45 main() { | 44 main() => original.main(); |
vsm
2012/03/28 17:17:57
I think we can just get rid of this trampoline fil
kasperl
2012/03/28 17:56:29
I think the plan is to get rid of htmlconverter.py
| |
46 window.addEventListener('DOMContentLoaded', (e) => original.main(), false); | |
47 } | |
48 """ | |
49 | |
50 ENTRY_POINT_HTML = """ | |
51 #library('entry'); | |
52 #import('dart:html'); | |
53 #import('%s', prefix: 'original'); | |
54 main() { | |
55 window.on.contentLoaded.add((e) => original.main()); | |
56 } | |
57 """ | 45 """ |
58 | 46 |
59 CSS_TEMPLATE = '<style type="text/css">%s</style>' | 47 CSS_TEMPLATE = '<style type="text/css">%s</style>' |
60 CHROMIUM_SCRIPT_TEMPLATE = '<script type="application/javascript">%s</script>' | 48 CHROMIUM_SCRIPT_TEMPLATE = '<script type="application/javascript">%s</script>' |
61 | 49 |
62 DARTIUM_TO_JS_SCRIPT = """ | 50 DARTIUM_TO_JS_SCRIPT = """ |
63 <script type="text/javascript"> | 51 <script type="text/javascript"> |
64 (function() { | 52 (function() { |
65 // Let the user know that Dart is required. | 53 // Let the user know that Dart is required. |
66 if (!window.navigator.webkitStartDart) { | 54 if (!window.navigator.webkitStartDart) { |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 body = adjustImports(body) | 130 body = adjustImports(body) |
143 | 131 |
144 if HTML_IMPORT_MATCHER.search(body): | 132 if HTML_IMPORT_MATCHER.search(body): |
145 useDartHtml = True | 133 useDartHtml = True |
146 | 134 |
147 inputfile = join(indir, 'code.dart') | 135 inputfile = join(indir, 'code.dart') |
148 with open(inputfile, 'w') as f: | 136 with open(inputfile, 'w') as f: |
149 f.write("#library('inlinedcode');\n") | 137 f.write("#library('inlinedcode');\n") |
150 f.write(body) | 138 f.write(body) |
151 | 139 |
152 if useDartHtml: | |
153 entryPoint = ENTRY_POINT_HTML | |
154 else: | |
155 entryPoint = ENTRY_POINT_DOM | |
156 | |
157 wrappedfile = join(indir, 'entry.dart') | 140 wrappedfile = join(indir, 'entry.dart') |
158 with open(wrappedfile, 'w') as f: | 141 with open(wrappedfile, 'w') as f: |
159 f.write(entryPoint % inputfile) | 142 f.write(ENTRY_POINT % inputfile) |
160 | 143 |
161 status, out, err = execute(self.compileCommand(wrappedfile, outdir), | 144 status, out, err = execute(self.compileCommand(wrappedfile, outdir), |
162 self.verbose) | 145 self.verbose) |
163 if status: | 146 if status: |
164 raise ConverterException('compilation errors') | 147 raise ConverterException('compilation errors') |
165 | 148 |
166 # Inline the compiled code in the page | 149 # Inline the compiled code in the page |
167 with open(self.outputFileName(wrappedfile, outdir), 'r') as f: | 150 with open(self.outputFileName(wrappedfile, outdir), 'r') as f: |
168 res = f.read() | 151 res = f.read() |
169 | 152 |
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
566 if 'dartium' in options.target: | 549 if 'dartium' in options.target: |
567 convertForDartium(filename, options.out, | 550 convertForDartium(filename, options.out, |
568 outfile.replace(extension, '-dart' + extension), options.verbose) | 551 outfile.replace(extension, '-dart' + extension), options.verbose) |
569 except Exception as e: | 552 except Exception as e: |
570 print "%sERROR%s: %s" % (RED_COLOR, NO_COLOR, str(e)) | 553 print "%sERROR%s: %s" % (RED_COLOR, NO_COLOR, str(e)) |
571 return 1 | 554 return 1 |
572 return 0 | 555 return 0 |
573 | 556 |
574 if __name__ == '__main__': | 557 if __name__ == '__main__': |
575 sys.exit(main()) | 558 sys.exit(main()) |
OLD | NEW |