main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 
  2. /* TODO:
  3. * Create syntax for automatically including links to outmost documents in a specified folder.
  4. * Create syntax for automatically documenting methods in specified headers based on above comments.
  5. Follow include "", but not include <> when listing types.
  6. */
  7. #include "../../Source/DFPSR/includeFramework.h"
  8. using namespace dsr;
  9. // This program is only for maintaining the library's documentation,
  10. // so it's okay to use features specific to the Linux operating system.
  11. String resourceFolderPath;
  12. bool string_beginsWith(const ReadableString &a, const ReadableString &b) {
  13. return string_caseInsensitiveMatch(string_before(a, string_length(b)), b);
  14. }
  15. String substituteCharacters(ReadableString text) {
  16. String result;
  17. for (int i = 0; i < string_length(text); i++) {
  18. DsrChar c = text[i];
  19. if (c == U'<') {
  20. string_append(result, U"&lt;");
  21. } else if (c == U'>') {
  22. string_append(result, U"&gt;");
  23. } else if (c == U'\\') {
  24. string_append(result, U"&bsol;");
  25. } else {
  26. string_appendChar(result, text[i]);
  27. }
  28. }
  29. return result;
  30. }
  31. bool codeBlock = false;
  32. void processContent(String &target, String content) {
  33. string_split_callback([&target](ReadableString section) {
  34. //printText(U"Processing: ", section, U"\n");
  35. if (string_length(section) == 0) {
  36. //printText(U" Break\n");+
  37. string_append(target, U"\n</P><P>\n");
  38. } else if (string_match(section, U"*")) {
  39. //printText(U" Dot\n");
  40. string_append(target, U"<IMG SRC=\"Images/SmallDot.png\">\n");
  41. } else if (string_match(section, U"---")) {
  42. //printText(U" Border\n");
  43. string_append(target, U"</P><IMG SRC=\"Images/Border.png\"><P>\n");
  44. } else if (string_beginsWith(section, U"<-")) {
  45. ReadableString arguments = string_from(section, 2);
  46. int splitIndex = string_findFirst(arguments, U'|');
  47. if (splitIndex > -1) {
  48. ReadableString link = string_removeOuterWhiteSpace(string_before(arguments, splitIndex));
  49. ReadableString text = string_removeOuterWhiteSpace(string_after(arguments, splitIndex));
  50. //printText(U" Link to ", link, U" as ", text, U"\n");
  51. string_append(target, U"<A href=\"", link, "\">", text, "</A>");
  52. } else {
  53. //printText(U" Link to ", arguments, U"\n");
  54. string_append(target, U"<A href=\"", arguments, "\">", arguments, "</A>");
  55. }
  56. } else if (string_beginsWith(section, U"Image:")) {
  57. ReadableString arguments = string_from(section, 6);
  58. int splitIndex = string_findFirst(arguments, U'|');
  59. if (splitIndex > -1) {
  60. ReadableString image = string_removeOuterWhiteSpace(string_before(arguments, splitIndex));
  61. ReadableString text = string_removeOuterWhiteSpace(string_after(arguments, splitIndex));
  62. //printText(U" Image at ", image, U" as ", text, U"\n");
  63. string_append(target, U"<IMG SRC=\"", image, "\" ALT=\"", text, "\">\n");
  64. } else {
  65. //printText(U" Image at ", arguments, U"\n");
  66. string_append(target, U"<IMG SRC=\"", arguments, "\" ALT=\"\">\n");
  67. }
  68. } else if (string_beginsWith(section, U"Title:")) {
  69. ReadableString title = string_from(section, 6);
  70. //printText(U" Title: ", title, U"\n");
  71. string_append(target, U"</P><H1>", title, U"</H1><P>");
  72. } else if (string_beginsWith(section, U"Title2:")) {
  73. ReadableString title = string_from(section, 7);
  74. //printText(U" Title2: ", title, U"\n");
  75. string_append(target, U"</P><H2>", title, U"</H2><P>");
  76. } else if (string_beginsWith(section, U"Title3:")) {
  77. ReadableString title = string_from(section, 7);
  78. //printText(U" Title3: ", title, U"\n");
  79. string_append(target, U"</P><H3>", title, U"</H3><P>");
  80. } else if (string_beginsWith(section, U"CodeStart:")) {
  81. ReadableString code = string_from(section, 10);
  82. string_append(target, U"<PRE><BLOCKQUOTE>", substituteCharacters(code));
  83. codeBlock = true;
  84. } else if (string_beginsWith(section, U"CodeEnd:")) {
  85. string_append(target, U"</BLOCKQUOTE></PRE>");
  86. codeBlock = false;
  87. } else {
  88. if (codeBlock) {
  89. string_append(target, substituteCharacters(section), U"\n");
  90. } else {
  91. string_append(target, section, U"\n");
  92. }
  93. }
  94. }, content, U'\n');
  95. }
  96. String generateHtml(String content) {
  97. String style = string_load(file_combinePaths(resourceFolderPath, U"Default.css"));
  98. String result = U"<!DOCTYPE html> <HTML lang=en> <HEAD> <STYLE>\n";
  99. string_append(result, style);
  100. string_append(result, U"</STYLE> </HEAD> <BODY>\n");
  101. string_append(result, U"<IMG SRC=\"Images/Title.png\" ALT=\"Images/Title.png\">\n");
  102. string_append(result, U"<P>\n");
  103. processContent(result, content);
  104. string_append(result, U"</P>\n");
  105. string_append(result, U"</BODY> </HTML>\n");
  106. return result;
  107. }
  108. static ReadableString getExtensionless(const String& filename) {
  109. int lastDotIndex = string_findLast(filename, U'.');
  110. if (lastDotIndex != -1) {
  111. return string_removeOuterWhiteSpace(string_before(filename, lastDotIndex));
  112. } else {
  113. return U"?";
  114. }
  115. }
  116. void processFolder(const ReadableString& sourceFolderPath, const ReadableString& targetFolderPath) {
  117. file_getFolderContent(sourceFolderPath, [targetFolderPath](const ReadableString& sourcePath, const ReadableString& entryName, EntryType entryType) {
  118. printText("* Entry: ", entryName, " as ", entryType, "\n");
  119. if (entryType == EntryType::Folder) {
  120. // TODO: Create new output folders if needed for nested output.
  121. //processFolder(sourcePath, file_combinePaths(targetFolderPath, entryName));
  122. } else if (entryType == EntryType::File) {
  123. ReadableString extensionless = getExtensionless(entryName);
  124. String targetPath = file_combinePaths(targetFolderPath, extensionless + U".html");
  125. printText(U"Generating ", targetPath, U" from ", sourcePath, U" using the style ", resourceFolderPath, U"\n");
  126. String content = string_load(sourcePath);
  127. String result = generateHtml(content);
  128. string_save(file_combinePaths(targetFolderPath, targetPath), result);
  129. }
  130. });
  131. }
  132. DSR_MAIN_CALLER(dsrMain)
  133. void dsrMain(List<String> args) {
  134. if (args.length() != 4) {
  135. printText(U"The generator needs input, output and resource folder paths as three arguments!\n");
  136. } else {
  137. String sourceFolderPath = file_getAbsolutePath(args[1]);
  138. String targetFolderPath = file_getAbsolutePath(args[2]);
  139. resourceFolderPath = args[3];
  140. printText(U"Processing ", targetFolderPath, U" from ", sourceFolderPath, U" using the style ", resourceFolderPath, U"\n");
  141. processFolder(sourceFolderPath, targetFolderPath);
  142. printText(U"Done\n");
  143. }
  144. }