1
0

cmDocumentationFormatterHTML.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. //----------------------------------------------------------------------------
  15. static bool cmDocumentationIsHyperlinkChar(char c)
  16. {
  17. // This is not a complete list but works for CMake documentation.
  18. return ((c >= 'A' && c <= 'Z') ||
  19. (c >= 'a' && c <= 'z') ||
  20. (c >= '0' && c <= '9') ||
  21. c == '-' || c == '.' || c == '/' || c == '~' || c == '@' ||
  22. c == ':' || c == '_' || c == '&' || c == '?' || c == '=');
  23. }
  24. //----------------------------------------------------------------------------
  25. static void cmDocumentationPrintHTMLChar(std::ostream& os, char c)
  26. {
  27. // Use an escape sequence if necessary.
  28. static cmDocumentationEntry escapes[] =
  29. {
  30. {"<", "&lt;", 0},
  31. {">", "&gt;", 0},
  32. {"&", "&amp;", 0},
  33. {"\n", "<br>", 0},
  34. {0,0,0}
  35. };
  36. for(const cmDocumentationEntry* op = escapes; op->name; ++op)
  37. {
  38. if(op->name[0] == c)
  39. {
  40. os << op->brief;
  41. return;
  42. }
  43. }
  44. // No escape sequence is needed.
  45. os << c;
  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::PrintSection(std::ostream& os,
  78. const cmDocumentationEntry* section,
  79. const char* name)
  80. {
  81. if(name)
  82. {
  83. os << "<h2>" << name << "</h2>\n";
  84. }
  85. if(!section) { return; }
  86. for(const cmDocumentationEntry* op = section; op->brief;)
  87. {
  88. if(op->name)
  89. {
  90. os << "<ul>\n";
  91. for(;op->name;++op)
  92. {
  93. os << " <li>\n";
  94. if(op->name[0])
  95. {
  96. os << " <b><code>";
  97. this->PrintHTMLEscapes(os, op->name);
  98. os << "</code></b>: ";
  99. }
  100. this->PrintHTMLEscapes(os, op->brief);
  101. if(op->full)
  102. {
  103. os << "<br>\n ";
  104. this->PrintFormatted(os, op->full);
  105. }
  106. os << "\n";
  107. os << " </li>\n";
  108. }
  109. os << "</ul>\n";
  110. }
  111. else
  112. {
  113. this->PrintFormatted(os, op->brief);
  114. os << "\n";
  115. ++op;
  116. }
  117. }
  118. }
  119. void cmDocumentationFormatterHTML::PrintPreformatted(std::ostream& os,
  120. const char* text)
  121. {
  122. os << "<pre>";
  123. this->PrintHTMLEscapes(os, text);
  124. os << "</pre>\n ";
  125. }
  126. void cmDocumentationFormatterHTML::PrintParagraph(std::ostream& os,
  127. const char* text)
  128. {
  129. os << "<p>";
  130. this->PrintHTMLEscapes(os, text);
  131. }
  132. //----------------------------------------------------------------------------
  133. void cmDocumentationFormatterHTML::PrintHeader(const char* /*name*/,
  134. std::ostream& os)
  135. {
  136. os << "<html><body>\n";
  137. }
  138. //----------------------------------------------------------------------------
  139. void cmDocumentationFormatterHTML::PrintFooter(std::ostream& os)
  140. {
  141. os << "</body></html>\n";
  142. }
  143. //----------------------------------------------------------------------------
  144. void cmDocumentationFormatterHTML::PrintHTMLEscapes(std::ostream& os,
  145. const char* text)
  146. {
  147. // Hyperlink prefixes.
  148. static const char* hyperlinks[] = {"http://", "ftp://", "mailto:", 0};
  149. // Print each character.
  150. for(const char* p = text; *p;)
  151. {
  152. // Handle hyperlinks specially to make them active.
  153. bool found_hyperlink = false;
  154. for(const char** h = hyperlinks; !found_hyperlink && *h; ++h)
  155. {
  156. if(strncmp(p, *h, strlen(*h)) == 0)
  157. {
  158. p = cmDocumentationPrintHTMLLink(os, p);
  159. found_hyperlink = true;
  160. }
  161. }
  162. // Print other characters normally.
  163. if(!found_hyperlink)
  164. {
  165. cmDocumentationPrintHTMLChar(os, *p++);
  166. }
  167. }
  168. }