-
Notifications
You must be signed in to change notification settings - Fork 16
/
XmlToMarkDown.cs
158 lines (142 loc) · 6.29 KB
/
XmlToMarkDown.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace PxtlCa.XmlCommentMarkDownGenerator
{
public static class XmlToMarkdown
{
public static string ToMarkDown(this string s)
{
return s.ToMarkDown(new ConversionContext {
UnexpectedTagAction = UnexpectedTagActionEnum.Error
, WarningLogger = new TextWriterWarningLogger(Console.Error)
});
}
public static string ToMarkDown(this string s, ConversionContext context)
{
var xdoc = XDocument.Parse(s);
return xdoc
.ToMarkDown(context)
.RemoveRedundantLineBreaks();
}
public static string ToMarkDown(this Stream s)
{
var xdoc = XDocument.Load(s);
return xdoc
.ToMarkDown(new ConversionContext { UnexpectedTagAction = UnexpectedTagActionEnum.Error, WarningLogger = new TextWriterWarningLogger(Console.Error) })
.RemoveRedundantLineBreaks();
}
private static Dictionary<string, string> _MemberNamePrefixDict = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
["F:"] = "Field",
["P:"] = "Property",
["T:"] = "Type",
["E:"] = "Event",
["M:"] = "Method",
};
/// <summary>
/// Write out the given XML Node as Markdown. Recursive function used internally.
/// </summary>
/// <param name="node">The xml node to write out.</param>
/// <param name="ConversionContext">The Conversion Context that will be passed around and manipulated over the course of the translation.</param>
/// <returns>The converted markdown text.</returns>
public static string ToMarkDown(this XNode node, ConversionContext context)
{
if(node is XDocument)
{
node = ((XDocument)node).Root;
}
string name;
if (node.NodeType == XmlNodeType.Element)
{
var el = (XElement)node;
name = el.Name.LocalName;
if (name == "member")
{
string expandedName = null;
if(!_MemberNamePrefixDict.TryGetValue(el.Attribute("name").Value.Substring(0,2), out expandedName))
{
expandedName = "none";
}
name = expandedName.ToLowerInvariant();
}
if (name == "see")
{
var anchor = el.Attribute("cref") != null && el.Attribute("cref").Value.StartsWith("!:#");
name = anchor ? "seeAnchor" : "seePage";
}
//treat first Param element separately to add table headers.
if (name.EndsWith("param")
&& node
.ElementsBeforeSelf()
.LastOrDefault()
?.Name
?.LocalName != "param")
{
name = "firstparam";
}
try {
var vals = TagRenderer.Dict[name].ValueExtractor(el, context).ToArray();
return string.Format(TagRenderer.Dict[name].FormatString, args: vals);
}
catch(KeyNotFoundException ex)
{
var lineInfo = (IXmlLineInfo)node;
switch(context.UnexpectedTagAction)
{
case UnexpectedTagActionEnum.Error:
throw new XmlException($@"Unknown element type ""{ name }""", ex, lineInfo.LineNumber, lineInfo.LinePosition);
case UnexpectedTagActionEnum.Warn:
context.WarningLogger.LogWarning($@"Unknown element type ""{ name }"" on line {lineInfo.LineNumber}, pos {lineInfo.LinePosition}");
break;
case UnexpectedTagActionEnum.Accept:
//do nothing;
break;
default:
throw new InvalidOperationException($"Unexpected {nameof(UnexpectedTagActionEnum)}");
}
}
}
if (node.NodeType == XmlNodeType.Text)
return Regex.Replace(((XText)node).Value.Replace('\n', ' '), @"\s+", " ");
return "";
}
private static readonly Regex _PrefixReplacerRegex = new Regex(@"(^[A-Z]\:)");
internal static string[] ExtractNameAndBodyFromMember(XElement node, ConversionContext context)
{
var newName = Regex.Replace(node.Attribute("name").Value, $@":{Regex.Escape(context.AssemblyName)}\.", ":"); //remove leading namespace if it matches the assembly name
//TODO: do same for function parameters
newName = _PrefixReplacerRegex.Replace(newName, match => _MemberNamePrefixDict[match.Value] + " "); //expand prefixes into more verbose words for member.
return new[]
{
newName,
node.Nodes().ToMarkDown(context)
};
}
internal static string[] ExtractNameAndBody(string att, XElement node, ConversionContext context)
{
return new[]
{
node.Attribute(att)?.Value,
node.Nodes().ToMarkDown(context)
};
}
internal static string ToMarkDown(this IEnumerable<XNode> es, ConversionContext context)
{
return es.Aggregate("", (current, x) => current + x.ToMarkDown(context));
}
internal static string ToCodeBlock(this string s)
{
var lines = s.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
var blank = lines[0].TakeWhile(x => x == ' ').Count() - 4;
return string.Join("\n", lines.Select(x => new string(x.SkipWhile((y, i) => i < blank).ToArray()))).TrimEnd();
}
static string RemoveRedundantLineBreaks(this string s)
{
return Regex.Replace(s, @"\n\n\n+", "\n\n");
}
}
}