1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDocumentationFormatterHTML.cxx,v $
6 Date: $Date: 2007/11/27 20:59:22 $
7 Version: $Revision: 1.7 $
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 "cmDocumentationFormatterHTML.h"
18 #include "cmDocumentationSection.h"
20 //----------------------------------------------------------------------------
21 static bool cmDocumentationIsHyperlinkChar(char c
)
23 // This is not a complete list but works for CMake documentation.
24 return ((c
>= 'A' && c
<= 'Z') ||
25 (c
>= 'a' && c
<= 'z') ||
26 (c
>= '0' && c
<= '9') ||
27 c
== '-' || c
== '.' || c
== '/' || c
== '~' || c
== '@' ||
28 c
== ':' || c
== '_' || c
== '&' || c
== '?' || c
== '=');
31 //----------------------------------------------------------------------------
32 static void cmDocumentationPrintHTMLChar(std::ostream
& os
, char c
)
34 // Use an escape sequence if necessary.
35 std::map
<char,std::string
> escapes
;
36 escapes
['<'] = "<";
37 escapes
['>'] = ">";
38 escapes
['&'] = "&";
39 escapes
['\n'] = "<br>";
41 if (escapes
.find(c
) == escapes
.end())
43 // No escape sequence is needed.
52 //----------------------------------------------------------------------------
53 const char* cmDocumentationPrintHTMLLink(std::ostream
& os
, const char* begin
)
55 // Look for the end of the link.
56 const char* end
= begin
;
57 while(cmDocumentationIsHyperlinkChar(*end
))
62 // Print the hyperlink itself.
64 for(const char* c
= begin
; c
!= end
; ++c
)
66 cmDocumentationPrintHTMLChar(os
, *c
);
70 // The name of the hyperlink is the text itself.
71 for(const char* c
= begin
; c
!= end
; ++c
)
73 cmDocumentationPrintHTMLChar(os
, *c
);
77 // Return the position at which to continue scanning the input
83 cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
84 :cmDocumentationFormatter()
88 void cmDocumentationFormatterHTML
89 ::PrintSection(std::ostream
& os
,
90 const cmDocumentationSection
§ion
,
95 os
<< "<h2><a name=\"section_" << name
<< "\"/>" << name
<< "</h2>\n";
98 const std::vector
<cmDocumentationEntry
> &entries
=
102 for(std::vector
<cmDocumentationEntry
>::const_iterator op
103 = entries
.begin(); op
!= entries
.end(); ++ op
)
107 os
<< " <li><a href=\"#command_"
108 << op
->Name
.c_str() << "\"><b><code>";
109 this->PrintHTMLEscapes(os
, op
->Name
.c_str());
110 os
<< "</code></b></a></li>";
115 for(std::vector
<cmDocumentationEntry
>::const_iterator op
= entries
.begin();
116 op
!= entries
.end();)
121 for(;op
!= entries
.end() && op
->Name
.size(); ++op
)
126 os
<< " <a name=\"command_"<<
127 op
->Name
.c_str() << "\"><b><code>";
128 this->PrintHTMLEscapes(os
, op
->Name
.c_str());
129 os
<< "</code></b></a>: ";
131 this->PrintHTMLEscapes(os
, op
->Brief
.c_str());
135 this->PrintFormatted(os
, op
->Full
.c_str());
144 this->PrintFormatted(os
, op
->Brief
.c_str());
151 void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream
& os
,
155 this->PrintHTMLEscapes(os
, text
);
159 void cmDocumentationFormatterHTML::PrintParagraph(std::ostream
& os
,
163 this->PrintHTMLEscapes(os
, text
);
166 //----------------------------------------------------------------------------
167 void cmDocumentationFormatterHTML::PrintHeader(const char* /*name*/,
170 os
<< "<html><body>\n";
173 //----------------------------------------------------------------------------
174 void cmDocumentationFormatterHTML::PrintFooter(std::ostream
& os
)
176 os
<< "</body></html>\n";
179 //----------------------------------------------------------------------------
180 void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream
& os
,
183 // Hyperlink prefixes.
184 static const char* hyperlinks
[] = {"http://", "ftp://", "mailto:", 0};
186 // Print each character.
187 for(const char* p
= text
; *p
;)
189 // Handle hyperlinks specially to make them active.
190 bool found_hyperlink
= false;
191 for(const char** h
= hyperlinks
; !found_hyperlink
&& *h
; ++h
)
193 if(strncmp(p
, *h
, strlen(*h
)) == 0)
195 p
= cmDocumentationPrintHTMLLink(os
, p
);
196 found_hyperlink
= true;
200 // Print other characters normally.
203 cmDocumentationPrintHTMLChar(os
, *p
++);