cmake/Source/cmDocumentationFormatter.cxx

194 lines
4.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmDocumentationFormatter.h"
2016-10-30 18:24:19 +01:00
#include "cmDocumentationEntry.h"
2014-08-03 19:52:23 +02:00
#include "cmDocumentationSection.h"
2016-10-30 18:24:19 +01:00
#include <ostream>
#include <string.h>
#include <string>
#include <vector>
2016-07-09 11:21:54 +02:00
cmDocumentationFormatter::cmDocumentationFormatter()
: TextWidth(77)
, TextIndent("")
{
}
cmDocumentationFormatter::~cmDocumentationFormatter()
{
}
2013-03-16 19:13:01 +02:00
void cmDocumentationFormatter::PrintFormatted(std::ostream& os,
const char* text)
{
2016-07-09 11:21:54 +02:00
if (!text) {
return;
2016-07-09 11:21:54 +02:00
}
const char* ptr = text;
2016-07-09 11:21:54 +02:00
while (*ptr) {
// Any ptrs starting in a space are treated as preformatted text.
std::string preformatted;
2016-07-09 11:21:54 +02:00
while (*ptr == ' ') {
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
preformatted.append(1, ch);
2016-07-09 11:21:54 +02:00
}
if (*ptr) {
++ptr;
preformatted.append(1, '\n');
}
2016-07-09 11:21:54 +02:00
}
if (!preformatted.empty()) {
this->PrintPreformatted(os, preformatted.c_str());
2016-07-09 11:21:54 +02:00
}
// Other ptrs are treated as paragraphs.
std::string paragraph;
2016-07-09 11:21:54 +02:00
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
paragraph.append(1, ch);
2016-07-09 11:21:54 +02:00
}
if (*ptr) {
++ptr;
paragraph.append(1, '\n');
2016-07-09 11:21:54 +02:00
}
if (!paragraph.empty()) {
this->PrintParagraph(os, paragraph.c_str());
}
2016-07-09 11:21:54 +02:00
}
}
2014-08-03 19:52:23 +02:00
void cmDocumentationFormatter::PrintPreformatted(std::ostream& os,
2016-07-09 11:21:54 +02:00
const char* text)
{
2014-08-03 19:52:23 +02:00
bool newline = true;
2016-07-09 11:21:54 +02:00
for (const char* ptr = text; *ptr; ++ptr) {
if (newline && *ptr != '\n') {
2014-08-03 19:52:23 +02:00
os << this->TextIndent;
newline = false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
os << *ptr;
2016-07-09 11:21:54 +02:00
if (*ptr == '\n') {
2014-08-03 19:52:23 +02:00
newline = true;
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
os << "\n";
}
void cmDocumentationFormatter::PrintParagraph(std::ostream& os,
const char* text)
{
os << this->TextIndent;
this->PrintColumn(os, text);
os << "\n";
}
void cmDocumentationFormatter::SetIndent(const char* indent)
{
this->TextIndent = indent;
}
2016-07-09 11:21:54 +02:00
void cmDocumentationFormatter::PrintColumn(std::ostream& os, const char* text)
2014-08-03 19:52:23 +02:00
{
// Print text arranged in an indented column of fixed witdh.
const char* l = text;
long column = 0;
bool newSentence = false;
bool firstLine = true;
int width = this->TextWidth - static_cast<int>(strlen(this->TextIndent));
// Loop until the end of the text.
2016-07-09 11:21:54 +02:00
while (*l) {
2014-08-03 19:52:23 +02:00
// Parse the next word.
const char* r = l;
2016-07-09 11:21:54 +02:00
while (*r && (*r != '\n') && (*r != ' ')) {
++r;
}
2014-08-03 19:52:23 +02:00
// Does it fit on this line?
2016-07-09 11:21:54 +02:00
if (r - l < (width - column - (newSentence ? 1 : 0))) {
2014-08-03 19:52:23 +02:00
// Word fits on this line.
2016-07-09 11:21:54 +02:00
if (r > l) {
if (column) {
2014-08-03 19:52:23 +02:00
// Not first word on line. Separate from the previous word
// by a space, or two if this is a new sentence.
2016-07-09 11:21:54 +02:00
if (newSentence) {
2014-08-03 19:52:23 +02:00
os << " ";
column += 2;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
os << " ";
column += 1;
}
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
// First word on line. Print indentation unless this is the
// first line.
2016-07-09 11:21:54 +02:00
os << (firstLine ? "" : this->TextIndent);
}
2014-08-03 19:52:23 +02:00
// Print the word.
2016-07-09 11:21:54 +02:00
os.write(l, static_cast<long>(r - l));
newSentence = (*(r - 1) == '.');
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (*r == '\n') {
2014-08-03 19:52:23 +02:00
// Text provided a newline. Start a new line.
os << "\n";
++r;
column = 0;
firstLine = false;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
// No provided newline. Continue this line.
2016-07-09 11:21:54 +02:00
column += static_cast<long>(r - l);
}
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
// Word does not fit on this line. Start a new line.
os << "\n";
firstLine = false;
2016-07-09 11:21:54 +02:00
if (r > l) {
2014-08-03 19:52:23 +02:00
os << this->TextIndent;
2016-07-09 11:21:54 +02:00
os.write(l, static_cast<long>(r - l));
column = static_cast<long>(r - l);
newSentence = (*(r - 1) == '.');
} else {
2014-08-03 19:52:23 +02:00
column = 0;
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Move to beginning of next word. Skip over whitespace.
l = r;
2017-04-14 19:02:05 +02:00
while (*l == ' ') {
2016-07-09 11:21:54 +02:00
++l;
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmDocumentationFormatter::PrintSection(
std::ostream& os, cmDocumentationSection const& section)
2014-08-03 19:52:23 +02:00
{
os << section.GetName() << "\n";
2016-07-09 11:21:54 +02:00
const std::vector<cmDocumentationEntry>& entries = section.GetEntries();
2018-01-26 17:06:56 +01:00
for (cmDocumentationEntry const& entry : entries) {
if (!entry.Name.empty()) {
os << " " << entry.Name;
2015-08-17 11:37:30 +02:00
this->TextIndent = " ";
2016-07-09 11:21:54 +02:00
int align = static_cast<int>(strlen(this->TextIndent)) - 4;
2018-01-26 17:06:56 +01:00
for (int i = static_cast<int>(entry.Name.size()); i < align; ++i) {
2014-08-03 19:52:23 +02:00
os << " ";
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
if (entry.Name.size() > strlen(this->TextIndent) - 4) {
2014-08-03 19:52:23 +02:00
os << "\n";
2016-07-09 11:21:54 +02:00
os.write(this->TextIndent, strlen(this->TextIndent) - 2);
}
2014-08-03 19:52:23 +02:00
os << "= ";
2018-01-26 17:06:56 +01:00
this->PrintColumn(os, entry.Brief.c_str());
2014-08-03 19:52:23 +02:00
os << "\n";
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
os << "\n";
this->TextIndent = "";
2018-01-26 17:06:56 +01:00
this->PrintFormatted(os, entry.Brief.c_str());
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
os << "\n";
}