DocConverter.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Context.h"
  23. #include "File.h"
  24. #include "FileSystem.h"
  25. #include "List.h"
  26. #include "ProcessUtils.h"
  27. #include "StringUtils.h"
  28. #include <cstdio>
  29. #include <cstring>
  30. #ifdef WIN32
  31. #include <windows.h>
  32. #endif
  33. #include "DebugNew.h"
  34. using namespace Urho3D;
  35. SharedPtr<Context> context_(new Context());
  36. SharedPtr<FileSystem> fileSystem_(new FileSystem(context_));
  37. String inDir_;
  38. String outDir_;
  39. String mainPageName_;
  40. int main(int argc, char** argv);
  41. void Run(const Vector<String>& arguments);
  42. void ProcessFile(const String& fileName);
  43. String AssembleString(const Vector<String>& tokens, unsigned startIndex);
  44. int main(int argc, char** argv)
  45. {
  46. Vector<String> arguments;
  47. #ifdef WIN32
  48. arguments = ParseArguments(GetCommandLineW());
  49. #else
  50. arguments = ParseArguments(argc, argv);
  51. #endif
  52. Run(arguments);
  53. return 0;
  54. }
  55. void Run(const Vector<String>& arguments)
  56. {
  57. if (arguments.Size() < 3)
  58. {
  59. ErrorExit(
  60. "Usage: DocConverter <dox input path> <wiki output path> <mainpage name>"
  61. );
  62. }
  63. inDir_ = AddTrailingSlash(arguments[0]);
  64. outDir_ = AddTrailingSlash(arguments[1]);
  65. mainPageName_ = arguments[2];
  66. Vector<String> docFiles;
  67. fileSystem_->ScanDir(docFiles, inDir_, "*.dox", SCAN_FILES, false);
  68. for (unsigned i = 0; i < docFiles.Size(); ++i)
  69. ProcessFile(inDir_ + docFiles[i]);
  70. }
  71. void ProcessFile(const String& fileName)
  72. {
  73. PrintUnicodeLine("Processing document file " + fileName);
  74. File inputFile(context_, fileName);
  75. String outputFileName;
  76. File outputFile(context_);
  77. bool inVerbatim = false;
  78. if (!inputFile.IsOpen())
  79. {
  80. PrintUnicodeLine("WARNING: Failed to open input file " + fileName + ", skipping");
  81. return;
  82. }
  83. while (!inputFile.IsEof())
  84. {
  85. String line = inputFile.ReadLine();
  86. line.Replace("%", "");
  87. Vector<String> tokens = line.Split(' ');
  88. if (!inVerbatim)
  89. {
  90. // Replace links
  91. for (;;)
  92. {
  93. unsigned refIndex = line.Find("\\ref");
  94. if (refIndex != String::NPOS)
  95. {
  96. //PrintUnicodeLine("Handling \\ref on line " + line);
  97. Vector<String> refTokens = line.Substring(refIndex).Split(' ');
  98. if (refTokens.Size() > 1)
  99. {
  100. String refTarget = refTokens[1];
  101. unsigned refTargetEnd = line.Find(' ', refIndex + 5 + refTarget.Length());
  102. if (refTarget.EndsWith("."))
  103. {
  104. refTarget = refTarget.Substring(0, refTarget.Length() - 1);
  105. refTargetEnd--;
  106. }
  107. unsigned refBeginQuote = line.Find('\"', refTargetEnd);
  108. unsigned refEndQuote = line.Find('\"', refBeginQuote+1);
  109. if (refTarget.Find("::") == String::NPOS)
  110. {
  111. if (refBeginQuote != String::NPOS && refEndQuote != String::NPOS)
  112. line = line.Substring(0, refIndex) + "[" + refTarget + " " + line.Substring(refBeginQuote + 1, refEndQuote - refBeginQuote - 1) + "]" + line.Substring(refEndQuote + 1);
  113. else
  114. line = line.Substring(0, refIndex) + "[" + refTarget + "]" + line.Substring(refTargetEnd);
  115. }
  116. else
  117. {
  118. String refClass = refTarget.Substring(0, refTarget.Find("::"));
  119. line = line.Substring(0, refIndex) + line.Substring(refBeginQuote + 1, refEndQuote - refBeginQuote - 1) + line.Substring(refEndQuote + 1);
  120. }
  121. }
  122. else
  123. {
  124. PrintUnicodeLine("WARNING: \\ref tag which could not be handled");
  125. break;
  126. }
  127. //PrintUnicodeLine("Line after ref handling: " + line);
  128. }
  129. else
  130. break;
  131. }
  132. if (line.StartsWith("\\mainpage") && tokens.Size() > 1)
  133. {
  134. outputFileName = outDir_ + mainPageName_ + ".wiki";
  135. if (!outputFile.Open(outputFileName, FILE_WRITE))
  136. PrintUnicodeLine("WARNING: Failed to open output file " + outputFileName);
  137. outputFile.WriteLine("#labels featured");
  138. outputFile.WriteLine("= " + AssembleString(tokens, 1) + " =");
  139. }
  140. else if (line.StartsWith("\\page") && tokens.Size() > 1)
  141. {
  142. outputFileName = outDir_ + tokens[1] + ".wiki";
  143. if (!outputFile.Open(outputFileName, FILE_WRITE))
  144. PrintUnicodeLine("WARNING: Failed to open output file " + outputFileName);
  145. if (tokens.Size() > 2)
  146. outputFile.WriteLine("= " + AssembleString(tokens, 2) + " =");
  147. else
  148. outputFile.WriteLine("= " + tokens[1] + " =");
  149. }
  150. else if (line.StartsWith("\\section"))
  151. {
  152. if (tokens.Size() > 2)
  153. outputFile.WriteLine("== " + AssembleString(tokens, 2) + " ==");
  154. else
  155. outputFile.WriteLine("== " + tokens[1] + " ==");
  156. }
  157. else if (line.StartsWith("\\verbatim") || line.StartsWith("\\code"))
  158. {
  159. outputFile.WriteLine("{{{");
  160. inVerbatim = true;
  161. }
  162. else if (line.StartsWith("- "))
  163. outputFile.WriteLine(" * " + line.Substring(2));
  164. else if (line.StartsWith("{") || line.StartsWith("}") || line.StartsWith("namespace") || line.StartsWith("/*") || line.StartsWith("*/"))
  165. continue;
  166. else
  167. outputFile.WriteLine(line);
  168. }
  169. else
  170. {
  171. if (line.StartsWith("\\endverbatim") || line.StartsWith("\\endcode"))
  172. {
  173. outputFile.WriteLine("}}}");
  174. inVerbatim = false;
  175. }
  176. else
  177. outputFile.WriteLine(line);
  178. }
  179. }
  180. }
  181. String AssembleString(const Vector<String>& tokens, unsigned startIndex)
  182. {
  183. String ret;
  184. for (unsigned i = startIndex; i < tokens.Size(); ++i)
  185. {
  186. if (i > startIndex)
  187. ret += ' ';
  188. ret += tokens[i];
  189. }
  190. return ret;
  191. }