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 <QtCore/QStringList>
14
15#include <functional>
16#include <memory>
17#include <optional>
18
19QT_BEGIN_NAMESPACE
20
21class Aggregate;
22class CatalogEntrySource;
23class CodeMarker;
24class HrefResolver;
25class LinkResolver;
26class QDocDatabase;
27namespace IR { struct AllMembersIR; struct Document; class ListExpander; }
28
29/*!
30 \class TemplateGenerator
31 \internal
32 \brief Generates documentation using external templates and a pre-built IR.
33
34 TemplateGenerator implements OutputProducer and DocumentationHandler to
35 generate documentation without inheriting from Generator. It uses
36 DocumentationTraverser for tree traversal and delegates content generation
37 to templates via the IR system.
38
39 \section1 Architecture
40
41 The generator follows a composition-based design:
42 \list
43 \li \b{OutputProducer}: Lifecycle interface (prepare/produce/finalize)
44 \li \b{DocumentationHandler}: Content generation callbacks for traverser
45 \li \b{DocumentationTraverser}: Shared tree traversal logic
46 \li \b{DocumentWriter}: Output abstraction (file or string for tests)
47 \endlist
48
49 \sa DocumentationTraverser, DocumentationHandler, OutputProducer
50*/
52{
53public:
54 using RenderFailureHandler = std::function<void(const QString &message)>;
55
56 explicit TemplateGenerator(FileResolver &fileResolver, QDocDatabase &qdb,
57 const QString &format, RenderFailureHandler reportFailure);
58 ~TemplateGenerator() override;
59
60 // === OutputProducer interface ===
61 void prepare() override;
62 void produce() override;
63 void finalize() override;
64 [[nodiscard]] QString format() const override;
65
66 // === DocumentationHandler interface ===
67 void beginDocument(const QString &fileName) override;
68 void endDocument() override;
69 [[nodiscard]] QString fileName(const Node *node) const override;
70 void generateCollectionNode(CollectionNode *cn, CodeMarker *marker) override;
72 void generatePageNode(PageNode *pn, CodeMarker *marker) override;
73 void generateCppReferencePage(Aggregate *aggregate, CodeMarker *marker) override;
74 void generateQmlTypePage(QmlTypeNode *qcn, CodeMarker *marker) override;
75 void generateProxyPage(Aggregate *aggregate, CodeMarker *marker) override;
76 void mergeCollections(CollectionNode *cn) override;
77
78 // Public accessors for configuration
79 [[nodiscard]] QString fileExtension() const;
80
81private:
82 // Render phase: Format IR according to templates.
83 void renderDocument(const IR::Document &ir, const QString &templateBaseName);
84 void renderJson(const QJsonObject &json, const QString &templateBaseName);
85
86 [[nodiscard]] QString currentDocumentName() const;
87 void generateMemberListingPage(const Node *node, const IR::AllMembersIR &allMembers);
88 void generateObsoleteMembersPage(const Aggregate *aggregate);
89
90 // Include resolution for Inja {% include %} directives.
91 [[nodiscard]] std::optional<QString> resolveInclude(const QString &name) const;
92
93 // Filename computation (adapted from Generator)
94 [[nodiscard]] QString fileBase(const Node *node) const;
95
96 // Creates the production writer (FileDocumentWriter).
97 void createDefaultWriter();
98
99 FileResolver &m_fileResolver;
100 QDocDatabase &m_qdb;
101 QString m_format;
102 RenderFailureHandler m_reportFailure;
103 std::unique_ptr<FileDocumentWriter> m_writer;
104 QStringList m_documentNames;
105 std::optional<OutputContext> m_context;
106 QString m_templateDir;
107 QString m_fileExtension = QStringLiteral("html");
108 std::unique_ptr<HrefResolver> m_hrefResolver;
109 std::unique_ptr<LinkResolver> m_linkResolver;
110 std::unique_ptr<CatalogEntrySource> m_catalogSource;
111 std::unique_ptr<IR::ListExpander> m_listExpander;
112 bool m_emitStylesheet = false;
113 QString m_stylesheetName;
114
115 void copyAssets();
116 void resolveImagePaths(IR::Document &ir);
117
118 // For testing: allow injection of mock writers
119 friend class TemplateGeneratorTest;
120};
121
122QT_END_NAMESPACE
123
124#endif // TEMPLATEGENERATOR_H
A class for holding the members of a collection of doc pages.
contains all the information for a single config variable in a .qdocconf file.
Definition config.h:43
The Config class contains the configuration variables for controlling how qdoc produces documentation...
Definition config.h:95
InclusionPolicy createInclusionPolicy() const
Definition config.cpp:1621
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:86
static QmlTypeNode * qmlTypeContext()
Definition generator.h:92
static bool autolinkErrors()
Definition generator.h:87
Assembles IR Documents from pre-extracted metadata.
Definition builder.h:15
Document buildPageIR(PageMetadata pm) const
Definition builder.cpp:196
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.
Interface for documentation output producers.
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...
A class for creating vectors of collections for documentation.
Definition sections.h:80
Sections(const Aggregate *aggregate)
This constructor builds the section vectors based on the type of the aggregate node.
Definition sections.cpp:372
bool hasObsoleteMembers(SectionPtrVector *summary_spv, SectionPtrVector *details_spv) const
Returns true if any sections in this object contain obsolete members.
Definition sections.cpp:967
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
std::function< void(const QString &message)> RenderFailureHandler
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
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
TemplateGenerator(FileResolver &fileResolver, QDocDatabase &qdb, const QString &format, RenderFailureHandler reportFailure)
Constructs a producer for format using fileResolver and qdb.
void generateGenericCollectionPage(CollectionNode *cn, CodeMarker *marker) override
Definition builder.cpp:14
std::optional< IR::AllMembersIR > extractAllMembersIR(const PageNode *pn, const HrefResolver *hrefResolver)
Combined button and popup list for selecting options.
QList< const Section * > SectionPtrVector
Definition sections.h:77
Intermediate representation of the all-members listing page.
Definition member.h:119
Intermediate representation for a documentation topic.
Definition document.h:201
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:169
virtual bool isPageNode() const
Returns true if this node represents something that generates a documentation page.
Definition node.h:150
virtual bool isTextPageNode() const
Returns true if the node is a PageNode but not an Aggregate.
Definition node.h:155
Aggregate * parent() const
Returns the node's parent pointer, that is, the aggregate under which the node is documented.
Definition node.h:211
virtual bool isDeprecated() const
Returns true if this node's status is Deprecated.
Definition node.h:136
virtual bool isAggregate() const
Returns true if this node is an aggregate, which means it inherits Aggregate and can therefore have c...
Definition node.h:138
virtual bool isCollectionNode() const
Returns true if this is an instance of CollectionNode.
Definition node.h:146
Bundles output configuration without static variables.
static void processDocumentBlocks(IR::ListExpander *expander, LinkResolver *resolver, IR::Document &ir, const Node *page, const NodeExtractor::SemanticOrigins *origins=nullptr)
static void writeOrDiscardRenderResult(FileDocumentWriter *writer, const InjaBridge::RenderContext &context, const InjaBridge::RenderResult &result, const TemplateGenerator::RenderFailureHandler &reportFailure)
static std::optional< LoadedTemplate > loadTemplate(const QString &templateDir, const QString &name)