2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2008-02-19 22:33:43 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2008-02-19 22:33:43 +03:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2008-02-19 22:33:43 +03:00
|
|
|
#include "cmDocumentationFormatterDocbook.h"
|
|
|
|
#include "cmDocumentationSection.h"
|
2012-09-07 12:35:08 +04:00
|
|
|
#include <algorithm>
|
|
|
|
#include <ctype.h> // for isalnum
|
2012-09-10 16:33:41 +04:00
|
|
|
|
|
|
|
static int cmIsAlnum(int c)
|
|
|
|
{
|
|
|
|
return isalnum(c);
|
|
|
|
}
|
|
|
|
|
2008-02-19 22:33:43 +03:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this function is a copy of the one in the HTML formatter
|
|
|
|
// the three functions below are slightly modified copies
|
|
|
|
static bool cmDocumentationIsHyperlinkCharDocbook(char c)
|
|
|
|
{
|
|
|
|
// This is not a complete list but works for CMake documentation.
|
|
|
|
return ((c >= 'A' && c <= 'Z') ||
|
|
|
|
(c >= 'a' && c <= 'z') ||
|
|
|
|
(c >= '0' && c <= '9') ||
|
|
|
|
c == '-' || c == '.' || c == '/' || c == '~' || c == '@' ||
|
|
|
|
c == ':' || c == '_' || c == '&' || c == '?' || c == '=');
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
static void cmDocumentationPrintDocbookChar(std::ostream& os, char c)
|
|
|
|
{
|
|
|
|
// Use an escape sequence if necessary.
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case '<':
|
|
|
|
os << "<";
|
|
|
|
break;
|
2012-08-13 21:42:58 +04:00
|
|
|
case '>':
|
2008-02-19 22:33:43 +03:00
|
|
|
os << ">";
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
os << "&";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
os << c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
const char* cmDocumentationPrintDocbookLink(std::ostream& os,const char* begin)
|
|
|
|
{
|
|
|
|
// Look for the end of the link.
|
|
|
|
const char* end = begin;
|
|
|
|
while(cmDocumentationIsHyperlinkCharDocbook(*end))
|
|
|
|
{
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the hyperlink itself.
|
|
|
|
os << "<ulink url=\"";
|
|
|
|
for(const char* c = begin; c != end; ++c)
|
|
|
|
{
|
|
|
|
cmDocumentationPrintDocbookChar(os, *c);
|
|
|
|
}
|
|
|
|
os << "\" />";
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmDocumentationPrintDocbookEscapes(std::ostream& os, const char* text)
|
|
|
|
{
|
|
|
|
// Hyperlink prefixes.
|
|
|
|
static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
|
|
|
|
|
|
|
|
// Print each character.
|
|
|
|
for(const char* p = text; *p;)
|
|
|
|
{
|
|
|
|
// Handle hyperlinks specially to make them active.
|
|
|
|
bool found_hyperlink = false;
|
|
|
|
for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
|
|
|
|
{
|
|
|
|
if(strncmp(p, *h, strlen(*h)) == 0)
|
|
|
|
{
|
|
|
|
p = cmDocumentationPrintDocbookLink(os, p);
|
|
|
|
found_hyperlink = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print other characters normally.
|
|
|
|
if(!found_hyperlink)
|
|
|
|
{
|
|
|
|
cmDocumentationPrintDocbookChar(os, *p++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:41:49 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2008-02-19 22:33:43 +03:00
|
|
|
cmDocumentationFormatterDocbook::cmDocumentationFormatterDocbook()
|
|
|
|
:cmDocumentationFormatter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:41:49 +04:00
|
|
|
//----------------------------------------------------------------------------
|
2008-02-19 22:33:43 +03:00
|
|
|
void cmDocumentationFormatterDocbook
|
|
|
|
::PrintSection(std::ostream& os,
|
|
|
|
const cmDocumentationSection §ion,
|
|
|
|
const char* name)
|
|
|
|
{
|
2012-09-07 12:41:49 +04:00
|
|
|
os << "<sect1 id=\"";
|
|
|
|
this->PrintId(os, 0, name);
|
|
|
|
os << "\">\n<title>" << name << "</title>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
|
2008-05-05 20:02:36 +04:00
|
|
|
std::string prefix = this->ComputeSectionLinkPrefix(name);
|
2012-09-07 12:41:49 +04:00
|
|
|
const std::vector<cmDocumentationEntry> &entries = section.GetEntries();
|
2008-02-19 22:33:43 +03:00
|
|
|
|
2012-09-07 12:55:58 +04:00
|
|
|
bool hasSubSections = false;
|
2012-08-13 21:42:58 +04:00
|
|
|
for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
|
2012-09-07 12:35:55 +04:00
|
|
|
op != entries.end(); ++op)
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
|
|
|
if(op->Name.size())
|
|
|
|
{
|
2012-09-07 12:55:58 +04:00
|
|
|
hasSubSections = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool inAbstract = false;
|
|
|
|
for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
|
|
|
|
op != entries.end(); ++op)
|
|
|
|
{
|
|
|
|
if(op->Name.size())
|
|
|
|
{
|
|
|
|
if(inAbstract)
|
|
|
|
{
|
|
|
|
os << "</abstract>\n";
|
|
|
|
inAbstract = false;
|
|
|
|
}
|
2012-09-07 12:35:55 +04:00
|
|
|
os << "<sect2 id=\"";
|
|
|
|
this->PrintId(os, prefix.c_str(), op->Name);
|
|
|
|
os << "\">\n<title>";
|
|
|
|
cmDocumentationPrintDocbookEscapes(os, op->Name.c_str());
|
|
|
|
os << "</title>\n";
|
|
|
|
if(op->Full.size())
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
2012-09-07 12:35:55 +04:00
|
|
|
os << "<abstract>\n<para>";
|
2008-02-19 22:33:43 +03:00
|
|
|
cmDocumentationPrintDocbookEscapes(os, op->Brief.c_str());
|
2012-09-07 12:35:55 +04:00
|
|
|
os << "</para>\n</abstract>\n";
|
|
|
|
this->PrintFormatted(os, op->Full.c_str());
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
2012-09-07 12:35:55 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this->PrintFormatted(os, op->Brief.c_str());
|
|
|
|
}
|
|
|
|
os << "</sect2>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-07 12:55:58 +04:00
|
|
|
if(hasSubSections && op == entries.begin())
|
|
|
|
{
|
|
|
|
os << "<abstract>\n";
|
|
|
|
inAbstract = true;
|
|
|
|
}
|
2008-02-19 22:33:43 +03:00
|
|
|
this->PrintFormatted(os, op->Brief.c_str());
|
|
|
|
}
|
|
|
|
}
|
2012-09-07 12:41:49 +04:00
|
|
|
|
|
|
|
// empty sections are not allowed in docbook.
|
|
|
|
if(entries.empty())
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
2012-09-07 12:41:49 +04:00
|
|
|
os << "<para/>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
2012-09-07 12:41:49 +04:00
|
|
|
|
|
|
|
os << "</sect1>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
|
|
|
|
2012-09-07 12:41:49 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmDocumentationFormatterDocbook
|
|
|
|
::PrintPreformatted(std::ostream& os, const char* text)
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
2012-09-07 12:55:58 +04:00
|
|
|
os << "<para>\n<programlisting>";
|
2008-02-19 22:33:43 +03:00
|
|
|
cmDocumentationPrintDocbookEscapes(os, text);
|
2012-09-07 12:55:58 +04:00
|
|
|
os << "</programlisting>\n</para>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
|
|
|
|
2012-09-07 12:41:49 +04:00
|
|
|
void cmDocumentationFormatterDocbook
|
|
|
|
::PrintParagraph(std::ostream& os, const char* text)
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
|
|
|
os << "<para>";
|
|
|
|
cmDocumentationPrintDocbookEscapes(os, text);
|
2012-09-07 12:41:49 +04:00
|
|
|
os << "</para>\n";
|
2008-02-19 22:33:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2012-09-07 12:41:49 +04:00
|
|
|
void cmDocumentationFormatterDocbook
|
|
|
|
::PrintHeader(const char* docname, const char* appname, std::ostream& os)
|
2008-02-19 22:33:43 +03:00
|
|
|
{
|
2012-09-09 18:55:22 +04:00
|
|
|
this->Docname = docname;
|
2012-09-07 12:35:08 +04:00
|
|
|
|
2008-02-19 22:33:43 +03:00
|
|
|
// this one is used to ensure that we don't create multiple link targets
|
2012-08-13 21:42:58 +04:00
|
|
|
// with the same name. We can clear it here since we are at the
|
2008-02-19 22:33:43 +03:00
|
|
|
// start of a document here.
|
|
|
|
this->EmittedLinkIds.clear();
|
|
|
|
|
|
|
|
os << "<?xml version=\"1.0\" ?>\n"
|
2012-09-07 12:41:49 +04:00
|
|
|
"<!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.5//EN\" "
|
|
|
|
"\"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd\" [\n"
|
2008-02-19 22:33:43 +03:00
|
|
|
"<!ENTITY % addindex \"IGNORE\">\n"
|
|
|
|
"<!ENTITY % English \"INCLUDE\"> ]>\n"
|
|
|
|
"<article>\n"
|
|
|
|
"<articleinfo>\n"
|
2008-10-10 19:23:35 +04:00
|
|
|
"<title>" << docname << " - " << appname << "</title>\n"
|
2008-02-19 22:33:43 +03:00
|
|
|
"</articleinfo>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmDocumentationFormatterDocbook::PrintFooter(std::ostream& os)
|
|
|
|
{
|
|
|
|
os << "</article>\n";
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:35:08 +04:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmDocumentationFormatterDocbook
|
|
|
|
::PrintId(std::ostream& os, const char* prefix, std::string id)
|
|
|
|
{
|
2012-09-10 16:33:41 +04:00
|
|
|
std::replace_if(id.begin(), id.end(),
|
|
|
|
std::not1(std::ptr_fun(cmIsAlnum)), '_');
|
2012-09-07 12:35:08 +04:00
|
|
|
if(prefix)
|
|
|
|
{
|
|
|
|
id = std::string(prefix) + "." + id;
|
|
|
|
}
|
2012-09-09 18:55:22 +04:00
|
|
|
os << this->Docname << '.' << id;
|
2012-09-07 12:35:08 +04:00
|
|
|
|
|
|
|
// make sure that each id exists only once. Since it seems
|
|
|
|
// not easily possible to determine which link refers to which id,
|
|
|
|
// we have at least to make sure that the duplicated id's get a
|
|
|
|
// different name (by appending an increasing number), Alex
|
|
|
|
if (this->EmittedLinkIds.find(id) == this->EmittedLinkIds.end())
|
|
|
|
{
|
|
|
|
this->EmittedLinkIds.insert(id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static unsigned int i=0;
|
|
|
|
os << i++;
|
|
|
|
}
|
|
|
|
}
|