writer.cpp 5.7 KB

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