cmDocumentationFormatterHTML.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDocumentationFormatterHTML.h"
  14. #include "cmDocumentationSection.h"
  15. //----------------------------------------------------------------------------
  16. static bool cmDocumentationIsHyperlinkChar(char c)
  17. {
  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 == '=');
  24. }
  25. //----------------------------------------------------------------------------
  26. static void cmDocumentationPrintHTMLChar(std::ostream& os, char c)
  27. {
  28. // Use an escape sequence if necessary.
  29. switch (c)
  30. {
  31. case '<':
  32. os << "&lt;";
  33. break;
  34. case '>':
  35. os << "&gt;";
  36. break;
  37. case '&':
  38. os << "&amp;";
  39. break;
  40. case '\n':
  41. os << "<br>";
  42. break;
  43. default:
  44. os << c;
  45. }
  46. }
  47. //----------------------------------------------------------------------------
  48. const char* cmDocumentationPrintHTMLLink(std::ostream& os, const char* begin)
  49. {
  50. // Look for the end of the link.
  51. const char* end = begin;
  52. while(cmDocumentationIsHyperlinkChar(*end))
  53. {
  54. ++end;
  55. }
  56. // Print the hyperlink itself.
  57. os << "<a href=\"";
  58. for(const char* c = begin; c != end; ++c)
  59. {
  60. cmDocumentationPrintHTMLChar(os, *c);
  61. }
  62. os << "\">";
  63. // The name of the hyperlink is the text itself.
  64. for(const char* c = begin; c != end; ++c)
  65. {
  66. cmDocumentationPrintHTMLChar(os, *c);
  67. }
  68. os << "</a>";
  69. // Return the position at which to continue scanning the input
  70. // string.
  71. return end;
  72. }
  73. cmDocumentationFormatterHTML::cmDocumentationFormatterHTML()
  74. :cmDocumentationFormatter()
  75. {
  76. }
  77. void cmDocumentationFormatterHTML
  78. ::PrintSection(std::ostream& os,
  79. const cmDocumentationSection &section,
  80. const char* name)
  81. {
  82. if(name)
  83. {
  84. os << "<h2><a name=\"section_" << name << "\"/>" << name << "</h2>\n";
  85. }
  86. const std::vector<cmDocumentationEntry> &entries =
  87. section.GetEntries();
  88. os << "<ul>\n";
  89. for(std::vector<cmDocumentationEntry>::const_iterator op
  90. = entries.begin(); op != entries.end(); ++ op )
  91. {
  92. if(op->Name.size())
  93. {
  94. os << " <li><a href=\"#command_"
  95. << op->Name.c_str() << "\"><b><code>";
  96. this->PrintHTMLEscapes(os, op->Name.c_str());
  97. os << "</code></b></a></li>";
  98. }
  99. }
  100. os << "</ul>\n" ;
  101. for(std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
  102. op != entries.end();)
  103. {
  104. if(op->Name.size())
  105. {
  106. os << "<ul>\n";
  107. for(;op != entries.end() && op->Name.size(); ++op)
  108. {
  109. os << " <li>\n";
  110. if(op->Name.size())
  111. {
  112. os << " <a name=\"command_"<<
  113. op->Name.c_str() << "\"><b><code>";
  114. this->PrintHTMLEscapes(os, op->Name.c_str());
  115. os << "</code></b></a>: ";
  116. }
  117. this->PrintHTMLEscapes(os, op->Brief.c_str());
  118. if(op->Full.size())
  119. {
  120. os << "<br>\n ";
  121. this->PrintFormatted(os, op->Full.c_str());
  122. }
  123. os << "\n";
  124. os << " </li>\n";
  125. }
  126. os << "</ul>\n";
  127. }
  128. else
  129. {
  130. this->PrintFormatted(os, op->Brief.c_str());
  131. os << "\n";
  132. ++op;
  133. }
  134. }
  135. }
  136. void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
  137. const char* text)
  138. {
  139. os << "<pre>";
  140. this->PrintHTMLEscapes(os, text);
  141. os << "</pre>\n ";
  142. }
  143. void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
  144. const char* text)
  145. {
  146. os << "<p>";
  147. this->PrintHTMLEscapes(os, text);
  148. }
  149. //----------------------------------------------------------------------------
  150. void cmDocumentationFormatterHTML::PrintHeader(const char* /*name*/,
  151. std::ostream& os)
  152. {
  153. os << "<html><body>\n";
  154. }
  155. //----------------------------------------------------------------------------
  156. void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
  157. {
  158. os << "</body></html>\n";
  159. }
  160. //----------------------------------------------------------------------------
  161. void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
  162. const char* text)
  163. {
  164. // Hyperlink prefixes.
  165. static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
  166. // Print each character.
  167. for(const char* p = text; *p;)
  168. {
  169. // Handle hyperlinks specially to make them active.
  170. bool found_hyperlink = false;
  171. for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
  172. {
  173. if(strncmp(p, *h, strlen(*h)) == 0)
  174. {
  175. p = cmDocumentationPrintHTMLLink(os, p);
  176. found_hyperlink = true;
  177. }
  178. }
  179. // Print other characters normally.
  180. if(!found_hyperlink)
  181. {
  182. cmDocumentationPrintHTMLChar(os, *p++);
  183. }
  184. }
  185. }
  186. void cmDocumentationFormatterHTML
  187. ::PrintIndex(std::ostream& os,
  188. std::vector<const cmDocumentationSection *>& sections)
  189. {
  190. os << "<h2><a name=\"section_Index\"/>Master Index</h2>\n";
  191. os << "<ul>\n";
  192. for(unsigned int i=0; i < sections.size(); ++i)
  193. {
  194. std::string name = sections[i]->
  195. GetName((this->GetForm()));
  196. os << " <li><a href=\"#section_" << name << "\"<b>" << name << "</b></a></li>\n";
  197. }
  198. os << "</ul>\n";
  199. }