~ubuntu-branches/ubuntu/wily/libwpd/wily-proposed

« back to all changes in this revision

Viewing changes to src/conv/html/HtmlDocumentGenerator.cpp

  • Committer: Package Import Robot
  • Author(s): Rene Engelhard
  • Date: 2014-08-08 00:35:26 UTC
  • mfrom: (11.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20140808003526-7pku3062w50cnsod
Tags: 0.10.0-2
* upload to unstable

* fix debian/copyright for MPL-2.0 | LGPL-2.1+ dual-license 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2
 
/* libwpd
3
 
 * Version: MPL 2.0 / LGPLv2.1+
4
 
 *
5
 
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 
 *
9
 
 * Major Contributor(s):
10
 
 * Copyright (C) 2002-2004 William Lachance (wrlach@gmail.com)
11
 
 * Copyright (C) 2002 Marc Maurer (uwog@uwog.net)
12
 
 * Copyright (C) 2004 Fridrich Strba (fridrich.strba@bluewin.ch)
13
 
 * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com)
14
 
 *
15
 
 * For minor contributions see the git repository.
16
 
 *
17
 
 * Alternatively, the contents of this file may be used under the terms
18
 
 * of the GNU Lesser General Public License Version 2.1 or later
19
 
 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
20
 
 * applicable instead of those above.
21
 
 *
22
 
 * For further information visit http://libwpd.sourceforge.net
23
 
 */
24
 
 
25
 
/* "This product is not manufactured, approved, or supported by
26
 
 * Corel Corporation or Corel Corporation Limited."
27
 
 */
28
 
 
29
 
#include <stdio.h>
30
 
#include <iostream>
31
 
#include "HtmlDocumentGenerator.h"
32
 
 
33
 
// use the BELL code to represent a TAB for now
34
 
#define UCS_TAB 0x0009
35
 
 
36
 
HtmlDocumentGenerator::HtmlDocumentGenerator() :
37
 
        m_ignore(false),
38
 
        m_pOutputStream(&std::cout),
39
 
        m_footNotesStream(),
40
 
        m_endNotesStream(),
41
 
        m_commentsStream(),
42
 
        m_textBoxesStream(),
43
 
        m_dummyStream(),
44
 
        m_footNotesCount(0),
45
 
        m_endNotesCount(0),
46
 
        m_commentsCount(0),
47
 
        m_textBoxesCount(0),
48
 
        m_commentNumber(1),
49
 
        m_textBoxNumber(1)
50
 
{
51
 
}
52
 
 
53
 
HtmlDocumentGenerator::~HtmlDocumentGenerator()
54
 
{
55
 
}
56
 
 
57
 
void HtmlDocumentGenerator::setDocumentMetaData(const WPXPropertyList &propList)
58
 
{
59
 
        if (propList["meta:initial-creator"])
60
 
                *m_pOutputStream << "<meta name=\"author\" content=\"" << propList["meta:initial-creator"]->getStr().cstr() << "\">" << std::endl;
61
 
        if (propList["dc:creator"])
62
 
                *m_pOutputStream << "<meta name=\"typist\" content=\"" << propList["dc:creator"]->getStr().cstr() << "\">" << std::endl;
63
 
        if (propList["dc:subject"])
64
 
                *m_pOutputStream << "<meta name=\"subject\" content=\"" << propList["dc:subject"]->getStr().cstr() << "\">" << std::endl;
65
 
        if (propList["dc:publisher"])
66
 
                *m_pOutputStream << "<meta name=\"publisher\" content=\"" << propList["dc:publisher"]->getStr().cstr() << "\">" << std::endl;
67
 
        if (propList["meta:keyword"])
68
 
                *m_pOutputStream << "<meta name=\"keywords\" content=\"" << propList["meta:keyword"]->getStr().cstr() << "\">" << std::endl;
69
 
        if (propList["dc:language"])
70
 
                *m_pOutputStream << "<meta name=\"language\" content=\"" << propList["dc:language"]->getStr().cstr() << "\">" << std::endl;
71
 
        if (propList["dc:description"])
72
 
                *m_pOutputStream << "<meta name=\"abstract\" content=\"" << propList["dc:description"]->getStr().cstr() << "\">" << std::endl;
73
 
        if (propList["libwpd:descriptive-name"])
74
 
        {
75
 
                *m_pOutputStream << "<meta name=\"descriptive-name\" content=\"" << propList["libwpd:descriptive-name"]->getStr().cstr() << "\">" << std::endl;
76
 
                *m_pOutputStream << "<title>" << propList["libwpd:descriptive-name"]->getStr().cstr() << "</title>" << std::endl;
77
 
        }
78
 
        if (propList["libwpd:descriptive-type"])
79
 
                *m_pOutputStream << "<meta name=\"descriptive-type\" content=\"" << propList["libwpd:descriptive-type"]->getStr().cstr() << "\">" << std::endl;
80
 
}
81
 
 
82
 
void HtmlDocumentGenerator::startDocument()
83
 
{
84
 
        *m_pOutputStream << "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">" << std::endl;
85
 
        *m_pOutputStream << "<html>" << std::endl;
86
 
        *m_pOutputStream << "<head>" << std::endl;
87
 
        *m_pOutputStream << "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" >" << std::endl;
88
 
        *m_pOutputStream << "</head>" << std::endl;
89
 
        *m_pOutputStream << "<body>" << std::endl;
90
 
}
91
 
 
92
 
void HtmlDocumentGenerator::endDocument()
93
 
{
94
 
        if (m_footNotesStream.str().length())
95
 
        {
96
 
                *m_pOutputStream << "<p><b>FOOTNOTES</b></p>" << std::endl;
97
 
                *m_pOutputStream << m_footNotesStream.str() << std::endl;
98
 
        }
99
 
        if (m_endNotesStream.str().length())
100
 
        {
101
 
                *m_pOutputStream << "<p><b>ENDNOTES</b></p>" << std::endl;
102
 
                *m_pOutputStream << m_endNotesStream.str() << std::endl;
103
 
        }
104
 
        if (m_commentsStream.str().length())
105
 
        {
106
 
                *m_pOutputStream << "<p><b>COMMENTS AND ANNOTATIONS</b></p>" << std::endl;
107
 
                *m_pOutputStream << m_commentsStream.str() << std::endl;
108
 
        }
109
 
        if (m_textBoxesStream.str().length())
110
 
        {
111
 
                *m_pOutputStream << "<p><b>TEXT BOXES</b></p>" << std::endl;
112
 
                *m_pOutputStream << m_textBoxesStream.str() << std::endl;
113
 
        }
114
 
        *m_pOutputStream << "</body>" << std::endl;
115
 
        *m_pOutputStream << "</html>" << std::endl;
116
 
}
117
 
 
118
 
void HtmlDocumentGenerator::openHeader(const WPXPropertyList & /* propList */)
119
 
{
120
 
        m_ignore = true;
121
 
}
122
 
 
123
 
void HtmlDocumentGenerator::closeHeader()
124
 
{
125
 
        m_ignore = false;
126
 
}
127
 
 
128
 
 
129
 
void HtmlDocumentGenerator::openFooter(const WPXPropertyList & /* propList */)
130
 
{
131
 
        m_ignore = true;
132
 
}
133
 
 
134
 
void HtmlDocumentGenerator::closeFooter()
135
 
{
136
 
        m_ignore = false;
137
 
}
138
 
 
139
 
void HtmlDocumentGenerator::openParagraph(const WPXPropertyList &propList, const WPXPropertyListVector & /* tabStops */)
140
 
{
141
 
        if (!m_ignore)
142
 
        {
143
 
                *m_pOutputStream << "<p style=\"";
144
 
 
145
 
                if (propList["fo:text-align"])
146
 
                {
147
 
 
148
 
                        if (propList["fo:text-align"]->getStr() == WPXString("end")) // stupid OOo convention..
149
 
                                *m_pOutputStream << "text-align:right;";
150
 
                        else
151
 
                                *m_pOutputStream << "text-align:" << propList["fo:text-align"]->getStr().cstr() << ";";
152
 
                }
153
 
                if (propList["fo:text-indent"])
154
 
                        *m_pOutputStream << "text-indent:" << propList["fo:text-indent"]->getStr().cstr() << ";";
155
 
 
156
 
                if (propList["fo:line-height"] && propList["fo:line-height"]->getDouble() != 1.0)
157
 
                        *m_pOutputStream << "line-height:" << propList["fo:line-height"]->getDouble() << ";";
158
 
                *m_pOutputStream << "\">";
159
 
        }
160
 
}
161
 
 
162
 
void HtmlDocumentGenerator::closeParagraph()
163
 
{
164
 
        if (!m_ignore)
165
 
        {
166
 
                *m_pOutputStream << "</p>" << std::endl;
167
 
        }
168
 
}
169
 
 
170
 
void HtmlDocumentGenerator::openSpan(const WPXPropertyList &propList)
171
 
{
172
 
        if (!m_ignore)
173
 
        {
174
 
 
175
 
                *m_pOutputStream << "<span style=\"";
176
 
                if (propList["style:font-name"])
177
 
                        *m_pOutputStream << "font-family: \'" << propList["style:font-name"]->getStr().cstr() << "\';";
178
 
                if (propList["fo:font-size"])
179
 
                        *m_pOutputStream << "font-size: " << propList["fo:font-size"]->getStr().cstr() << ";";
180
 
                if (propList["fo:font-weight"])
181
 
                        *m_pOutputStream << "font-weight: " << propList["fo:font-weight"]->getStr().cstr() << ";";
182
 
                if (propList["fo:font-style"])
183
 
                        *m_pOutputStream << "font-style: " << propList["fo:font-style"]->getStr().cstr() << ";";
184
 
                if (propList["style:text-crossing-out"] && propList["style:text-crossing-out"]->getStr() == WPXString("single-line"))
185
 
                        *m_pOutputStream << "text-decoration:line-through;";
186
 
                if (propList["style:text-underline"]) // don't know if double underline is possible
187
 
                        *m_pOutputStream << "text-decoration:underline;";
188
 
                if (propList["style:text-blinking"])
189
 
                        *m_pOutputStream << "text-decoration:blink;";
190
 
                if (propList["fo:color"])
191
 
                        *m_pOutputStream << "color:" << propList["fo:color"]->getStr().cstr() << ";";
192
 
                if (propList["style:text-background-color"])
193
 
                        *m_pOutputStream << "background-color:" << propList["style:text-background-color"]->getStr().cstr() << ";";
194
 
 
195
 
                *m_pOutputStream << "\">";
196
 
        }
197
 
}
198
 
 
199
 
void HtmlDocumentGenerator::closeSpan()
200
 
{
201
 
        if (!m_ignore)
202
 
        {
203
 
                *m_pOutputStream << "</span>" << std::endl;
204
 
        }
205
 
}
206
 
 
207
 
void HtmlDocumentGenerator::insertTab()
208
 
{
209
 
        if (!m_ignore)
210
 
        {
211
 
                // Does not have a lot of effect since tabs in html are ignorable white-space
212
 
                *m_pOutputStream << "\t";
213
 
        }
214
 
}
215
 
 
216
 
void HtmlDocumentGenerator::insertLineBreak()
217
 
{
218
 
        if (!m_ignore)
219
 
        {
220
 
 
221
 
                *m_pOutputStream << "<br>" << std::endl;
222
 
        }
223
 
}
224
 
 
225
 
void HtmlDocumentGenerator::insertText(const WPXString &text)
226
 
{
227
 
        if (!m_ignore)
228
 
        {
229
 
 
230
 
                WPXString tempUTF8(text, true);
231
 
                *m_pOutputStream << tempUTF8.cstr();
232
 
        }
233
 
}
234
 
 
235
 
void HtmlDocumentGenerator::insertSpace()
236
 
{
237
 
        if (!m_ignore)
238
 
        {
239
 
                *m_pOutputStream << "&nbsp;";
240
 
        }
241
 
}
242
 
 
243
 
void HtmlDocumentGenerator::openOrderedListLevel(const WPXPropertyList & /* propList */)
244
 
{
245
 
        if (!m_ignore)
246
 
        {
247
 
                *m_pOutputStream << "<ol>" << std::endl;
248
 
        }
249
 
}
250
 
 
251
 
void HtmlDocumentGenerator::closeOrderedListLevel()
252
 
{
253
 
        if (!m_ignore)
254
 
        {
255
 
                *m_pOutputStream << "</ol>" << std::endl;
256
 
        }
257
 
}
258
 
 
259
 
void HtmlDocumentGenerator::openUnorderedListLevel(const WPXPropertyList & /* propList */)
260
 
{
261
 
        if (!m_ignore)
262
 
        {
263
 
                *m_pOutputStream << "<ul>" << std::endl;
264
 
        }
265
 
}
266
 
 
267
 
void HtmlDocumentGenerator::closeUnorderedListLevel()
268
 
{
269
 
        if (!m_ignore)
270
 
        {
271
 
                *m_pOutputStream << "</ul>" << std::endl;
272
 
        }
273
 
}
274
 
 
275
 
 
276
 
void HtmlDocumentGenerator::openListElement(const WPXPropertyList & /* propList */, const WPXPropertyListVector &/* tabStops */)
277
 
{
278
 
        if (!m_ignore)
279
 
        {
280
 
                *m_pOutputStream << "<li>";
281
 
        }
282
 
}
283
 
 
284
 
void HtmlDocumentGenerator::closeListElement()
285
 
{
286
 
        if (!m_ignore)
287
 
        {
288
 
                *m_pOutputStream << "</li>" << std::endl;
289
 
        }
290
 
}
291
 
 
292
 
void HtmlDocumentGenerator::openFootnote(const WPXPropertyList &propList)
293
 
{
294
 
        if (!m_ignore)
295
 
        {
296
 
                if (!m_footNotesCount++)
297
 
                {
298
 
                        if (propList["libwpd:number"])
299
 
                                *m_pOutputStream << "<sup>(footnote: " << propList["libwpd:number"]->getStr().cstr() << ")</sup>";
300
 
                        m_pOutputStream = &m_footNotesStream;
301
 
                        // Cheesey hack..
302
 
                        if (propList["libwpd:number"])
303
 
                                *m_pOutputStream << "<p>" << propList["libwpd:number"]->getStr().cstr() << ":</p>";
304
 
                        else
305
 
                                *m_pOutputStream << "<p/>";
306
 
                }
307
 
                else
308
 
                        m_pOutputStream = &m_dummyStream;
309
 
        }
310
 
}
311
 
 
312
 
void HtmlDocumentGenerator::closeFootnote()
313
 
{
314
 
        if (!m_ignore)
315
 
        {
316
 
                if (!(--m_footNotesCount))
317
 
                {
318
 
                        *m_pOutputStream << "<p/>" << std::endl;
319
 
                        m_pOutputStream = &std::cout;
320
 
                }
321
 
        }
322
 
}
323
 
 
324
 
void HtmlDocumentGenerator::openEndnote(const WPXPropertyList &propList)
325
 
{
326
 
        if (!m_ignore)
327
 
        {
328
 
                if (!m_endNotesCount++)
329
 
                {
330
 
                        if (propList["libwpd:number"])
331
 
                                *m_pOutputStream << "<sup>(endnote: " << propList["libwpd:number"]->getStr().cstr() << ")</sup>";
332
 
                        m_pOutputStream = &m_footNotesStream;
333
 
                        // Cheesey hack..
334
 
                        if (propList["libwpd:number"])
335
 
                                *m_pOutputStream << "<p>" << propList["libwpd:number"]->getStr().cstr() << ":</p>";
336
 
                        else
337
 
                                *m_pOutputStream << "<p/>";
338
 
                }
339
 
                else
340
 
                        m_pOutputStream = &m_dummyStream;
341
 
        }
342
 
}
343
 
 
344
 
void HtmlDocumentGenerator::closeEndnote()
345
 
{
346
 
        if (!m_ignore)
347
 
        {
348
 
                if (!(--m_endNotesCount))
349
 
                {
350
 
                        *m_pOutputStream << "<p/>" << std::endl;
351
 
                        m_pOutputStream = &std::cout;
352
 
                }
353
 
        }
354
 
}
355
 
 
356
 
void HtmlDocumentGenerator::openComment(const WPXPropertyList & /*propList*/)
357
 
{
358
 
        if (!m_ignore)
359
 
        {
360
 
                if (!m_commentsCount++)
361
 
                {
362
 
                        *m_pOutputStream << "<sup>(comment: " << m_commentNumber << ")</sup>";
363
 
                        m_pOutputStream = &m_commentsStream;
364
 
                        *m_pOutputStream << "<p>Comment " << m_commentNumber++ << ":</p>" << std::endl;
365
 
                        *m_pOutputStream << "<p/>";
366
 
                }
367
 
                else
368
 
                        m_pOutputStream = &m_dummyStream;
369
 
        }
370
 
}
371
 
 
372
 
void HtmlDocumentGenerator::closeComment()
373
 
{
374
 
        if (!m_ignore)
375
 
        {
376
 
                if (!(--m_commentsCount))
377
 
                {
378
 
                        *m_pOutputStream << "<p/>" << std::endl;
379
 
                        m_pOutputStream = &std::cout;
380
 
                }
381
 
        }
382
 
}
383
 
 
384
 
void HtmlDocumentGenerator::openTextBox(const WPXPropertyList & /*propList*/)
385
 
{
386
 
        if (!m_ignore)
387
 
        {
388
 
                if (!m_textBoxesCount++)
389
 
                {
390
 
                        *m_pOutputStream << "<sup>(text box: " << m_textBoxNumber << ")</sup>";
391
 
                        m_pOutputStream = &m_commentsStream;
392
 
                        *m_pOutputStream << "<p>Text Box " << m_textBoxNumber++ << ":</p>" << std::endl;
393
 
                        m_pOutputStream = &m_textBoxesStream;
394
 
                        *m_pOutputStream << "<p/>";
395
 
                }
396
 
                else
397
 
                        m_pOutputStream = &m_dummyStream;
398
 
        }
399
 
}
400
 
 
401
 
void HtmlDocumentGenerator::closeTextBox()
402
 
{
403
 
        if (!m_ignore)
404
 
        {
405
 
                if (!(--m_textBoxesCount))
406
 
                {
407
 
                        *m_pOutputStream << "<p/>" << std::endl;
408
 
                        m_pOutputStream = &std::cout;
409
 
                }
410
 
        }
411
 
}
412
 
 
413
 
void HtmlDocumentGenerator::openTable(const WPXPropertyList & /* propList */, const WPXPropertyListVector & /* columns */)
414
 
{
415
 
        if (!m_ignore)
416
 
        {
417
 
                *m_pOutputStream << "<table border=\"1\">" << std::endl;
418
 
                *m_pOutputStream << "<tbody>" << std::endl;
419
 
        }
420
 
}
421
 
 
422
 
void HtmlDocumentGenerator::openTableRow(const WPXPropertyList & /* propList */)
423
 
{
424
 
        if (!m_ignore)
425
 
        {
426
 
                *m_pOutputStream << "<tr>" << std::endl;
427
 
        }
428
 
}
429
 
 
430
 
void HtmlDocumentGenerator::closeTableRow()
431
 
{
432
 
        if (!m_ignore)
433
 
        {
434
 
                *m_pOutputStream << "</tr>" << std::endl;
435
 
        }
436
 
}
437
 
 
438
 
void HtmlDocumentGenerator::openTableCell(const WPXPropertyList &propList)
439
 
{
440
 
        if (!m_ignore)
441
 
        {
442
 
                *m_pOutputStream << "<td style=\"";
443
 
                if (propList["fo:background-color"])
444
 
                        *m_pOutputStream << "background-color:" << propList["fo:background-color"]->getStr().cstr() << ";";
445
 
 
446
 
                *m_pOutputStream << "\" ";
447
 
 
448
 
                if (propList["table:number-columns-spanned"])
449
 
                        *m_pOutputStream << "colspan=\"" << propList["table:number-columns-spanned"]->getInt() << "\" ";
450
 
                if (propList["table:number-rows-spanned"])
451
 
                        *m_pOutputStream << "rowspan=\"" << propList["table:number-rows-spanned"]->getInt() << "\" ";
452
 
 
453
 
                *m_pOutputStream << ">" << std::endl;
454
 
        }
455
 
}
456
 
 
457
 
void HtmlDocumentGenerator::closeTableCell()
458
 
{
459
 
        if (!m_ignore)
460
 
        {
461
 
                *m_pOutputStream << "</td>" << std::endl;
462
 
        }
463
 
}
464
 
 
465
 
void HtmlDocumentGenerator::closeTable()
466
 
{
467
 
        if (!m_ignore)
468
 
        {
469
 
                *m_pOutputStream << "</tbody>" << std::endl;
470
 
                *m_pOutputStream << "</table>" << std::endl;
471
 
        }
472
 
}
473
 
/* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */