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
injabridge.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 INJABRIDGE_H
5#define INJABRIDGE_H
6
7#include <QtCore/qglobal.h>
8
9#if !defined(QT_NO_EXCEPTIONS) && !defined(INJA_NOEXCEPTION) && !defined(JSON_NOEXCEPTION)
10 && (__EXCEPTIONS || _CPPUNWIND)
11# define QDOC_TEMPLATE_LIBS_THROW 1
12#else
13# define QDOC_TEMPLATE_LIBS_THROW 0
14#endif
15
17// Preserve the failing render's context when library errors cannot
18// reach a catch boundary.
19QT_BEGIN_NAMESPACE
20[[noreturn]] void qdocFatalTemplateRenderError(const char *what);
21QT_END_NAMESPACE
22
23# define INJA_THROW(exception)
24 QT_PREPEND_NAMESPACE(qdocFatalTemplateRenderError)((exception).what())
25# define JSON_THROW_USER(exception)
26 QT_PREPEND_NAMESPACE(qdocFatalTemplateRenderError)((exception).what())
27#endif
28
29// nlohmann::json comes in through inja; both redefinitions above must
30// precede this include.
31#include <inja/inja.hpp>
32
33#include <QJsonObject>
34#include <QJsonArray>
35#include <QJsonValue>
36#include <QString>
37
38#include <functional>
39#include <optional>
40#include <variant>
41
42QT_BEGIN_NAMESPACE
43
44class InjaBridge
45{
46public:
47 using IncludeCallback = std::function<std::optional<QString>(const QString &name)>;
48
49 struct RenderContext
50 {
51 QString format;
52 QString page;
53 QString templatePath;
54 };
55
56 struct RenderFailure
57 {
58 QString message;
59 };
60
61 using RenderResult = std::variant<QString, RenderFailure>;
62
63 static QString renderErrorText(const RenderContext &context, const QString &message);
64
65 static nlohmann::json toInjaJson(const QJsonValue &value);
66 static nlohmann::json toInjaJson(const QJsonObject &obj);
67 static nlohmann::json toInjaJson(const QJsonArray &array);
68
69 static RenderResult render(const QString &templateStr, const QJsonObject &data,
70 const RenderContext &context = { });
71 static RenderResult render(const QString &templateStr, const QJsonObject &data,
72 const IncludeCallback &includeCallback,
73 const RenderContext &context = { });
74 static RenderResult renderFile(const QString &templatePath, const QJsonObject &data,
75 const RenderContext &context = { });
76
77private:
78 InjaBridge() = default;
79};
80
81QT_END_NAMESPACE
82
83#endif // INJABRIDGE_H
static std::string renderSignatureSpans(const nlohmann::json &spans, const QString &format)
static std::string escapeHtml(const std::string &input)
static void registerCallbacks(inja::Environment &env, const QString &format)
static InjaBridge::RenderResult renderWithPolicy(const InjaBridge::RenderContext &context, const std::function< QString()> &render)
static std::string renderSpanMarkdown(const nlohmann::json &s)
static std::string renderLinkSpan(const std::string &text, const std::string &href, bool hasHref, bool isMarkdown)
#define QDOC_TEMPLATE_LIBS_THROW
Definition injabridge.h:13