ファイル
YMM4-UndoTree/UndoTree/UndoTreeExport.cs
T

40 行
1.9 KiB
C#

using System.Text;
namespace UndoTree;
internal static class UndoTreeExport
{
public static string ToMarkdown(UndoTreeRecord record, string? projectPath)
{
var children = record.Nodes.ToLookup(node => node.Parent);
var ids = record.Nodes.Select(node => node.Id).ToHashSet();
var text = new StringBuilder();
text.Append("# ").Append(Texts.UndoTree).Append(Environment.NewLine);
if (!string.IsNullOrWhiteSpace(projectPath))
text.Append(Environment.NewLine).Append(projectPath).Append(Environment.NewLine);
text.Append(Environment.NewLine);
foreach (var root in record.Nodes.Where(node => node.Parent < 0 || !ids.Contains(node.Parent)))
Append(text, root, children, 0);
return text.ToString();
}
static void Append(StringBuilder text, UndoTreeRecordNode node, ILookup<int, UndoTreeRecordNode> children, int depth)
{
var indent = new string(' ', depth * 2);
var marks = (node.Saved ? $"[{Texts.SavedMark}]" : string.Empty) + (node.Tag.Length > 0 ? $"[{node.Tag}]" : string.Empty);
var label = node.Parent < 0 && node.Label.Length == 0 ? Texts.InitialState : node.Label;
var time = node.Time == DateTime.MinValue ? string.Empty : $" ({node.Time:T})";
text.Append(indent).Append("- ").Append(marks.Length > 0 ? marks + " " : string.Empty).Append(label).Append(time).Append(Environment.NewLine);
foreach (var command in node.Commands)
{
var name = command.Target.Length > 0 ? $"{command.Target} / {command.Member}" : command.Member;
text.Append(indent).Append(" - ").Append(name).Append(command.Detail.Length > 0 ? $": {command.Detail}" : string.Empty).Append(Environment.NewLine);
}
foreach (var child in children[node.Id].OrderBy(item => item.Id))
Append(text, child, children, depth + 1);
}
}