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