using Markdig.Extensions.Tables;
using Markdig.Syntax.Inlines;
using Markdig.Syntax;
public static class MarkdownHelper
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 블럭 나열하기 - EnumerateBlock(block, level)
/// <summary>
/// 블럭 나열하기
/// </summary>
/// <param name="block">블럭</param>
/// <param name="level">레벨</param>
public static void EnumerateBlock(Block block, int level)
{
if(block is Table table)
{
WriteElementType(block, level, false);
Console.Write(" : [");
TableRow firstTableRow = table[0] as TableRow;
for(int i = 0; i < firstTableRow.Count; i++)
{
TableColumnDefinition tableColumnDefinition = table.ColumnDefinitions[i];
if(i > 0)
{
Console.Write(",");
}
if(tableColumnDefinition.Alignment.HasValue)
{
Console.Write($"{tableColumnDefinition.Alignment.Value}");
}
else
{
Console.Write($"{TableColumnAlign.Left}");
}
}
Console.WriteLine("]");
foreach(Block childBlock in table)
{
if(childBlock is TableRow tableRow)
{
EnumerateTableRow(tableRow, level + 1);
}
else
{
EnumerateBlock(childBlock, level + 1);
}
}
}
else
{
if(block is LeafBlock)
{
if(block is ParagraphBlock paragraphBlock)
{
WriteElementType(block, level, true);
EnumerateInline(paragraphBlock.Inline, level + 1);
}
else if(block is HeadingBlock headingBlock)
{
WriteElementType(block, level, false);
Console.WriteLine($" : {headingBlock.Level}");
EnumerateInline(headingBlock.Inline, level + 1);
}
else if(block is FencedCodeBlock fencedCodeBlock)
{
WriteElementType(block, level, false);
Console.WriteLine($" : {fencedCodeBlock.Info}");
Console.WriteLine("----------------------------------------------------------------------------------------------------");
foreach(object line in fencedCodeBlock.Lines)
{
Console.WriteLine(line.ToString());
}
Console.WriteLine("----------------------------------------------------------------------------------------------------");
}
else
{
WriteElementType(block, level, true);
return;
}
}
else if(block is ContainerBlock containerBlock)
{
WriteElementType(block, level, true);
foreach(Block childBlock in containerBlock)
{
EnumerateBlock(childBlock, level + 1);
}
}
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 엘리먼트 타입 쓰기 - WriteElementType(inline, level, newLine)
/// <summary>
/// 엘리먼트 타입 쓰기
/// </summary>
/// <param name="inline">인라인</param>
/// <param name="level">레벨</param>
/// <param name="newLine">개행 여부</param>
private static void WriteElementType(Inline inline, int level, bool newLine = true)
{
int count = level * 4;
string padding = count > 0 ? new string(' ', count) : string.Empty;
string elementType = $"{padding}{inline.GetType().Name}";
if(newLine)
{
Console.WriteLine(elementType);
}
else
{
Console.Write(elementType);
}
}
#endregion
#region 인라인 열거하기 - EnumerateInline(inline, level)
/// <summary>
/// 인라인 열거하기
/// </summary>
/// <param name="inline">인라인</param>
/// <param name="level">레벨</param>
private static void EnumerateInline(Inline inline, int level)
{
if(inline is ContainerInline containerInline)
{
if(containerInline is LinkInline linkInline)
{
WriteElementType(inline, level, false);
string isImage = linkInline.IsImage ? "(이미지)" : "(링크)";
Console.WriteLine($" : {isImage} {linkInline.Url}");
foreach(Inline childInline in containerInline)
{
EnumerateInline(childInline, level + 1);
}
}
else
{
WriteElementType(inline, level, true);
foreach(Inline childInline in containerInline)
{
EnumerateInline(childInline, level + 1);
}
}
}
else if(inline is LeafInline leafInline)
{
if(inline is LiteralInline literalInline)
{
WriteElementType(inline, level, false);
Console.WriteLine(" : " + literalInline.Content);
}
else if(inline is CodeInline codeInline)
{
WriteElementType(inline, level, false);
Console.WriteLine(" : " + codeInline.Content);
}
else
{
WriteElementType(inline, level, true);
}
return;
}
}
#endregion
#region 엘리먼트 타입 쓰기 - WriteElementType(block, level, newLine)
/// <summary>
/// 엘리먼트 타입 쓰기
/// </summary>
/// <param name="block">블럭</param>
/// <param name="level">레벨</param>
/// <param name="newLine">신규 행 여부</param>
private static void WriteElementType(Block block, int level, bool newLine = true)
{
int count = level * 4;
string padding = count > 0 ? new string(' ', count) : string.Empty;
string elementType = $"{padding}{block.GetType().Name}";
if(newLine)
{
Console.WriteLine(elementType);
}
else
{
Console.Write(elementType);
}
}
#endregion
#region 테이블 셀 열거하기 - EnumerateTableCell(tableCell, level)
/// <summary>
/// 테이블 셀 열거하기
/// </summary>
/// <param name="tableCell">테이블 셀</param>
/// <param name="level">레벨</param>
private static void EnumerateTableCell(TableCell tableCell, int level)
{
WriteElementType(tableCell, level, true);
foreach(Block childBlock in tableCell)
{
EnumerateBlock(childBlock, level + 1);
}
}
#endregion
#region 테이블 행 열거하기 - EnumerateTableRow(tableRow, level)
/// <summary>
/// 테이블 행 열거하기
/// </summary>
/// <param name="tableRow">테이블 행</param>
/// <param name="level">레벨</param>
private static void EnumerateTableRow(TableRow tableRow, int level)
{
WriteElementType(tableRow, level, true);
foreach(Block childBlock in tableRow)
{
if(childBlock is TableCell tableCell)
{
EnumerateTableCell(tableCell, level + 1);
}
else
{
EnumerateBlock(childBlock, level + 1);
}
}
}
#endregion
}