cmDocumentationFormatterHTML.cxx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_" << name << "\"/>" << name << "</h2>\n";
  114. }
  115. os << "<ul>\n";
  116. for(std::vector<cmDocumentationEntry>::const_iterator op
  117. = entries.begin(); op != entries.end(); ++ op )
  118. {
  119. if(op->Name.size())
  120. {
  121. os << " <li><a href=\"#" << prefix << ":";
  122. cmDocumentationPrintHTMLId(os, op->Name.c_str());
  123. os << "\"><b><code>";
  124. this->PrintHTMLEscapes(os, op->Name.c_str());
  125. os << "</code></b></a></li>";
  126. }
  127. }
  128. os << "</ul>\n" ;
  129. }
  130. for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
  131. op != entries.end();)
  132. {
  133. if(op->Name.size())
  134. {
  135. os << "<ul>\n";
  136. for(;op != entries.end() && op->Name.size(); ++op)
  137. {
  138. os << " <li>\n";
  139. if(op->Name.size())
  140. {
  141. os << " <a name=\"" << prefix << ":";
  142. cmDocumentationPrintHTMLId(os, op->Name.c_str());
  143. os << "\"><b><code>";
  144. this->PrintHTMLEscapes(os, op->Name.c_str());
  145. os << "</code></b></a>: ";
  146. }
  147. this->PrintHTMLEscapes(os, op->Brief.c_str());
  148. if(op->Full.size())
  149. {
  150. os << "<br>\n ";
  151. this->PrintFormatted(os, op->Full.c_str());
  152. }
  153. os << "\n";
  154. os << " </li>\n";
  155. }
  156. os << "</ul>\n";
  157. }
  158. else
  159. {
  160. this->PrintFormatted(os, op->Brief.c_str());
  161. os << "\n";
  162. ++op;
  163. }
  164. }
  165. }
  166. void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
  167. const char* text)
  168. {
  169. os << "<pre>";
  170. this->PrintHTMLEscapes(os, text);
  171. os << "</pre>\n ";
  172. }
  173. void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
  174. const char* text)
  175. {
  176. os << "<p>";
  177. this->PrintHTMLEscapes(os, text);
  178. }
  179. //----------------------------------------------------------------------------
  180. void cmDocumentationFormatterHTML::PrintHeader(const char* docname,
  181. const char* appname,
  182. std::ostream& os)
  183. {
  184. os << "<html><head><title>";
  185. os << docname << " - " << appname;
  186. os << "</title></head><body>\n";
  187. }
  188. //----------------------------------------------------------------------------
  189. void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
  190. {
  191. os << "</body></html>\n";
  192. }
  193. //----------------------------------------------------------------------------
  194. void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
  195. const char* text)
  196. {
  197. // Hyperlink prefixes.
  198. static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
  199. // Print each character.
  200. for(const char* p = text; *p;)
  201. {
  202. // Handle hyperlinks specially to make them active.
  203. bool found_hyperlink = false;
  204. for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
  205. {
  206. if(strncmp(p, *h, strlen(*h)) == 0)
  207. {
  208. p = cmDocumentationPrintHTMLLink(os, p);
  209. found_hyperlink = true;
  210. }
  211. }
  212. // Print other characters normally.
  213. if(!found_hyperlink)
  214. {
  215. cmDocumentationPrintHTMLChar(os, *p++);
  216. }
  217. }
  218. }
  219. void cmDocumentationFormatterHTML
  220. ::PrintIndex(std::ostream& os,
  221. std::vector<const cmDocumentationSection *>& sections)
  222. {
  223. // skip the index if only the help for a single item is printed
  224. if ((sections.size() == 1)
  225. && (sections[0]->GetName(this->GetForm()) != 0 )
  226. && (std::string(sections[0]->GetName(this->GetForm())) == "SingleItem"))
  227. {
  228. return;
  229. }
  230. os << "<h2><a name=\"section_Index\"/>Master Index "
  231. << "CMake " << cmVersion::GetCMakeVersion()
  232. << "</h2>\n";
  233. os << "<ul>\n";
  234. for(unsigned int i=0; i < sections.size(); ++i)
  235. {
  236. std::string name = sections[i]->
  237. GetName((this->GetForm()));
  238. os << " <li><a href=\"#section_"
  239. << name << "\"<b>" << name << "</b></a></li>\n";
  240. }
  241. os << "</ul>\n";
  242. }