main.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, "classscripting_1_1"))) continue; // Make sure the current file is about "scripting" namespace member class
  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 functions **/
  70. Class cl; // Store class data
  71. Parser::parse_compounddef(doc.RootElement(), cl);
  72. if (cl.constants.empty() && cl.functions.empty()) continue; // If there are no constants or functions, do not create a file
  73. // Save class, so it gets included in the home page
  74. classes.push_back(cl);
  75. /** Fill in the data in the provided page template file and save as new file.
  76. File entries to be replaced:
  77. "${SRG_CLASSSUMMARY}": Insert the provided summary info of the class, if available.
  78. "${SRG_CLASSINSTANCES}": Insert the provided instances info of the class, if available.
  79. "${SRG_CLASSCONSTANTS}": Insert a table with all constants in the class and their data.
  80. "${SRG_FUNCDATATABLE}": Insert a table with all functions in the class and their data.
  81. "${SRG_CLASSNAME}": Insert the class name.
  82. "${SRG_REF_[class]}": Insert the name of the referenced class, as well as its reference URL.
  83. File formatting replacements:
  84. "${SRG_NEWPARAGRAPH}": Insert two new lines to start a new paragraph.
  85. "${SRG_TABLENEWPARAGRAPH}": Insert two HTML line breaks to start a new paragraph in a table.
  86. [""] (double quotation marks): Replace with [`] for code formatting.
  87. **/
  88. // Prepare target data (add a notice at the top of the generated file)
  89. std::string target_data = Writer::write_file_notice(page_template_filename) + page_template;
  90. // Entries
  91. replace(target_data, "${SRG_CLASSSUMMARY}", cl.summary, "None.");
  92. replace(target_data, "${SRG_CLASSINSTANCES}", cl.instances, "None.");
  93. replace(target_data, "${SRG_CLASSCONSTANTS}", Writer::write_constants_table(cl.constants), "None.");
  94. replace(target_data, "${SRG_CLASSFUNCTIONS}", Writer::write_function_table(cl.functions), "None.");
  95. replace(target_data, "${SRG_CLASSNAME}", "`" + cl.name + "`");
  96. regex_replace(target_data, std::regex("\\$\\{SRG_REF_(.+?)\\}"), Writer::write_class_ref("$1"));
  97. // Formatting
  98. replace(target_data, "${SRG_NEWPARAGRAPH} ", "\r\n\r\n");
  99. replace(target_data, "${SRG_TABLENEWPARAGRAPH}", "<br /><br />");
  100. replace(target_data, "\"\"", "`");
  101. // Write to target file
  102. write_file(output_dir_path / std::filesystem::path("Scripting" + cl.name + ".md"), target_data);
  103. std::cout << "Generated reference for class \"" << cl.name << "\"." << std::endl;
  104. }
  105. // Sort classes by their names (A-Z)
  106. std::sort(classes.begin(), classes.end(),
  107. [](const Class& lhs, const Class& rhs) { return lhs.name < rhs.name; });
  108. /** Fill in the data in the provided home page template file and save as new file.
  109. File entries to be replaced:
  110. "${SRG_CLASSLIST}": Insert a list with all classes which have been parsed.
  111. **/
  112. // Prepare target data (add a notice at the top of the generated file)
  113. std::string target_data = Writer::write_file_notice(home_template_filename) + home_template;
  114. // Entries
  115. replace(target_data, "${SRG_CLASSLIST}", Writer::write_class_list(classes));
  116. // Write to target file
  117. write_file(output_dir_path / std::filesystem::path("Scripting_Reference.md"), target_data);
  118. return 0;
  119. }