Skip to content

Commit 4574215

Browse files
feat: support plain-text help documentation
1 parent 12e723a commit 4574215

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

Code/Plugin/Commands/HelpSystem/DocsCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,6 @@ private static void MakeDocFile(string type, StringBuilder content)
127127
if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);
128128

129129
using var sw = File.CreateText(Path.Combine(folder, $"#{type}.txt"));
130-
sw.Write(content.ToString());
130+
sw.Write(DocsProvider.Render(content.ToString(), DocsFormat.PlainText));
131131
}
132132
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SER.Code.Plugin.Commands.HelpSystem;
2+
3+
public enum DocsFormat
4+
{
5+
Markdown,
6+
PlainText
7+
}

Code/Plugin/Commands/HelpSystem/DocsProvider.cs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ private static string BulletList(IEnumerable<string> items) =>
3939
private static string Heading(string title, int level = 2) =>
4040
$"{new string('#', level)} {title}";
4141

42+
public static string Render(string content, DocsFormat format) =>
43+
format == DocsFormat.Markdown ? content : RenderPlainText(content);
44+
4245
public static readonly Dictionary<HelpOption, Func<string>> GeneralOptions = new()
4346
{
4447
[HelpOption.Start] = GetStartHelpPage,
@@ -52,7 +55,18 @@ private static string Heading(string title, int level = 2) =>
5255
[HelpOption.Keywords] = GetKeywordHelpPage
5356
};
5457

55-
public static bool GetGeneralOutput(ArraySegment<string> args, ICommandSender sender, out string response)
58+
public static bool GetGeneralOutput(
59+
ArraySegment<string> args,
60+
ICommandSender sender,
61+
out string response,
62+
DocsFormat format = DocsFormat.Markdown)
63+
{
64+
var result = GetGeneralOutputMarkdown(args, sender, out var markdownResponse);
65+
response = Render(markdownResponse, format);
66+
return result;
67+
}
68+
69+
private static bool GetGeneralOutputMarkdown(ArraySegment<string> args, ICommandSender sender, out string response)
5670
{
5771
var arg = args.Array?[args.Offset].ToLowerInvariant()
5872
?? throw new CoreInvariantException("Help arguments were provided in an invalid format.");
@@ -141,7 +155,12 @@ public static bool GetGeneralOutput(ArraySegment<string> args, ICommandSender se
141155
return false;
142156
}
143157

144-
public static string GetOptionsList()
158+
public static string GetOptionsList(DocsFormat format = DocsFormat.Markdown)
159+
{
160+
return Render(GetOptionsListMarkdown(), format);
161+
}
162+
163+
private static string GetOptionsListMarkdown()
145164
{
146165
return $"""
147166
# SER help
@@ -338,7 +357,7 @@ public static string GetKeywordInfo(IKeywordContext keyword)
338357
public static string GetKeywordHelpPage()
339358
{
340359
return
341-
"""
360+
$"""
342361
Keywords alter how the script behaves, not by changing someones role, but the internal script execution.
343362
They can range from simple things from stopping the script to handling advanced logic.
344363
@@ -1028,6 +1047,61 @@ public static bool GetPropertiesForType(string typeName, out string response)
10281047
return true;
10291048
}
10301049

1050+
private static string RenderPlainText(string content)
1051+
{
1052+
var lines = content.Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
1053+
var output = new StringBuilder();
1054+
var inCodeBlock = false;
1055+
1056+
for (var index = 0; index < lines.Length; index++)
1057+
{
1058+
var line = lines[index];
1059+
var trimmed = line.Trim();
1060+
1061+
if (trimmed.StartsWith("```", StringComparison.Ordinal))
1062+
{
1063+
inCodeBlock = !inCodeBlock;
1064+
continue;
1065+
}
1066+
1067+
if (inCodeBlock)
1068+
{
1069+
output.AppendLine(line);
1070+
continue;
1071+
}
1072+
1073+
if (trimmed.Length == 0)
1074+
{
1075+
output.AppendLine();
1076+
continue;
1077+
}
1078+
1079+
var heading = trimmed.TakeWhile(c => c == '#').Count();
1080+
if (heading is > 0 and <= 6 && trimmed.Length > heading && char.IsWhiteSpace(trimmed[heading]))
1081+
{
1082+
var title = StripInlineMarkdown(trimmed[(heading + 1)..].Trim());
1083+
output.AppendLine(title);
1084+
output.AppendLine(new string(heading == 1 ? '=' : '-', Math.Max(3, title.Length)));
1085+
continue;
1086+
}
1087+
1088+
output.AppendLine(StripInlineMarkdown(line));
1089+
}
1090+
1091+
return output.ToString().TrimEnd();
1092+
}
1093+
1094+
private static string StripInlineMarkdown(string line)
1095+
{
1096+
line = line.Replace("\\`", "`");
1097+
line = System.Text.RegularExpressions.Regex.Replace(line, @"`([^`]*)`", "$1");
1098+
line = System.Text.RegularExpressions.Regex.Replace(line, @"\[([^\]]+)\]\([^\)]+\)", "$1");
1099+
line = line.Replace("**", string.Empty).Replace("__", string.Empty);
1100+
line = System.Text.RegularExpressions.Regex.Replace(line, @"(?<!\w)[*_](.*?)[*_](?!\w)", "$1");
1101+
line = System.Text.RegularExpressions.Regex.Replace(line, @"^\s*>\s?", "");
1102+
return line;
1103+
}
1104+
10311105
// ai
10321106
public static bool GetPropertiesAdvanced(
10331107
ArraySegment<string> args,

Code/Plugin/Commands/HelpSystem/HelpCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out s
1414
{
1515
if (arguments.Count > 0)
1616
{
17-
return DocsProvider.GetGeneralOutput(arguments, sender, out response);
17+
return DocsProvider.GetGeneralOutput(arguments, sender, out response, DocsFormat.PlainText);
1818
}
1919

20-
response = DocsProvider.GetOptionsList();
20+
response = DocsProvider.GetOptionsList(DocsFormat.PlainText);
2121
return true;
2222
}
2323
}

0 commit comments

Comments
 (0)