cmRST.cxx 15 KB

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