/
HtmlHelpExporter.cs
58 lines (48 loc) · 1.92 KB
/
HtmlHelpExporter.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
// Copyright Bastian Eicher et al.
// Licensed under the GNU Lesser Public License
using System.Web;
namespace ZeroInstall.Commands.Basic.Exporters;
/// <summary>
/// Exports all <see cref="CliCommand"/> help texts as HTML.
/// </summary>
public class HtmlHelpExporter : HelpExporterBase
{
protected override string CommandListHeader() => $"""
<table>
<tr>
<th>{Resources.Command}</th>
<th>{Resources.Description}</th>
</tr>
""";
protected override string CommandListEntry(string? name, string description) => $"""
<tr>
<td><a href='#{name?.Replace(" ", "_")}'><nobr><code>{name}</code></nobr></a></td>
<td>{HtmlEncode(description)}</td>
</tr>
""";
protected override string CommandListFooter() => @"</table>";
protected override string CommandDetails(string? name, string description, string usage) => $"""
<a name='{name?.Replace(" ", "_")}'></a><h1>{name}</h1>
<p>{HtmlEncode(description)}</p>
<p><b>{Resources.Usage}</b> <code>0install {name} {usage}</code></p>
""";
protected override string OptionListHeader() => $"""
<table>
<tr>
<th>Option</th>
<th>{Resources.Description}</th>
</tr>
""";
protected override string OptionListEntry(IEnumerable<string> prototypes, string description) => $"""
<tr>
<td>{string.Join("<br/>", prototypes.Select(x => $"<nobr><code>{HtmlEncode(x)}</code></nobr>"))}</td>
<td>{HtmlEncode(description)}</td>
</tr>
""";
protected override string OptionListFooter() => "</table>";
private static string HtmlEncode(string value)
=> HttpUtility.HtmlEncode(value)
.Replace("\n", "<br/>")
.Replace("{", "<code>")
.Replace("}", "</code>");
}