cmRST.cxx 14 KB

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