main.cpp 6.5 KB

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