Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
templategenerator.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef TEMPLATEGENERATOR_H
5#define TEMPLATEGENERATOR_H
6
10#include "outputcontext.h"
11#include "filesystem/fileresolver.h"
12
13#include <memory>
14#include <optional>
15
16QT_BEGIN_NAMESPACE
17
18class Aggregate;
19class CodeMarker;
20class HrefResolver;
21class LinkResolver;
22class QDocDatabase;
23namespace IR { struct AllMembersIR; struct Document; }
24
25/*!
26 \class TemplateGenerator
27 \internal
28 \brief Generates documentation using external templates and a pre-built IR.
29
30 TemplateGenerator implements OutputProducer and DocumentationHandler to
31 generate documentation without inheriting from Generator. It uses
32 DocumentationTraverser for tree traversal and delegates content generation
33 to templates via the IR system.
34
35 \section1 Architecture
36
37 The generator follows a composition-based design:
38 \list
39 \li \b{OutputProducer}: Lifecycle interface (prepare/produce/finalize)
40 \li \b{DocumentationHandler}: Content generation callbacks for traverser
41 \li \b{DocumentationTraverser}: Shared tree traversal logic
42 \li \b{DocumentWriter}: Output abstraction (file or string for tests)
43 \endlist
44
45 \sa DocumentationTraverser, DocumentationHandler, OutputProducer
46*/
47class TemplateGenerator : public OutputProducer, public DocumentationHandler
48{
49public:
50 explicit TemplateGenerator(FileResolver &fileResolver, QDocDatabase &qdb,
51 const QString &format = QString());
52 ~TemplateGenerator() override;
53
54 // === OutputProducer interface ===
55 void prepare() override;
56 void produce() override;
57 void finalize() override;
58 [[nodiscard]] QString format() const override;
59
60 // === DocumentationHandler interface ===
61 void beginDocument(const QString &fileName) override;
62 void endDocument() override;
63 [[nodiscard]] QString fileName(const Node *node) const override;
64 void generateCollectionNode(CollectionNode *cn, CodeMarker *marker) override;
66 void generatePageNode(PageNode *pn, CodeMarker *marker) override;
67 void generateCppReferencePage(Aggregate *aggregate, CodeMarker *marker) override;
68 void generateQmlTypePage(QmlTypeNode *qcn, CodeMarker *marker) override;
69 void generateProxyPage(Aggregate *aggregate, CodeMarker *marker) override;
70 void mergeCollections(CollectionNode *cn) override;
71
72 // Public accessors for configuration
73 [[nodiscard]] QString fileExtension() const;
74
75private:
76 // Render phase: Format IR according to templates.
77 void renderDocument(const IR::Document &ir, const QString &templateBaseName);
78 void renderJson(const QJsonObject &json, const QString &templateBaseName);
79 void generateMemberListingPage(const Node *node, const IR::AllMembersIR &allMembers);
80
81 // Include resolution for Inja {% include %} directives.
82 [[nodiscard]] QString resolveInclude(const QString &name) const;
83
84 // Filename computation (adapted from Generator)
85 [[nodiscard]] QString fileBase(const Node *node) const;
86
87 // Creates the production writer (FileDocumentWriter).
88 void createDefaultWriter();
89
90 FileResolver &m_fileResolver;
91 QDocDatabase &m_qdb;
92 QString m_format;
93 std::unique_ptr<FileDocumentWriter> m_writer;
94 std::optional<OutputContext> m_context;
95 QString m_templateDir;
96 QString m_fileExtension = QStringLiteral("html");
97 std::unique_ptr<HrefResolver> m_hrefResolver;
98 std::unique_ptr<LinkResolver> m_linkResolver;
99 bool m_emitStylesheet = false;
100 QString m_stylesheetName;
101
102 void copyAssets();
103 void resolveImagePaths(IR::Document &ir);
104
105 // For testing: allow injection of mock writers
106 friend class TemplateGeneratorTest;
107};
108
109QT_END_NAMESPACE
110
111#endif // TEMPLATEGENERATOR_H
A class for holding the members of a collection of doc pages.
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:85
InclusionPolicy createInclusionPolicy() const
Definition config.cpp:1608
Interface handling documentation generation during tree traversal.
Traverses the node tree and dispatches to a handler for documentation generation.
void traverse(Node *root, DocumentationHandler &handler)
Encapsulate the logic that QDoc uses to find files whose path is provided by the user and that are re...
static bool noLinkErrors()
Definition generator.h:85
static QmlTypeNode * qmlTypeContext()
Definition generator.h:91
static bool autolinkErrors()
Definition generator.h:86
Assembles IR Documents from pre-extracted metadata.
Definition builder.h:15
Document buildPageIR(PageMetadata pm) const
Definition builder.cpp:59
Singleton registry for discovering output producers by format.
void registerProducer(OutputProducer *producer)
Registers producer with this registry.
void unregisterProducer(OutputProducer *producer)
Unregisters producer from this registry.
static OutputProducerRegistry & instance()
Returns the singleton registry instance.
A PageNode is a Node that generates a documentation page.
Definition pagenode.h:19
This class provides exclusive access to the qdoc database, which consists of a forrest of trees and a...
NamespaceNode * primaryTreeRoot()
Returns a pointer to the root node of the primary tree.
void mergeCollections(CollectionNode *c)
Finds all the collection nodes with the same name and type as c and merges their members into the mem...
Generates documentation using external templates and a pre-built IR.
void generateProxyPage(Aggregate *aggregate, CodeMarker *marker) override
void endDocument() override
void beginDocument(const QString &fileName) override
void generateCppReferencePage(Aggregate *aggregate, CodeMarker *marker) override
void generatePageNode(PageNode *pn, CodeMarker *marker) override
void finalize() override
Finalizes output production.
void prepare() override
Prepares the producer for an output run.
QString fileExtension() const
TemplateGenerator(FileResolver &fileResolver, QDocDatabase &qdb, const QString &format=QString())
QString fileName(const Node *node) const override
void produce() override
Produces documentation output.
void generateCollectionNode(CollectionNode *cn, CodeMarker *marker) override
QString format() const override
Returns the format identifier for this producer (e.g., "HTML", "DocBook", "template").
void mergeCollections(CollectionNode *cn) override
void generateQmlTypePage(QmlTypeNode *qcn, CodeMarker *marker) override
void generateGenericCollectionPage(CollectionNode *cn, CodeMarker *marker) override
Definition builder.cpp:14
std::optional< IR::AllMembersIR > extractAllMembersIR(const PageNode *pn, const HrefResolver *hrefResolver)
IR::PageMetadata extractPageMetadata(const PageNode *pn, const HrefResolver *hrefResolver)
Combined button and popup list for selecting options.
Intermediate representation of the all-members listing page.
Definition member.h:112
Intermediate representation for a documentation topic.
Definition document.h:81
The Node class is the base class for all the nodes in QDoc's parse tree.
bool hasFileNameBase() const
Returns true if the node's file name base has been set.
Definition node.h:167
Genus genus() const override
Returns this node's Genus.
Definition node.h:85
virtual bool isPageNode() const
Returns true if this node represents something that generates a documentation page.
Definition node.h:148
virtual bool isTextPageNode() const
Returns true if the node is a PageNode but not an Aggregate.
Definition node.h:153
Aggregate * parent() const
Returns the node's parent pointer.
Definition node.h:208
virtual bool isCollectionNode() const
Returns true if this is an instance of CollectionNode.
Definition node.h:144
Bundles output configuration without static variables.
static QString nodeTypeKey(const Node *node)
static void resolveDocumentLinks(LinkResolver *resolver, IR::Document &ir, const Node *relative)