1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmDocumentationFormatterHTML.h"
13 #include "cmDocumentationSection.h"
14 #include "cmVersion.h"
15 //----------------------------------------------------------------------------
16 static bool cmDocumentationIsHyperlinkChar(char c)
18 // This is not a complete list but works for CMake documentation.
19 return ((c >= 'A' && c <= 'Z') ||
20 (c >= 'a' && c <= 'z') ||
21 (c >= '0' && c <= '9') ||
22 c == '-' || c == '.' || c == '/' || c == '~' || c == '@' ||
23 c == ':' || c == '_' || c == '&' || c == '?' || c == '=');
26 //----------------------------------------------------------------------------
27 static void cmDocumentationPrintHTMLChar(std::ostream& os, char c)
29 // Use an escape sequence if necessary.
49 //----------------------------------------------------------------------------
50 bool cmDocumentationHTMLIsIdChar(char c)
52 // From the HTML specification:
53 // ID and NAME tokens must begin with a letter ([A-Za-z]) and may
54 // be followed by any number of letters, digits ([0-9]), hyphens
55 // ("-"), underscores ("_"), colons (":"), and periods (".").
56 return ((c >= 'A' && c <= 'Z') ||
57 (c >= 'a' && c <= 'z') ||
58 (c >= '0' && c <= '9') ||
59 c == '-' || c == '_' || c == ':' || c == '.');
62 //----------------------------------------------------------------------------
63 void cmDocumentationPrintHTMLId(std::ostream& os, const char* begin)
65 for(const char* c = begin; *c; ++c)
67 if(cmDocumentationHTMLIsIdChar(*c))
74 //----------------------------------------------------------------------------
75 const char* cmDocumentationPrintHTMLLink(std::ostream& os, const char* begin)
77 // Look for the end of the link.
78 const char* end = begin;
79 while(cmDocumentationIsHyperlinkChar(*end))
84 // Print the hyperlink itself.
86 for(const char* c = begin; c != end; ++c)
88 cmDocumentationPrintHTMLChar(os, *c);
92 // The name of the hyperlink is the text itself.
93 for(const char* c = begin; c != end; ++c)
95 cmDocumentationPrintHTMLChar(os, *c);
99 // Return the position at which to continue scanning the input
105 cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
106 :cmDocumentationFormatter()
110 void cmDocumentationFormatterHTML
111 ::PrintSection(std::ostream& os,
112 const cmDocumentationSection §ion,
115 std::string prefix = this->ComputeSectionLinkPrefix(name);
117 const std::vector<cmDocumentationEntry> &entries =
118 section.GetEntries();
120 // skip the index if the help for only a single item (--help-command,
121 // --help-policy, --help-property, --help-module) is printed
122 bool isSingleItemHelp = ((name!=0) && (strcmp(name, "SingleItem")==0));
124 if (!isSingleItemHelp)
128 os << "<h2><a name=\"section_";
129 cmDocumentationPrintHTMLId(os, name);
130 os << "\"></a>" << name << "</h2>\n";
134 for(std::vector<cmDocumentationEntry>::const_iterator op
135 = entries.begin(); op != entries.end(); ++ op )
140 for(;op != entries.end() && op->Name.size(); ++op)
144 os << " <li><a href=\"#" << prefix << ":";
145 cmDocumentationPrintHTMLId(os, op->Name.c_str());
146 os << "\"><b><code>";
147 this->PrintHTMLEscapes(os, op->Name.c_str());
148 os << "</code></b></a></li>\n";
152 break; // Skip outer loop termination test
157 for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
158 op != entries.end();)
163 for(;op != entries.end() && op->Name.size(); ++op)
168 os << " <a name=\"" << prefix << ":";
169 cmDocumentationPrintHTMLId(os, op->Name.c_str());
170 os << "\"></a><b><code>";
171 this->PrintHTMLEscapes(os, op->Name.c_str());
172 os << "</code></b>: ";
174 this->PrintHTMLEscapes(os, op->Brief.c_str());
178 this->PrintFormatted(os, op->Full.c_str());
187 this->PrintFormatted(os, op->Brief.c_str());
194 void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
198 this->PrintHTMLEscapes(os, text);
202 void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
206 this->PrintHTMLEscapes(os, text);
210 //----------------------------------------------------------------------------
211 void cmDocumentationFormatterHTML::PrintHeader(const char* docname,
215 os << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
216 << " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
217 os << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
218 << " xml:lang=\"en\" lang=\"en\">\n";
219 os << "<head><meta http-equiv=\"Content-Type\" "
220 << "content=\"text/html;charset=utf-8\" /><title>";
221 os << docname << " - " << appname;
222 os << "</title></head><body>\n";
225 //----------------------------------------------------------------------------
226 void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
228 os << "</body></html>\n";
231 //----------------------------------------------------------------------------
232 void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
235 // Hyperlink prefixes.
236 static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
238 // Print each character.
239 for(const char* p = text; *p;)
241 // Handle hyperlinks specially to make them active.
242 bool found_hyperlink = false;
243 for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
245 if(strncmp(p, *h, strlen(*h)) == 0)
247 p = cmDocumentationPrintHTMLLink(os, p);
248 found_hyperlink = true;
252 // Print other characters normally.
255 cmDocumentationPrintHTMLChar(os, *p++);
260 void cmDocumentationFormatterHTML
261 ::PrintIndex(std::ostream& os,
262 std::vector<const cmDocumentationSection *>& sections)
264 // skip the index if only the help for a single item is printed
265 if ((sections.size() == 1)
266 && (sections[0]->GetName(this->GetForm()) != 0 )
267 && (std::string(sections[0]->GetName(this->GetForm())) == "SingleItem"))
272 os << "<h2><a name=\"section_Index\"></a>Master Index "
273 << "CMake " << cmVersion::GetCMakeVersion()
276 if (!sections.empty())
279 for(unsigned int i=0; i < sections.size(); ++i)
281 std::string name = sections[i]->GetName((this->GetForm()));
282 os << " <li><a href=\"#section_";
283 cmDocumentationPrintHTMLId(os, name.c_str());
284 os << "\"><b>" << name << "</b></a></li>\n";