// Copyright © Selmo Technology GmbH. All Rights Reserved

using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Helpers
{
    public class CodeBlockRenderer : HtmlObjectRenderer<CodeBlock>
    {
        protected override void Write(HtmlRenderer renderer, CodeBlock obj)
        {
            var fencedCodeBlock = obj as FencedCodeBlock;
            var language = fencedCodeBlock?.Info ?? "";

            if (string.IsNullOrEmpty(language))
            {
                language = "plaintext";
            }

            var code = obj.Lines.ToString();

            renderer.EnsureLine();
            renderer.Write($"<pre><code class=\"language-{language}\">");
            renderer.WriteEscape(code);
            renderer.WriteLine("</code></pre>");
            renderer.EnsureLine();
        }
    }
}