main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 
  2. #include "../../Source/DFPSR/includeFramework.h"
  3. using namespace dsr;
  4. // This program is only for maintaining the library's documentation,
  5. // so it's okay to use features specific to the Linux operating system.
  6. String ResourceFolderPath;
  7. bool string_beginsWith(const ReadableString &a, const ReadableString &b) {
  8. return string_caseInsensitiveMatch(string_before(a, string_length(b)), b);
  9. }
  10. String substituteCharacters(ReadableString text) {
  11. String result;
  12. for (int i = 0; i < string_length(text); i++) {
  13. DsrChar c = text[i];
  14. if (c == U'<') {
  15. string_append(result, U"&lt;");
  16. } else if (c == U'>') {
  17. string_append(result, U"&gt;");
  18. } else if (c == U'\\') {
  19. string_append(result, U"&bsol;");
  20. } else {
  21. string_appendChar(result, text[i]);
  22. }
  23. }
  24. return result;
  25. }
  26. bool codeBlock = false;
  27. void processContent(String &target, String content) {
  28. string_split_callback([&target](ReadableString section) {
  29. //printText(U"Processing: ", section, U"\n");
  30. if (string_length(section) == 0) {
  31. //printText(U" Break\n");+
  32. string_append(target, U"\n</P><P>\n");
  33. } else if (string_match(section, U"*")) {
  34. //printText(U" Dot\n");
  35. string_append(target, U"<IMG SRC=\"Images/SmallDot.png\">\n");
  36. } else if (string_match(section, U"---")) {
  37. //printText(U" Border\n");
  38. string_append(target, U"</P><IMG SRC=\"Images/Border.png\"><P>\n");
  39. } else if (string_beginsWith(section, U"<-")) {
  40. ReadableString arguments = string_from(section, 2);
  41. int splitIndex = string_findFirst(arguments, U'|');
  42. if (splitIndex > -1) {
  43. ReadableString link = string_removeOuterWhiteSpace(string_before(arguments, splitIndex));
  44. ReadableString text = string_removeOuterWhiteSpace(string_after(arguments, splitIndex));
  45. //printText(U" Link to ", link, U" as ", text, U"\n");
  46. string_append(target, U"<A href=\"", link, "\">", text, "</A>");
  47. } else {
  48. //printText(U" Link to ", arguments, U"\n");
  49. string_append(target, U"<A href=\"", arguments, "\">", arguments, "</A>");
  50. }
  51. } else if (string_beginsWith(section, U"Image:")) {
  52. ReadableString arguments = string_from(section, 6);
  53. int splitIndex = string_findFirst(arguments, U'|');
  54. if (splitIndex > -1) {
  55. ReadableString image = string_removeOuterWhiteSpace(string_before(arguments, splitIndex));
  56. ReadableString text = string_removeOuterWhiteSpace(string_after(arguments, splitIndex));
  57. //printText(U" Image at ", image, U" as ", text, U"\n");
  58. string_append(target, U"<IMG SRC=\"", image, "\" ALT=\"", text, "\">\n");
  59. } else {
  60. //printText(U" Image at ", arguments, U"\n");
  61. string_append(target, U"<IMG SRC=\"", arguments, "\" ALT=\"\">\n");
  62. }
  63. } else if (string_beginsWith(section, U"Title:")) {
  64. ReadableString title = string_from(section, 6);
  65. //printText(U" Title: ", title, U"\n");
  66. string_append(target, U"</P><H1>", title, U"</H1><P>");
  67. } else if (string_beginsWith(section, U"Title2:")) {
  68. ReadableString title = string_from(section, 7);
  69. //printText(U" Title2: ", title, U"\n");
  70. string_append(target, U"</P><H2>", title, U"</H2><P>");
  71. } else if (string_beginsWith(section, U"Title3:")) {
  72. ReadableString title = string_from(section, 7);
  73. //printText(U" Title3: ", title, U"\n");
  74. string_append(target, U"</P><H3>", title, U"</H3><P>");
  75. } else if (string_beginsWith(section, U"CodeStart:")) {
  76. ReadableString code = string_from(section, 10);
  77. string_append(target, U"<PRE><BLOCKQUOTE>", substituteCharacters(code));
  78. codeBlock = true;
  79. } else if (string_beginsWith(section, U"CodeEnd:")) {
  80. string_append(target, U"</BLOCKQUOTE></PRE>");
  81. codeBlock = false;
  82. } else {
  83. if (codeBlock) {
  84. string_append(target, substituteCharacters(section), U"\n");
  85. } else {
  86. string_append(target, section, U"\n");
  87. }
  88. }
  89. }, content, U'\n');
  90. }
  91. String generateHtml(String content) {
  92. String style = string_load(ResourceFolderPath + U"Default.css");
  93. String result = U"<!DOCTYPE html> <HTML lang=en> <HEAD> <STYLE>\n";
  94. string_append(result, style);
  95. string_append(result, U"</STYLE> </HEAD> <BODY>\n");
  96. string_append(result, U"<IMG SRC=\"Images/Title.png\" ALT=\"Images/Title.png\">\n");
  97. string_append(result, U"<P>\n");
  98. processContent(result, content);
  99. string_append(result, U"</P>\n");
  100. string_append(result, U"</BODY> </HTML>\n");
  101. return result;
  102. }
  103. int main(int argn, char **argv) {
  104. if (argn != 4) {
  105. printText(U"The generator needs filenames for target!\n");
  106. return 1;
  107. }
  108. String SourceFilePath = argv[1];
  109. String TargetFilePath = argv[2];
  110. ResourceFolderPath = argv[3];
  111. printText(U"Generating ", TargetFilePath, U" from ", SourceFilePath, U" using the style", ResourceFolderPath, U"\n");
  112. String content = string_load(SourceFilePath);
  113. String result = generateHtml(content);
  114. string_save(TargetFilePath, result);
  115. printText(U"Done\n");
  116. return 0;
  117. }