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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDocumentationFormatter.h"
#include <algorithm>
#include <cassert>
#include <iomanip>
#include <ostream>
#include <string>
#include <utility>
#include <vector>
#include "cmDocumentationEntry.h"
#include "cmDocumentationSection.h"
#include "cmSystemTools.h"
namespace {
const char* skipSpaces(const char* ptr)
{
assert(ptr);
for (; *ptr == ' '; ++ptr) {
;
}
return ptr;
}
const char* skipToSpace(const char* ptr)
{
assert(ptr);
for (; *ptr && (*ptr != '\n') && (*ptr != ' '); ++ptr) {
;
}
return ptr;
}
}
void cmDocumentationFormatter::PrintFormatted(std::ostream& os,
std::string const& text) const
{
if (text.empty()) {
return;
}
for (const char* ptr = text.c_str(); *ptr;) {
// Any ptrs starting in a space are treated as preformatted text.
std::string preformatted;
while (*ptr == ' ') {
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
preformatted.append(1, ch);
}
if (*ptr) {
++ptr;
preformatted.append(1, '\n');
}
}
if (!preformatted.empty()) {
this->PrintPreformatted(os, preformatted);
}
// Other ptrs are treated as paragraphs.
std::string paragraph;
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
paragraph.append(1, ch);
}
if (*ptr) {
++ptr;
paragraph.append(1, '\n');
}
if (!paragraph.empty()) {
this->PrintParagraph(os, paragraph);
}
}
}
void cmDocumentationFormatter::PrintPreformatted(std::ostream& os,
std::string const& text) const
{
if (this->TextIndent) {
auto indented = text;
auto padding = std::string(this->TextIndent, ' ');
cmSystemTools::ReplaceString(indented, "\n", "\n" + padding);
indented = std::move(padding) + indented;
os << indented << '\n';
} else {
os << text << '\n';
}
}
void cmDocumentationFormatter::PrintParagraph(std::ostream& os,
std::string const& text) const
{
if (this->TextIndent) {
os << std::string(this->TextIndent, ' ');
}
this->PrintColumn(os, text);
os << '\n';
}
void cmDocumentationFormatter::PrintColumn(std::ostream& os,
std::string const& text) const
{
// Print text arranged in an indented column of fixed width.
bool newSentence = false;
bool firstLine = true;
assert(this->TextIndent < this->TextWidth);
const std::ptrdiff_t width = this->TextWidth - this->TextIndent;
std::ptrdiff_t column = 0;
// Loop until the end of the text.
for (const char *l = text.c_str(), *r = skipToSpace(text.c_str()); *l;
l = skipSpaces(r), r = skipToSpace(l)) {
// Does it fit on this line?
if (r - l < width - column - std::ptrdiff_t(newSentence)) {
// Word fits on this line.
if (r > l) {
if (column) {
// Not first word on line. Separate from the previous word
// by a space, or two if this is a new sentence.
os << &(" "[std::size_t(!newSentence)]);
column += 1u + std::ptrdiff_t(newSentence);
} else if (!firstLine && this->TextIndent) {
// First word on line. Print indentation unless this is the
// first line.
os << std::string(this->TextIndent, ' ');
}
// Print the word.
os.write(l, r - l);
newSentence = (*(r - 1) == '.');
}
if (*r == '\n') {
// Text provided a newline. Start a new line.
os << '\n';
++r;
column = 0;
firstLine = false;
} else {
// No provided newline. Continue this line.
column += r - l;
}
} else {
// Word does not fit on this line. Start a new line.
os << '\n';
firstLine = false;
if (r > l) {
os << std::string(this->TextIndent, ' ');
os.write(l, r - l);
column = r - l;
newSentence = (*(r - 1) == '.');
} else {
column = 0;
}
}
// Move to beginning of next word. Skip over whitespace.
}
}
void cmDocumentationFormatter::PrintSection(
std::ostream& os, cmDocumentationSection const& section)
{
const std::size_t PREFIX_SIZE =
sizeof(cmDocumentationEntry::CustomNamePrefix) + 1u;
// length of the "= " literal (see below)
const std::size_t SUFFIX_SIZE = 2u;
// legacy magic number ;-)
const std::size_t NAME_SIZE = 29u;
const std::size_t PADDING_SIZE = PREFIX_SIZE + SUFFIX_SIZE;
const std::size_t TITLE_SIZE = NAME_SIZE + PADDING_SIZE;
const auto 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->PrintColumn(os, entry.Brief);
os << '\n';
} else {
os << '\n';
this->TextIndent = 0u;
this->PrintFormatted(os, entry.Brief);
}
}
os << '\n';
this->TextIndent = savedIndent;
}
|