File: cmDocumentationFormatter.cxx

package info (click to toggle)
cmake 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,412 kB
  • sloc: ansic: 403,924; cpp: 290,826; sh: 4,091; python: 3,357; yacc: 3,106; lex: 1,189; f90: 532; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 230; perl: 217; objc: 215; xml: 198; makefile: 98; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (184 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#include "cmDocumentationFormatter.h"

#include <algorithm> // IWYU pragma: keep
#include <cassert>
#include <iomanip>
#include <iterator>
#include <ostream>
#include <string>
#include <vector>

#include <cm/string_view>
#include <cmext/string_view>

#include "cmDocumentationEntry.h"
#include "cmDocumentationSection.h"
#include "cmStringAlgorithms.h"

namespace {
auto const EOL = "\n"_s;
auto const SPACE = " "_s;
auto const TWO_SPACES = "  "_s;
auto const MAX_WIDTH_PADDING =
  std::string(cmDocumentationFormatter::TEXT_WIDTH, ' ');

void FormatLine(std::back_insert_iterator<std::vector<cm::string_view>> outIt,
                cm::string_view const text, cm::string_view const padding)
{
  auto tokens = cmTokenizedView(text, ' ', cmTokenizerMode::New);
  if (tokens.empty()) {
    return;
  }

  // Push padding in front of a first line
  if (!padding.empty()) {
    outIt = padding;
  }

  auto currentWidth = padding.size();
  auto newSentence = false;

  for (auto token : tokens) {
    // It's no need to add a space if this is a very first
    // word on a line.
    auto const needSpace = currentWidth > padding.size();
    // Evaluate the size of a current token + possibly spaces before it.
    auto const tokenWithSpaceSize = token.size() + std::size_t(needSpace) +
      std::size_t(needSpace && newSentence);
    // Check if a current word fits on a line.
    // Also, take in account:
    //  - extra space if not a first word on a line
    //  - extra space if last token ends w/ a period
    if (currentWidth + tokenWithSpaceSize <=
        cmDocumentationFormatter::TEXT_WIDTH) {
      // If not a first word on a line...
      if (needSpace) {
        // ... add a space after the last token +
        // possibly one more space if the last token
        // ends with a period (means, end of a sentence).
        outIt = newSentence ? TWO_SPACES : SPACE;
      }
      outIt = token;
      currentWidth += tokenWithSpaceSize;
    } else {
      // Start a new line!
      outIt = EOL;
      if (!padding.empty()) {
        outIt = padding;
      }
      outIt = token;
      currentWidth = padding.size() + token.size();
    }

    // Start a new sentence if the current word ends with period
    newSentence = token.back() == '.';
  }
  // Always add EOL at the end of formatted text
  outIt = EOL;
}
} // anonymous namespace

std::string cmDocumentationFormatter::Format(std::string text) const
{
  // Exit early on empty text
  if (text.empty()) {
    return {};
  }

  assert(this->TextIndent < this->TEXT_WIDTH);

  auto const padding =
    cm::string_view(MAX_WIDTH_PADDING.c_str(), this->TextIndent);

  std::vector<cm::string_view> tokens;
  auto outIt = std::back_inserter(tokens);
  auto prevWasPreFormatted = false;

  // NOTE Can't use `cmTokenizedView()` cuz every sequential EOL does matter
  // (and `cmTokenizedView()` will squeeze 'em)
  for ( // clang-format off
      std::string::size_type start = 0
    , end = text.find('\n')
    ; start < text.size()
    ; start = end + ((end != std::string::npos) ? 1 : 0)
    , end = text.find('\n', start)
    ) // clang-format on
  {
    auto const isLastLine = end == std::string::npos;
    auto const line = isLastLine
      ? cm::string_view{ text.c_str() + start }
      : cm::string_view{ text.c_str() + start, end - start };

    if (!line.empty() && line.front() == ' ') {
      // Preformatted lines go as is w/ a leading padding
      if (!padding.empty()) {
        outIt = padding;
      }
      outIt = line;
      prevWasPreFormatted = true;
    } else {
      // Separate a normal paragraph from a pre-formatted
      // w/ an extra EOL
      if (prevWasPreFormatted) {
        outIt = EOL;
      }
      if (line.empty()) {
        if (!isLastLine) {
          outIt = EOL;
        }
      } else {
        FormatLine(outIt, line, padding);
      }
      prevWasPreFormatted = false;
    }
    if (!isLastLine) {
      outIt = EOL;
    }
  }

  if (prevWasPreFormatted) {
    outIt = EOL;
  }

  return cmJoinStrings(tokens, {}, {});
}

void cmDocumentationFormatter::PrintSection(
  std::ostream& os, cmDocumentationSection const& section)
{
  std::size_t const PREFIX_SIZE =
    sizeof(cmDocumentationEntry::CustomNamePrefix) + 1u;
  // length of the "= " literal (see below)
  std::size_t const SUFFIX_SIZE = 2u;
  // legacy magic number ;-)
  std::size_t const NAME_SIZE = 29u;

  std::size_t const PADDING_SIZE = PREFIX_SIZE + SUFFIX_SIZE;
  std::size_t const TITLE_SIZE = NAME_SIZE + PADDING_SIZE;

  auto const savedIndent = this->TextIndent;

  os << section.GetName() << '\n';

  for (cmDocumentationEntry const& entry : section.GetEntries()) {
    if (!entry.Name.empty()) {
      this->TextIndent = TITLE_SIZE;
      os << std::setw(PREFIX_SIZE) << std::left << entry.CustomNamePrefix
         << std::setw(int(std::max(NAME_SIZE, entry.Name.size())))
         << entry.Name;
      if (entry.Name.size() > NAME_SIZE) {
        os << '\n' << std::setw(int(this->TextIndent - PREFIX_SIZE)) << ' ';
      }
      os << "= " << this->Format(entry.Brief).substr(this->TextIndent);
    } else {
      this->TextIndent = 0u;
      os << '\n' << this->Format(entry.Brief);
    }
  }

  os << '\n';

  this->TextIndent = savedIndent;
}