11#include <QtCore/qfile.h>
12#include <QtCore/qxmlstream.h>
16using namespace Qt::StringLiterals;
19
20
21
22
23
24
25
29
30
31
32
41
42
43
44
45
46void TOCWriter::generateTOC(
const QString &fileName,
const QString &indexTitle)
48 if (indexTitle.isEmpty())
51 auto *root = m_qdb->findNodeForTarget(indexTitle,
nullptr);
56 if (!getEntries(root))
60 if (!file.open(QFile::WriteOnly)) {
61 Location().error(
"Failed to open %1 for writing"_L1.arg(fileName));
65 QXmlStreamWriter writer(&file);
66 writer.setAutoFormatting(
true);
67 writer.writeStartDocument();
68 writer.writeStartElement(u"section"_s);
69 writer.writeAttribute(u"ref"_s, m_gen->fullDocumentLocation(root));
70 writer.writeAttribute(u"title"_s, indexTitle);
71 writeEntry(writer, root);
72 writer.writeEndElement();
73 writer.writeEndDocument();
79
80
81
82void TOCWriter::writeEntry(QXmlStreamWriter &writer,
const Node *node)
84 const auto entries{getEntries(node)};
89 constexpr int maxDepth{16};
91 if (++m_recursionDepth > maxDepth) {
92 Location().error(
"Maximum nesting level (%1) exceeded "
93 "when writing table of contents for '%2'"_L1
94 .arg(QString::number(maxDepth), m_project));
96 for (
const auto &e : *entries) {
97 writer.writeStartElement(u"section"_s);
98 writer.writeAttribute(u"ref"_s, m_gen->fullDocumentLocation(e.first));
99 writer.writeAttribute(u"title"_s, e.second);
100 writeEntry(writer, e.first);
101 writer.writeEndElement();
108
109
110
111std::optional<TOCWriter::TitledNodeList>
TOCWriter::getEntries(
const Node *node)
const
119 TitledNodeList result;
124 if (
const auto *target = m_qdb->findNodeForAtom(atom,
nullptr, ref))
125 result.append(std::make_pair(target, atom->linkText()));
126 else if (!Generator::noLinkErrors())
127 node->doc().location().warning(
"Can't link to '%1'"_L1.arg(atom->string()));
131 if (result.isEmpty())
The Atom class is the fundamental unit for representing documents internally.
AtomType type() const
Return the type of this atom.
const Atom * next() const
Return the next atom in the atom list.
const Atom * next(AtomType t) const
Return the next Atom in the list if it is of AtomType t.
const Text & body() const
This class provides exclusive access to the qdoc database, which consists of a forrest of trees and a...
static QDocDatabase * qdocDB()
Creates the singleton.
Table of contents writer.
const Atom * firstAtom() const
The Node class is the base class for all the nodes in QDoc's parse tree.
const Doc & doc() const
Returns a reference to the node's Doc data member.