/
TOCHTMLWriter.cpp
147 lines (134 loc) · 4.82 KB
/
TOCHTMLWriter.cpp
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
/************************************************************************
**
** Copyright (C) 2016-2020 Kevin B. Hendricks, Stratford, Ontario, Canada
** Copyright (C) 2012 Dave Heiland
** Copyright (C) 2012 John Schember <john@nachtimwald.com>
**
** This file is part of Sigil.
**
** Sigil is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Sigil is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Sigil. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include <QtCore/QXmlStreamWriter>
#include "TOCHTMLWriter.h"
#include "sigil_constants.h"
#include "Misc/Utility.h"
const QString SGC_TOC_CSS_FILENAME = "sgc-toc.css";
TOCHTMLWriter::TOCHTMLWriter(const QString &toc_bookpath,
const QString &css_bookpath,
TOCModel::TOCEntry toc_root_entry,
const QString &title)
:
m_Writer(0),
m_TOCRootEntry(toc_root_entry),
m_TOCBookPath(toc_bookpath),
m_CSSBookPath(css_bookpath),
m_title(title)
{
}
TOCHTMLWriter::~TOCHTMLWriter()
{
if (m_Writer) {
delete m_Writer;
m_Writer = 0;
}
}
QString TOCHTMLWriter::WriteXML(const QString &version)
{
QString out;
// Use QXmlStreamWriter to ensure correct conversion of &, <, etc.
if (m_Writer) {
delete m_Writer;
m_Writer = 0;
}
m_Writer = new QXmlStreamWriter(&out);
m_Writer->writeStartDocument();
if (version.startsWith('2')) {
m_Writer->writeDTD("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n");
m_Writer->writeStartElement("html");
m_Writer->writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
m_Writer->writeCharacters("\n");
} else {
m_Writer->writeDTD("<!DOCTYPE html>\n");
m_Writer->writeStartElement("html");
m_Writer->writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
m_Writer->writeAttribute("xmlns:epub", "http://www.idpf.org/2007/ops");
m_Writer->writeCharacters("\n");
}
WriteHead();
WriteBody();
m_Writer->writeEndDocument();
return out;
}
void TOCHTMLWriter::WriteHead()
{
// Title
m_Writer->writeStartElement("head");
m_Writer->writeCharacters("\n");
m_Writer->writeTextElement("title", "Contents");
m_Writer->writeCharacters("\n");
// TOC
m_Writer->writeStartElement("link");
QString href = Utility::buildRelativePath(m_TOCBookPath, m_CSSBookPath);
href = Utility::URLEncodePath(href);
m_Writer->writeAttribute("href", href);
m_Writer->writeAttribute("rel", "stylesheet");
m_Writer->writeAttribute("type", "text/css");
m_Writer->writeEndElement();
m_Writer->writeCharacters("\n");
// End head
m_Writer->writeEndElement();
m_Writer->writeCharacters("\n");
}
void TOCHTMLWriter::WriteBody()
{
m_Writer->writeStartElement("body");
m_Writer->writeCharacters("\n");
// Page heading
m_Writer->writeStartElement("div");
m_Writer->writeAttribute("class", "sgc-toc-title");
m_Writer->writeCharacters(m_title);
m_Writer->writeEndElement();
m_Writer->writeCharacters("\n");
// Entries
WriteEntries(m_TOCRootEntry);
}
void TOCHTMLWriter::WriteEntries(TOCModel::TOCEntry parent_entry, int level)
{
foreach(TOCModel::TOCEntry entry, parent_entry.children) {
m_Writer->writeStartElement("div");
m_Writer->writeAttribute("class", "sgc-toc-level-" % QString::number(level));
m_Writer->writeCharacters("\n");
m_Writer->writeCharacters(" ");
m_Writer->writeStartElement("a");
// entry.target is now a full urlencoded bookpath that may have a fragment
QString href = entry.target;
// only process internal not external hrefs
if (href.indexOf(":") == -1) {
std::pair<QString, QString> pieces = Utility::parseRelativeHREF(entry.target);
QString fragment = pieces.second;
href = Utility::buildRelativePath(m_TOCBookPath, pieces.first);
href = Utility::buildRelativeHREF(href, pieces.second);
}
m_Writer->writeAttribute("href", href);
m_Writer->writeCharacters(entry.text);
m_Writer->writeEndElement();
m_Writer->writeCharacters("\n");
// Recursively write out subheadings
WriteEntries(entry, level + 1);
m_Writer->writeEndElement();
m_Writer->writeCharacters("\n");
}
}