main.cpp 6.4 KB

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