Skip to content

Commit f37cb6c

Browse files
committed
Add render options
1 parent 900ae57 commit f37cb6c

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

src/Render/BitzArt.XDoc.PlainText/PlainTextRenderer.cs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,45 @@ public class PlainTextRenderer
1313
/// Converts an XML documentation node to the plain text.
1414
/// </summary>
1515
/// <param name="documentation"></param>
16+
/// <param name="forceSingleLine"></param>
17+
/// <param name="useShortTypeNames"></param>
1618
/// <returns></returns>
17-
public static string Render(MemberDocumentation? documentation)
19+
public static string Render(
20+
MemberDocumentation? documentation,
21+
bool forceSingleLine = false,
22+
bool useShortTypeNames = true)
1823
{
1924
if (documentation == null)
2025
{
2126
return string.Empty;
2227
}
2328

24-
return new PlainTextRenderer(documentation).Render();
29+
return new PlainTextRenderer(documentation, new RendererOptions
30+
{
31+
ForceSingleLine = forceSingleLine,
32+
UseShortTypeNames = useShortTypeNames
33+
}).Render();
2534
}
2635

2736
/// <summary>
2837
/// The documentation instance to be rendered by this renderer.
2938
/// This field is initialized in the constructor and remains readonly afterward.
3039
/// </summary>
31-
protected readonly MemberDocumentation Documentation;
40+
private readonly MemberDocumentation _documentation;
41+
42+
private readonly RendererOptions _options;
3243

33-
protected PlainTextRenderer(MemberDocumentation documentation)
44+
private PlainTextRenderer(MemberDocumentation documentation, RendererOptions rendererOptions)
3445
{
35-
Documentation = documentation;
46+
_options = rendererOptions;
47+
_documentation = documentation;
3648
}
3749

3850
/// <summary>
3951
/// Renders the current documentation to plain text.
4052
/// </summary>
4153
/// <returns>A normalized plain text representation of the documentation.</returns>
42-
protected string Render() => Normalize(Render(Documentation.Node));
54+
private string Render() => Normalize(Render(_documentation.Node));
4355

4456
/// <summary>
4557
/// Renders the content of an XML node to plain text.
@@ -70,9 +82,9 @@ private string Normalize(string input)
7082
.Where(o => !string.IsNullOrWhiteSpace(o))
7183
.ToList();
7284

73-
var result = string.Join("\n", lines);
85+
var separator = _options.ForceSingleLine ? " " : "\n";
7486

75-
return result;
87+
return string.Join(separator, lines);
7688
}
7789

7890
/// <summary>
@@ -102,9 +114,9 @@ private string RenderXmlElement(XmlElement element)
102114
/// </summary>
103115
/// <param name="element"></param>
104116
/// <returns></returns>
105-
protected virtual string RenderReference(XmlElement element)
117+
private string RenderReference(XmlElement element)
106118
{
107-
var documentationReference = Documentation.GetReference(element);
119+
var documentationReference = _documentation.GetReference(element);
108120

109121
if (documentationReference == null)
110122
{
@@ -134,18 +146,28 @@ private string RenderSimpleDocumentationReference(DocumentationReference documen
134146

135147
if (cref.Prefix is "T:")
136148
{
137-
return cref.ShortType;
149+
if (_options.UseShortTypeNames)
150+
{
151+
return cref.ShortType;
152+
}
153+
154+
return cref.Type;
138155
}
139156

140157
if (cref.Prefix is "M:" or "P:" or "F:")
141158
{
142-
return $"{cref.ShortType}.{cref.Member}";
159+
if (_options.UseShortTypeNames)
160+
{
161+
return $"{cref.ShortType}.{cref.Member}";
162+
}
163+
164+
return $"{cref.Type}.{cref.Member}";
143165
}
144166

145167
return string.Empty;
146168
}
147169

148-
private static string RenderDocumentationReference(DocumentationReference documentationReference)
170+
private string RenderDocumentationReference(DocumentationReference documentationReference)
149171
{
150172
if (documentationReference.Target == null)
151173
{
@@ -167,10 +189,6 @@ private static string RenderDocumentationReference(DocumentationReference docume
167189
};
168190

169191
return text;
170-
171-
// var renderer = new PlainTextRenderer(documentationReference.Target);
172-
//
173-
// return renderer.Render();
174192
}
175193

176194
/// <summary>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace BitzArt.XDoc;
2+
3+
/// <summary>
4+
/// Configuration options for controlling XML documentation rendering behavior.
5+
/// </summary>
6+
public record RendererOptions
7+
{
8+
/// <summary>
9+
/// Indicating whether the content should be forced to render as a single line.
10+
/// When set to true, any line breaks in the content will be removed or replaced.
11+
/// When set to false, the content will maintain its original line structure.
12+
/// </summary>
13+
public bool ForceSingleLine { get; init; } = true;
14+
15+
/// <summary>
16+
/// Indicating whether type names should be rendered in their short form.
17+
/// When set to true, simple type names will be used (e.g., "string" instead of "System.String").
18+
/// When set to false, fully qualified type names will be used.
19+
/// </summary>
20+
public bool UseShortTypeNames { get; init; } = true;
21+
}

src/Render/BitzArt.XDoc.PlainText/XDocExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ public static class XDocExtensions
99
/// Renders the documentation of a <see cref="Type"/> as plain text.
1010
/// </summary>
1111
/// <param name="documentation"></param>
12+
/// <param name="forceSingleLine"></param>
1213
/// <returns></returns>
13-
public static string ToPlainText(this MemberDocumentation? documentation)
14+
public static string ToPlainText(this MemberDocumentation? documentation, bool forceSingleLine = false)
1415
{
1516
if (documentation == null)
1617
{
1718
return string.Empty;
1819
}
1920

20-
return PlainTextRenderer.Render(documentation);
21+
return PlainTextRenderer.Render(documentation, forceSingleLine);
2122
}
2223
}

0 commit comments

Comments
 (0)