cmake/Source/cmRST.cxx

476 lines
15 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. */
2014-08-03 19:52:23 +02:00
#include "cmRST.h"
2020-02-01 23:06:01 +01:00
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <iterator>
#include <utility>
#include "cmsys/FStream.hxx"
2015-08-17 11:37:30 +02:00
#include "cmAlgorithms.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2014-08-03 19:52:23 +02:00
#include "cmVersion.h"
2016-10-30 18:24:19 +01:00
2019-11-11 23:01:05 +01:00
cmRST::cmRST(std::ostream& os, std::string docroot)
2016-07-09 11:21:54 +02:00
: OS(os)
2019-11-11 23:01:05 +01:00
, DocRoot(std::move(docroot))
2016-07-09 11:21:54 +02:00
, IncludeDepth(0)
, OutputLinePending(false)
, LastLineEndedInColonColon(false)
, Markup(MarkupNone)
, Directive(DirectiveNone)
, CMakeDirective("^.. (cmake:)?("
2021-09-14 00:13:48 +02:00
"command|envvar|genex|variable"
2016-07-09 11:21:54 +02:00
")::[ \t]+([^ \t\n]+)$")
, CMakeModuleDirective("^.. cmake-module::[ \t]+([^ \t\n]+)$")
, ParsedLiteralDirective("^.. parsed-literal::[ \t]*(.*)$")
, CodeBlockDirective("^.. code-block::[ \t]*(.*)$")
, ReplaceDirective("^.. (\\|[^|]+\\|) replace::[ \t]*(.*)$")
, IncludeDirective("^.. include::[ \t]+([^ \t\n]+)$")
, TocTreeDirective("^.. toctree::[ \t]*(.*)$")
, ProductionListDirective("^.. productionlist::[ \t]*(.*)$")
, NoteDirective("^.. note::[ \t]*(.*)$")
2019-11-11 23:01:05 +01:00
, ModuleRST(R"(^#\[(=*)\[\.rst:$)")
2016-07-09 11:21:54 +02:00
, CMakeRole("(:cmake)?:("
2021-09-14 00:13:48 +02:00
"command|cpack_gen|generator|genex|"
"variable|envvar|module|policy|"
2016-07-09 11:21:54 +02:00
"prop_cache|prop_dir|prop_gbl|prop_inst|prop_sf|"
"prop_test|prop_tgt|"
"manual"
"):`(<*([^`<]|[^` \t]<)*)([ \t]+<[^`]*>)?`")
2018-08-09 18:06:22 +02:00
, InlineLink("`(<*([^`<]|[^` \t]<)*)([ \t]+<[^`]*>)?`_")
, InlineLiteral("``([^`]*)``")
2016-07-09 11:21:54 +02:00
, Substitution("(^|[^A-Za-z0-9_])"
"((\\|[^| \t\r\n]([^|\r\n]*[^| \t\r\n])?\\|)(__|_|))"
"([^A-Za-z0-9_]|$)")
, TocTreeLink("^.*[ \t]+<([^>]+)>$")
2014-08-03 19:52:23 +02:00
{
this->Replace["|release|"] = cmVersion::GetCMakeVersion();
}
bool cmRST::ProcessFile(std::string const& fname, bool isModule)
{
cmsys::ifstream fin(fname.c_str());
2016-07-09 11:21:54 +02:00
if (fin) {
2014-08-03 19:52:23 +02:00
this->DocDir = cmSystemTools::GetFilenamePath(fname);
2016-07-09 11:21:54 +02:00
if (isModule) {
2014-08-03 19:52:23 +02:00
this->ProcessModule(fin);
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
this->ProcessRST(fin);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
this->OutputLinePending = true;
return true;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
return false;
}
void cmRST::ProcessRST(std::istream& is)
{
std::string line;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(is, line)) {
2014-08-03 19:52:23 +02:00
this->ProcessLine(line);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
this->Reset();
}
void cmRST::ProcessModule(std::istream& is)
{
std::string line;
std::string rst;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(is, line)) {
if (!rst.empty() && rst != "#") {
2014-08-03 19:52:23 +02:00
// Bracket mode: check for end bracket
std::string::size_type pos = line.find(rst);
2017-07-20 19:35:53 +02:00
if (pos == std::string::npos) {
2014-08-03 19:52:23 +02:00
this->ProcessLine(line);
2016-07-09 11:21:54 +02:00
} else {
if (line[0] != '#') {
2020-08-30 11:54:41 +02:00
line.resize(pos);
this->ProcessLine(line);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
rst.clear();
2014-08-03 19:52:23 +02:00
this->Reset();
this->OutputLinePending = true;
}
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
// Line mode: check for .rst start (bracket or line)
2016-07-09 11:21:54 +02:00
if (rst == "#") {
if (line == "#") {
2014-08-03 19:52:23 +02:00
this->ProcessLine("");
continue;
2017-07-20 19:35:53 +02:00
}
2020-08-30 11:54:41 +02:00
if (cmHasLiteralPrefix(line, "# ")) {
line.erase(0, 2);
this->ProcessLine(line);
2014-08-03 19:52:23 +02:00
continue;
}
2018-01-26 17:06:56 +01:00
rst.clear();
2017-07-20 19:35:53 +02:00
this->Reset();
this->OutputLinePending = true;
2016-07-09 11:21:54 +02:00
}
if (line == "#.rst:") {
2014-08-03 19:52:23 +02:00
rst = "#";
2016-07-09 11:21:54 +02:00
} else if (this->ModuleRST.find(line)) {
2014-08-03 19:52:23 +02:00
rst = "]" + this->ModuleRST.match(1) + "]";
}
}
2016-07-09 11:21:54 +02:00
}
if (rst == "#") {
2014-08-03 19:52:23 +02:00
this->Reset();
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
void cmRST::Reset()
{
2016-07-09 11:21:54 +02:00
if (!this->MarkupLines.empty()) {
2019-11-11 23:01:05 +01:00
cmRST::UnindentLines(this->MarkupLines);
2016-07-09 11:21:54 +02:00
}
switch (this->Directive) {
case DirectiveNone:
break;
case DirectiveParsedLiteral:
this->ProcessDirectiveParsedLiteral();
break;
case DirectiveLiteralBlock:
this->ProcessDirectiveLiteralBlock();
break;
case DirectiveCodeBlock:
this->ProcessDirectiveCodeBlock();
break;
case DirectiveReplace:
this->ProcessDirectiveReplace();
break;
case DirectiveTocTree:
this->ProcessDirectiveTocTree();
break;
}
2014-08-03 19:52:23 +02:00
this->Markup = MarkupNone;
this->Directive = DirectiveNone;
this->MarkupLines.clear();
}
void cmRST::ProcessLine(std::string const& line)
{
bool lastLineEndedInColonColon = this->LastLineEndedInColonColon;
this->LastLineEndedInColonColon = false;
// A line starting in .. is an explicit markup start.
2018-08-09 18:06:22 +02:00
if (line == ".." ||
(line.size() >= 3 && line[0] == '.' && line[1] == '.' &&
isspace(line[2]))) {
2014-08-03 19:52:23 +02:00
this->Reset();
2016-07-09 11:21:54 +02:00
this->Markup =
2017-07-20 19:35:53 +02:00
(line.find_first_not_of(" \t", 2) == std::string::npos ? MarkupEmpty
: MarkupNormal);
2020-08-30 11:54:41 +02:00
// XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
// NOLINTNEXTLINE(bugprone-branch-clone)
2016-07-09 11:21:54 +02:00
if (this->CMakeDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Output cmake domain directives and their content normally.
this->NormalLine(line);
2016-07-09 11:21:54 +02:00
} else if (this->CMakeModuleDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Process cmake-module directive: scan .cmake file comments.
std::string file = this->CMakeModuleDirective.match(1);
2016-07-09 11:21:54 +02:00
if (file.empty() || !this->ProcessInclude(file, IncludeModule)) {
2014-08-03 19:52:23 +02:00
this->NormalLine(line);
}
2016-07-09 11:21:54 +02:00
} else if (this->ParsedLiteralDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Record the literal lines to output after whole block.
this->Directive = DirectiveParsedLiteral;
this->MarkupLines.push_back(this->ParsedLiteralDirective.match(1));
2016-07-09 11:21:54 +02:00
} else if (this->CodeBlockDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Record the literal lines to output after whole block.
// Ignore the language spec and record the opening line as blank.
this->Directive = DirectiveCodeBlock;
2019-11-11 23:01:05 +01:00
this->MarkupLines.emplace_back();
2016-07-09 11:21:54 +02:00
} else if (this->ReplaceDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Record the replace directive content.
this->Directive = DirectiveReplace;
this->ReplaceName = this->ReplaceDirective.match(1);
this->MarkupLines.push_back(this->ReplaceDirective.match(2));
2016-07-09 11:21:54 +02:00
} else if (this->IncludeDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Process the include directive or output the directive and its
// content normally if it fails.
std::string file = this->IncludeDirective.match(1);
2016-07-09 11:21:54 +02:00
if (file.empty() || !this->ProcessInclude(file, IncludeNormal)) {
2014-08-03 19:52:23 +02:00
this->NormalLine(line);
}
2016-07-09 11:21:54 +02:00
} else if (this->TocTreeDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Record the toctree entries to process after whole block.
this->Directive = DirectiveTocTree;
this->MarkupLines.push_back(this->TocTreeDirective.match(1));
2016-07-09 11:21:54 +02:00
} else if (this->ProductionListDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Output productionlist directives and their content normally.
this->NormalLine(line);
2016-07-09 11:21:54 +02:00
} else if (this->NoteDirective.find(line)) {
2014-08-03 19:52:23 +02:00
// Output note directives and their content normally.
this->NormalLine(line);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// An explicit markup start followed nothing but whitespace and a
// blank line does not consume any indented text following.
2016-07-09 11:21:54 +02:00
else if (this->Markup == MarkupEmpty && line.empty()) {
2014-08-03 19:52:23 +02:00
this->NormalLine(line);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Indented lines following an explicit markup start are explicit markup.
2016-07-09 11:21:54 +02:00
else if (this->Markup && (line.empty() || isspace(line[0]))) {
2014-08-03 19:52:23 +02:00
this->Markup = MarkupNormal;
// Record markup lines if the start line was recorded.
2016-07-09 11:21:54 +02:00
if (!this->MarkupLines.empty()) {
2014-08-03 19:52:23 +02:00
this->MarkupLines.push_back(line);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// A blank line following a paragraph ending in "::" starts a literal block.
2016-07-09 11:21:54 +02:00
else if (lastLineEndedInColonColon && line.empty()) {
2014-08-03 19:52:23 +02:00
// Record the literal lines to output after whole block.
this->Markup = MarkupNormal;
this->Directive = DirectiveLiteralBlock;
2019-11-11 23:01:05 +01:00
this->MarkupLines.emplace_back();
2014-08-03 19:52:23 +02:00
this->OutputLine("", false);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Print non-markup lines.
2016-07-09 11:21:54 +02:00
else {
2014-08-03 19:52:23 +02:00
this->NormalLine(line);
2016-07-09 11:21:54 +02:00
this->LastLineEndedInColonColon =
2019-11-11 23:01:05 +01:00
(line.size() >= 2 && line[line.size() - 2] == ':' && line.back() == ':');
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
void cmRST::NormalLine(std::string const& line)
{
this->Reset();
this->OutputLine(line, true);
}
void cmRST::OutputLine(std::string const& line_in, bool inlineMarkup)
{
2016-07-09 11:21:54 +02:00
if (this->OutputLinePending) {
2014-08-03 19:52:23 +02:00
this->OS << "\n";
this->OutputLinePending = false;
2016-07-09 11:21:54 +02:00
}
if (inlineMarkup) {
2014-08-03 19:52:23 +02:00
std::string line = this->ReplaceSubstitutions(line_in);
std::string::size_type pos = 0;
2018-08-09 18:06:22 +02:00
for (;;) {
std::string::size_type* first = nullptr;
std::string::size_type role_start = std::string::npos;
std::string::size_type link_start = std::string::npos;
std::string::size_type lit_start = std::string::npos;
if (this->CMakeRole.find(line.c_str() + pos)) {
role_start = this->CMakeRole.start();
first = &role_start;
}
if (this->InlineLiteral.find(line.c_str() + pos)) {
lit_start = this->InlineLiteral.start();
if (!first || lit_start < *first) {
first = &lit_start;
}
}
if (this->InlineLink.find(line.c_str() + pos)) {
link_start = this->InlineLink.start();
if (!first || link_start < *first) {
first = &link_start;
}
}
if (first == &role_start) {
this->OS << line.substr(pos, role_start);
std::string text = this->CMakeRole.match(3);
// If a command reference has no explicit target and
// no explicit "(...)" then add "()" to the text.
if (this->CMakeRole.match(2) == "command" &&
this->CMakeRole.match(5).empty() &&
text.find_first_of("()") == std::string::npos) {
text += "()";
}
this->OS << "``" << text << "``";
pos += this->CMakeRole.end();
} else if (first == &lit_start) {
this->OS << line.substr(pos, lit_start);
std::string text = this->InlineLiteral.match(1);
pos += this->InlineLiteral.end();
this->OS << "``" << text << "``";
} else if (first == &link_start) {
this->OS << line.substr(pos, link_start);
std::string text = this->InlineLink.match(1);
bool escaped = false;
for (char c : text) {
if (escaped) {
escaped = false;
this->OS << c;
} else if (c == '\\') {
escaped = true;
} else {
this->OS << c;
}
}
pos += this->InlineLink.end();
} else {
break;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
this->OS << line.substr(pos) << "\n";
} else {
2014-08-03 19:52:23 +02:00
this->OS << line_in << "\n";
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
std::string cmRST::ReplaceSubstitutions(std::string const& line)
{
std::string out;
std::string::size_type pos = 0;
2016-07-09 11:21:54 +02:00
while (this->Substitution.find(line.c_str() + pos)) {
2014-08-03 19:52:23 +02:00
std::string::size_type start = this->Substitution.start(2);
std::string::size_type end = this->Substitution.end(2);
std::string substitute = this->Substitution.match(3);
2020-02-01 23:06:01 +01:00
auto replace = this->Replace.find(substitute);
2016-07-09 11:21:54 +02:00
if (replace != this->Replace.end()) {
2015-04-27 22:25:09 +02:00
std::pair<std::set<std::string>::iterator, bool> replaced =
2014-08-03 19:52:23 +02:00
this->Replaced.insert(substitute);
2016-07-09 11:21:54 +02:00
if (replaced.second) {
2014-08-03 19:52:23 +02:00
substitute = this->ReplaceSubstitutions(replace->second);
this->Replaced.erase(replaced.first);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
out += line.substr(pos, start);
out += substitute;
pos += end;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
out += line.substr(pos);
return out;
}
void cmRST::OutputMarkupLines(bool inlineMarkup)
{
2018-01-26 17:06:56 +01:00
for (auto line : this->MarkupLines) {
2016-07-09 11:21:54 +02:00
if (!line.empty()) {
2020-02-01 23:06:01 +01:00
line = cmStrCat(" ", line);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
this->OutputLine(line, inlineMarkup);
}
2014-08-03 19:52:23 +02:00
this->OutputLinePending = true;
}
bool cmRST::ProcessInclude(std::string file, IncludeType type)
{
bool found = false;
2016-07-09 11:21:54 +02:00
if (this->IncludeDepth < 10) {
2014-08-03 19:52:23 +02:00
cmRST r(this->OS, this->DocRoot);
r.IncludeDepth = this->IncludeDepth + 1;
r.OutputLinePending = this->OutputLinePending;
2016-07-09 11:21:54 +02:00
if (type != IncludeTocTree) {
2014-08-03 19:52:23 +02:00
r.Replace = this->Replace;
2016-07-09 11:21:54 +02:00
}
if (file[0] == '/') {
2014-08-03 19:52:23 +02:00
file = this->DocRoot + file;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
file = this->DocDir + "/" + file;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
found = r.ProcessFile(file, type == IncludeModule);
2016-07-09 11:21:54 +02:00
if (type != IncludeTocTree) {
2014-08-03 19:52:23 +02:00
this->Replace = r.Replace;
}
2016-07-09 11:21:54 +02:00
this->OutputLinePending = r.OutputLinePending;
}
2014-08-03 19:52:23 +02:00
return found;
}
void cmRST::ProcessDirectiveParsedLiteral()
{
this->OutputMarkupLines(true);
}
void cmRST::ProcessDirectiveLiteralBlock()
{
this->OutputMarkupLines(false);
}
void cmRST::ProcessDirectiveCodeBlock()
{
this->OutputMarkupLines(false);
}
void cmRST::ProcessDirectiveReplace()
{
// Record markup lines as replacement text.
std::string& replacement = this->Replace[this->ReplaceName];
2015-04-27 22:25:09 +02:00
replacement += cmJoin(this->MarkupLines, " ");
2018-01-26 17:06:56 +01:00
this->ReplaceName.clear();
2014-08-03 19:52:23 +02:00
}
void cmRST::ProcessDirectiveTocTree()
{
// Process documents referenced by toctree directive.
2018-01-26 17:06:56 +01:00
for (std::string const& line : this->MarkupLines) {
2016-07-09 11:21:54 +02:00
if (!line.empty() && line[0] != ':') {
if (this->TocTreeLink.find(line)) {
std::string const& link = this->TocTreeLink.match(1);
this->ProcessInclude(link + ".rst", IncludeTocTree);
} else {
this->ProcessInclude(line + ".rst", IncludeTocTree);
2014-08-03 19:52:23 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
void cmRST::UnindentLines(std::vector<std::string>& lines)
{
// Remove the common indentation from the second and later lines.
std::string indentText;
std::string::size_type indentEnd = 0;
bool first = true;
2016-07-09 11:21:54 +02:00
for (size_t i = 1; i < lines.size(); ++i) {
2014-08-03 19:52:23 +02:00
std::string const& line = lines[i];
// Do not consider empty lines.
2016-07-09 11:21:54 +02:00
if (line.empty()) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Record indentation on first non-empty line.
2016-07-09 11:21:54 +02:00
if (first) {
2014-08-03 19:52:23 +02:00
first = false;
indentEnd = line.find_first_not_of(" \t");
indentText = line.substr(0, indentEnd);
continue;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Truncate indentation to match that on this line.
2015-08-17 11:37:30 +02:00
indentEnd = std::min(indentEnd, line.size());
2016-07-09 11:21:54 +02:00
for (std::string::size_type j = 0; j != indentEnd; ++j) {
if (line[j] != indentText[j]) {
2014-08-03 19:52:23 +02:00
indentEnd = j;
break;
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
// Update second and later lines.
2016-07-09 11:21:54 +02:00
for (size_t i = 1; i < lines.size(); ++i) {
2014-08-03 19:52:23 +02:00
std::string& line = lines[i];
2016-07-09 11:21:54 +02:00
if (!line.empty()) {
2014-08-03 19:52:23 +02:00
line = line.substr(indentEnd);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
auto it = lines.cbegin();
2015-08-17 11:37:30 +02:00
size_t leadingEmpty = std::distance(it, cmFindNot(lines, std::string()));
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
auto rit = lines.crbegin();
2016-07-09 11:21:54 +02:00
size_t trailingEmpty =
std::distance(rit, cmFindNot(cmReverseRange(lines), std::string()));
2015-08-17 11:37:30 +02:00
2019-11-11 23:01:05 +01:00
if ((leadingEmpty + trailingEmpty) >= lines.size()) {
// All lines are empty. The markup block is empty. Leave only one.
lines.resize(1);
return;
}
2020-02-01 23:06:01 +01:00
auto contentEnd = cmRotate(lines.begin(), lines.begin() + leadingEmpty,
lines.end() - trailingEmpty);
2015-08-17 11:37:30 +02:00
lines.erase(contentEnd, lines.end());
2014-08-03 19:52:23 +02:00
}