main.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SuperTux - Scripting reference generator
  2. // Copyright (C) 2023 Vankata453
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. /** This program converts Doxygen XML output files into
  17. Markdown scripting reference documentation, using templates. **/
  18. /* Requirements: C++17, TinyXML2 library */
  19. #include <algorithm>
  20. #include <vector>
  21. #include <iostream>
  22. #include <filesystem>
  23. #include <tinyxml2.h>
  24. #include "class.hpp"
  25. #include "parser.hpp"
  26. #include "util.hpp"
  27. #include "writer.hpp"
  28. int main(int argc, char** argv)
  29. {
  30. /** Get required parameters **/
  31. std::string input_dir, home_tmpl_file, page_tmpl_file, output_dir(".");
  32. for (int i = 1; i < argc; i++)
  33. {
  34. if (param_matches(argc, argv, i, "-d", "--dir", "--directory")) // Input directory has been provided
  35. input_dir = argv[i + 1];
  36. else if (param_matches(argc, argv, i, "-h", "--home", "--home-template")) // Home template has been provided
  37. home_tmpl_file = argv[i + 1];
  38. else if (param_matches(argc, argv, i, "-p", "--page", "--page-template")) // Page template has been provided
  39. page_tmpl_file = argv[i + 1];
  40. else if (param_matches(argc, argv, i, "-o", "--output", "--output-directory")) // Output directory has been provided
  41. output_dir = argv[i + 1];
  42. }
  43. if (input_dir.empty() || home_tmpl_file.empty() || page_tmpl_file.empty()) // Do not allow empty parameters
  44. {
  45. std::cout << "Usage: [\"-d\", \"--dir\", \"--directory\"] (string [REQUIRED])" << std::endl;
  46. std::cout << " [\"-h\", \"--home\", \"--home-template\"] (string [REQUIRED])" << std::endl;
  47. std::cout << " [\"-p\", \"--page\", \"--page-template\"] (string [REQUIRED])" << std::endl;
  48. std::cout << " [\"-o\", \"--output\", \"--output-directory\"] (string [DEFAULT = \".\" (current)])" << std::endl;
  49. return 1;
  50. }
  51. /** Read template files **/
  52. const std::string home_template = read_file(home_tmpl_file);
  53. const std::string page_template = read_file(page_tmpl_file);
  54. const std::string home_template_filename = std::filesystem::path(home_tmpl_file).filename();
  55. const std::string page_template_filename = std::filesystem::path(page_tmpl_file).filename();
  56. /** Prepare other variables **/
  57. std::vector<Class> classes;
  58. std::filesystem::path output_dir_path = output_dir;
  59. /** Loop through all files in the provided directory and parse ones containing XML class data **/
  60. for (const auto& dir_entry : std::filesystem::recursive_directory_iterator(input_dir))
  61. {
  62. std::filesystem::path fspath = dir_entry;
  63. const std::string filename = fspath.filename();
  64. if (!(std::filesystem::is_regular_file(dir_entry) &&
  65. (starts_with(filename, "class") || starts_with(filename, "namespace")))) continue; // Make sure the current file is about a class or a namespace
  66. /** Read data from current XML class data file **/
  67. tinyxml2::XMLDocument doc;
  68. doc.LoadFile(fspath.c_str());
  69. /** Parse the class and its members **/
  70. Class cl; // Store class data
  71. Parser::parse_compounddef(doc.RootElement(), cl);
  72. if (cl.constants.empty() && cl.variables.empty() && cl.functions.empty()) continue; // If the class has no content, do not save it
  73. // Save the class
  74. classes.push_back(std::move(cl));
  75. }
  76. // Sort classes by their names (A-Z)
  77. std::sort(classes.begin(), classes.end(),
  78. [](const Class& lhs, const Class& rhs) { return lhs.name < rhs.name; });
  79. /** Loop through all classes and write their data to files **/
  80. for (Class& cl : classes)
  81. {
  82. // Remove a few internal base classes
  83. for (auto it = cl.base_classes.begin(); it != cl.base_classes.end();)
  84. {
  85. if (it->second == "ssq::ExposableClass" || it->second == "ExposableClass")
  86. cl.base_classes.erase(it++);
  87. else
  88. ++it;
  89. }
  90. /** Fill in the data in the provided page template file and save as new file.
  91. File entries to be replaced:
  92. "${SRG_CLASSSUMMARY}": Insert the provided summary info of the class, if available.
  93. "${SRG_CLASSINSTANCES}": Insert the provided instances info of the class, if available.
  94. "${SRG_CLASSCONSTANTS}": Insert a table with all constants in the class and their data.
  95. "${SRG_FUNCDATATABLE}": Insert a table with all functions in the class and their data.
  96. "${SRG_CLASSNAME}": Insert the class name.
  97. "${SRG_REF_[class]}": Insert the name of the referenced class, as well as its reference URL.
  98. File formatting replacements:
  99. "${SRG_NEWPARAGRAPH}": Insert two new lines to start a new paragraph.
  100. "${SRG_TABLENEWPARAGRAPH}": Insert two HTML line breaks to start a new paragraph in a table.
  101. [""] (double quotation marks): Replace with [`] for code formatting.
  102. **/
  103. // Prepare target data (add a notice at the top of the generated file)
  104. std::string target_data = Writer::write_file_notice(page_template_filename) + page_template;
  105. // Entries
  106. replace(target_data, "${SRG_CLASSSUMMARY}", cl.summary, "None.");
  107. replace(target_data, "${SRG_CLASSINSTANCES}", cl.instances, "None.");
  108. replace(target_data, "${SRG_CLASSINHERITANCE}", Writer::write_inheritance_list(classes, cl.base_classes, cl.derived_classes), "None.");
  109. replace(target_data, "${SRG_CLASSCONSTANTS}", Writer::write_constants_table(cl.constants), "None.");
  110. replace(target_data, "${SRG_CLASSVARIABLES}", Writer::write_variables_table(cl.variables), "None.");
  111. replace(target_data, "${SRG_CLASSFUNCTIONS}", Writer::write_function_table(cl.functions), "None.");
  112. replace(target_data, "${SRG_CLASSNAME}", "`" + cl.name + "`");
  113. regex_replace(target_data, std::regex("\\$\\{SRG_REF_(.+?)\\}"), Writer::write_class_ref("$1"));
  114. // Formatting
  115. replace(target_data, "${SRG_NEWPARAGRAPH} ", "\r\n\r\n");
  116. replace(target_data, "${SRG_TABLENEWPARAGRAPH}", "<br /><br />");
  117. replace(target_data, "\"\"", "`");
  118. replace(target_data, "NOTE:", "<br /><br />**NOTE:**");
  119. replace(target_data, "Note:", "<br /><br />**NOTE:**");
  120. // Write to target file
  121. write_file(output_dir_path / std::filesystem::path("Scripting" + cl.name + ".md"), target_data);
  122. std::cout << "Generated reference for class \"" << cl.name << "\"." << std::endl;
  123. }
  124. /** Fill in the data in the provided home page template file and save as new file.
  125. File entries to be replaced:
  126. "${SRG_CLASSLIST}": Insert a list with all classes which have been parsed.
  127. **/
  128. // Prepare target data (add a notice at the top of the generated file)
  129. std::string target_data = Writer::write_file_notice(home_template_filename) + home_template;
  130. // Entries
  131. replace(target_data, "${SRG_CLASSLIST}", Writer::write_class_list(classes));
  132. // Write to target file
  133. write_file(output_dir_path / std::filesystem::path("Scripting_Reference.md"), target_data);
  134. return 0;
  135. }