Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmDocumentationFormatterHTML.cxx
blobd95d00719d7474d90b9f1fac620bcf3d720ecc9c
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDocumentationFormatterHTML.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-06 16:34:23 $
7 Version: $Revision: 1.11 $
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 switch (c)
37 case '<':
38 os << "&lt;";
39 break;
40 case '>':
41 os << "&gt;";
42 break;
43 case '&':
44 os << "&amp;";
45 break;
46 case '\n':
47 os << "<br>";
48 break;
49 default:
50 os << c;
54 //----------------------------------------------------------------------------
55 const char* cmDocumentationPrintHTMLLink(std::ostream& os, const char* begin)
57 // Look for the end of the link.
58 const char* end = begin;
59 while(cmDocumentationIsHyperlinkChar(*end))
61 ++end;
64 // Print the hyperlink itself.
65 os << "<a href=\"";
66 for(const char* c = begin; c != end; ++c)
68 cmDocumentationPrintHTMLChar(os, *c);
70 os << "\">";
72 // The name of the hyperlink is the text itself.
73 for(const char* c = begin; c != end; ++c)
75 cmDocumentationPrintHTMLChar(os, *c);
77 os << "</a>";
79 // Return the position at which to continue scanning the input
80 // string.
81 return end;
85 cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
86 :cmDocumentationFormatter()
90 void cmDocumentationFormatterHTML
91 ::PrintSection(std::ostream& os,
92 const cmDocumentationSection &section,
93 const char* name)
95 if(name)
97 os << "<h2><a name=\"section_" << name << "\"/>" << name << "</h2>\n";
100 const std::vector<cmDocumentationEntry> &entries =
101 section.GetEntries();
103 os << "<ul>\n";
104 for(std::vector<cmDocumentationEntry>::const_iterator op
105 = entries.begin(); op != entries.end(); ++ op )
107 if(op->Name.size())
109 os << " <li><a href=\"#command_"
110 << op->Name.c_str() << "\"><b><code>";
111 this->PrintHTMLEscapes(os, op->Name.c_str());
112 os << "</code></b></a></li>";
115 os << "</ul>\n" ;
117 for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
118 op != entries.end();)
120 if(op->Name.size())
122 os << "<ul>\n";
123 for(;op != entries.end() && op->Name.size(); ++op)
125 os << " <li>\n";
126 if(op->Name.size())
128 os << " <a name=\"command_"<<
129 op->Name.c_str() << "\"><b><code>";
130 this->PrintHTMLEscapes(os, op->Name.c_str());
131 os << "</code></b></a>: ";
133 this->PrintHTMLEscapes(os, op->Brief.c_str());
134 if(op->Full.size())
136 os << "<br>\n ";
137 this->PrintFormatted(os, op->Full.c_str());
139 os << "\n";
140 os << " </li>\n";
142 os << "</ul>\n";
144 else
146 this->PrintFormatted(os, op->Brief.c_str());
147 os << "\n";
148 ++op;
153 void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
154 const char* text)
156 os << "<pre>";
157 this->PrintHTMLEscapes(os, text);
158 os << "</pre>\n ";
161 void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
162 const char* text)
164 os << "<p>";
165 this->PrintHTMLEscapes(os, text);
168 //----------------------------------------------------------------------------
169 void cmDocumentationFormatterHTML::PrintHeader(const char* /*name*/,
170 std::ostream& os)
172 os << "<html><body>\n";
175 //----------------------------------------------------------------------------
176 void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
178 os << "</body></html>\n";
181 //----------------------------------------------------------------------------
182 void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
183 const char* text)
185 // Hyperlink prefixes.
186 static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
188 // Print each character.
189 for(const char* p = text; *p;)
191 // Handle hyperlinks specially to make them active.
192 bool found_hyperlink = false;
193 for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
195 if(strncmp(p, *h, strlen(*h)) == 0)
197 p = cmDocumentationPrintHTMLLink(os, p);
198 found_hyperlink = true;
202 // Print other characters normally.
203 if(!found_hyperlink)
205 cmDocumentationPrintHTMLChar(os, *p++);
210 void cmDocumentationFormatterHTML
211 ::PrintIndex(std::ostream& os,
212 std::vector<const cmDocumentationSection *>& sections)
214 os << "<h2><a name=\"section_Index\"/>Master Index</h2>\n";
215 os << "<ul>\n";
216 for(unsigned int i=0; i < sections.size(); ++i)
218 std::string name = sections[i]->
219 GetName((this->GetForm()));
220 os << " <li><a href=\"#section_"
221 << name << "\"<b>" << name << "</b></a></li>\n";
223 os << "</ul>\n";