cmDocumentationFormatterText.cxx 4.5 KB

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