cmDocumentationFormatterHTML.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmDocumentationFormatterHTML.h"
  11. #include "cmDocumentationSection.h"
  12. #include "cmVersion.h"
  13. //----------------------------------------------------------------------------
  14. static bool cmDocumentationIsHyperlinkChar(char c)
  15. {
  16. // This is not a complete list but works for CMake documentation.
  17. return ((c >= 'A' && c <= 'Z') ||
  18. (c >= 'a' && c <= 'z') ||
  19. (c >= '0' && c <= '9') ||
  20. c == '-' || c == '.' || c == '/' || c == '~' || c == '@' ||
  21. c == ':' || c == '_' || c == '&' || c == '?' || c == '=');
  22. }
  23. //----------------------------------------------------------------------------
  24. static void cmDocumentationPrintHTMLChar(std::ostream& os, char c)
  25. {
  26. // Use an escape sequence if necessary.
  27. switch (c)
  28. {
  29. case '<':
  30. os << "&lt;";
  31. break;
  32. case '>':
  33. os << "&gt;";
  34. break;
  35. case '&':
  36. os << "&amp;";
  37. break;
  38. case '\n':
  39. os << "<br />";
  40. break;
  41. default:
  42. os << c;
  43. }
  44. }
  45. //----------------------------------------------------------------------------
  46. bool cmDocumentationHTMLIsIdChar(char c)
  47. {
  48. // From the HTML specification:
  49. // ID and NAME tokens must begin with a letter ([A-Za-z]) and may
  50. // be followed by any number of letters, digits ([0-9]), hyphens
  51. // ("-"), underscores ("_"), colons (":"), and periods (".").
  52. return ((c >= 'A' && c <= 'Z') ||
  53. (c >= 'a' && c <= 'z') ||
  54. (c >= '0' && c <= '9') ||
  55. c == '-' || c == '_' || c == ':' || c == '.');
  56. }
  57. //----------------------------------------------------------------------------
  58. void cmDocumentationPrintHTMLId(std::ostream& os, const char* begin)
  59. {
  60. for(const char* c = begin; *c; ++c)
  61. {
  62. if(cmDocumentationHTMLIsIdChar(*c))
  63. {
  64. os << *c;
  65. }
  66. }
  67. }
  68. //----------------------------------------------------------------------------
  69. const char* cmDocumentationPrintHTMLLink(std::ostream& os, const char* begin)
  70. {
  71. // Look for the end of the link.
  72. const char* end = begin;
  73. while(cmDocumentationIsHyperlinkChar(*end))
  74. {
  75. ++end;
  76. }
  77. // Print the hyperlink itself.
  78. os << "<a href=\"";
  79. for(const char* c = begin; c != end; ++c)
  80. {
  81. cmDocumentationPrintHTMLChar(os, *c);
  82. }
  83. os << "\">";
  84. // The name of the hyperlink is the text itself.
  85. for(const char* c = begin; c != end; ++c)
  86. {
  87. cmDocumentationPrintHTMLChar(os, *c);
  88. }
  89. os << "</a>";
  90. // Return the position at which to continue scanning the input
  91. // string.
  92. return end;
  93. }
  94. cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
  95. :cmDocumentationFormatter()
  96. {
  97. }
  98. void cmDocumentationFormatterHTML
  99. ::PrintSection(std::ostream& os,
  100. const cmDocumentationSection &section,
  101. const char* name)
  102. {
  103. std::string prefix = this->ComputeSectionLinkPrefix(name);
  104. const std::vector<cmDocumentationEntry> &entries =
  105. section.GetEntries();
  106. // skip the index if the help for only a single item (--help-command,
  107. // --help-policy, --help-property, --help-module) is printed
  108. bool isSingleItemHelp = ((name!=0) && (strcmp(name, "SingleItem")==0));
  109. if (!isSingleItemHelp)
  110. {
  111. if (name)
  112. {
  113. os << "<h2><a name=\"section_";
  114. cmDocumentationPrintHTMLId(os, name);
  115. os << "\"></a>" << name << "</h2>\n";
  116. }
  117. // Is a list needed?
  118. for(std::vector<cmDocumentationEntry>::const_iterator op
  119. = entries.begin(); op != entries.end(); ++ op )
  120. {
  121. if (op->Name.size())
  122. {
  123. os << "<ul>\n";
  124. for(;op != entries.end() && op->Name.size(); ++op)
  125. {
  126. if(op->Name.size())
  127. {
  128. os << " <li><a href=\"#" << prefix << ":";
  129. cmDocumentationPrintHTMLId(os, op->Name.c_str());
  130. os << "\"><b><code>";
  131. this->PrintHTMLEscapes(os, op->Name.c_str());
  132. os << "</code></b></a></li>\n";
  133. }
  134. }
  135. os << "</ul>\n" ;
  136. break; // Skip outer loop termination test
  137. }
  138. }
  139. }
  140. for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
  141. op != entries.end();)
  142. {
  143. if(op->Name.size())
  144. {
  145. os << "<ul>\n";
  146. for(;op != entries.end() && op->Name.size(); ++op)
  147. {
  148. os << " <li>\n";
  149. if(op->Name.size())
  150. {
  151. os << " <a name=\"" << prefix << ":";
  152. cmDocumentationPrintHTMLId(os, op->Name.c_str());
  153. os << "\"></a><b><code>";
  154. this->PrintHTMLEscapes(os, op->Name.c_str());
  155. os << "</code></b>: ";
  156. }
  157. this->PrintHTMLEscapes(os, op->Brief.c_str());
  158. if(op->Full.size())
  159. {
  160. os << "<br />\n ";
  161. this->PrintFormatted(os, op->Full.c_str());
  162. }
  163. os << "\n";
  164. os << " </li>\n";
  165. }
  166. os << "</ul>\n";
  167. }
  168. else
  169. {
  170. this->PrintFormatted(os, op->Brief.c_str());
  171. os << "\n";
  172. ++op;
  173. }
  174. }
  175. }
  176. void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
  177. const char* text)
  178. {
  179. os << "<pre>";
  180. this->PrintHTMLEscapes(os, text);
  181. os << "</pre>\n ";
  182. }
  183. void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
  184. const char* text)
  185. {
  186. os << "<p>";
  187. this->PrintHTMLEscapes(os, text);
  188. os << "</p>\n";
  189. }
  190. //----------------------------------------------------------------------------
  191. void cmDocumentationFormatterHTML::PrintHeader(const char* docname,
  192. const char* appname,
  193. std::ostream& os)
  194. {
  195. os << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
  196. << " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
  197. os << "<html xmlns=\"http://www.w3.org/1999/xhtml\""
  198. << " xml:lang=\"en\" lang=\"en\">\n";
  199. os << "<head><meta http-equiv=\"Content-Type\" "
  200. << "content=\"text/html;charset=utf-8\" /><title>";
  201. os << docname << " - " << appname;
  202. os << "</title></head><body>\n";
  203. }
  204. //----------------------------------------------------------------------------
  205. void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
  206. {
  207. os << "</body></html>\n";
  208. }
  209. //----------------------------------------------------------------------------
  210. void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
  211. const char* text)
  212. {
  213. // Hyperlink prefixes.
  214. static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
  215. // Print each character.
  216. for(const char* p = text; *p;)
  217. {
  218. // Handle hyperlinks specially to make them active.
  219. bool found_hyperlink = false;
  220. for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
  221. {
  222. if(strncmp(p, *h, strlen(*h)) == 0)
  223. {
  224. p = cmDocumentationPrintHTMLLink(os, p);
  225. found_hyperlink = true;
  226. }
  227. }
  228. // Print other characters normally.
  229. if(!found_hyperlink)
  230. {
  231. cmDocumentationPrintHTMLChar(os, *p++);
  232. }
  233. }
  234. }
  235. void cmDocumentationFormatterHTML
  236. ::PrintIndex(std::ostream& os,
  237. std::vector<const cmDocumentationSection *>& sections)
  238. {
  239. // skip the index if only the help for a single item is printed
  240. if ((sections.size() == 1)
  241. && (sections[0]->GetName(this->GetForm()) != 0 )
  242. && (std::string(sections[0]->GetName(this->GetForm())) == "SingleItem"))
  243. {
  244. return;
  245. }
  246. os << "<h2><a name=\"section_Index\"></a>Master Index "
  247. << "CMake " << cmVersion::GetCMakeVersion()
  248. << "</h2>\n";
  249. if (!sections.empty())
  250. {
  251. os << "<ul>\n";
  252. for(unsigned int i=0; i < sections.size(); ++i)
  253. {
  254. std::string name = sections[i]->GetName((this->GetForm()));
  255. os << " <li><a href=\"#section_";
  256. cmDocumentationPrintHTMLId(os, name.c_str());
  257. os << "\"><b>" << name << "</b></a></li>\n";
  258. }
  259. os << "</ul>\n";
  260. }
  261. }