cmDocumentationFormatter.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmDocumentationFormatter.h"
  4. #include <algorithm> // IWYU pragma: keep
  5. #include <cassert>
  6. #include <iomanip>
  7. #include <iterator>
  8. #include <ostream>
  9. #include <string>
  10. #include <vector>
  11. #include <cm/string_view>
  12. #include <cmext/string_view>
  13. #include "cmDocumentationEntry.h"
  14. #include "cmDocumentationSection.h"
  15. #include "cmStringAlgorithms.h"
  16. namespace {
  17. auto const EOL = "\n"_s;
  18. auto const SPACE = " "_s;
  19. auto const TWO_SPACES = " "_s;
  20. auto const MAX_WIDTH_PADDING =
  21. std::string(cmDocumentationFormatter::TEXT_WIDTH, ' ');
  22. void FormatLine(std::back_insert_iterator<std::vector<cm::string_view>> outIt,
  23. cm::string_view const text, cm::string_view const padding)
  24. {
  25. auto tokens = cmTokenizedView(text, ' ', cmTokenizerMode::New);
  26. if (tokens.empty()) {
  27. return;
  28. }
  29. // Push padding in front of a first line
  30. if (!padding.empty()) {
  31. outIt = padding;
  32. }
  33. auto currentWidth = padding.size();
  34. auto newSentence = false;
  35. for (auto token : tokens) {
  36. // It's no need to add a space if this is a very first
  37. // word on a line.
  38. auto const needSpace = currentWidth > padding.size();
  39. // Evaluate the size of a current token + possibly spaces before it.
  40. auto const tokenWithSpaceSize = token.size() + std::size_t(needSpace) +
  41. std::size_t(needSpace && newSentence);
  42. // Check if a current word fits on a line.
  43. // Also, take in account:
  44. // - extra space if not a first word on a line
  45. // - extra space if last token ends w/ a period
  46. if (currentWidth + tokenWithSpaceSize <=
  47. cmDocumentationFormatter::TEXT_WIDTH) {
  48. // If not a first word on a line...
  49. if (needSpace) {
  50. // ... add a space after the last token +
  51. // possibly one more space if the last token
  52. // ends with a period (means, end of a sentence).
  53. outIt = newSentence ? TWO_SPACES : SPACE;
  54. }
  55. outIt = token;
  56. currentWidth += tokenWithSpaceSize;
  57. } else {
  58. // Start a new line!
  59. outIt = EOL;
  60. if (!padding.empty()) {
  61. outIt = padding;
  62. }
  63. outIt = token;
  64. currentWidth = padding.size() + token.size();
  65. }
  66. // Start a new sentence if the current word ends with period
  67. newSentence = token.back() == '.';
  68. }
  69. // Always add EOL at the end of formatted text
  70. outIt = EOL;
  71. }
  72. } // anonymous namespace
  73. std::string cmDocumentationFormatter::Format(cm::string_view text) const
  74. {
  75. // Exit early on empty text
  76. if (text.empty()) {
  77. return {};
  78. }
  79. assert(this->TextIndent < this->TEXT_WIDTH);
  80. auto const padding =
  81. cm::string_view(MAX_WIDTH_PADDING.c_str(), this->TextIndent);
  82. std::vector<cm::string_view> tokens;
  83. auto outIt = std::back_inserter(tokens);
  84. auto prevWasPreFormatted = false;
  85. // NOTE Can't use `cmTokenizedView()` cuz every sequential EOL does matter
  86. // (and `cmTokenizedView()` will squeeze 'em)
  87. for ( // clang-format off
  88. std::string::size_type start = 0
  89. , end = text.find('\n')
  90. ; start < text.size()
  91. ; start = end + ((end != std::string::npos) ? 1 : 0)
  92. , end = text.find('\n', start)
  93. ) // clang-format on
  94. {
  95. auto const isLastLine = end == std::string::npos;
  96. auto const line =
  97. isLastLine ? text.substr(start) : text.substr(start, end - start);
  98. if (!line.empty() && line.front() == ' ') {
  99. // Preformatted lines go as is w/ a leading padding
  100. if (!padding.empty()) {
  101. outIt = padding;
  102. }
  103. outIt = line;
  104. prevWasPreFormatted = true;
  105. } else {
  106. // Separate a normal paragraph from a pre-formatted
  107. // w/ an extra EOL
  108. if (prevWasPreFormatted) {
  109. outIt = EOL;
  110. }
  111. if (line.empty()) {
  112. if (!isLastLine) {
  113. outIt = EOL;
  114. }
  115. } else {
  116. FormatLine(outIt, line, padding);
  117. }
  118. prevWasPreFormatted = false;
  119. }
  120. if (!isLastLine) {
  121. outIt = EOL;
  122. }
  123. }
  124. if (prevWasPreFormatted) {
  125. outIt = EOL;
  126. }
  127. return cmJoinStrings(tokens, {}, {});
  128. }
  129. void cmDocumentationFormatter::PrintSection(
  130. std::ostream& os, cmDocumentationSection const& section)
  131. {
  132. std::size_t const PREFIX_SIZE =
  133. sizeof(cmDocumentationEntry::CustomNamePrefix) + 1u;
  134. // length of the "= " literal (see below)
  135. std::size_t const SUFFIX_SIZE = 2u;
  136. // legacy magic number ;-)
  137. std::size_t const NAME_SIZE = 29u;
  138. std::size_t const PADDING_SIZE = PREFIX_SIZE + SUFFIX_SIZE;
  139. std::size_t const TITLE_SIZE = NAME_SIZE + PADDING_SIZE;
  140. auto const savedIndent = this->TextIndent;
  141. os << section.GetName() << '\n';
  142. for (cmDocumentationEntry const& entry : section.GetEntries()) {
  143. if (!entry.Name.empty()) {
  144. this->TextIndent = TITLE_SIZE;
  145. os << std::setw(PREFIX_SIZE) << std::left << entry.CustomNamePrefix
  146. << std::setw(int(std::max(NAME_SIZE, entry.Name.size())))
  147. << entry.Name;
  148. if (entry.Name.size() > NAME_SIZE) {
  149. os << '\n' << std::setw(int(this->TextIndent - PREFIX_SIZE)) << ' ';
  150. }
  151. os << "= " << this->Format(entry.Brief).substr(this->TextIndent);
  152. } else {
  153. this->TextIndent = 0u;
  154. os << '\n' << this->Format(entry.Brief);
  155. }
  156. }
  157. os << '\n';
  158. this->TextIndent = savedIndent;
  159. }