writer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "writer.hpp"
  17. #include <sstream>
  18. #include "util.hpp"
  19. namespace Writer {
  20. std::string format_description(std::string desc)
  21. {
  22. replace(desc, "\"\"", "`");
  23. replace(desc, "NOTE:", "<br /><br />**NOTE:**");
  24. replace(desc, "Note:", "<br /><br />**NOTE:**");
  25. return desc;
  26. }
  27. std::string write_file_notice(const std::string& template_file)
  28. {
  29. std::stringstream notice;
  30. notice << "> This file is auto-generated from the [SuperTux source code](https://github.com/SuperTux/supertux/tree/master/src), "
  31. << "using the template [" << template_file << "](https://github.com/SuperTux/wiki/tree/master/templates/" << template_file << ")."
  32. << std::endl << std::endl;
  33. return notice.str();
  34. }
  35. std::string write_inheritance_list(const std::vector<Class>& classes,
  36. const Class::BaseClasses& base_classes,
  37. const std::vector<std::string>& derived_classes)
  38. {
  39. std::stringstream list;
  40. if (!base_classes.empty())
  41. {
  42. list << "This class inherits functions and variables from the following base classes:" << std::endl;
  43. // List of all base classes
  44. for (const auto& [_, base_class] : base_classes)
  45. {
  46. // Check whether the current class is documented
  47. const bool documented = std::any_of(classes.begin(), classes.end(), [base_class](const Class& cl) { return cl.name == base_class; });
  48. list << "* " << (documented ? write_class_ref(base_class) : base_class) << std::endl;
  49. }
  50. if (!derived_classes.empty())
  51. list << std::endl;
  52. }
  53. if (!derived_classes.empty())
  54. {
  55. list << "The following classes inherit functions and variables from this class:" << std::endl;
  56. // List of all derived classes
  57. for (const std::string& derived_class : derived_classes)
  58. {
  59. // Check whether the current class is documented
  60. const bool documented = std::any_of(classes.begin(), classes.end(), [derived_class](const Class& cl) { return cl.name == derived_class; });
  61. list << "* " << (documented ? write_class_ref(derived_class) : derived_class) << std::endl;
  62. }
  63. }
  64. return list.str();
  65. }
  66. std::string write_constants_table(const std::vector<Constant>& constants)
  67. {
  68. if (constants.empty()) return "";
  69. std::stringstream table;
  70. // Table beginning
  71. table << "Constant | Explanation" << std::endl;
  72. table << "---------|---------" << std::endl;
  73. // Table contents (constants)
  74. for (const Constant& con : constants)
  75. {
  76. // Print out type, name, initializer (if available) and description
  77. table << "`" << con.type << " " << con.name << (con.initializer.empty() ? "" : " " + con.initializer)
  78. << "` | " << format_description(con.description) << std::endl;
  79. }
  80. return table.str();
  81. }
  82. std::string write_variables_table(const std::vector<Variable>& variables)
  83. {
  84. if (variables.empty()) return "";
  85. std::stringstream table;
  86. // Table beginning
  87. table << "Variable | Explanation" << std::endl;
  88. table << "---------|---------" << std::endl;
  89. // Table contents (variables)
  90. for (const Variable& var : variables)
  91. {
  92. // Print out type, name and description
  93. table << "`" << var.type << " " << var.name << "` | " << format_description(var.description) << std::endl;
  94. }
  95. return table.str();
  96. }
  97. std::string write_function_table(const std::vector<Function>& functions)
  98. {
  99. if (functions.empty()) return "";
  100. std::stringstream table;
  101. // Table beginning
  102. table << "Method | Explanation" << std::endl;
  103. table << "-------|-------" << std::endl;
  104. // Table contents (functions)
  105. for (const Function& func : functions)
  106. {
  107. // Print out function
  108. table << "`" << func.type << " " << func.name << "(";
  109. for (size_t i = 0; i < func.parameters.size(); i++)
  110. {
  111. if (i != 0) table << ", ";
  112. table << func.parameters[i].type << " " << func.parameters[i].name;
  113. if (!func.parameters[i].default_value.empty())
  114. table << " = " << func.parameters[i].default_value;
  115. }
  116. table << ")`";
  117. // Print out description of function
  118. table << " | ";
  119. if (func.deprecated)
  120. {
  121. table << "**Deprecated!**";
  122. if (!func.deprecation_msg.empty())
  123. table << " " << format_description(func.deprecation_msg);
  124. // Add line breaks only if a description is available
  125. if (!func.description.empty())
  126. table << "<br /><br />";
  127. }
  128. table << format_description(func.description);
  129. // Print out descriptions of parameters
  130. if (std::any_of(func.parameters.begin(), func.parameters.end(), [](const Parameter& param) { return !param.description.empty(); }))
  131. {
  132. if (!func.deprecation_msg.empty() || !func.description.empty())
  133. table << "<br /><br />";
  134. bool has_printed_param_desc = false;
  135. for (const Parameter& param : func.parameters)
  136. {
  137. if (param.description.empty()) continue;
  138. table << (has_printed_param_desc ? "<br />" : "") << " `" << param.name << "` - " << format_description(param.description);
  139. has_printed_param_desc = true;
  140. }
  141. }
  142. // End table entry
  143. table << std::endl;
  144. }
  145. return table.str();
  146. }
  147. std::string write_class_list(const std::vector<Class>& classes)
  148. {
  149. std::stringstream list;
  150. // For each class name, create an list entry
  151. for (const Class& cl : classes)
  152. {
  153. list << "* " << write_class_ref(cl.name) << std::endl;
  154. }
  155. return list.str();
  156. }
  157. std::string write_class_ref(const std::string& name)
  158. {
  159. return "[" + name + "](https://github.com/SuperTux/supertux/wiki/Scripting" + name + ")";
  160. }
  161. } // namespace Writer