doxygen/src/docsets.cpp

534 lines
16 KiB
C++
Raw Normal View History

2022-10-26 16:06:26 +08:00
/******************************************************************************
*
2023-04-25 15:44:26 +08:00
* Copyright (C) 1997-2021 by Dimitri van Heesch.
2022-10-26 16:06:26 +08:00
*
* Permission to use, copy, modify, and distribute this software and its
* documentation under the terms of the GNU General Public License is hereby
* granted. No representations are made about the suitability of this software
* for any purpose. It is provided "as is" without express or implied warranty.
* See the GNU General Public License for more details.
*
* Documents produced by Doxygen are derivative works derived from the
* input used in their production; they are not affected by this license.
*
*/
2023-04-25 15:44:26 +08:00
#include <set>
#include <stack>
#include <fstream>
2022-10-26 16:06:26 +08:00
#include "docsets.h"
#include "config.h"
#include "message.h"
#include "doxygen.h"
#include "groupdef.h"
#include "classdef.h"
#include "filedef.h"
#include "memberdef.h"
#include "namespacedef.h"
#include "util.h"
2023-04-25 15:44:26 +08:00
#include "textstream.h"
2022-10-26 16:06:26 +08:00
2023-04-25 15:44:26 +08:00
struct DocSets::Private
{
QCString indent();
std::ofstream ntf;
TextStream nts;
std::ofstream ttf;
TextStream tts;
std::stack<bool> indentStack;
std::set<std::string> scopes;
};
DocSets::DocSets() : p(std::make_unique<Private>())
2022-10-26 16:06:26 +08:00
{
}
DocSets::~DocSets()
{
}
void DocSets::initialize()
{
// -- get config options
QCString projectName = Config_getString(PROJECT_NAME);
if (projectName.isEmpty()) projectName="root";
QCString bundleId = Config_getString(DOCSET_BUNDLE_ID);
if (bundleId.isEmpty()) bundleId="org.doxygen.Project";
QCString feedName = Config_getString(DOCSET_FEEDNAME);
if (feedName.isEmpty()) feedName="FeedName";
2023-04-25 15:44:26 +08:00
QCString feedURL = Config_getString(DOCSET_FEEDURL);
if (feedURL.isEmpty()) feedURL="FeedUrl";
2022-10-26 16:06:26 +08:00
QCString publisherId = Config_getString(DOCSET_PUBLISHER_ID);
if (publisherId.isEmpty()) publisherId="PublisherId";
QCString publisherName = Config_getString(DOCSET_PUBLISHER_NAME);
if (publisherName.isEmpty()) publisherName="PublisherName";
QCString projectNumber = Config_getString(PROJECT_NUMBER);
if (projectNumber.isEmpty()) projectNumber="ProjectNumber";
// -- write Makefile
{
2023-04-25 15:44:26 +08:00
QCString mfName = Config_getString(HTML_OUTPUT) + "/Makefile";
std::ofstream ts(mfName.str(),std::ofstream::out | std::ofstream::binary);
if (!ts.is_open())
{
term("Could not open file %s for writing\n",qPrint(mfName));
}
2022-10-26 16:06:26 +08:00
2023-04-25 15:44:26 +08:00
ts << "DOCSET_NAME=" << bundleId << ".docset\n"
"DOCSET_CONTENTS=$(DOCSET_NAME)/Contents\n"
"DOCSET_RESOURCES=$(DOCSET_CONTENTS)/Resources\n"
"DOCSET_DOCUMENTS=$(DOCSET_RESOURCES)/Documents\n"
"DESTDIR=~/Library/Developer/Shared/Documentation/DocSets\n"
"XCODE_INSTALL=\"$(shell xcode-select -print-path)\"\n"
"\n"
"all: docset\n"
"\n"
"docset:\n"
"\tmkdir -p $(DOCSET_DOCUMENTS)\n"
"\tcp Nodes.xml $(DOCSET_RESOURCES)\n"
"\tcp Tokens.xml $(DOCSET_RESOURCES)\n"
"\tcp Info.plist $(DOCSET_CONTENTS)\n"
"\ttar --exclude $(DOCSET_NAME) \\\n"
"\t --exclude Nodes.xml \\\n"
"\t --exclude Tokens.xml \\\n"
"\t --exclude Info.plist \\\n"
"\t --exclude Makefile -c -f - . \\\n"
"\t | (cd $(DOCSET_DOCUMENTS); tar xvf -)\n"
"\t$(XCODE_INSTALL)/usr/bin/docsetutil index $(DOCSET_NAME)\n"
"\trm -f $(DOCSET_DOCUMENTS)/Nodes.xml\n"
"\trm -f $(DOCSET_DOCUMENTS)/Info.plist\n"
"\trm -f $(DOCSET_DOCUMENTS)/Makefile\n"
"\trm -f $(DOCSET_RESOURCES)/Nodes.xml\n"
"\trm -f $(DOCSET_RESOURCES)/Tokens.xml\n"
"\n"
"clean:\n"
"\trm -rf $(DOCSET_NAME)\n"
"\n"
"install: docset\n"
"\tmkdir -p $(DESTDIR)\n"
"\tcp -R $(DOCSET_NAME) $(DESTDIR)\n"
"\n"
"uninstall:\n"
"\trm -rf $(DESTDIR)/$(DOCSET_NAME)\n"
"\n"
"always:\n";
2022-10-26 16:06:26 +08:00
}
// -- write Info.plist
{
2023-04-25 15:44:26 +08:00
QCString plName = Config_getString(HTML_OUTPUT) + "/Info.plist";
std::ofstream ts(plName.str(),std::ofstream::out | std::ofstream::binary);
if (!ts.is_open())
{
term("Could not open file %s for writing\n",qPrint(plName));
}
2022-10-26 16:06:26 +08:00
2023-04-25 15:44:26 +08:00
ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\"\n"
"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>CFBundleName</key>\n"
" <string>" << projectName << "</string>\n"
" <key>CFBundleIdentifier</key>\n"
" <string>" << bundleId << "</string>\n"
" <key>CFBundleVersion</key>\n"
" <string>" << projectNumber << "</string>\n"
" <key>DocSetFeedName</key>\n"
" <string>" << feedName << "</string>\n"
" <key>DocSetFeedUrl</key>\n"
" <string>" << feedURL << "</string>\n"
" <key>DocSetPublisherIdentifier</key>\n"
" <string>" << publisherId << "</string>\n"
" <key>DocSetPublisherName</key>\n"
" <string>" << publisherName << "</string>\n"
// markers for Dash
" <key>DashDocSetFamily</key>\n"
" <string>doxy</string>\n"
" <key>DocSetPlatformFamily</key>\n"
" <string>doxygen</string>\n"
"</dict>\n"
"</plist>\n";
2022-10-26 16:06:26 +08:00
}
// -- start Nodes.xml
QCString notes = Config_getString(HTML_OUTPUT) + "/Nodes.xml";
2023-04-25 15:44:26 +08:00
p->ntf.open(notes.str(),std::ofstream::out | std::ofstream::binary);
if (!p->ntf.is_open())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
term("Could not open file %s for writing\n",qPrint(notes));
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->nts.setStream(&p->ntf);
2022-10-26 16:06:26 +08:00
//QCString indexName=Config_getBool(GENERATE_TREEVIEW)?"main":"index";
QCString indexName="index";
2023-04-25 15:44:26 +08:00
p->nts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
p->nts << "<DocSetNodes version=\"1.0\">\n";
p->nts << " <TOC>\n";
p->nts << " <Node>\n";
p->nts << " <Name>Root</Name>\n";
p->nts << " <Path>" << indexName << Doxygen::htmlFileExtension << "</Path>\n";
p->nts << " <Subnodes>\n";
p->indentStack.push(true);
2022-10-26 16:06:26 +08:00
QCString tokens = Config_getString(HTML_OUTPUT) + "/Tokens.xml";
2023-04-25 15:44:26 +08:00
p->ttf.open(tokens.str(),std::ofstream::out | std::ofstream::binary);
if (!p->ttf.is_open())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
term("Could not open file %s for writing\n",qPrint(tokens));
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->tts.setStream(&p->ttf);
p->tts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
p->tts << "<Tokens version=\"1.0\">\n";
2022-10-26 16:06:26 +08:00
}
void DocSets::finalize()
{
2023-04-25 15:44:26 +08:00
if (!p->indentStack.top())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " </Node>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->indentStack.pop();
p->nts << " </Subnodes>\n";
p->nts << " </Node>\n";
p->nts << " </TOC>\n";
p->nts << "</DocSetNodes>\n";
p->nts.flush();
p->ntf.close();
2022-10-26 16:06:26 +08:00
2023-04-25 15:44:26 +08:00
p->tts << "</Tokens>\n";
p->tts.flush();
p->ttf.close();
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
QCString DocSets::Private::indent()
2022-10-26 16:06:26 +08:00
{
QCString result;
2023-04-25 15:44:26 +08:00
result.fill(' ',static_cast<int>(indentStack.size()+2)*2);
2022-10-26 16:06:26 +08:00
return result;
}
void DocSets::incContentsDepth()
{
2023-04-25 15:44:26 +08:00
//printf("DocSets::incContentsDepth() depth=%zu\n",p->indentStack.size());
p->nts << p->indent() << "<Subnodes>\n";
p->indentStack.push(true);
2022-10-26 16:06:26 +08:00
}
void DocSets::decContentsDepth()
{
2023-04-25 15:44:26 +08:00
if (!p->indentStack.top())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " </Node>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << "</Subnodes>\n";
p->indentStack.pop();
//printf("DocSets::decContentsDepth() depth=%zu\n",p->indentStack.size());
2022-10-26 16:06:26 +08:00
}
void DocSets::addContentsItem(bool isDir,
2023-04-25 15:44:26 +08:00
const QCString &name,
const QCString &ref,
const QCString &file,
const QCString &anchor,
2022-10-26 16:06:26 +08:00
bool /* separateIndex */,
bool /* addToNavIndex */,
const Definition * /*def*/)
{
(void)isDir;
2023-04-25 15:44:26 +08:00
//printf("DocSets::addContentsItem(%s) depth=%zu\n",name,p->indentStack.size());
2022-10-26 16:06:26 +08:00
if (ref==0)
{
2023-04-25 15:44:26 +08:00
if (!p->indentStack.top())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " </Node>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->indentStack.top()=false;
p->nts << p->indent() << " <Node>\n";
p->nts << p->indent() << " <Name>" << convertToXML(name) << "</Name>\n";
if (!file.isEmpty() && file[0]=='^') // URL marker
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " <URL>" << convertToXML(&file[1])
<< "</URL>\n";
2022-10-26 16:06:26 +08:00
}
else // relative file
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " <Path>";
if (!file.isEmpty() && file[0]=='!') // user specified file
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << convertToXML(&file[1]);
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
else if (!file.isEmpty()) // doxygen generated file
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << addHtmlExtensionIfMissing(file);
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
p->nts << "</Path>\n";
if (!file.isEmpty() && !anchor.isEmpty())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
p->nts << p->indent() << " <Anchor>" << anchor << "</Anchor>\n";
2022-10-26 16:06:26 +08:00
}
}
}
}
void DocSets::addIndexItem(const Definition *context,const MemberDef *md,
2023-04-25 15:44:26 +08:00
const QCString &,const QCString &)
2022-10-26 16:06:26 +08:00
{
if (md==0 && context==0) return;
const FileDef *fd = 0;
const ClassDef *cd = 0;
const NamespaceDef *nd = 0;
if (md)
{
fd = md->getFileDef();
cd = md->getClassDef();
nd = md->getNamespaceDef();
if (!md->isLinkable()) return; // internal symbol
}
QCString scope;
QCString type;
QCString decl;
// determine language
QCString lang;
SrcLangExt langExt = SrcLangExt_Cpp;
if (md)
{
langExt = md->getLanguage();
}
else if (context)
{
langExt = context->getLanguage();
}
switch (langExt)
{
case SrcLangExt_Cpp:
case SrcLangExt_ObjC:
{
if (md && (md->isObjCMethod() || md->isObjCProperty()))
lang="occ"; // Objective C/C++
else if (fd && fd->name().right(2).lower()==".c")
lang="c"; // Plain C
else if (cd==0 && nd==0)
lang="c"; // Plain C symbol outside any class or namespace
else
lang="cpp"; // C++
}
break;
case SrcLangExt_IDL: lang="idl"; break; // IDL
case SrcLangExt_CSharp: lang="csharp"; break; // C#
case SrcLangExt_PHP: lang="php"; break; // PHP4/5
case SrcLangExt_D: lang="d"; break; // D
case SrcLangExt_Java: lang="java"; break; // Java
case SrcLangExt_JS: lang="javascript"; break; // JavaScript
case SrcLangExt_Python: lang="python"; break; // Python
case SrcLangExt_Fortran: lang="fortran"; break; // Fortran
case SrcLangExt_VHDL: lang="vhdl"; break; // VHDL
case SrcLangExt_XML: lang="xml"; break; // DBUS XML
case SrcLangExt_SQL: lang="sql"; break; // Sql
case SrcLangExt_Markdown:lang="markdown"; break; // Markdown
case SrcLangExt_Slice: lang="slice"; break; // Slice
2023-04-25 15:44:26 +08:00
case SrcLangExt_Lex: lang="lex"; break; // Lex
2022-10-26 16:06:26 +08:00
case SrcLangExt_Unknown: lang="unknown"; break; // should not happen!
}
if (md)
{
if (context==0)
{
if (md->getGroupDef())
context = md->getGroupDef();
else if (md->getFileDef())
context = md->getFileDef();
}
if (context==0) return; // should not happen
switch (md->memberType())
{
case MemberType_Define:
type="macro"; break;
case MemberType_Function:
if (cd && (cd->compoundType()==ClassDef::Interface ||
cd->compoundType()==ClassDef::Class))
{
if (md->isStatic())
type="clm"; // class member
else
type="instm"; // instance member
}
else if (cd && cd->compoundType()==ClassDef::Protocol)
{
if (md->isStatic())
type="intfcm"; // interface class member
else
type="intfm"; // interface member
}
else
type="func";
break;
case MemberType_Variable:
type="data"; break;
case MemberType_Typedef:
type="tdef"; break;
case MemberType_Enumeration:
type="enum"; break;
case MemberType_EnumValue:
type="econst"; break;
//case MemberDef::Prototype:
// type="prototype"; break;
case MemberType_Signal:
type="signal"; break;
case MemberType_Slot:
type="slot"; break;
case MemberType_Friend:
type="ffunc"; break;
case MemberType_DCOP:
type="dcop"; break;
case MemberType_Property:
if (cd && cd->compoundType()==ClassDef::Protocol)
type="intfp"; // interface property
else
type="instp"; // instance property
break;
case MemberType_Event:
type="event"; break;
case MemberType_Interface:
type="ifc"; break;
case MemberType_Service:
type="svc"; break;
case MemberType_Sequence:
type="sequence"; break;
case MemberType_Dictionary:
type="dictionary"; break;
}
cd = md->getClassDef();
nd = md->getNamespaceDef();
if (cd)
{
scope = cd->qualifiedName();
}
else if (nd)
{
scope = nd->name();
}
2023-04-25 15:44:26 +08:00
fd = md->getFileDef();
if (fd)
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
decl = fd->name();
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
writeToken(p->tts,md,type,lang,scope,md->anchor(),decl);
2022-10-26 16:06:26 +08:00
}
else if (context && context->isLinkable())
{
if (fd==0 && context->definitionType()==Definition::TypeFile)
{
fd = toFileDef(context);
}
if (cd==0 && context->definitionType()==Definition::TypeClass)
{
cd = toClassDef(context);
}
if (nd==0 && context->definitionType()==Definition::TypeNamespace)
{
nd = toNamespaceDef(context);
}
if (fd)
{
type="file";
}
else if (cd)
{
scope = cd->qualifiedName();
if (cd->isTemplate())
{
type="tmplt";
}
else if (cd->compoundType()==ClassDef::Protocol)
{
type="intf";
if (scope.right(2)=="-p") scope=scope.left(scope.length()-2);
}
else if (cd->compoundType()==ClassDef::Interface)
{
type="cl";
}
else if (cd->compoundType()==ClassDef::Category)
{
type="cat";
}
else
{
type = "cl";
}
2023-04-25 15:44:26 +08:00
const IncludeInfo *ii = cd->includeInfo();
2022-10-26 16:06:26 +08:00
if (ii)
{
decl=ii->includeName;
}
}
else if (nd)
{
scope = nd->name();
type = "ns";
}
2023-04-25 15:44:26 +08:00
if (p->scopes.find(context->getOutputFileBase().str())==p->scopes.end())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
writeToken(p->tts,context,type,lang,scope,QCString(),decl);
p->scopes.insert(context->getOutputFileBase().str());
2022-10-26 16:06:26 +08:00
}
}
}
2023-04-25 15:44:26 +08:00
void DocSets::writeToken(TextStream &t,
2022-10-26 16:06:26 +08:00
const Definition *d,
const QCString &type,
const QCString &lang,
2023-04-25 15:44:26 +08:00
const QCString &scope,
const QCString &anchor,
const QCString &decl)
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
t << " <Token>\n";
t << " <TokenIdentifier>\n";
2022-10-26 16:06:26 +08:00
QCString name = d->name();
if (name.right(2)=="-p") name=name.left(name.length()-2);
2023-04-25 15:44:26 +08:00
t << " <Name>" << convertToXML(name) << "</Name>\n";
2022-10-26 16:06:26 +08:00
if (!lang.isEmpty())
{
2023-04-25 15:44:26 +08:00
t << " <APILanguage>" << lang << "</APILanguage>\n";
2022-10-26 16:06:26 +08:00
}
if (!type.isEmpty())
{
2023-04-25 15:44:26 +08:00
t << " <Type>" << type << "</Type>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
if (!scope.isEmpty())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
t << " <Scope>" << convertToXML(scope) << "</Scope>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
t << " </TokenIdentifier>\n";
t << " <Path>" << addHtmlExtensionIfMissing(d->getOutputFileBase()) << "</Path>\n";
if (!anchor.isEmpty())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
t << " <Anchor>" << anchor << "</Anchor>\n";
2022-10-26 16:06:26 +08:00
}
QCString tooltip = d->briefDescriptionAsTooltip();
if (!tooltip.isEmpty())
{
2023-04-25 15:44:26 +08:00
t << " <Abstract>" << convertToXML(tooltip) << "</Abstract>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
if (!decl.isEmpty())
2022-10-26 16:06:26 +08:00
{
2023-04-25 15:44:26 +08:00
t << " <DeclaredIn>" << convertToXML(decl) << "</DeclaredIn>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
t << " </Token>\n";
2022-10-26 16:06:26 +08:00
}
2023-04-25 15:44:26 +08:00
void DocSets::addIndexFile(const QCString &name)
2022-10-26 16:06:26 +08:00
{
(void)name;
}