Skip to content

Commit 8248c9d

Browse files
toreinioQt Cherry-pick Bot
authored andcommitted
qdoc: Allow tying a \keyword and a \target to a section title
Previously, a \keyword could only be used to create a link target to the topic it appears in. Adding a link target to a specific location within the topic (e.g. a \page) was only possible with the \target command; however, a \target does not add a keyword entry to the Qt Help project (.qhp) file like a \keyword does. Enable tying a \keyword to a \section1..4 title within a documentation comment. If a \keyword appears directly before a \section command, the section's href is adopted by the keyword and recorded into the .qhp. This way, it's possible to have keyword in the Assistant's or Qt Creator Help's index that refer to a section. These indexed sections are also available for the context help. Likewise, these changes alter the behavior of the \target command to be slightly cleaner: \target hola \section1 Hello Will make \l {hola} link to the anchor generated for the section, instead of a (somewhat arbitrary) location just above the section. Add a new value `ContentsKeyword` to TargetRec::TargetType enum to facilitate this. As a drive-by, add internal documentation for all TargetType enum values to clarify their roles. Fixes: QTBUG-128573 Change-Id: Ifacdfab174f1aad7fcb7420416e7b56c461ea04e Reviewed-by: Paul Wicking <[email protected]> (cherry picked from commit 9e94ec6) Reviewed-by: Qt Cherry-pick Bot <[email protected]>
1 parent 21524c7 commit 8248c9d

File tree

17 files changed

+96
-18
lines changed

17 files changed

+96
-18
lines changed

src/qdoc/qdoc/doc/qdoc-manual-markupcmds.qdoc

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,14 +1771,21 @@
17711771
and the location to generated indices.
17721772

17731773
The \\keyword command is like the \l {target-command} {\\target}
1774-
command, except when linking to keyword the link goes to the top of
1775-
the QDoc comment where the \\keyword appears in. If you want to
1776-
create a link target to a \c section unit within a \\page, use
1777-
\\target instead.
1778-
1779-
Keywords are also registered in indices of generated offline
1780-
documentation files (.qch). This allows users to look up the location
1781-
by keyword, for instance, in
1774+
command, except that when linking to a keyword, by default the link
1775+
goes to the top of the QDoc comment (topic) the \\keyword appears in.
1776+
1777+
If you want to create a keyword for a \c section unit within a
1778+
topic, add the \\keyword directly above the section title:
1779+
1780+
\badcode
1781+
\keyword debug
1782+
\section1 Debug command line option (--debug)
1783+
...
1784+
\endcode
1785+
1786+
Unlike \\target, keywords are registered in indices of generated
1787+
offline documentation files (.qch). This allows users to look up the
1788+
location by keyword, for instance, in
17821789
\l[QtAssistant]{Searching for Keywords}{Qt Assistant's Index Search},
17831790
and makes the keyword accessible in \QC's context help.
17841791

src/qdoc/qdoc/src/qdoc/helpprojectwriter.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,15 @@ bool HelpProjectWriter::generateSection(HelpProject &project, QXmlStreamWriter &
253253
auto appendDocKeywords = [&](const Node *n) {
254254
for (const auto *kw : n->doc().keywords()) {
255255
if (!kw->string().isEmpty()) {
256+
QStringList ref_parts = m_gen->fullDocumentLocation(n).split('#'_L1);
257+
// Use keyword's custom anchor if it has one
258+
if (kw->count() > 1) {
259+
if (ref_parts.count() > 1)
260+
ref_parts.pop_back();
261+
ref_parts << kw->string(1);
262+
}
256263
project.m_keywords.append(Keyword(kw->string(), kw->string(),
257-
m_gen->fullDocumentLocation(n)));
264+
ref_parts.join('#'_L1)));
258265
}
259266
}
260267
};

src/qdoc/qdoc/src/qdoc/tree.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ QT_BEGIN_NAMESPACE
3434
is being generated.
3535
*/
3636

37+
/*!
38+
\class TargetRec
39+
\brief A record of a linkable target within the documentation.
40+
*/
41+
42+
/*!
43+
\enum TargetRec::TargetType
44+
45+
A type of a linkable target record.
46+
47+
\value Unknown
48+
Unknown/target not set.
49+
\value Target
50+
A location marked with a \\target command.
51+
\value Keyword
52+
A location marked with a \\keyword command.
53+
\value Contents
54+
A table of contents item (section title).
55+
\value ContentsKeyword
56+
A \\keyword tied to a section title.
57+
*/
58+
3759
/*!
3860
Constructs a Tree. \a qdb is the pointer to the singleton
3961
qdoc database that is constructing the tree. This might not
@@ -788,6 +810,18 @@ void Tree::addTargetsToTargetMap(Node *node) {
788810
}
789811
}
790812

813+
/*
814+
If atom \a a is immediately followed by a
815+
section title (\section1..\section4 command),
816+
returns the SectionLeft atom; otherwise nullptr.
817+
*/
818+
static const Atom *nextSection(const Atom *a)
819+
{
820+
while (a && a->next(Atom::SectionRight))
821+
a = a->next(); // skip closing section atoms
822+
return a ? a->next(Atom::SectionLeft) : nullptr;
823+
}
824+
791825
/*!
792826
\internal
793827
@@ -801,9 +835,11 @@ void Tree::addKeywordsToTargetMaps(Node *node) {
801835
QString ref = refForAtom(i);
802836
QString title = i->string();
803837
if (!ref.isEmpty() && !title.isEmpty()) {
804-
auto *target = new TargetRec(ref, TargetRec::Keyword, node, 1);
838+
auto *target = new TargetRec(ref, nextSection(i) ? TargetRec::ContentsKeyword : TargetRec::Keyword, node, 1);
805839
m_nodesByTargetRef.insert(Utilities::asAsciiPrintable(title), target);
806840
m_nodesByTargetTitle.insert(title, target);
841+
if (!target->isEmpty())
842+
i->append(target->m_ref);
807843
}
808844
}
809845
}
@@ -948,6 +984,9 @@ const PageNode *Tree::findPageNodeByTitle(const QString &title) const
948984
/*!
949985
Returns a canonical title for the \a atom, if the \a atom
950986
is a SectionLeft, SectionHeadingLeft, Keyword, or Target.
987+
988+
If a target or a keyword is immediately followed by a
989+
section, the former adopts the title (ref) of the latter.
951990
*/
952991
QString Tree::refForAtom(const Atom *atom)
953992
{
@@ -964,6 +1003,8 @@ QString Tree::refForAtom(const Atom *atom)
9641003
case Atom::Target:
9651004
[[fallthrough]];
9661005
case Atom::Keyword:
1006+
if (const auto *section = nextSection(atom))
1007+
return refForAtom(section);
9671008
return Utilities::asAsciiPrintable(atom->string());
9681009
default:
9691010
return {};

src/qdoc/qdoc/src/qdoc/tree.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ class QDocDatabase;
2424
struct TargetRec
2525
{
2626
public:
27-
enum TargetType { Unknown, Target, Keyword, Contents };
27+
enum TargetType { Unknown, Target, Keyword, Contents, ContentsKeyword };
2828

2929
TargetRec(QString name, TargetRec::TargetType type, Node *node, int priority)
30-
: m_node(node), m_ref(std::move(name)), m_type(type), m_priority(priority)
30+
: m_node(node),
31+
m_ref(std::move(name)),
32+
m_type(type == ContentsKeyword ? Keyword : type),
33+
m_priority(priority)
3134
{
3235
// Discard the dedicated ref for keywords - they always
3336
// link to the top of the QDoc comment they appear in

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/docbook/autolinking.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@
2929
<db:section xml:id="someprop">
3030
<db:title>someProp</db:title>
3131
</db:section>
32+
<db:section xml:id="indexedkeyword-property">
33+
<db:title>indexedKeyword property</db:title>
34+
</db:section>
3235
</db:article>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/html/autolinking.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ <h3 id="toc">Contents</h3>
1212
<ul>
1313
<li class="level1"><a href="#testqdoc">TestQDoc</a></li>
1414
<li class="level1"><a href="#someprop">someProp</a></li>
15+
<li class="level1"><a href="#indexedkeyword-property">indexedKeyword property</a></li>
1516
</ul>
1617
</div>
1718
<div class="sidebar-content" id="sidebar-content"></div></div>
@@ -30,6 +31,7 @@ <h2 id="testqdoc">TestQDoc</h2>
3031
<li><a href="obsolete-classes.html#testqdoc">Obsolete Classes#TestQDoc</a></li>
3132
</ul>
3233
<h2 id="someprop">someProp</h2>
34+
<h2 id="indexedkeyword-property">indexedKeyword property</h2>
3335
</div>
3436
<!-- @@@autolinking.html -->
3537
</body>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/html/test.index

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
<qmlproperty name="name" fullname="QDoc.Test.AbstractParent.name" status="active" access="public" location="parent.qdoc" documented="true" type="string" attached="false" writable="true" brief="Name of this parent"/>
2121
</qmlclass>
2222
<page name="autolinking.html" href="autolinking.html" status="active" location="classlists.qdoc" documented="true" subtype="page" title="Autolinking" fulltitle="Autolinking" subtitle="">
23+
<keyword name="indexedkeyword" title="indexedKeyword"/>
2324
<contents name="testqdoc" title="TestQDoc" level="1"/>
2425
<contents name="someprop" title="someProp" level="1"/>
26+
<contents name="indexedkeyword-property" title="indexedKeyword property" level="1"/>
2527
</page>
2628
<page name="cmaketest" href="test-cmaketest-example.html" status="active" location="cmaketest.qdoc" documented="true" subtype="example" title="CMake Example Project" fulltitle="CMake Example Project" subtitle="">
2729
<page name="cmaketest/main.cpp" href="test-cmaketest-main-cpp.html" status="active" subtype="file" title="" fulltitle="main.cpp Example File" subtitle="cmaketest/main.cpp"/>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/html/test.qhp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<keyword name="group.third" id="Type::group.third" ref="qml-qdoc-test-type.html#group.third-prop"/>
151151
<keyword name="id" id="Type::id" ref="qml-qdoc-test-type.html#id-prop"/>
152152
<keyword name="id" id="TestDerived::id" ref="testqdoc-testderived.html#id"/>
153+
<keyword name="indexedKeyword" id="indexedKeyword" ref="autolinking.html#indexedkeyword-property"/>
153154
<keyword name="inlineFunction" id="Test::inlineFunction" ref="testqdoc-test.html#inlineFunction"/>
154155
<keyword name="int" id="QML.int" ref="qml-int.html"/>
155156
<keyword name="int" id="QML.QDoc.Test1.int" ref="qml-int.html"/>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/webxml/autolinking.webxml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
<WebXML>
33
<document>
44
<page name="autolinking.html" href="autolinking.html" status="active" location="classlists.qdoc" documented="true" subtype="page" title="Autolinking" fulltitle="Autolinking" subtitle="">
5+
<keyword name="indexedkeyword" title="indexedKeyword"/>
56
<contents name="testqdoc" title="TestQDoc" level="1"/>
67
<contents name="someprop" title="someProp" level="1"/>
8+
<contents name="indexedkeyword-property" title="indexedKeyword property" level="1"/>
79
<description>
810
<section id="testqdoc">
911
<heading level="1">TestQDoc</heading>
@@ -30,6 +32,9 @@
3032
<section id="someprop">
3133
<heading level="1">someProp</heading>
3234
</section>
35+
<section id="indexedkeyword-property">
36+
<heading level="1">indexedKeyword property</heading>
37+
</section>
3338
</description>
3439
</page>
3540
</document>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/expected/webxml/test.index

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
<qmlproperty name="name" fullname="QDoc.Test.AbstractParent.name" status="active" access="public" location="parent.qdoc" documented="true" type="string" attached="false" writable="true" brief="Name of this parent"/>
2121
</qmlclass>
2222
<page name="autolinking.html" href="autolinking.html" status="active" location="classlists.qdoc" documented="true" subtype="page" title="Autolinking" fulltitle="Autolinking" subtitle="">
23+
<keyword name="indexedkeyword" title="indexedKeyword"/>
2324
<contents name="testqdoc" title="TestQDoc" level="1"/>
2425
<contents name="someprop" title="someProp" level="1"/>
26+
<contents name="indexedkeyword-property" title="indexedKeyword property" level="1"/>
2527
</page>
2628
<page name="cmaketest" href="test-cmaketest-example.html" status="active" location="cmaketest.qdoc" documented="true" subtype="example" title="CMake Example Project" fulltitle="CMake Example Project" subtitle=""/>
2729
<qmlclass name="Child" qml-module-name="QDoc.Test" qml-base-type="QDoc.Test::AbstractParent" fullname="QDoc.Test.Child" href="qml-qdoc-test-child.html" status="active" access="public" location="parent.qdoc" since="1.1" documented="true" title="Child" fulltitle="Child" subtitle="" brief="A Child inheriting its parent">

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/comprehensiveproject/src/classlists.qdoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737

3838
//! a section title shadowing a known property name
3939
\section1 someProp
40+
41+
//! A section title tied to a keyword; the keyword entry
42+
//! in the qhp uses the ref of the section title
43+
\keyword indexedKeyword
44+
\section1 indexedKeyword property
4045
*/
4146

4247
/*!

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/outputfromqdocfiles/expected/docbook/qdoctests-qdocfileoutput.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-targets">Linking to a section title</db:link></db:para>
6060
</db:listitem>
6161
<db:listitem>
62-
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-test-target">Linking using a \target string</db:link></db:para>
62+
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-targets">Linking using a \target string</db:link></db:para>
6363
</db:listitem>
6464
<db:listitem>
6565
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml">Linking using a \keyword string</db:link></db:para>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/outputfromqdocfiles/expected/html/qdoctests-qdocfileoutput.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ <h2 id="linking">Linking</h2>
4848
<ul>
4949
<li><a href="qdoctests-qdocfileoutput-linking.html">Linking to a page title</a></li>
5050
<li><a href="qdoctests-qdocfileoutput-linking.html#link-targets">Linking to a section title</a></li>
51-
<li><a href="qdoctests-qdocfileoutput-linking.html#link-test-target">Linking using a \target string</a></li>
51+
<li><a href="qdoctests-qdocfileoutput-linking.html#link-targets">Linking using a \target string</a></li>
5252
<li><a href="qdoctests-qdocfileoutput-linking.html">Linking using a \keyword string</a></li>
5353
</ul>
5454
<h2 id="qdoc-linking-test">QDoc Linking Test</h2>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/outputfromqdocfiles/expected/webxml/qdoctests-qdocfileoutput.webxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</item>
7272
<item>
7373
<para>
74-
<link raw="link-test-target" href="qdoctests-qdocfileoutput-linking.html#link-test-target" type="page" page="Testing QDoc's link command">Linking using a \target string</link></para>
74+
<link raw="link-test-target" href="qdoctests-qdocfileoutput-linking.html#link-targets" type="page" page="Testing QDoc's link command">Linking using a \target string</link></para>
7575
</item>
7676
<item>
7777
<para>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/tocnavigation/expected/docbook/qdoctests-qdocfileoutput.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-targets">Linking to a section title</db:link></db:para>
6060
</db:listitem>
6161
<db:listitem>
62-
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-test-target">Linking using a \target string</db:link></db:para>
62+
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml#link-targets">Linking using a \target string</db:link></db:para>
6363
</db:listitem>
6464
<db:listitem>
6565
<db:para><db:link xlink:href="qdoctests-qdocfileoutput-linking.xml">Linking using a \keyword string</db:link></db:para>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/tocnavigation/expected/html/qdoctests-qdocfileoutput.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h2 id="linking">Linking</h2>
5050
<ul>
5151
<li><a href="qdoctests-qdocfileoutput-linking.html">Linking to a page title</a></li>
5252
<li><a href="qdoctests-qdocfileoutput-linking.html#link-targets">Linking to a section title</a></li>
53-
<li><a href="qdoctests-qdocfileoutput-linking.html#link-test-target">Linking using a \target string</a></li>
53+
<li><a href="qdoctests-qdocfileoutput-linking.html#link-targets">Linking using a \target string</a></li>
5454
<li><a href="qdoctests-qdocfileoutput-linking.html">Linking using a \keyword string</a></li>
5555
</ul>
5656
<h2 id="qdoc-linking-test">QDoc Linking Test</h2>

src/qdoc/qdoc/tests/validateqdocoutputfiles/testdata/tocnavigation/expected/webxml/qdoctests-qdocfileoutput.webxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
</item>
7272
<item>
7373
<para>
74-
<link raw="link-test-target" href="qdoctests-qdocfileoutput-linking.html#link-test-target" type="page" page="Testing QDoc's link command">Linking using a \target string</link></para>
74+
<link raw="link-test-target" href="qdoctests-qdocfileoutput-linking.html#link-targets" type="page" page="Testing QDoc's link command">Linking using a \target string</link></para>
7575
</item>
7676
<item>
7777
<para>

0 commit comments

Comments
 (0)