1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDocumentationFormatterDocbook.cxx,v $
6 Date: $Date: 2009-01-10 14:01:37 $
7 Version: $Revision: 1.5 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDocumentationFormatterDocbook.h"
18 #include "cmDocumentationSection.h"
19 //----------------------------------------------------------------------------
21 // this function is a copy of the one in the HTML formatter
22 // the three functions below are slightly modified copies
23 static bool cmDocumentationIsHyperlinkCharDocbook(char c
)
25 // This is not a complete list but works for CMake documentation.
26 return ((c
>= 'A' && c
<= 'Z') ||
27 (c
>= 'a' && c
<= 'z') ||
28 (c
>= '0' && c
<= '9') ||
29 c
== '-' || c
== '.' || c
== '/' || c
== '~' || c
== '@' ||
30 c
== ':' || c
== '_' || c
== '&' || c
== '?' || c
== '=');
33 //----------------------------------------------------------------------------
34 static void cmDocumentationPrintDocbookChar(std::ostream
& os
, char c
)
36 // Use an escape sequence if necessary.
53 //----------------------------------------------------------------------------
54 const char* cmDocumentationPrintDocbookLink(std::ostream
& os
,const char* begin
)
56 // Look for the end of the link.
57 const char* end
= begin
;
58 while(cmDocumentationIsHyperlinkCharDocbook(*end
))
63 // Print the hyperlink itself.
64 os
<< "<ulink url=\"";
65 for(const char* c
= begin
; c
!= end
; ++c
)
67 cmDocumentationPrintDocbookChar(os
, *c
);
74 //----------------------------------------------------------------------------
75 void cmDocumentationPrintDocbookEscapes(std::ostream
& os
, const char* text
)
77 // Hyperlink prefixes.
78 static const char* hyperlinks
[] = {"http://", "ftp://", "mailto:", 0};
80 // Print each character.
81 for(const char* p
= text
; *p
;)
83 // Handle hyperlinks specially to make them active.
84 bool found_hyperlink
= false;
85 for(const char** h
= hyperlinks
; !found_hyperlink
&& *h
; ++h
)
87 if(strncmp(p
, *h
, strlen(*h
)) == 0)
89 p
= cmDocumentationPrintDocbookLink(os
, p
);
90 found_hyperlink
= true;
94 // Print other characters normally.
97 cmDocumentationPrintDocbookChar(os
, *p
++);
103 cmDocumentationFormatterDocbook::cmDocumentationFormatterDocbook()
104 :cmDocumentationFormatter()
108 void cmDocumentationFormatterDocbook
109 ::PrintSection(std::ostream
& os
,
110 const cmDocumentationSection
§ion
,
115 std::string id
= "section_";
117 if (this->EmittedLinkIds
.find(id
) == this->EmittedLinkIds
.end())
119 this->EmittedLinkIds
.insert(id
);
120 os
<< "<sect1 id=\"section_" << name
<< "\">\n"
121 "<title>\n" << name
<< "</title>\n";
125 static unsigned int i
=0;
127 os
<< "<sect1 id=\"section_" << name
<< i
<< "\">\n"
128 "<title>\n" << name
<< "</title>\n";
132 std::string prefix
= this->ComputeSectionLinkPrefix(name
);
134 const std::vector
<cmDocumentationEntry
> &entries
=
135 section
.GetEntries();
137 if (!entries
.empty())
139 os
<< "<itemizedlist>\n";
140 for(std::vector
<cmDocumentationEntry
>::const_iterator op
141 = entries
.begin(); op
!= entries
.end(); ++ op
)
145 os
<< " <listitem><link linkend=\"" << prefix
<< "_";
146 cmDocumentationPrintDocbookEscapes(os
, op
->Name
.c_str());
147 os
<< "\"><emphasis><literal>";
148 cmDocumentationPrintDocbookEscapes(os
, op
->Name
.c_str());
149 os
<< "</literal></emphasis></link></listitem>\n";
152 os
<< "</itemizedlist>\n" ;
155 for(std::vector
<cmDocumentationEntry
>::const_iterator op
= entries
.begin();
156 op
!= entries
.end();)
160 for(;op
!= entries
.end() && op
->Name
.size(); ++op
)
164 os
<< " <para id=\"" << prefix
<< "_";
165 cmDocumentationPrintDocbookEscapes(os
, op
->Name
.c_str());
167 // make sure that each id exists only once. Since it seems
168 // not easily possible to determine which link refers to which id,
169 // we have at least to make sure that the duplicated id's get a
170 // different name (by appending an increasing number), Alex
171 std::string id
= prefix
;
174 if (this->EmittedLinkIds
.find(id
) == this->EmittedLinkIds
.end())
176 this->EmittedLinkIds
.insert(id
);
180 static unsigned int i
=0;
184 // continue as normal...
186 os
<< "\"><sect2><title>";
187 cmDocumentationPrintDocbookEscapes(os
, op
->Name
.c_str());
188 os
<< "</title></sect2> ";
190 cmDocumentationPrintDocbookEscapes(os
, op
->Brief
.c_str());
198 // a line break seems to be simply a line break with docbook
200 this->PrintFormatted(os
, op
->Full
.c_str());
207 this->PrintFormatted(os
, op
->Brief
.c_str());
218 void cmDocumentationFormatterDocbook::PrintPreformatted(std::ostream
& os
,
221 os
<< "<literallayout>";
222 cmDocumentationPrintDocbookEscapes(os
, text
);
223 os
<< "</literallayout>\n ";
226 void cmDocumentationFormatterDocbook::PrintParagraph(std::ostream
& os
,
230 cmDocumentationPrintDocbookEscapes(os
, text
);
234 //----------------------------------------------------------------------------
235 void cmDocumentationFormatterDocbook::PrintHeader(const char* docname
,
239 // this one is used to ensure that we don't create multiple link targets
240 // with the same name. We can clear it here since we are at the
241 // start of a document here.
242 this->EmittedLinkIds
.clear();
244 os
<< "<?xml version=\"1.0\" ?>\n"
245 "<!DOCTYPE article PUBLIC \"-//OASIS//DTD DocBook V4.2//EN\" "
246 "\"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd\" [\n"
247 "<!ENTITY % addindex \"IGNORE\">\n"
248 "<!ENTITY % English \"INCLUDE\"> ]>\n"
251 "<title>" << docname
<< " - " << appname
<< "</title>\n"
255 //----------------------------------------------------------------------------
256 void cmDocumentationFormatterDocbook::PrintFooter(std::ostream
& os
)
258 os
<< "</article>\n";