Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, 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 library dev_compiler.src.html_gen; | |
| 6 | |
| 7 /// A class to generate an html page. | |
| 8 class HtmlGen { | |
| 9 final StringBuffer _buffer = new StringBuffer(); | |
|
vsm
2015/10/27 20:04:55
We've generally been omitting the type when there
devoncarew
2015/10/27 20:32:35
Done.
| |
| 10 final List<String> _tags = []; | |
| 11 final List<bool> _indents = []; | |
| 12 | |
| 13 bool _startOfLine = true; | |
| 14 String _indent = ''; | |
| 15 | |
| 16 HtmlGen() { | |
| 17 _init(); | |
| 18 } | |
| 19 | |
| 20 void _init() { | |
| 21 writeln('<!DOCTYPE html>'); | |
| 22 writeln(); | |
| 23 writeln('<!-- generated by dev_compiler -->'); | |
| 24 writeln(); | |
| 25 } | |
| 26 | |
| 27 void start( | |
| 28 {String title, | |
| 29 String cssRef, | |
| 30 String theme, | |
| 31 String jsScript, | |
| 32 String inlineStyle}) { | |
| 33 startTag('html', newLine: false); | |
| 34 writeln(); | |
| 35 startTag('head'); | |
| 36 writeln('<meta charset="utf-8">'); | |
| 37 writeln( | |
| 38 '<meta name="viewport" content="width=device-width, initial-scale=1.0">' ); | |
|
vsm
2015/10/27 20:04:55
nit: line len
devoncarew
2015/10/27 20:32:35
Done.
| |
| 39 if (title != null) { | |
| 40 writeln('<title>${title}</title>'); | |
| 41 } | |
| 42 if (cssRef != null) { | |
| 43 writeln('<link href="${cssRef}" rel="stylesheet" media="screen">'); | |
| 44 } | |
| 45 if (theme != null) { | |
| 46 writeln('<link href="${theme}" rel="stylesheet">'); | |
| 47 } | |
| 48 if (jsScript != null) { | |
| 49 writeln('<script src="${jsScript}"></script>'); | |
| 50 } | |
| 51 if (inlineStyle != null) { | |
| 52 startTag('style'); | |
| 53 writeln(inlineStyle); | |
| 54 endTag(); | |
| 55 } | |
| 56 endTag(); | |
| 57 writeln(); | |
| 58 startTag('body', newLine: false); | |
| 59 writeln(); | |
| 60 } | |
| 61 | |
| 62 void startTag(String tag, {String attributes, String c, bool newLine: true}) { | |
| 63 if (c != null && c.isNotEmpty) { | |
| 64 if (attributes == null) { | |
| 65 attributes = 'class="${c}"'; | |
| 66 } else { | |
| 67 attributes += ' class="${c}"'; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 if (attributes != null) { | |
| 72 if (newLine) { | |
| 73 writeln('<${tag} ${attributes}>'); | |
| 74 } else { | |
| 75 write('<${tag} ${attributes}>'); | |
| 76 } | |
| 77 } else { | |
| 78 if (newLine) { | |
| 79 writeln('<${tag}>'); | |
| 80 } else { | |
| 81 write('<${tag}>'); | |
| 82 } | |
| 83 } | |
| 84 _indents.add(newLine); | |
| 85 if (newLine) { | |
| 86 _indent = '$_indent\t'; | |
| 87 } | |
| 88 _tags.add(tag); | |
| 89 } | |
| 90 | |
| 91 void span({String text, String c}) => tag('span', text: text, c: c); | |
| 92 | |
| 93 void tag(String tag, | |
| 94 {String text, String c, String href, String attributes}) { | |
| 95 if (attributes == null) attributes = ''; | |
| 96 if (text == null) text = ''; | |
| 97 | |
| 98 if (c != null && c.isNotEmpty) attributes += ' class="${c}"'; | |
| 99 if (href != null) attributes += ' href="${href}"'; | |
| 100 | |
| 101 if (attributes.isNotEmpty) attributes = ' ${attributes.trim()}'; | |
| 102 | |
| 103 writeln('<$tag$attributes>$text</$tag>'); | |
| 104 } | |
| 105 | |
| 106 void endTag() { | |
| 107 String tag = _tags.removeLast(); | |
| 108 bool wasIndent = _indents.removeLast(); | |
| 109 if (wasIndent) { | |
| 110 _indent = _indent.substring(0, _indent.length - 1); | |
| 111 } | |
| 112 writeln('</${tag}>'); | |
| 113 } | |
| 114 | |
| 115 void end() { | |
| 116 // body | |
| 117 endTag(); | |
| 118 // html | |
| 119 endTag(); | |
| 120 } | |
| 121 | |
| 122 String toString() => _buffer.toString(); | |
| 123 | |
| 124 void reset() { | |
| 125 _buffer.clear(); | |
| 126 _startOfLine = true; | |
| 127 _tags.clear(); | |
| 128 _indents.clear(); | |
| 129 _indent = ''; | |
| 130 | |
| 131 _init(); | |
| 132 } | |
| 133 | |
| 134 void write(String str) { | |
| 135 if (_startOfLine) { | |
| 136 _buffer.write(_indent); | |
| 137 _startOfLine = false; | |
| 138 } | |
| 139 _buffer.write(str); | |
| 140 } | |
| 141 | |
| 142 void writeln([String str]) { | |
| 143 if (str == null) { | |
| 144 write('\n'); | |
| 145 } else { | |
| 146 write('${str}\n'); | |
| 147 } | |
| 148 _startOfLine = true; | |
| 149 } | |
| 150 } | |
| OLD | NEW |