Doxygen
Loading...
Searching...
No Matches
qhp.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 1997-2022 Dimitri van Heesch.
3 *
4 * Permission to use, copy, modify, and distribute this software and its
5 * documentation under the terms of the GNU General Public License is hereby
6 * granted. No representations are made about the suitability of this software
7 * for any purpose. It is provided "as is" without express or implied warranty.
8 * See the GNU General Public License for more details.
9 *
10 * Documents produced by Doxygen are derivative works derived from the
11 * input used in their production; they are not affected by this license.
12 */
13
14#include <algorithm>
15#include <memory>
16#include <string.h>
17#include <vector>
18#include <cassert>
19#include <mutex>
20
21#include "config.h"
22#include "debug.h"
23#include "doxygen.h"
24#include "groupdef.h"
25#include "memberdef.h"
26#include "message.h"
27#include "qhp.h"
28#include "textstream.h"
29#include "util.h"
30#include "stringutil.h"
31#include "portable.h"
32#include "language.h"
33#include "version.h"
34
35static std::once_flag g_blankWritten;
36
37
38static inline void writeIndent(TextStream &t,int indent)
39{
41 {
42 for (int i=0;i<indent;i++) t << " ";
43 }
44}
45
47{
48 private:
49 struct Node
50 {
51 enum class Type { Root, Dir, Section };
52 // constructor for root node
53 Node() : type(Type::Root), parent(nullptr) {}
54 // constructor for dir node
55 Node(Node *parent_) : type(Type::Dir), parent(parent_) {}
56 // constructor for section node
57 Node(Node *parent_, const DString &title_,const DString &ref_)
58 : type(Type::Section), parent(parent_), title(title_), ref(ref_) {}
60 Node *parent = nullptr;
63 std::vector<std::unique_ptr<Node>> children;
64 };
65
68
69 void traverse(const Node &root,TextStream &t,int indent) const
70 {
71 /* Input: Output:
72 * =================================================
73 * Section1 <section title=".." ref="..">
74 * Dir1
75 * Section2 <section title=".." ref="..">
76 * Dir2
77 * Section3 <section title=".." ref=".."/>
78 * </section>
79 * </section>
80 * Section4 <section title=".." ref="..>
81 * Dir3
82 * Dir4
83 * Section5 <section title=.." ref=.."/>
84 * </section>
85 * Section6 <section title=".." ref=".."/>
86 */
87 size_t numChildren = root.children.size();
88 size_t i=0;
89 while (i<numChildren)
90 {
91 if (root.children[i]->type==Node::Type::Section)
92 {
93 i++;
94 if (i<numChildren && root.children[i]->type==Node::Type::Dir)
95 {
96 // we have a dir node
97 writeIndent(t,indent);
98 t << "<section title=\"" << convertToXML(root.children[i-1]->title) << "\""
99 << " ref=\"" << convertToXML(root.children[i-1]->ref) << "\">\n";
100 while (i<numChildren && root.children[i]->type==Node::Type::Dir)
101 {
102 traverse(*root.children[i].get(),t,indent+1);
103 i++;
104 }
105 writeIndent(t,indent);
106 t << "</section>\n";
107 }
108 else // we have a leaf section node
109 {
110 writeIndent(t,indent);
111 t << "<section title=\"" << convertToXML(root.children[i-1]->title) << "\""
112 << " ref=\"" << convertToXML(root.children[i-1]->ref) << "\"/>\n";
113 }
114 }
115 else // dir without preceding section (no extra indent)
116 {
117 traverse(*root.children[i].get(),t,indent);
118 i++;
119 }
120 }
121 }
122
123 public:
124 void addSection(const DString &title,const DString &ref)
125 {
126 m_current->children.push_back(std::make_unique<Node>(m_current,title,ref));
127 }
128 void incLevel()
129 {
130 auto newNode = new Node(m_current);
131 m_current->children.push_back(std::unique_ptr<Node>(newNode));
132 m_current = newNode;
133 }
134 void decLevel()
135 {
136 assert(m_current->parent!=nullptr);
137 if (m_current->parent)
138 {
140 }
141 }
142 void writeToc(TextStream &t) const
143 {
144 writeIndent(t,2);
145 t << "<toc>\n";
146 traverse(m_root,t,3);
147 writeIndent(t,2);
148 t << "</toc>\n";
149 }
150};
151
161
163{
164 DString projectName = Config_getString(PROJECT_NAME);
165 DString versionText = Config_getString(PROJECT_NUMBER);
166 if (projectName.empty()) projectName="Root";
167 if (!versionText.empty()) projectName+=" "+versionText;
168 return projectName;
169}
170
171static DString makeFileName(const DString & withoutExtension)
172{
173 DString result=withoutExtension;
174 if (!result.empty())
175 {
176 if (result.at(0)=='!') // relative URL -> strip marker
177 {
178 result=result.mid(1);
179 }
180 else // add specified HTML extension
181 {
183 }
184 }
185 return result;
186}
187
188static DString makeRef(const DString & withoutExtension, const DString & anchor)
189{
190 //printf("QHP::makeRef(%s,%s)\n",withoutExtension,anchor);
191 if (withoutExtension.empty()) return DString();
192 DString result = makeFileName(withoutExtension);
193 if (anchor.empty()) return result;
194 return result+"#"+anchor;
195}
196
197Qhp::Qhp() : p(std::make_unique<Private>()) {}
198Qhp::~Qhp() = default;
199
201{
202 /*
203 <QtHelpProject version="1.0">
204 <namespace>mycompany.com.myapplication.1_0</namespace>
205 <virtualFolder>doc</virtualFolder>
206 <customFilter name="My Application 1.0">
207 <filterAttribute>myapp</filterAttribute>
208 <filterAttribute>1.0</filterAttribute>
209 </customFilter>
210 <filterSection>
211 <filterAttribute>myapp</filterAttribute>
212 <filterAttribute>1.0</filterAttribute>
213 ..
214 */
215 DString fileName = Config_getString(HTML_OUTPUT) + "/" + qhpFileName;
216 p->docFile = Portable::openOutputStream(fileName);
217 if (!p->docFile.is_open())
218 {
219 term("Could not open file {} for writing\n",fileName);
220 }
221 p->doc.setStream(&p->docFile);
222
223 p->doc << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
224 p->doc << "<QtHelpProject version=\"1.0\">\n";
225 writeIndent(p->doc,1);
226 p->doc << "<namespace>" << convertToXML(Config_getString(QHP_NAMESPACE)) << "</namespace>\n";
227 writeIndent(p->doc,1);
228 p->doc << "<virtualFolder>" << convertToXML(Config_getString(QHP_VIRTUAL_FOLDER)) << "</virtualFolder>\n";
229
230 // Add custom filter
231 DString filterName = Config_getString(QHP_CUST_FILTER_NAME);
232 if (!filterName.empty())
233 {
234 writeIndent(p->doc,1);
235 p->doc << "<customFilter name=\"" << convertToXML(filterName) << "\">\n";
236
237 StringVector customFilterAttributes =
238 split(Config_getString(QHP_CUST_FILTER_ATTRS).str(), " ");
239 for (const auto &attr : customFilterAttributes)
240 {
241 writeIndent(p->doc,2);
242 p->doc << "<filterAttribute>" << convertToXML(attr) << "</filterAttribute>\n";
243 }
244 writeIndent(p->doc,1);
245 p->doc << "</customFilter>\n";
246 }
247
248 writeIndent(p->doc,1);
249 p->doc << "<filterSection>\n";
250
251 // Add section attributes
252 StringVector sectionFilterAttributes = split(Config_getString(QHP_SECT_FILTER_ATTRS).str(), " ");
253 // always add doxygen as filter attribute
254 if (std::find(sectionFilterAttributes.begin(), sectionFilterAttributes.end(), "doxygen") ==
255 sectionFilterAttributes.end())
256 {
257 sectionFilterAttributes.emplace_back("doxygen");
258 }
259 for (const auto &attr : sectionFilterAttributes)
260 {
261 writeIndent(p->doc,2);
262 p->doc << "<filterAttribute>" << convertToXML(attr) << "</filterAttribute>\n";
263 }
264
265 // Add extra root node to the TOC
266 p->sectionTree.addSection(getFullProjectName(),"index"+Doxygen::htmlFileExtension);
267 p->sectionTree.incLevel();
268
269 writeIndent(p->index,2);
270 p->index << "<keywords>\n";
271}
272
274{
275 // close root node
276 p->sectionTree.decLevel();
277
278 // Finish TOC
279 p->sectionTree.writeToc(p->doc);
280
281 // Finish index
282 writeIndent(p->index,2);
283 p->index << "</keywords>\n";
284 p->doc << p->index.str();
285
286 // Finish files
287 writeIndent(p->doc,2);
288 p->doc << "<files>\n";
289 for (auto &s : p->files)
290 {
291 writeIndent(p->doc,3);
292 p->doc << s << "\n";
293 }
294 writeIndent(p->doc,2);
295 p->doc << "</files>\n";
296
297 writeIndent(p->doc,1);
298 p->doc << "</filterSection>\n";
299 p->doc << "</QtHelpProject>\n";
300
301 p->doc.flush();
302 p->docFile.close();
303}
304
306{
307 p->sectionTree.incLevel();
308}
309
311{
312 p->sectionTree.decLevel();
313}
314
315void Qhp::addContentsItem(bool /* isDir */, const DString & name, const DString & /*ref*/,
316 const DString & file, const DString &anchor, bool /* separateIndex */,
317 bool /* addToNavIndex */, const Definition * /*def*/, const DString & /*nameAsHtml*/)
318{
319 /*
320 <toc>
321 <section title="My Application Manual" ref="index.html">
322 <section title="Chapter 1" ref="doc.html#chapter1"/>
323 <section title="Chapter 2" ref="doc.html#chapter2"/>
324 <section title="Chapter 3" ref="doc.html#chapter3"/>
325 </section>
326 </toc>
327 */
328
329 DString f = file;
330 if (!f.empty() && f.at(0)=='^') return; // absolute URL not supported
331
332 if (f.empty())
333 {
334 f = "doxygen_blank";
336 std::call_once(g_blankWritten,[this,&f]()
337 {
338 DString fileName = Config_getString(HTML_OUTPUT) + "/" + f;
339 std::ofstream blankFile = Portable::openOutputStream(fileName); // we just need an empty file
340 if (!blankFile.is_open())
341 {
342 term("Could not open file {} for writing\n",fileName);
343 }
344 TextStream blank;
345 blank.setStream(&blankFile);
346 blank << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
347 blank << "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"" + theTranslator->trISOLang() + "\">\n";
348 blank << "<head>\n";
349 blank << "<title>Validator / crawler helper</title>\n";
350 blank << "<meta http-equiv=\"Content-Type\" content=\"text/xhtml;charset=UTF-8\"/>\n";
351 blank << "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=11\"/>\n";
352
353 blank << "<meta name=\"generator\" content=\"Doxygen " + getDoxygenVersion() + "\"/>\n";
354 blank << "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n";
355 blank << "</head>\n";
356 blank << "<body>\n";
357 blank << "</body>\n";
358 blank << "</html>\n";
359 blank.flush();
360 blankFile.close();
361 addFile(f);
362 });
363 }
364 DString finalRef = makeRef(f, anchor);
365 p->sectionTree.addSection(name,finalRef);
366}
367
368void Qhp::addIndexItem(const Definition *context,const MemberDef *md,
369 const DString &sectionAnchor,const DString &word)
370{
371 //printf("addIndexItem(%s %s %s\n",
372 // context?context->name().data():"<none>",
373 // md?md->name().data():"<none>",
374 // qPrint(word));
375
376 if (context && md) // member
377 {
378 if (sectionAnchor.empty() && !md->hasDocumentation()) return;
379 DString cfname = md->getOutputFileBase();
380 DString argStr = md->argsString();
381 DString level1 = context->name();
382 DString level2 = !word.empty() ? word : md->name();
383 DString anchor = !sectionAnchor.empty() ? sectionAnchor : md->anchor();
384 DString ref;
385
386 // <keyword name="foo" id="MyApplication::foo" ref="doc.html#foo"/>
387 ref = makeRef(cfname, anchor);
388 DString id = level1+"::"+level2;
389 writeIndent(p->index,3);
390 p->index << "<keyword name=\"" << convertToXML(level2 + argStr) << "\""
391 " id=\"" << convertToXML(id + "_" + anchor) << "\""
392 " ref=\"" << convertToXML(ref) << "\"/>\n";
393 }
394 else if (context) // container
395 {
396 // <keyword name="Foo" id="Foo" ref="doc.html#Foo"/>
397 DString contRef = context->getOutputFileBase();
398 DString level1 = !word.empty() ? word : context->name();
399 DString ref = makeRef(contRef,sectionAnchor);
400 writeIndent(p->index,3);
401 p->index << "<keyword name=\"" << convertToXML(level1) << "\""
402 << " id=\"" << convertToXML(level1 +"_" + sectionAnchor) << "\""
403 << " ref=\"" << convertToXML(ref) << "\"/>\n";
404 }
405}
406
407void Qhp::addFile(const DString & fileName)
408{
409 p->files.insert(("<file>" + convertToXML(fileName) + "</file>").str());
410}
411
412void Qhp::addIndexFile(const DString & fileName)
413{
414 addFile(fileName);
415}
416
417void Qhp::addImageFile(const DString &fileName)
418{
419 addFile(fileName);
420}
421
422void Qhp::addStyleSheetFile(const DString &fileName)
423{
424 addFile(fileName);
425}
426
428{
429 DString qchFile = Config_getString(QCH_FILE);
430 if (!qchFile.empty())
431 {
432 return qchFile;
433 }
434
435 DString projectName = Config_getString(PROJECT_NAME);
436 DString versionText = Config_getString(PROJECT_NUMBER);
437
438 return DString("../qch/")
439 + (projectName.empty() ? DString("index") : projectName)
440 + (versionText.empty() ? DString("") : DString("-") + versionText)
441 + DString(".qch");
442}
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:88
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:322
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:152
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:690
@ Qhp
Definition debug.h:45
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:133
The common base class of all entity definitions found in the sources.
Definition definition.h:77
virtual const DString & name() const =0
virtual bool hasDocumentation() const =0
virtual DString anchor() const =0
virtual DString getOutputFileBase() const =0
Class representing a directory in the file system.
Definition dir.h:73
static DString htmlFileExtension
Definition doxygen.h:115
A model of a class/file/namespace member symbol.
Definition memberdef.h:45
virtual DString argsString() const =0
QhpSectionTree sectionTree
Definition qhp.cpp:159
TextStream doc
Definition qhp.cpp:156
TextStream index
Definition qhp.cpp:157
StringSet files
Definition qhp.cpp:158
std::ofstream docFile
Definition qhp.cpp:155
Qhp()
Definition qhp.cpp:197
void addIndexFile(const DString &name)
Definition qhp.cpp:412
void incContentsDepth()
Definition qhp.cpp:305
void decContentsDepth()
Definition qhp.cpp:310
void initialize()
Definition qhp.cpp:200
void finalize()
Definition qhp.cpp:273
std::unique_ptr< Private > p
Definition qhp.h:55
~Qhp()
static DString getQchFileName()
Definition qhp.cpp:427
void addContentsItem(bool isDir, const DString &name, const DString &ref, const DString &file, const DString &anchor, bool separateIndex, bool addToNavIndex, const Definition *def, const DString &)
Definition qhp.cpp:315
void addIndexItem(const Definition *context, const MemberDef *md, const DString &sectionAnchor, const DString &title)
Definition qhp.cpp:368
void addStyleSheetFile(const DString &name)
Definition qhp.cpp:422
void addFile(const DString &)
Definition qhp.cpp:407
static const DString qhpFileName
Definition qhp.h:49
void addImageFile(const DString &name)
Definition qhp.cpp:417
Node m_root
Definition qhp.cpp:66
void writeToc(TextStream &t) const
Definition qhp.cpp:142
void incLevel()
Definition qhp.cpp:128
void traverse(const Node &root, TextStream &t, int indent) const
Definition qhp.cpp:69
Node * m_current
Definition qhp.cpp:67
void decLevel()
Definition qhp.cpp:134
void addSection(const DString &title, const DString &ref)
Definition qhp.cpp:124
Text streaming class that buffers data.
Definition textstream.h:36
void setStream(std::ostream *s)
Sets or changes the std::ostream to write to.
Definition textstream.h:67
void flush()
Flushes the buffer.
Definition textstream.h:212
virtual DString trISOLang()=0
#define Config_getString(name)
Definition config.h:32
std::set< std::string > StringSet
Definition containers.h:31
std::vector< std::string > StringVector
Definition containers.h:33
Translator * theTranslator
Definition language.cpp:71
#define term(fmt,...)
Definition message.h:137
std::ofstream openOutputStream(const DString &name, bool append=false)
Definition portable.cpp:665
Definition dstring.h:917
Portable versions of functions that are platform dependent.
static DString makeFileName(const DString &withoutExtension)
Definition qhp.cpp:171
static DString getFullProjectName()
Definition qhp.cpp:162
static DString makeRef(const DString &withoutExtension, const DString &anchor)
Definition qhp.cpp:188
static std::once_flag g_blankWritten
Definition qhp.cpp:35
static void writeIndent(TextStream &t, int indent)
Definition qhp.cpp:38
Some helper functions for std::string.
StringVector split(const std::string &s, const std::string &delimiter)
split input string s by string delimiter delimiter.
Definition stringutil.h:117
Node(Node *parent_, const DString &title_, const DString &ref_)
Definition qhp.cpp:57
std::vector< std::unique_ptr< Node > > children
Definition qhp.cpp:63
Node(Node *parent_)
Definition qhp.cpp:55
void addHtmlExtensionIfMissing(DString &fName)
Definition util.cpp:3933
DString convertToXML(const DString &s, bool keepEntities, const bool citeEntry)
Definition util.cpp:3234
A bunch of utility functions.