cmRST.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmRST.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmSystemTools.h"
  6. #include "cmVersion.h"
  7. #include "cmsys/FStream.hxx"
  8. #include <algorithm>
  9. #include <ctype.h>
  10. #include <iterator>
  11. #include <stddef.h>
  12. #include <utility>
  13. cmRST::cmRST(std::ostream& os, std::string const& docroot)
  14. : OS(os)
  15. , DocRoot(docroot)
  16. , IncludeDepth(0)
  17. , OutputLinePending(false)
  18. , LastLineEndedInColonColon(false)
  19. , Markup(MarkupNone)
  20. , Directive(DirectiveNone)
  21. , CMakeDirective("^.. (cmake:)?("
  22. "command|variable"
  23. ")::[ \t]+([^ \t\n]+)$")
  24. , CMakeModuleDirective("^.. cmake-module::[ \t]+([^ \t\n]+)$")
  25. , ParsedLiteralDirective("^.. parsed-literal::[ \t]*(.*)$")
  26. , CodeBlockDirective("^.. code-block::[ \t]*(.*)$")
  27. , ReplaceDirective("^.. (\\|[^|]+\\|) replace::[ \t]*(.*)$")
  28. , IncludeDirective("^.. include::[ \t]+([^ \t\n]+)$")
  29. , TocTreeDirective("^.. toctree::[ \t]*(.*)$")
  30. , ProductionListDirective("^.. productionlist::[ \t]*(.*)$")
  31. , NoteDirective("^.. note::[ \t]*(.*)$")
  32. , ModuleRST("^#\\[(=*)\\[\\.rst:$")
  33. , CMakeRole("(:cmake)?:("
  34. "command|generator|variable|module|policy|"
  35. "prop_cache|prop_dir|prop_gbl|prop_inst|prop_sf|"
  36. "prop_test|prop_tgt|"
  37. "manual"
  38. "):`(<*([^`<]|[^` \t]<)*)([ \t]+<[^`]*>)?`")
  39. , Substitution("(^|[^A-Za-z0-9_])"
  40. "((\\|[^| \t\r\n]([^|\r\n]*[^| \t\r\n])?\\|)(__|_|))"
  41. "([^A-Za-z0-9_]|$)")
  42. , TocTreeLink("^.*[ \t]+<([^>]+)>$")
  43. {
  44. this->Replace["|release|"] = cmVersion::GetCMakeVersion();
  45. }
  46. bool cmRST::ProcessFile(std::string const& fname, bool isModule)
  47. {
  48. cmsys::ifstream fin(fname.c_str());
  49. if (fin) {
  50. this->DocDir = cmSystemTools::GetFilenamePath(fname);
  51. if (isModule) {
  52. this->ProcessModule(fin);
  53. } else {
  54. this->ProcessRST(fin);
  55. }
  56. this->OutputLinePending = true;
  57. return true;
  58. }
  59. return false;
  60. }
  61. void cmRST::ProcessRST(std::istream& is)
  62. {
  63. std::string line;
  64. while (cmSystemTools::GetLineFromStream(is, line)) {
  65. this->ProcessLine(line);
  66. }
  67. this->Reset();
  68. }
  69. void cmRST::ProcessModule(std::istream& is)
  70. {
  71. std::string line;
  72. std::string rst;
  73. while (cmSystemTools::GetLineFromStream(is, line)) {
  74. if (!rst.empty() && rst != "#") {
  75. // Bracket mode: check for end bracket
  76. std::string::size_type pos = line.find(rst);
  77. if (pos == std::string::npos) {
  78. this->ProcessLine(line);
  79. } else {
  80. if (line[0] != '#') {
  81. this->ProcessLine(line.substr(0, pos));
  82. }
  83. rst.clear();
  84. this->Reset();
  85. this->OutputLinePending = true;
  86. }
  87. } else {
  88. // Line mode: check for .rst start (bracket or line)
  89. if (rst == "#") {
  90. if (line == "#") {
  91. this->ProcessLine("");
  92. continue;
  93. }
  94. if (line.substr(0, 2) == "# ") {
  95. this->ProcessLine(line.substr(2));
  96. continue;
  97. }
  98. rst.clear();
  99. this->Reset();
  100. this->OutputLinePending = true;
  101. }
  102. if (line == "#.rst:") {
  103. rst = "#";
  104. } else if (this->ModuleRST.find(line)) {
  105. rst = "]" + this->ModuleRST.match(1) + "]";
  106. }
  107. }
  108. }
  109. if (rst == "#") {
  110. this->Reset();
  111. }
  112. }
  113. void cmRST::Reset()
  114. {
  115. if (!this->MarkupLines.empty()) {
  116. this->UnindentLines(this->MarkupLines);
  117. }
  118. switch (this->Directive) {
  119. case DirectiveNone:
  120. break;
  121. case DirectiveParsedLiteral:
  122. this->ProcessDirectiveParsedLiteral();
  123. break;
  124. case DirectiveLiteralBlock:
  125. this->ProcessDirectiveLiteralBlock();
  126. break;
  127. case DirectiveCodeBlock:
  128. this->ProcessDirectiveCodeBlock();
  129. break;
  130. case DirectiveReplace:
  131. this->ProcessDirectiveReplace();
  132. break;
  133. case DirectiveTocTree:
  134. this->ProcessDirectiveTocTree();
  135. break;
  136. }
  137. this->Markup = MarkupNone;
  138. this->Directive = DirectiveNone;
  139. this->MarkupLines.clear();
  140. }
  141. void cmRST::ProcessLine(std::string const& line)
  142. {
  143. bool lastLineEndedInColonColon = this->LastLineEndedInColonColon;
  144. this->LastLineEndedInColonColon = false;
  145. // A line starting in .. is an explicit markup start.
  146. if (line == ".." || (line.size() >= 3 && line[0] == '.' && line[1] == '.' &&
  147. isspace(line[2]))) {
  148. this->Reset();
  149. this->Markup =
  150. (line.find_first_not_of(" \t", 2) == std::string::npos ? MarkupEmpty
  151. : MarkupNormal);
  152. if (this->CMakeDirective.find(line)) {
  153. // Output cmake domain directives and their content normally.
  154. this->NormalLine(line);
  155. } else if (this->CMakeModuleDirective.find(line)) {
  156. // Process cmake-module directive: scan .cmake file comments.
  157. std::string file = this->CMakeModuleDirective.match(1);
  158. if (file.empty() || !this->ProcessInclude(file, IncludeModule)) {
  159. this->NormalLine(line);
  160. }
  161. } else if (this->ParsedLiteralDirective.find(line)) {
  162. // Record the literal lines to output after whole block.
  163. this->Directive = DirectiveParsedLiteral;
  164. this->MarkupLines.push_back(this->ParsedLiteralDirective.match(1));
  165. } else if (this->CodeBlockDirective.find(line)) {
  166. // Record the literal lines to output after whole block.
  167. // Ignore the language spec and record the opening line as blank.
  168. this->Directive = DirectiveCodeBlock;
  169. this->MarkupLines.push_back("");
  170. } else if (this->ReplaceDirective.find(line)) {
  171. // Record the replace directive content.
  172. this->Directive = DirectiveReplace;
  173. this->ReplaceName = this->ReplaceDirective.match(1);
  174. this->MarkupLines.push_back(this->ReplaceDirective.match(2));
  175. } else if (this->IncludeDirective.find(line)) {
  176. // Process the include directive or output the directive and its
  177. // content normally if it fails.
  178. std::string file = this->IncludeDirective.match(1);
  179. if (file.empty() || !this->ProcessInclude(file, IncludeNormal)) {
  180. this->NormalLine(line);
  181. }
  182. } else if (this->TocTreeDirective.find(line)) {
  183. // Record the toctree entries to process after whole block.
  184. this->Directive = DirectiveTocTree;
  185. this->MarkupLines.push_back(this->TocTreeDirective.match(1));
  186. } else if (this->ProductionListDirective.find(line)) {
  187. // Output productionlist directives and their content normally.
  188. this->NormalLine(line);
  189. } else if (this->NoteDirective.find(line)) {
  190. // Output note directives and their content normally.
  191. this->NormalLine(line);
  192. }
  193. }
  194. // An explicit markup start followed nothing but whitespace and a
  195. // blank line does not consume any indented text following.
  196. else if (this->Markup == MarkupEmpty && line.empty()) {
  197. this->NormalLine(line);
  198. }
  199. // Indented lines following an explicit markup start are explicit markup.
  200. else if (this->Markup && (line.empty() || isspace(line[0]))) {
  201. this->Markup = MarkupNormal;
  202. // Record markup lines if the start line was recorded.
  203. if (!this->MarkupLines.empty()) {
  204. this->MarkupLines.push_back(line);
  205. }
  206. }
  207. // A blank line following a paragraph ending in "::" starts a literal block.
  208. else if (lastLineEndedInColonColon && line.empty()) {
  209. // Record the literal lines to output after whole block.
  210. this->Markup = MarkupNormal;
  211. this->Directive = DirectiveLiteralBlock;
  212. this->MarkupLines.push_back("");
  213. this->OutputLine("", false);
  214. }
  215. // Print non-markup lines.
  216. else {
  217. this->NormalLine(line);
  218. this->LastLineEndedInColonColon =
  219. (line.size() >= 2 && line[line.size() - 2] == ':' &&
  220. line[line.size() - 1] == ':');
  221. }
  222. }
  223. void cmRST::NormalLine(std::string const& line)
  224. {
  225. this->Reset();
  226. this->OutputLine(line, true);
  227. }
  228. void cmRST::OutputLine(std::string const& line_in, bool inlineMarkup)
  229. {
  230. if (this->OutputLinePending) {
  231. this->OS << "\n";
  232. this->OutputLinePending = false;
  233. }
  234. if (inlineMarkup) {
  235. std::string line = this->ReplaceSubstitutions(line_in);
  236. std::string::size_type pos = 0;
  237. while (this->CMakeRole.find(line.c_str() + pos)) {
  238. this->OS << line.substr(pos, this->CMakeRole.start());
  239. std::string text = this->CMakeRole.match(3);
  240. // If a command reference has no explicit target and
  241. // no explicit "(...)" then add "()" to the text.
  242. if (this->CMakeRole.match(2) == "command" &&
  243. this->CMakeRole.match(5).empty() &&
  244. text.find_first_of("()") == std::string::npos) {
  245. text += "()";
  246. }
  247. this->OS << "``" << text << "``";
  248. pos += this->CMakeRole.end();
  249. }
  250. this->OS << line.substr(pos) << "\n";
  251. } else {
  252. this->OS << line_in << "\n";
  253. }
  254. }
  255. std::string cmRST::ReplaceSubstitutions(std::string const& line)
  256. {
  257. std::string out;
  258. std::string::size_type pos = 0;
  259. while (this->Substitution.find(line.c_str() + pos)) {
  260. std::string::size_type start = this->Substitution.start(2);
  261. std::string::size_type end = this->Substitution.end(2);
  262. std::string substitute = this->Substitution.match(3);
  263. std::map<std::string, std::string>::iterator replace =
  264. this->Replace.find(substitute);
  265. if (replace != this->Replace.end()) {
  266. std::pair<std::set<std::string>::iterator, bool> replaced =
  267. this->Replaced.insert(substitute);
  268. if (replaced.second) {
  269. substitute = this->ReplaceSubstitutions(replace->second);
  270. this->Replaced.erase(replaced.first);
  271. }
  272. }
  273. out += line.substr(pos, start);
  274. out += substitute;
  275. pos += end;
  276. }
  277. out += line.substr(pos);
  278. return out;
  279. }
  280. void cmRST::OutputMarkupLines(bool inlineMarkup)
  281. {
  282. for (auto line : this->MarkupLines) {
  283. if (!line.empty()) {
  284. line = " " + line;
  285. }
  286. this->OutputLine(line, inlineMarkup);
  287. }
  288. this->OutputLinePending = true;
  289. }
  290. bool cmRST::ProcessInclude(std::string file, IncludeType type)
  291. {
  292. bool found = false;
  293. if (this->IncludeDepth < 10) {
  294. cmRST r(this->OS, this->DocRoot);
  295. r.IncludeDepth = this->IncludeDepth + 1;
  296. r.OutputLinePending = this->OutputLinePending;
  297. if (type != IncludeTocTree) {
  298. r.Replace = this->Replace;
  299. }
  300. if (file[0] == '/') {
  301. file = this->DocRoot + file;
  302. } else {
  303. file = this->DocDir + "/" + file;
  304. }
  305. found = r.ProcessFile(file, type == IncludeModule);
  306. if (type != IncludeTocTree) {
  307. this->Replace = r.Replace;
  308. }
  309. this->OutputLinePending = r.OutputLinePending;
  310. }
  311. return found;
  312. }
  313. void cmRST::ProcessDirectiveParsedLiteral()
  314. {
  315. this->OutputMarkupLines(true);
  316. }
  317. void cmRST::ProcessDirectiveLiteralBlock()
  318. {
  319. this->OutputMarkupLines(false);
  320. }
  321. void cmRST::ProcessDirectiveCodeBlock()
  322. {
  323. this->OutputMarkupLines(false);
  324. }
  325. void cmRST::ProcessDirectiveReplace()
  326. {
  327. // Record markup lines as replacement text.
  328. std::string& replacement = this->Replace[this->ReplaceName];
  329. replacement += cmJoin(this->MarkupLines, " ");
  330. this->ReplaceName.clear();
  331. }
  332. void cmRST::ProcessDirectiveTocTree()
  333. {
  334. // Process documents referenced by toctree directive.
  335. for (std::string const& line : this->MarkupLines) {
  336. if (!line.empty() && line[0] != ':') {
  337. if (this->TocTreeLink.find(line)) {
  338. std::string const& link = this->TocTreeLink.match(1);
  339. this->ProcessInclude(link + ".rst", IncludeTocTree);
  340. } else {
  341. this->ProcessInclude(line + ".rst", IncludeTocTree);
  342. }
  343. }
  344. }
  345. }
  346. void cmRST::UnindentLines(std::vector<std::string>& lines)
  347. {
  348. // Remove the common indentation from the second and later lines.
  349. std::string indentText;
  350. std::string::size_type indentEnd = 0;
  351. bool first = true;
  352. for (size_t i = 1; i < lines.size(); ++i) {
  353. std::string const& line = lines[i];
  354. // Do not consider empty lines.
  355. if (line.empty()) {
  356. continue;
  357. }
  358. // Record indentation on first non-empty line.
  359. if (first) {
  360. first = false;
  361. indentEnd = line.find_first_not_of(" \t");
  362. indentText = line.substr(0, indentEnd);
  363. continue;
  364. }
  365. // Truncate indentation to match that on this line.
  366. indentEnd = std::min(indentEnd, line.size());
  367. for (std::string::size_type j = 0; j != indentEnd; ++j) {
  368. if (line[j] != indentText[j]) {
  369. indentEnd = j;
  370. break;
  371. }
  372. }
  373. }
  374. // Update second and later lines.
  375. for (size_t i = 1; i < lines.size(); ++i) {
  376. std::string& line = lines[i];
  377. if (!line.empty()) {
  378. line = line.substr(indentEnd);
  379. }
  380. }
  381. std::vector<std::string>::const_iterator it = lines.begin();
  382. size_t leadingEmpty = std::distance(it, cmFindNot(lines, std::string()));
  383. std::vector<std::string>::const_reverse_iterator rit = lines.rbegin();
  384. size_t trailingEmpty =
  385. std::distance(rit, cmFindNot(cmReverseRange(lines), std::string()));
  386. std::vector<std::string>::iterator contentEnd = cmRotate(
  387. lines.begin(), lines.begin() + leadingEmpty, lines.end() - trailingEmpty);
  388. lines.erase(contentEnd, lines.end());
  389. }