-
Notifications
You must be signed in to change notification settings - Fork 28
/
HtmlGenerator.cs
279 lines (242 loc) · 6.69 KB
/
HtmlGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
namespace ScribdMpubToEpubConverter
{
public sealed class UTF8StringWriter : StringWriter
{
public override Encoding Encoding
{
get { return Encoding.UTF8; }
}
}
struct HtmlTableColumn
{
public string Text { get; set; }
public string Style { get; set; }
}
struct HtmlAttribute
{
public string LocalName { get; set; }
public string Value { get; set; }
}
class HtmlGenerator
{
private XmlWriter Writer { get; set; }
public HtmlGenerator(XmlWriter writer, string title)
{
Writer = writer;
/*
* <?xml version="1.0" encoding="utf-8" standalone="no"?>
*/
Writer.WriteStartDocument(false);
/*
* <!DOCTYPE html
* PUBLIC
* "-//W3C//DTD XHTML 1.1//EN"
* "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
*/
Writer.WriteDocType("html", "-//W3C//DTD XHTML 1.1//EN", "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd", null);
/*
* <html>
*/
Writer.WriteStartElement("html", "http://www.w3.org/1999/xhtml");
/*
* <head>
*/
Writer.WriteStartElement("head");
/*
* <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>
*/
var styles_relative_path = EPUB2.EpubDirectories.OEBPS_Styles.Replace(
EPUB2.EpubDirectories.OEBPS,
"../"
);
Writer.WriteStartElement("link");
Writer.WriteAttributeString("href", styles_relative_path + "style.css");
Writer.WriteAttributeString("rel", "stylesheet");
Writer.WriteAttributeString("type", "text/css");
Writer.WriteEndElement();
/*
* <title>@title</title>
*/
Writer.WriteElementString("title", title);
Writer.WriteEndElement();
/*
* <body>
*/
Writer.WriteStartElement("body");
}
public void AddImage(string filename, string alt, bool should_center)
{
/*
* <div @should_center>
* <img src="../Images/@filename" alt="@alt" height="100%" />
* </div>
*/
Writer.WriteStartElement("div");
// Center image if needed
if (should_center)
Writer.WriteAttributeString("style", "text-align: center;");
Writer.WriteStartElement("img");
Writer.WriteAttributeString("src", "../Images/" + filename);
Writer.WriteAttributeString("alt", alt);
Writer.WriteAttributeString("height", "100%");
Writer.WriteEndElement();
Writer.WriteEndElement();
}
public void AddText(string element, string content, params HtmlAttribute[] attributes)
{
/*
* <@element @attributes...>@content</@element>
*/
Writer.WriteStartElement(element);
if (attributes != null)
{
foreach (var attribute in attributes)
{
// HACK! Don't add attributes with empty names.
if (attribute.LocalName == null)
continue;
Writer.WriteAttributeString(attribute.LocalName, attribute.Value);
}
}
Writer.WriteString(content);
Writer.WriteEndElement();
}
// NOTE: Only the attribute with the LocalName "href" will
// be added to the <a> element, all other will be added to the <p> element.
public void AddLink(string content, params HtmlAttribute[] attributes)
{
/*
* <p @attributes...>
* <a @attributes["href"]>@content</a>
* </p>
*/
Writer.WriteStartElement("p");
if (attributes != null)
{
foreach (var attribute in attributes)
{
// HACK! Don't add attributes with empty names.
if (attribute.LocalName == null)
continue;
// Don't add href attribute to <p>
if (attribute.LocalName == "href")
continue;
Writer.WriteAttributeString(attribute.LocalName, attribute.Value);
}
}
Writer.WriteStartElement("a");
if (attributes != null)
{
foreach (var attribute in attributes)
{
// HACK! Don't add attributes with empty names.
if (attribute.LocalName == null)
continue;
// Add only href attribute to <a>
if (attribute.LocalName != "href")
continue;
Writer.WriteAttributeString(attribute.LocalName, attribute.Value);
}
}
Writer.WriteString(content);
Writer.WriteEndElement();
Writer.WriteEndElement();
}
public void AddPageBreak()
{
/*
* <div style="page-break-before: always;" />
*/
Writer.WriteStartElement("div");
Writer.WriteAttributeString("style", "page-break-before: always;");
Writer.WriteEndElement();
}
public void AddBorder(double width, string style)
{
/*
* <div style="@style_attribute" />
*/
var style_attribute = "border-style: " + style +
"; border-color: rgb(0, 0, 0); border-top-width: 1px; width: " +
width.ToString() + ";";
Writer.WriteStartElement("div");
Writer.WriteAttributeString("style", style_attribute);
Writer.WriteEndElement();
}
public void AddHorizontalRule()
{
/*
* <hr/>
*/
Writer.WriteStartElement("hr");
Writer.WriteEndElement();
}
public void AddSpacer(double size)
{
/*
* <br @size;"/>
*/
Writer.WriteStartElement("br");
if (size != 1)
Writer.WriteAttributeString("style", "line-height: " + size);
Writer.WriteEndElement();
}
public void StartTable(int column_count)
{
/*
* <table style="@TableStyle">
* <colgroup>
* ...
* <col width="@average_colum_width@ />
* ...
* </colgroup>
*/
const string TableStyle = "table-layout: fixed; white-space: nowrap; border-collapse: collapse; border-spacing: 0px; margin: 5% auto;";
// Calculate average width in procentage
var average_column_width = 100.0 / column_count;
Writer.WriteStartElement("table");
Writer.WriteAttributeString("style", TableStyle);
Writer.WriteStartElement("colgroup");
for (int i = 0; i < column_count; ++i)
{
Writer.WriteStartElement("col");
Writer.WriteAttributeString("width", average_column_width.ToString("0.##") + "%");
Writer.WriteEndElement();
}
Writer.WriteEndElement();
}
public void AddTableRow(List<HtmlTableColumn> columns)
{
/*
* <tr>
* ...
* <td style="@column.Style">@column.Text</td>
* ...
* </tr>
*/
Writer.WriteStartElement("tr");
foreach (var column in columns)
{
Writer.WriteStartElement("td");
// Set style attribute, if there is a style present
if (column.Style != null)
Writer.WriteAttributeString("style", column.Style);
Writer.WriteString(column.Text);
Writer.WriteEndElement();
}
Writer.WriteEndElement();
}
public void EndTable()
{
/*
* </table>
*/
Writer.WriteEndElement();
AddPageBreak();
}
}
}