cmDocumentation.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDocumentation.h"
  14. #include "cmSystemTools.h"
  15. //----------------------------------------------------------------------------
  16. static const cmDocumentationEntry cmDocumentationStandardOptions[] =
  17. {
  18. {"--copyright", "Print the CMake copyright and exit.", 0},
  19. {"--help", "Print usage information and exit.",
  20. "Usage describes the basic command line interface and its options."},
  21. {"--help-full", "Print full help and exit.",
  22. "Full help displays most of the documentation provided by the UNIX "
  23. "man page. It is provided for use on non-UNIX platforms, but is "
  24. "also convenient if the man page is not installed."},
  25. {"--help-html", "Print full help in HTML format.",
  26. "This option is used by CMake authors to help produce web pages."},
  27. {"--help-man", "Print a UNIX man page and exit.",
  28. "This option is used by CMake authors to generate the UNIX man page."},
  29. {"--version", "Show program name/version banner and exit.", 0},
  30. {0,0,0}
  31. };
  32. //----------------------------------------------------------------------------
  33. static const cmDocumentationEntry cmDocumentationCommandsHeader[] =
  34. {
  35. {0,
  36. "The following commands are available in CMakeLists.txt code:", 0},
  37. {0,0,0}
  38. };
  39. //----------------------------------------------------------------------------
  40. static const cmDocumentationEntry cmDocumentationGeneratorsHeader[] =
  41. {
  42. {0,
  43. "The following generators are available on this platform:", 0},
  44. {0,0,0}
  45. };
  46. //----------------------------------------------------------------------------
  47. const cmDocumentationEntry cmDocumentationMailingList[] =
  48. {
  49. {0,
  50. "For help and discussion about using cmake, a mailing list is provided "
  51. "at [email protected]. Please first read the full documentation at "
  52. "http://www.cmake.org before posting questions to the list.", 0},
  53. {0,0,0}
  54. };
  55. //----------------------------------------------------------------------------
  56. const cmDocumentationEntry cmDocumentationAuthor[] =
  57. {
  58. {0,
  59. "This manual page was generated by \"cmake --help-man\".", 0},
  60. {0,0,0}
  61. };
  62. //----------------------------------------------------------------------------
  63. const cmDocumentationEntry cmDocumentationCopyright[] =
  64. {
  65. {0,
  66. "Copyright (c) 2002 Kitware, Inc., Insight Consortium. "
  67. "All rights reserved.", 0},
  68. {0,
  69. "Redistribution and use in source and binary forms, with or without "
  70. "modification, are permitted provided that the following conditions are "
  71. "met:", 0},
  72. {"",
  73. "Redistributions of source code must retain the above copyright notice, "
  74. "this list of conditions and the following disclaimer.", 0},
  75. {"",
  76. "Redistributions in binary form must reproduce the above copyright "
  77. "notice, this list of conditions and the following disclaimer in the "
  78. "documentation and/or other materials provided with the distribution.",
  79. 0},
  80. {"",
  81. "The names of Kitware, Inc., the Insight Consortium, or the names of "
  82. "any consortium members, or of any contributors, may not be used to "
  83. "endorse or promote products derived from this software without "
  84. "specific prior written permission.", 0},
  85. {"",
  86. "Modified source versions must be plainly marked as such, and must "
  87. "not be misrepresented as being the original software.", 0},
  88. {0,
  89. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "
  90. "``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT "
  91. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR "
  92. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR "
  93. "CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, "
  94. "EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, "
  95. "PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR "
  96. "PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF "
  97. "LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING "
  98. "NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS "
  99. "SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.", 0},
  100. {0, 0, 0}
  101. };
  102. //----------------------------------------------------------------------------
  103. cmDocumentation::cmDocumentation()
  104. {
  105. this->CurrentForm = TextForm;
  106. this->TextIndent = "";
  107. this->TextWidth = 77;
  108. }
  109. //----------------------------------------------------------------------------
  110. void cmDocumentation::PrintCopyright(std::ostream& os)
  111. {
  112. os << "CMake version " CMake_VERSION_FULL "\n";
  113. for(const cmDocumentationEntry* op = cmDocumentationCopyright;
  114. op->brief; ++op)
  115. {
  116. if(op->name)
  117. {
  118. os << " * ";
  119. this->TextIndent = " ";
  120. this->PrintColumn(os, op->brief);
  121. }
  122. else
  123. {
  124. this->TextIndent = "";
  125. this->PrintColumn(os, op->brief);
  126. }
  127. os << "\n";
  128. }
  129. }
  130. //----------------------------------------------------------------------------
  131. void cmDocumentation::PrintVersion(std::ostream& os)
  132. {
  133. os << "CMake version " CMake_VERSION_FULL "\n";
  134. }
  135. //----------------------------------------------------------------------------
  136. void cmDocumentation::AddSection(const char* name,
  137. const cmDocumentationEntry* d)
  138. {
  139. this->Names.push_back(name);
  140. this->Sections.push_back(d);
  141. }
  142. //----------------------------------------------------------------------------
  143. void cmDocumentation::ClearSections()
  144. {
  145. this->Names.erase(this->Names.begin(), this->Names.end());
  146. this->Sections.erase(this->Sections.begin(), this->Sections.end());
  147. }
  148. //----------------------------------------------------------------------------
  149. void cmDocumentation::PrintDocumentation(Type ht, std::ostream& os)
  150. {
  151. switch (ht)
  152. {
  153. case cmDocumentation::Usage: this->PrintDocumentationUsage(os); break;
  154. case cmDocumentation::Full: this->PrintDocumentationFull(os); break;
  155. case cmDocumentation::HTML: this->PrintDocumentationHTML(os); break;
  156. case cmDocumentation::Man: this->PrintDocumentationMan(os); break;
  157. case cmDocumentation::Copyright: this->PrintCopyright(os); break;
  158. case cmDocumentation::Version: this->PrintVersion(os); break;
  159. default: break;
  160. }
  161. }
  162. //----------------------------------------------------------------------------
  163. bool cmDocumentation::PrintRequestedDocumentation(std::ostream& os)
  164. {
  165. bool result = true;
  166. // Loop over requested documentation types.
  167. for(RequestedMapType::const_iterator i = this->RequestedMap.begin();
  168. i != this->RequestedMap.end(); ++i)
  169. {
  170. // If a file name was given, use it. Otherwise, default to the
  171. // given stream.
  172. std::ofstream* fout = 0;
  173. std::ostream* s = &os;
  174. if(i->second.length() > 0)
  175. {
  176. fout = new std::ofstream(i->second.c_str(), std::ios::out);
  177. if(fout)
  178. {
  179. s = fout;
  180. }
  181. else
  182. {
  183. result = false;
  184. }
  185. }
  186. // Print this documentation type to the stream.
  187. this->PrintDocumentation(i->first, *s);
  188. // Check for error.
  189. if(!*s)
  190. {
  191. result = false;
  192. }
  193. // Close the file if we wrote one.
  194. if(fout)
  195. {
  196. delete fout;
  197. }
  198. }
  199. return result;
  200. }
  201. //----------------------------------------------------------------------------
  202. bool cmDocumentation::CheckOptions(int argc, const char* const* argv)
  203. {
  204. // Providing zero arguments gives usage information.
  205. if(argc == 1)
  206. {
  207. this->RequestedMap[cmDocumentation::Usage] = "";
  208. return true;
  209. }
  210. // Search for supported help options.
  211. bool result = false;
  212. for(int i=1; i < argc; ++i)
  213. {
  214. // Check if this is a supported help option.
  215. Type type = cmDocumentation::None;
  216. if((strcmp(argv[i], "-help") == 0) ||
  217. (strcmp(argv[i], "--help") == 0) ||
  218. (strcmp(argv[i], "/?") == 0) ||
  219. (strcmp(argv[i], "-usage") == 0) ||
  220. (strcmp(argv[i], "-h") == 0) ||
  221. (strcmp(argv[i], "-H") == 0))
  222. {
  223. type = cmDocumentation::Usage;
  224. }
  225. else if(strcmp(argv[i], "--help-full") == 0)
  226. {
  227. type = cmDocumentation::Full;
  228. }
  229. else if(strcmp(argv[i], "--help-html") == 0)
  230. {
  231. type = cmDocumentation::HTML;
  232. }
  233. else if(strcmp(argv[i], "--help-man") == 0)
  234. {
  235. type = cmDocumentation::Man;
  236. }
  237. else if(strcmp(argv[i], "--copyright") == 0)
  238. {
  239. type = cmDocumentation::Copyright;
  240. }
  241. else if((strcmp(argv[i], "--version") == 0) ||
  242. (strcmp(argv[i], "-version") == 0) ||
  243. (strcmp(argv[i], "-V") == 0) ||
  244. (strcmp(argv[i], "/V") == 0))
  245. {
  246. type = cmDocumentation::Version;
  247. }
  248. if(type)
  249. {
  250. // This is a help option. See if there is a file name given.
  251. result = true;
  252. if((i+1 < argc) && (argv[i+1][0] != '-') &&
  253. (strcmp(argv[i+1], "/V") != 0) && (strcmp(argv[i+1], "/?") != 0))
  254. {
  255. this->RequestedMap[type] = argv[i+1];
  256. i = i+1;
  257. }
  258. else
  259. {
  260. this->RequestedMap[type] = "";
  261. }
  262. }
  263. }
  264. return result;
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmDocumentation::Print(Form f, std::ostream& os)
  268. {
  269. this->CurrentForm = f;
  270. for(unsigned int i=0; i < this->Sections.size(); ++i)
  271. {
  272. this->PrintSection(os, this->Sections[i], this->Names[i]);
  273. }
  274. }
  275. //----------------------------------------------------------------------------
  276. void cmDocumentation::SetNameSection(const cmDocumentationEntry* section)
  277. {
  278. this->SetSection(0, section, 0, this->NameSection);
  279. }
  280. //----------------------------------------------------------------------------
  281. void cmDocumentation::SetUsageSection(const cmDocumentationEntry* section)
  282. {
  283. this->SetSection(0, section, 0, this->UsageSection);
  284. }
  285. //----------------------------------------------------------------------------
  286. void cmDocumentation::SetDescriptionSection(const cmDocumentationEntry* section)
  287. {
  288. this->SetSection(0, section, 0, this->DescriptionSection);
  289. }
  290. //----------------------------------------------------------------------------
  291. void cmDocumentation::SetOptionsSection(const cmDocumentationEntry* section)
  292. {
  293. this->SetSection(0, section, cmDocumentationStandardOptions,
  294. this->OptionsSection);
  295. }
  296. //----------------------------------------------------------------------------
  297. void cmDocumentation::SetCommandsSection(const cmDocumentationEntry* section)
  298. {
  299. this->SetSection(cmDocumentationCommandsHeader, section, 0,
  300. this->CommandsSection);
  301. }
  302. //----------------------------------------------------------------------------
  303. void cmDocumentation::SetGeneratorsSection(const cmDocumentationEntry* section)
  304. {
  305. this->SetSection(cmDocumentationGeneratorsHeader, section, 0,
  306. this->GeneratorsSection);
  307. }
  308. //----------------------------------------------------------------------------
  309. void cmDocumentation::SetSeeAlsoList(const cmDocumentationEntry* also)
  310. {
  311. this->SeeAlsoString = ".B ";
  312. for(const cmDocumentationEntry* i = also; i->brief; ++i)
  313. {
  314. this->SeeAlsoString += i->brief;
  315. this->SeeAlsoString += (i+1)->brief? "(1), ":"(1)";
  316. }
  317. cmDocumentationEntry e = {0, 0, 0};
  318. e.brief = this->SeeAlsoString.c_str();
  319. this->SeeAlsoSection.push_back(e);
  320. e.brief = 0;
  321. this->SeeAlsoSection.push_back(e);
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmDocumentation::PrintSection(std::ostream& os,
  325. const cmDocumentationEntry* section,
  326. const char* name)
  327. {
  328. switch (this->CurrentForm)
  329. {
  330. case TextForm: this->PrintSectionText(os, section, name); break;
  331. case HTMLForm: this->PrintSectionHTML(os, section, name); break;
  332. case ManForm: this->PrintSectionMan(os, section, name); break;
  333. case UsageForm: this->PrintSectionUsage(os, section, name); break;
  334. }
  335. }
  336. //----------------------------------------------------------------------------
  337. void cmDocumentation::PrintSectionText(std::ostream& os,
  338. const cmDocumentationEntry* section,
  339. const char* name)
  340. {
  341. if(name)
  342. {
  343. os <<
  344. "---------------------------------------"
  345. "---------------------------------------\n";
  346. os << name << "\n\n";
  347. }
  348. if(!section) { return; }
  349. for(const cmDocumentationEntry* op = section; op->brief; ++op)
  350. {
  351. if(op->name)
  352. {
  353. if(op->name[0])
  354. {
  355. os << " " << op->name << "\n";
  356. }
  357. this->TextIndent = " ";
  358. this->PrintFormatted(os, op->brief);
  359. if(op->full)
  360. {
  361. os << "\n";
  362. this->PrintFormatted(os, op->full);
  363. }
  364. }
  365. else
  366. {
  367. this->TextIndent = "";
  368. this->PrintFormatted(os, op->brief);
  369. }
  370. os << "\n";
  371. }
  372. }
  373. //----------------------------------------------------------------------------
  374. void cmDocumentation::PrintSectionHTML(std::ostream& os,
  375. const cmDocumentationEntry* section,
  376. const char* name)
  377. {
  378. if(name)
  379. {
  380. os << "<h2>" << name << "</h2>\n";
  381. }
  382. if(!section) { return; }
  383. for(const cmDocumentationEntry* op = section; op->brief;)
  384. {
  385. if(op->name)
  386. {
  387. os << "<ul>\n";
  388. for(;op->name;++op)
  389. {
  390. os << " <li>\n";
  391. if(op->name[0])
  392. {
  393. os << " <b><code>";
  394. this->PrintHTMLEscapes(os, op->name);
  395. os << "</code></b>: ";
  396. }
  397. this->PrintHTMLEscapes(os, op->brief);
  398. if(op->full)
  399. {
  400. os << "<br>\n ";
  401. this->PrintFormatted(os, op->full);
  402. }
  403. os << "\n";
  404. os << " </li>\n";
  405. }
  406. os << "</ul>\n";
  407. }
  408. else
  409. {
  410. this->PrintFormatted(os, op->brief);
  411. os << "\n";
  412. ++op;
  413. }
  414. }
  415. }
  416. //----------------------------------------------------------------------------
  417. void cmDocumentation::PrintSectionMan(std::ostream& os,
  418. const cmDocumentationEntry* section,
  419. const char* name)
  420. {
  421. if(name)
  422. {
  423. os << ".SH " << name << "\n";
  424. }
  425. if(!section) { return; }
  426. for(const cmDocumentationEntry* op = section; op->brief; ++op)
  427. {
  428. if(op->name)
  429. {
  430. os << ".TP\n"
  431. << ".B " << (op->name[0]?op->name:"*") << "\n";
  432. this->PrintFormatted(os, op->brief);
  433. this->PrintFormatted(os, op->full);
  434. }
  435. else
  436. {
  437. os << ".PP\n";
  438. this->PrintFormatted(os, op->brief);
  439. }
  440. }
  441. }
  442. //----------------------------------------------------------------------------
  443. void cmDocumentation::PrintSectionUsage(std::ostream& os,
  444. const cmDocumentationEntry* section,
  445. const char* name)
  446. {
  447. if(name)
  448. {
  449. os << name << "\n";
  450. }
  451. if(!section) { return; }
  452. for(const cmDocumentationEntry* op = section; op->brief; ++op)
  453. {
  454. if(op->name)
  455. {
  456. os << " " << op->name;
  457. this->TextIndent = " ";
  458. int align = static_cast<int>(strlen(this->TextIndent))-4;
  459. for(int i = static_cast<int>(strlen(op->name)); i < align; ++i)
  460. {
  461. os << " ";
  462. }
  463. os << "= ";
  464. this->PrintColumn(os, op->brief);
  465. os << "\n";
  466. }
  467. else
  468. {
  469. os << "\n";
  470. this->TextIndent = "";
  471. this->PrintFormatted(os, op->brief);
  472. }
  473. }
  474. os << "\n";
  475. }
  476. //----------------------------------------------------------------------------
  477. void cmDocumentation::PrintFormatted(std::ostream& os, const char* text)
  478. {
  479. if(!text)
  480. {
  481. return;
  482. }
  483. const char* ptr = text;
  484. while(*ptr)
  485. {
  486. // Any ptrs starting in a space are treated as preformatted text.
  487. std::string preformatted;
  488. while(*ptr == ' ')
  489. {
  490. for(char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr)
  491. {
  492. preformatted.append(1, ch);
  493. }
  494. if(*ptr)
  495. {
  496. ++ptr;
  497. preformatted.append(1, '\n');
  498. }
  499. }
  500. if(preformatted.length())
  501. {
  502. this->PrintPreformatted(os, preformatted.c_str());
  503. }
  504. // Other ptrs are treated as paragraphs.
  505. std::string paragraph;
  506. for(char ch = *ptr; ch && ch != '\n'; ++ptr, ch = *ptr)
  507. {
  508. paragraph.append(1, ch);
  509. }
  510. if(*ptr)
  511. {
  512. ++ptr;
  513. paragraph.append(1, '\n');
  514. }
  515. if(paragraph.length())
  516. {
  517. this->PrintParagraph(os, paragraph.c_str());
  518. }
  519. }
  520. }
  521. //----------------------------------------------------------------------------
  522. void cmDocumentation::PrintPreformatted(std::ostream& os, const char* text)
  523. {
  524. switch (this->CurrentForm)
  525. {
  526. case TextForm: this->PrintPreformattedText(os, text); break;
  527. case HTMLForm: this->PrintPreformattedHTML(os, text); break;
  528. case ManForm: this->PrintPreformattedMan(os, text); break;
  529. case UsageForm: this->PrintPreformattedText(os, text); break;
  530. }
  531. }
  532. //----------------------------------------------------------------------------
  533. void cmDocumentation::PrintParagraph(std::ostream& os, const char* text)
  534. {
  535. switch (this->CurrentForm)
  536. {
  537. case TextForm: this->PrintParagraphText(os, text); break;
  538. case HTMLForm: this->PrintParagraphHTML(os, text); break;
  539. case ManForm: this->PrintParagraphMan(os, text); break;
  540. case UsageForm: this->PrintParagraphText(os, text); break;
  541. }
  542. }
  543. //----------------------------------------------------------------------------
  544. void cmDocumentation::PrintPreformattedText(std::ostream& os, const char* text)
  545. {
  546. bool newline = true;
  547. for(const char* ptr = text; *ptr; ++ptr)
  548. {
  549. if(newline)
  550. {
  551. os << this->TextIndent;
  552. newline = false;
  553. }
  554. os << *ptr;
  555. if(*ptr == '\n')
  556. {
  557. newline = true;
  558. }
  559. }
  560. os << "\n";
  561. }
  562. //----------------------------------------------------------------------------
  563. void cmDocumentation::PrintParagraphText(std::ostream& os, const char* text)
  564. {
  565. os << this->TextIndent;
  566. this->PrintColumn(os, text);
  567. os << "\n";
  568. }
  569. //----------------------------------------------------------------------------
  570. void cmDocumentation::PrintPreformattedHTML(std::ostream& os, const char* text)
  571. {
  572. os << "<pre>";
  573. this->PrintHTMLEscapes(os, text);
  574. os << "</pre>\n ";
  575. }
  576. //----------------------------------------------------------------------------
  577. void cmDocumentation::PrintParagraphHTML(std::ostream& os, const char* text)
  578. {
  579. os << "<p>";
  580. this->PrintHTMLEscapes(os, text);
  581. }
  582. //----------------------------------------------------------------------------
  583. void cmDocumentation::PrintPreformattedMan(std::ostream& os, const char* text)
  584. {
  585. os << text << "\n";
  586. }
  587. //----------------------------------------------------------------------------
  588. void cmDocumentation::PrintParagraphMan(std::ostream& os, const char* text)
  589. {
  590. os << text << "\n\n";
  591. }
  592. //----------------------------------------------------------------------------
  593. void cmDocumentation::PrintColumn(std::ostream& os, const char* text)
  594. {
  595. // Print text arranged in an indented column of fixed witdh.
  596. const char* l = text;
  597. int column = 0;
  598. bool newSentence = false;
  599. bool firstLine = true;
  600. int width = this->TextWidth - static_cast<int>(strlen(this->TextIndent));
  601. // Loop until the end of the text.
  602. while(*l)
  603. {
  604. // Parse the next word.
  605. const char* r = l;
  606. while(*r && (*r != '\n') && (*r != ' ')) { ++r; }
  607. // Does it fit on this line?
  608. if(r-l < (width-column-(newSentence?1:0)))
  609. {
  610. // Word fits on this line.
  611. if(r > l)
  612. {
  613. if(column)
  614. {
  615. // Not first word on line. Separate from the previous word
  616. // by a space, or two if this is a new sentence.
  617. if(newSentence)
  618. {
  619. os << " ";
  620. column += 2;
  621. }
  622. else
  623. {
  624. os << " ";
  625. column += 1;
  626. }
  627. }
  628. else
  629. {
  630. // First word on line. Print indentation unless this is the
  631. // first line.
  632. os << (firstLine?"":this->TextIndent);
  633. }
  634. // Print the word.
  635. os.write(l, static_cast<long>(r-l));
  636. newSentence = (*(r-1) == '.');
  637. }
  638. if(*r == '\n')
  639. {
  640. // Text provided a newline. Start a new line.
  641. os << "\n";
  642. ++r;
  643. column = 0;
  644. firstLine = false;
  645. }
  646. else
  647. {
  648. // No provided newline. Continue this line.
  649. column += static_cast<long>(r-l);
  650. }
  651. }
  652. else
  653. {
  654. // Word does not fit on this line. Start a new line.
  655. os << "\n";
  656. firstLine = false;
  657. if(r > l)
  658. {
  659. os << this->TextIndent;
  660. os.write(l, static_cast<long>(r-l));
  661. column = static_cast<long>(r-l);
  662. newSentence = (*(r-1) == '.');
  663. }
  664. }
  665. // Move to beginning of next word. Skip over whitespace.
  666. l = r;
  667. while(*l && (*l == ' ')) { ++l; }
  668. }
  669. }
  670. //----------------------------------------------------------------------------
  671. void cmDocumentation::PrintHTMLEscapes(std::ostream& os, const char* text)
  672. {
  673. static cmDocumentationEntry escapes[] =
  674. {
  675. {"<", "&lt;", 0},
  676. {">", "&gt;", 0},
  677. {"&", "&amp;", 0},
  678. {"\n", "<br>", 0},
  679. {0,0,0}
  680. };
  681. for(const char* p = text; *p; ++p)
  682. {
  683. bool found = false;
  684. for(const cmDocumentationEntry* op = escapes; !found && op->name; ++op)
  685. {
  686. if(op->name[0] == *p)
  687. {
  688. os << op->brief;
  689. found = true;
  690. }
  691. }
  692. if(!found)
  693. {
  694. os << *p;
  695. }
  696. }
  697. }
  698. //----------------------------------------------------------------------------
  699. void cmDocumentation::PrintDocumentationUsage(std::ostream& os)
  700. {
  701. this->CreateUsageDocumentation();
  702. this->Print(UsageForm, os);
  703. }
  704. //----------------------------------------------------------------------------
  705. void cmDocumentation::PrintDocumentationFull(std::ostream& os)
  706. {
  707. this->CreateFullDocumentation();
  708. this->Print(TextForm, os);
  709. }
  710. //----------------------------------------------------------------------------
  711. void cmDocumentation::PrintDocumentationHTML(std::ostream& os)
  712. {
  713. this->CreateFullDocumentation();
  714. os << "<html><body>\n";
  715. this->Print(HTMLForm, os);
  716. os << "</body></html>\n";
  717. }
  718. //----------------------------------------------------------------------------
  719. void cmDocumentation::PrintDocumentationMan(std::ostream& os)
  720. {
  721. this->CreateManDocumentation();
  722. os << ".TH CMake 1 \""
  723. << cmSystemTools::GetCurrentDateTime("%B %d, %Y").c_str()
  724. << "\" \"CMake " CMake_VERSION_FULL "\"\n";
  725. this->Print(ManForm, os);
  726. }
  727. //----------------------------------------------------------------------------
  728. void cmDocumentation::CreateUsageDocumentation()
  729. {
  730. this->ClearSections();
  731. if(!this->UsageSection.empty())
  732. {
  733. this->AddSection("Usage", &this->UsageSection[0]);
  734. }
  735. if(!this->OptionsSection.empty())
  736. {
  737. this->AddSection("Command-Line Options", &this->OptionsSection[0]);
  738. }
  739. if(!this->GeneratorsSection.empty())
  740. {
  741. this->AddSection("Generators", &this->GeneratorsSection[0]);
  742. }
  743. }
  744. //----------------------------------------------------------------------------
  745. void cmDocumentation::CreateFullDocumentation()
  746. {
  747. this->ClearSections();
  748. if(!this->NameSection.empty())
  749. {
  750. this->AddSection("Name", &this->NameSection[0]);
  751. }
  752. if(!this->UsageSection.empty())
  753. {
  754. this->AddSection("Usage", &this->UsageSection[0]);
  755. }
  756. if(!this->DescriptionSection.empty())
  757. {
  758. this->AddSection(0, &this->DescriptionSection[0]);
  759. }
  760. if(!this->OptionsSection.empty())
  761. {
  762. this->AddSection("Command-Line Options", &this->OptionsSection[0]);
  763. }
  764. if(!this->GeneratorsSection.empty())
  765. {
  766. this->AddSection("Generators", &this->GeneratorsSection[0]);
  767. }
  768. if(!this->CommandsSection.empty())
  769. {
  770. this->AddSection("Listfile Commands", &this->CommandsSection[0]);
  771. }
  772. this->AddSection("Copyright", cmDocumentationCopyright);
  773. this->AddSection("Mailing List", cmDocumentationMailingList);
  774. }
  775. //----------------------------------------------------------------------------
  776. void cmDocumentation::CreateManDocumentation()
  777. {
  778. this->ClearSections();
  779. if(!this->NameSection.empty())
  780. {
  781. this->AddSection("NAME", &this->NameSection[0]);
  782. }
  783. if(!this->UsageSection.empty())
  784. {
  785. this->AddSection("SYNOPSIS", &this->UsageSection[0]);
  786. }
  787. if(!this->DescriptionSection.empty())
  788. {
  789. this->AddSection("DESCRIPTION", &this->DescriptionSection[0]);
  790. }
  791. if(!this->OptionsSection.empty())
  792. {
  793. this->AddSection("OPTIONS", &this->OptionsSection[0]);
  794. }
  795. if(!this->GeneratorsSection.empty())
  796. {
  797. this->AddSection("GENERATORS", &this->GeneratorsSection[0]);
  798. }
  799. if(!this->CommandsSection.empty())
  800. {
  801. this->AddSection("COMMANDS", &this->CommandsSection[0]);
  802. }
  803. this->AddSection("COPYRIGHT", cmDocumentationCopyright);
  804. this->AddSection("MAILING LIST", cmDocumentationMailingList);
  805. if(!this->SeeAlsoSection.empty())
  806. {
  807. this->AddSection("SEE ALSO", &this->SeeAlsoSection[0]);
  808. }
  809. this->AddSection("AUTHOR", cmDocumentationAuthor);
  810. }
  811. //----------------------------------------------------------------------------
  812. void cmDocumentation::SetSection(const cmDocumentationEntry* header,
  813. const cmDocumentationEntry* section,
  814. const cmDocumentationEntry* footer,
  815. std::vector<cmDocumentationEntry>& vec)
  816. {
  817. vec.erase(vec.begin(), vec.end());
  818. if(header)
  819. {
  820. for(const cmDocumentationEntry* op = header; op->brief; ++op)
  821. {
  822. vec.push_back(*op);
  823. }
  824. }
  825. if(section)
  826. {
  827. for(const cmDocumentationEntry* op = section; op->brief; ++op)
  828. {
  829. vec.push_back(*op);
  830. }
  831. }
  832. if(footer)
  833. {
  834. for(const cmDocumentationEntry* op = footer; op->brief; ++op)
  835. {
  836. vec.push_back(*op);
  837. }
  838. }
  839. cmDocumentationEntry empty = {0,0,0};
  840. vec.push_back(empty);
  841. }