cmDocumentationFormatterText.cxx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "cmDocumentationFormatterText.h"
  14. cmDocumentationFormatterText::cmDocumentationFormatterText()
  15. :cmDocumentationFormatter()
  16. ,TextWidth(77)
  17. ,TextIndent("")
  18. {
  19. }
  20. void cmDocumentationFormatterText::PrintSection(std::ostream& os,
  21. const cmDocumentationEntry* section,
  22. const char* name)
  23. {
  24. if(name)
  25. {
  26. os <<
  27. "---------------------------------------"
  28. "---------------------------------------\n";
  29. os << name << "\n\n";
  30. }
  31. if(!section) { return; }
  32. for(const cmDocumentationEntry* op = section; op->brief.size(); ++op)
  33. {
  34. if(op->name.size())
  35. {
  36. if(op->name[0])
  37. {
  38. os << " " << op->name << "\n";
  39. }
  40. this->TextIndent = " ";
  41. this->PrintFormatted(os, op->brief.c_str());
  42. if(op->full.size())
  43. {
  44. os << "\n";
  45. this->PrintFormatted(os, op->full.c_str());
  46. }
  47. }
  48. else
  49. {
  50. this->TextIndent = "";
  51. this->PrintFormatted(os, op->brief.c_str());
  52. }
  53. os << "\n";
  54. }
  55. }
  56. void cmDocumentationFormatterText::PrintPreformatted(std::ostream& os,
  57. const char* text)
  58. {
  59. bool newline = true;
  60. for(const char* ptr = text; *ptr; ++ptr)
  61. {
  62. if(newline)
  63. {
  64. os << this->TextIndent;
  65. newline = false;
  66. }
  67. os << *ptr;
  68. if(*ptr == '\n')
  69. {
  70. newline = true;
  71. }
  72. }
  73. os << "\n";
  74. }
  75. void cmDocumentationFormatterText::PrintParagraph(std::ostream& os,
  76. const char* text)
  77. {
  78. os << this->TextIndent;
  79. this->PrintColumn(os, text);
  80. os << "\n";
  81. }
  82. void cmDocumentationFormatterText::SetIndent(const char* indent)
  83. {
  84. this->TextIndent = indent;
  85. }
  86. void cmDocumentationFormatterText::PrintColumn(std::ostream& os,
  87. const char* text)
  88. {
  89. // Print text arranged in an indented column of fixed witdh.
  90. const char* l = text;
  91. int column = 0;
  92. bool newSentence = false;
  93. bool firstLine = true;
  94. int width = this->TextWidth - static_cast<int>(strlen(this->TextIndent));
  95. // Loop until the end of the text.
  96. while(*l)
  97. {
  98. // Parse the next word.
  99. const char* r = l;
  100. while(*r && (*r != '\n') && (*r != ' ')) { ++r; }
  101. // Does it fit on this line?
  102. if(r-l < (width-column-(newSentence?1:0)))
  103. {
  104. // Word fits on this line.
  105. if(r > l)
  106. {
  107. if(column)
  108. {
  109. // Not first word on line. Separate from the previous word
  110. // by a space, or two if this is a new sentence.
  111. if(newSentence)
  112. {
  113. os << " ";
  114. column += 2;
  115. }
  116. else
  117. {
  118. os << " ";
  119. column += 1;
  120. }
  121. }
  122. else
  123. {
  124. // First word on line. Print indentation unless this is the
  125. // first line.
  126. os << (firstLine?"":this->TextIndent);
  127. }
  128. // Print the word.
  129. os.write(l, static_cast<long>(r-l));
  130. newSentence = (*(r-1) == '.');
  131. }
  132. if(*r == '\n')
  133. {
  134. // Text provided a newline. Start a new line.
  135. os << "\n";
  136. ++r;
  137. column = 0;
  138. firstLine = false;
  139. }
  140. else
  141. {
  142. // No provided newline. Continue this line.
  143. column += static_cast<long>(r-l);
  144. }
  145. }
  146. else
  147. {
  148. // Word does not fit on this line. Start a new line.
  149. os << "\n";
  150. firstLine = false;
  151. if(r > l)
  152. {
  153. os << this->TextIndent;
  154. os.write(l, static_cast<long>(r-l));
  155. column = static_cast<long>(r-l);
  156. newSentence = (*(r-1) == '.');
  157. }
  158. else
  159. {
  160. column = 0;
  161. }
  162. }
  163. // Move to beginning of next word. Skip over whitespace.
  164. l = r;
  165. while(*l && (*l == ' ')) { ++l; }
  166. }
  167. }