XmlDocRenderer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Diagnostics;
  20. using System.IO;
  21. using System.Text;
  22. using System.Text.RegularExpressions;
  23. //using System.Windows.Controls;
  24. using System.Xml;
  25. namespace ICSharpCode.ILSpy.XmlDoc
  26. {
  27. /// <summary>
  28. /// Renders XML documentation into a WPF <see cref="TextBlock"/>.
  29. /// </summary>
  30. public class XmlDocRenderer
  31. {
  32. readonly StringBuilder ret = new StringBuilder();
  33. public void AppendText(string text)
  34. {
  35. ret.Append(text);
  36. }
  37. public void AddXmlDocumentation(string xmlDocumentation)
  38. {
  39. if (xmlDocumentation == null)
  40. return;
  41. Debug.WriteLine(xmlDocumentation);
  42. try {
  43. XmlTextReader r = new XmlTextReader(new StringReader("<docroot>" + xmlDocumentation + "</docroot>"));
  44. r.XmlResolver = null;
  45. AddXmlDocumentation(r);
  46. } catch (XmlException) {
  47. }
  48. }
  49. static readonly Regex whitespace = new Regex(@"\s+");
  50. public void AddXmlDocumentation(XmlReader xml)
  51. {
  52. while (xml.Read()) {
  53. if (xml.NodeType == XmlNodeType.Element) {
  54. string elname = xml.Name.ToLowerInvariant();
  55. switch (elname) {
  56. case "filterpriority":
  57. case "remarks":
  58. xml.Skip();
  59. break;
  60. case "example":
  61. ret.Append(Environment.NewLine);
  62. ret.Append("Example:");
  63. ret.Append(Environment.NewLine);
  64. break;
  65. case "exception":
  66. ret.Append(Environment.NewLine);
  67. ret.Append(GetCref(xml["cref"]));
  68. ret.Append(": ");
  69. break;
  70. case "returns":
  71. ret.Append(Environment.NewLine);
  72. ret.Append("Returns: ");
  73. break;
  74. case "see":
  75. ret.Append(GetCref(xml["cref"]));
  76. ret.Append(xml["langword"]);
  77. break;
  78. case "seealso":
  79. ret.Append(Environment.NewLine);
  80. ret.Append("See also: ");
  81. ret.Append(GetCref(xml["cref"]));
  82. break;
  83. case "paramref":
  84. ret.Append(xml["name"]);
  85. break;
  86. case "param":
  87. ret.Append(Environment.NewLine);
  88. ret.Append(whitespace.Replace(xml["name"].Trim()," "));
  89. ret.Append(": ");
  90. break;
  91. case "typeparam":
  92. ret.Append(Environment.NewLine);
  93. ret.Append(whitespace.Replace(xml["name"].Trim()," "));
  94. ret.Append(": ");
  95. break;
  96. case "value":
  97. ret.Append(Environment.NewLine);
  98. ret.Append("Value: ");
  99. ret.Append(Environment.NewLine);
  100. break;
  101. case "br":
  102. case "para":
  103. ret.Append(Environment.NewLine);
  104. break;
  105. }
  106. } else if (xml.NodeType == XmlNodeType.Text) {
  107. ret.Append(whitespace.Replace(xml.Value, " "));
  108. }
  109. }
  110. }
  111. static string GetCref(string cref)
  112. {
  113. if (cref == null || cref.Trim().Length==0) {
  114. return "";
  115. }
  116. if (cref.Length < 2) {
  117. return cref;
  118. }
  119. if (cref.Substring(1, 1) == ":") {
  120. return cref.Substring(2, cref.Length - 2);
  121. }
  122. return cref;
  123. }
  124. // public TextBlock CreateTextBlock()
  125. // {
  126. // return new TextBlock { Text = ret.ToString() };
  127. // }
  128. }
  129. }