CVS resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmDocumentationFormatterHTML.cxx
blob9bd068cd4d9df3aef3ef2d3987cc61b3d8b1d40c
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmDocumentationFormatterHTML.cxx,v $
5 Language: C++
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['<'] = "&lt;";
37 escapes['>'] = "&gt;";
38 escapes['&'] = "&amp;";
39 escapes['\n'] = "<br>";
41 if (escapes.find(c) == escapes.end())
43 // No escape sequence is needed.
44 os << c;
45 return;
48 os << escapes[c];
49 return;
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))
59 ++end;
62 // Print the hyperlink itself.
63 os << "<a href=\"";
64 for(const char* c = begin; c != end; ++c)
66 cmDocumentationPrintHTMLChar(os, *c);
68 os << "\">";
70 // The name of the hyperlink is the text itself.
71 for(const char* c = begin; c != end; ++c)
73 cmDocumentationPrintHTMLChar(os, *c);
75 os << "</a>";
77 // Return the position at which to continue scanning the input
78 // string.
79 return end;
83 cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
84 :cmDocumentationFormatter()
88 void cmDocumentationFormatterHTML
89 ::PrintSection(std::ostream& os,
90 const cmDocumentationSection &section,
91 const char* name)
93 if(name)
95 os << "<h2><a name=\"section_" << name << "\"/>" << name << "</h2>\n";
98 const std::vector<cmDocumentationEntry> &entries =
99 section.GetEntries();
101 os << "<ul>\n";
102 for(std::vector<cmDocumentationEntry>::const_iterator op
103 = entries.begin(); op != entries.end(); ++ op )
105 if(op->Name.size())
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>";
113 os << "</ul>\n" ;
115 for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
116 op != entries.end();)
118 if(op->Name.size())
120 os << "<ul>\n";
121 for(;op != entries.end() && op->Name.size(); ++op)
123 os << " <li>\n";
124 if(op->Name.size())
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());
132 if(op->Full.size())
134 os << "<br>\n ";
135 this->PrintFormatted(os, op->Full.c_str());
137 os << "\n";
138 os << " </li>\n";
140 os << "</ul>\n";
142 else
144 this->PrintFormatted(os, op->Brief.c_str());
145 os << "\n";
146 ++op;
151 void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
152 const char* text)
154 os << "<pre>";
155 this->PrintHTMLEscapes(os, text);
156 os << "</pre>\n ";
159 void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
160 const char* text)
162 os << "<p>";
163 this->PrintHTMLEscapes(os, text);
166 //----------------------------------------------------------------------------
167 void cmDocumentationFormatterHTML::PrintHeader(const char* /*name*/,
168 std::ostream& os)
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,
181 const char* text)
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.
201 if(!found_hyperlink)
203 cmDocumentationPrintHTMLChar(os, *p++);