2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2007-09-19 17:05:28 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2007-09-19 17:05:28 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2007-09-19 17:05:28 +04:00
|
|
|
#include "cmDocumentationFormatter.h"
|
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#include "cmDocumentationEntry.h"
|
2013-09-14 00:09:52 +04:00
|
|
|
#include "cmDocumentationSection.h"
|
|
|
|
|
2016-09-01 21:59:28 +03:00
|
|
|
#include <ostream>
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmDocumentationFormatter::cmDocumentationFormatter()
|
|
|
|
: TextWidth(77)
|
|
|
|
, TextIndent("")
|
2007-09-19 17:05:28 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmDocumentationFormatter::~cmDocumentationFormatter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
void cmDocumentationFormatter::PrintFormatted(std::ostream& os,
|
2007-09-19 17:05:28 +04:00
|
|
|
const char* text)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!text) {
|
2007-09-19 17:05:28 +04:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-09-19 17:05:28 +04:00
|
|
|
const char* ptr = text;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*ptr) {
|
2007-09-19 17:05:28 +04:00
|
|
|
// Any ptrs starting in a space are treated as preformatted text.
|
|
|
|
std::string preformatted;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*ptr == ' ') {
|
|
|
|
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
|
2007-09-19 17:05:28 +04:00
|
|
|
preformatted.append(1, ch);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*ptr) {
|
2007-09-19 17:05:28 +04:00
|
|
|
++ptr;
|
|
|
|
preformatted.append(1, '\n');
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!preformatted.empty()) {
|
2007-09-19 17:05:28 +04:00
|
|
|
this->PrintPreformatted(os, preformatted.c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-09-19 17:05:28 +04:00
|
|
|
|
|
|
|
// Other ptrs are treated as paragraphs.
|
|
|
|
std::string paragraph;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr) {
|
2007-09-19 17:05:28 +04:00
|
|
|
paragraph.append(1, ch);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (*ptr) {
|
2007-09-19 17:05:28 +04:00
|
|
|
++ptr;
|
|
|
|
paragraph.append(1, '\n');
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (!paragraph.empty()) {
|
2007-09-19 17:05:28 +04:00
|
|
|
this->PrintParagraph(os, paragraph.c_str());
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2007-09-19 17:05:28 +04:00
|
|
|
}
|
|
|
|
|
2013-09-14 00:09:52 +04:00
|
|
|
void cmDocumentationFormatter::PrintPreformatted(std::ostream& os,
|
2016-05-16 17:34:04 +03:00
|
|
|
const char* text)
|
2008-05-05 20:02:36 +04:00
|
|
|
{
|
2013-09-14 00:09:52 +04:00
|
|
|
bool newline = true;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (const char* ptr = text; *ptr; ++ptr) {
|
|
|
|
if (newline && *ptr != '\n') {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << this->TextIndent;
|
|
|
|
newline = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
os << *ptr;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*ptr == '\n') {
|
2013-09-14 00:09:52 +04:00
|
|
|
newline = true;
|
2008-05-05 20:02:36 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-09-14 00:09:52 +04: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-05-16 17:34:04 +03:00
|
|
|
void cmDocumentationFormatter::PrintColumn(std::ostream& os, const char* text)
|
2013-09-14 00:09:52 +04: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-05-16 17:34:04 +03:00
|
|
|
while (*l) {
|
2013-09-14 00:09:52 +04:00
|
|
|
// Parse the next word.
|
|
|
|
const char* r = l;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*r && (*r != '\n') && (*r != ' ')) {
|
|
|
|
++r;
|
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
|
|
|
|
// Does it fit on this line?
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r - l < (width - column - (newSentence ? 1 : 0))) {
|
2013-09-14 00:09:52 +04:00
|
|
|
// Word fits on this line.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r > l) {
|
|
|
|
if (column) {
|
2013-09-14 00:09:52 +04:00
|
|
|
// Not first word on line. Separate from the previous word
|
|
|
|
// by a space, or two if this is a new sentence.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (newSentence) {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << " ";
|
|
|
|
column += 2;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << " ";
|
|
|
|
column += 1;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
// First word on line. Print indentation unless this is the
|
|
|
|
// first line.
|
2016-05-16 17:34:04 +03:00
|
|
|
os << (firstLine ? "" : this->TextIndent);
|
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
|
|
|
|
// Print the word.
|
2016-05-16 17:34:04 +03:00
|
|
|
os.write(l, static_cast<long>(r - l));
|
|
|
|
newSentence = (*(r - 1) == '.');
|
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (*r == '\n') {
|
2013-09-14 00:09:52 +04:00
|
|
|
// Text provided a newline. Start a new line.
|
|
|
|
os << "\n";
|
|
|
|
++r;
|
|
|
|
column = 0;
|
|
|
|
firstLine = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
// No provided newline. Continue this line.
|
2016-05-16 17:34:04 +03:00
|
|
|
column += static_cast<long>(r - l);
|
2013-09-12 18:05:46 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
// Word does not fit on this line. Start a new line.
|
|
|
|
os << "\n";
|
|
|
|
firstLine = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (r > l) {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << this->TextIndent;
|
2016-05-16 17:34:04 +03:00
|
|
|
os.write(l, static_cast<long>(r - l));
|
|
|
|
column = static_cast<long>(r - l);
|
|
|
|
newSentence = (*(r - 1) == '.');
|
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
column = 0;
|
2013-09-12 18:05:46 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
|
|
|
|
// Move to beginning of next word. Skip over whitespace.
|
|
|
|
l = r;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (*l && (*l == ' ')) {
|
|
|
|
++l;
|
2013-09-14 00:09:52 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
void cmDocumentationFormatter::PrintSection(
|
|
|
|
std::ostream& os, cmDocumentationSection const& section)
|
2013-09-14 00:09:52 +04:00
|
|
|
{
|
|
|
|
os << section.GetName() << "\n";
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
const std::vector<cmDocumentationEntry>& entries = section.GetEntries();
|
|
|
|
for (std::vector<cmDocumentationEntry>::const_iterator op = entries.begin();
|
|
|
|
op != entries.end(); ++op) {
|
|
|
|
if (!op->Name.empty()) {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << " " << op->Name;
|
2015-04-07 23:33:05 +03:00
|
|
|
this->TextIndent = " ";
|
2016-05-16 17:34:04 +03:00
|
|
|
int align = static_cast<int>(strlen(this->TextIndent)) - 4;
|
|
|
|
for (int i = static_cast<int>(op->Name.size()); i < align; ++i) {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << " ";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (op->Name.size() > strlen(this->TextIndent) - 4) {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << "\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
os.write(this->TextIndent, strlen(this->TextIndent) - 2);
|
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
os << "= ";
|
|
|
|
this->PrintColumn(os, op->Brief.c_str());
|
|
|
|
os << "\n";
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2013-09-14 00:09:52 +04:00
|
|
|
os << "\n";
|
|
|
|
this->TextIndent = "";
|
|
|
|
this->PrintFormatted(os, op->Brief.c_str());
|
2008-05-05 20:02:36 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2013-09-14 00:09:52 +04:00
|
|
|
os << "\n";
|
2008-05-05 20:02:36 +04:00
|
|
|
}
|