6#ifdef QDOC_TEMPLATE_GENERATOR_ENABLED
8#include "collectionnode.h"
9#include "inclusionfilter.h"
10#include "locationplan.h"
11#include "outputcontext.h"
12#include "qmltypenode.h"
15#include <QtCore/qdir.h>
19using namespace Qt::StringLiterals;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
41
42
43
44
45
46
47
48
49
50
52HrefResolver::HrefResolver(
const HrefResolverConfig &config)
58
59
60
61QString HrefResolver::fileName(
const Node *node)
const
63 return m_config.plan->fileName(node);
67
68
69
70
71
72
73QString HrefResolver::anchorForNode(
const Node *node)
const
75 return LocationPlan::resolveFragment(node);
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96HrefResult HrefResolver::hrefForNode(
const Node *node,
const Node *relative)
const
99 return HrefSuppressReason::NullNode;
101 const bool hasExplicitUrl = !node->url().isEmpty();
102 if (hasExplicitUrl && node->isExternalPage())
110 QString fn = hasExplicitUrl ? node->url().section(
'/'_L1, -1) : QString();
116 if (hasExplicitUrl) {
117 const qsizetype hashIndex = fn.indexOf(
'#'_L1);
119 fn.truncate(hashIndex);
127 const bool isCrossModulePlaceholder = node->isCollectionNode()
128 && !
static_cast<
const CollectionNode *>(node)
129 ->resolvedPhysicalModuleName().isEmpty();
130 const bool isLocal = !hasExplicitUrl && !node->isIndexNode() && !isCrossModulePlaceholder
131 && (!relative || node->tree() == relative->tree());
132 if (!hasExplicitUrl && !isLocal)
140 if (!node->isIndexNode() && !isCrossModulePlaceholder) {
141 const NodeContext context = node->createContext();
142 if (!InclusionFilter::isIncluded(m_config.inclusionPolicy, context))
143 return HrefSuppressReason::ExcludedByPolicy;
146 const Node *publicationNode = node;
147 if (!hasExplicitUrl && node->parent() && node->parent()->isQmlType()
148 && node->parent()->isAbstract()) {
155 const QmlTypeNode *qmlContext =
nullptr;
156 if (relative && relative->isQmlType())
157 qmlContext =
static_cast<
const QmlTypeNode *>(relative);
158 else if (m_config.qmlTypeContextFn)
159 qmlContext = m_config.qmlTypeContextFn();
161 if (qmlContext->inherits(node->parent())) {
162 publicationNode = qmlContext;
163 fn = fileName(qmlContext);
170 if (!node->isAggregate() && node->isDeprecated()
171 && qmlContext->url().isEmpty()) {
172 const QString ext =
'.'_L1 + m_config.context->fileExtension;
173 fn.chop(ext.length());
174 fn +=
"-obsolete"_L1 + ext;
176 }
else if (node->parent()->isInternal() && !m_config.context->noLinkErrors) {
177 node->doc().location().warning(
178 u"Cannot link to property in internal type '%1'"_s
179 .arg(node->parent()->name()));
180 return HrefSuppressReason::InternalAbstractQml;
185 std::optional<PublicationLocation> planned;
187 planned = m_config.plan->entity(publicationNode);
188 if (!planned && m_config.publications) {
189 planned = m_config.publications->entity(
190 publicationNode, m_config.format, m_config.context->outputDir.path());
193 return HrefSuppressReason::Unpublished;
195 if (publicationNode != node) {
196 planned->fragment = anchorForNode(node);
197 if (!node->isAggregate() && node->isDeprecated()) {
198 const qsizetype dot = fn.lastIndexOf(u'.');
200 fn.insert(dot,
"-obsolete"_L1);
205 return HrefSuppressReason::NoFileBase;
209 if (!node->isPageNode() || node->isPropertyGroup()) {
210 QString ref = planned ? planned->fragment : anchorForNode(node);
211 if (relative && fn == fileName(relative) && ref == anchorForNode(relative))
212 return HrefSuppressReason::SameFileAnchor;
218 if (relative && (node != relative) && !node->isExternalPage()
219 && (node->isIndexNode() || isCrossModulePlaceholder
220 || node->tree() != relative->tree())) {
221 link.prepend(crossModulePrefix(node, relative));
228
229
230
231
232
233
234
235
236
237
238
239
240
241QString HrefResolver::crossModulePrefix(
const Node *target,
const Node *source)
const
243 const QString &sourceDir = m_config.context->outputDir.path();
244 const QString currentSegment =
'/'_L1 + source->tree()->physicalModuleName() +
'/'_L1;
245 const qsizetype moduleIndex = sourceDir.indexOf(currentSegment);
253 QString targetModuleName;
254 if (target->isCollectionNode()) {
255 const auto *cn =
static_cast<
const CollectionNode *>(target);
256 targetModuleName = cn->resolvedPhysicalModuleName();
258 if (targetModuleName.isEmpty())
259 targetModuleName = target->tree()->physicalModuleName();
260 const QString targetSegment =
'/'_L1 + targetModuleName +
'/'_L1;
261 QString targetDir = sourceDir;
262 targetDir.replace(moduleIndex, currentSegment.size(), targetSegment);
264 const QString relPath = QDir(sourceDir).relativeFilePath(targetDir);
265 if (relPath.isEmpty() || relPath ==
"."_L1)
267 return relPath +
'/'_L1;