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 QDocDatabase;
21namespace IR { struct Document; }
22
23/*!
24 \class TemplateGenerator
25 \internal
26 \brief Generates documentation using external templates and a pre-built IR.
27
28 TemplateGenerator implements OutputProducer and DocumentationHandler to
29 generate documentation without inheriting from Generator. It uses
30 DocumentationTraverser for tree traversal and delegates content generation
31 to templates via the IR system.
32
33 \section1 Architecture
34
35 The generator follows a composition-based design:
36 \list
37 \li \b{OutputProducer}: Lifecycle interface (prepare/produce/finalize)
38 \li \b{DocumentationHandler}: Content generation callbacks for traverser
39 \li \b{DocumentationTraverser}: Shared tree traversal logic
40 \li \b{DocumentWriter}: Output abstraction (file or string for tests)
41 \endlist
42
43 \sa DocumentationTraverser, DocumentationHandler, OutputProducer
44*/
45class TemplateGenerator : public OutputProducer, public DocumentationHandler
46{
47public:
48 explicit TemplateGenerator(FileResolver &fileResolver, QDocDatabase &qdb,
49 const QString &format = QString());
50 ~TemplateGenerator() override;
51
52 // === OutputProducer interface ===
53 void prepare() override;
54 void produce() override;
55 void finalize() override;
56 [[nodiscard]] QString format() const override;
57
58 // === DocumentationHandler interface ===
59 void beginDocument(const QString &fileName) override;
60 void endDocument() override;
61 [[nodiscard]] QString fileName(const Node *node) const override;
62 void generateCollectionNode(CollectionNode *cn, CodeMarker *marker) override;
64 void generatePageNode(PageNode *pn, CodeMarker *marker) override;
65 void generateCppReferencePage(Aggregate *aggregate, CodeMarker *marker) override;
66 void generateQmlTypePage(QmlTypeNode *qcn, CodeMarker *marker) override;
67 void generateProxyPage(Aggregate *aggregate, CodeMarker *marker) override;
68 void mergeCollections(CollectionNode *cn) override;
69
70 // Public accessors for configuration
71 [[nodiscard]] QString fileExtension() const;
72
73private:
74 // Render phase: Format IR according to templates.
75 void renderDocument(const IR::Document &ir, const QString &templateBaseName);
76
77 // Include resolution for Inja {% include %} directives.
78 [[nodiscard]] QString resolveInclude(const QString &name) const;
79
80 // Filename computation (adapted from Generator)
81 [[nodiscard]] QString fileBase(const Node *node) const;
82
83 // Creates the production writer (FileDocumentWriter).
84 void createDefaultWriter();
85
86 FileResolver &m_fileResolver;
87 QDocDatabase &m_qdb;
88 QString m_format;
89 std::unique_ptr<FileDocumentWriter> m_writer;
90 std::optional<OutputContext> m_context;
91 QString m_templateDir;
92 QString m_fileExtension = QStringLiteral("html");
93
94 // For testing: allow injection of mock writers
95 friend class TemplateGeneratorTest;
96};
97
98QT_END_NAMESPACE
99
100#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
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...
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
IR::PageMetadata extractPageMetadata(const PageNode *pn)
Combined button and popup list for selecting options.
Intermediate representation for a documentation topic.
Definition document.h:22
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)