-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMarkdownVisitor.cs
169 lines (135 loc) · 4.59 KB
/
MarkdownVisitor.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
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Text;
using ClariusLabs.NuDoc;
namespace Inversion.Documentation.Generator {
public class MarkdownVisitor: Visitor {
private readonly StringBuilder _builder = new StringBuilder();
private string _name(string id) {
string name = id.Replace("`0", "{T0}").Replace("`1", "{T1}").Replace("`2", "{T2}").Replace("`3", "{T3}");
return name;
}
public string Markdown {
get { return _builder.ToString(); }
}
private TypeDeclaration _currentType;
public override void VisitMethod(Method method) {
_builder.AppendLine();
string methodName = (_currentType != null) ? method.Id.Replace(_currentType.Id.Substring(1), "").Substring(1) : method.Id;
_builder.AppendFormat("### `{0}`", _name(methodName));
base.VisitMember(method);
}
public override void VisitProperty(Property property) {
string propertyName = (_currentType != null) ? property.Id.Replace(_currentType.Id.Substring(1), "") : property.Id;
if (propertyName.StartsWith("P.")) {
_builder.AppendFormat("### `{0}`", propertyName.Replace("P.", "."));
} else {
_builder.AppendFormat("## `{0}`", _name(propertyName));
}
base.VisitMember(property);
}
public override void VisitMember(Member member) {
_builder.AppendLine();
_builder.AppendFormat("### `{0}`", _name(member.Id));
base.VisitMember(member);
}
public override void VisitType(TypeDeclaration member) {
_currentType = member;
_builder.AppendLine();
_builder.AppendFormat("## `{0}`", _name(member.Id));
base.VisitMember(member);
}
public override void VisitSummary(Summary summary) {
_builder.AppendLine();
base.VisitSummary(summary);
_builder.AppendLine();
_builder.AppendLine();
}
public override void VisitRemarks(Remarks remarks) {
_builder.AppendLine("#### Remarks");
base.VisitRemarks(remarks);
_builder.AppendLine();
}
public override void VisitExample(Example example) {
_builder.AppendLine("##### Example");
base.VisitExample(example);
_builder.AppendLine();
}
public override void VisitAssembly(AssemblyMembers assembly) {
base.VisitAssembly(assembly);
}
public override void VisitDocument(DocumentMembers document) {
base.VisitDocument(document);
}
public override void VisitDescription(Description description) {
base.VisitDescription(description);
}
public override void VisitC(C code) {
_builder.AppendFormat("`{0}`", code.Content);
base.VisitC(code);
}
public override void VisitCode(Code code) {
_builder.AppendLine();
_builder.AppendLine();
// Indent code with 4 spaces according to Markdown syntax.
foreach (var line in code.Content.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) {
_builder.Append(" ");
_builder.AppendLine(line);
}
base.VisitCode(code);
}
public override void VisitText(Text text) {
_builder.Append(text.Content);
base.VisitText(text);
}
public override void VisitPara(Para para) {
// Avoid double line breaks between adjacent <para> elements.
if (_builder.Length < 2 ||
new string(new char[] { _builder[_builder.Length - 2], _builder[_builder.Length - 1] }) != Environment.NewLine) {
_builder.AppendLine();
}
base.VisitPara(para);
_builder.AppendLine();
_builder.AppendLine();
}
private string _normalizeLink(string cref) {
return cref.Replace(":", "-").Replace("(", "-").Replace(")", "");
}
public override void VisitSee(See see) {
if (see.Cref != null) {
var cref = _normalizeLink(see.Cref);
_builder.AppendFormat(" [{0}]({1}) ", cref.Substring(2), cref);
}
}
public override void VisitSeeAlso(SeeAlso seeAlso) {
var cref = _normalizeLink(seeAlso.Cref);
_builder.AppendFormat("[{0}]({1})", cref.Substring(2), cref);
}
public override void VisitParamRef(ParamRef paramRef) {
base.VisitParamRef(paramRef);
if (!string.IsNullOrEmpty(paramRef.Name)) _builder.AppendFormat("`{0}`", paramRef.Name);
}
public override void VisitParam(Param param) {
_builder.AppendFormat("* `{0}`: ", param.Name);
base.VisitParam(param);
_builder.AppendLine();
}
public override void VisitTypeParam(TypeParam param) {
_builder.AppendFormat("`{0}`: ", param.Name);
base.VisitTypeParam(param);
_builder.AppendLine();
}
public override void VisitReturns(Returns returns) {
_builder.AppendLine();
_builder.AppendLine("**returns:** ");
base.VisitReturns(returns);
_builder.AppendLine();
_builder.AppendLine();
}
public override void VisitValue(Value value) {
_builder.AppendLine();
_builder.Append("**value:** ");
base.VisitValue(value);
_builder.AppendLine();
}
}
}