loxsmoke / mddox

Markdown documentation generator tool
MIT License
37 stars 10 forks source link

Can you add support to azure? #27

Closed robertosistemas closed 10 months ago

robertosistemas commented 10 months ago

using System; using System.Linq; using System.Text; using System.Text.RegularExpressions; using MdDox.MarkdownWriters.Interfaces;

namespace MdDox.MarkdownWriters { public class AzureMarkdownWriter : IMarkdownWriter { public string FormatName => "azure";

    #region Basic writing
    public StringBuilder allText = new StringBuilder();

    public void Write(string text)
    {
        if (text == null) return;
        allText.AppendLine(text);
    }

    public void WriteLine(string text)
    {
        if (text != null) allText.AppendLine(text);
        allText.AppendLine();
    }
    #endregion

    #region Formatted writing
    public void WriteH1(string text)
    {
        WriteLine("# " + EscapeSpecialChars(text));
    }
    public void WriteH2(string text)
    {
        WriteLine("## " + EscapeSpecialChars(text));
    }

    public void WriteH3(string text)
    {
        WriteLine("### " + EscapeSpecialChars(text));
    }

    public void WriteLink(string anchorName, string text)
    {
        allText.Append(Link(anchorName, text));
    }
    public void WriteHeadingLink(string text)
    {
        allText.Append(HeadingLink(text));
    }
    public void WriteAnchor(string anchorName)
    {
        allText.Append($"<a name=\"{anchorName}\"></a>");
    }
    public void WriteHorizontalRule()
    {
        allText.AppendLine();
        WriteLine("---");
    }
    #endregion

    #region Tables
    public void WriteTableTitle(params string[] tableHeadings)
    {
        Write("| " + string.Join(" | ", tableHeadings) + " |");
        Write("|" + string.Join("|", tableHeadings.Select(x => "---")) + "|");
    }

    public void WriteTableRow(params string[] texts)
    {
        Write("| " + string.Join(" | ", texts.Select(EscapeSpecialText)) + " |");
    }
    #endregion

    #region Text formatting
    string EscapeSpecialText(string text)
    {
        if (text == null) return "";
        text = ResolveTag(text, "paramref", "name");
        return EscapeSpecialChars(text);
    }

    string ResolveTag(string text, string tagName, string attributeName)
    {
        var regex = new Regex("<" + tagName + "( +)" + attributeName + "( *)=( *)\"(.*?)\"( *)/>");
        for (; ; )
        {
            var match = regex.Match(text);
            if (!match.Success) return text;

            var attributeValue = match.Groups[4].Value;
            text = text.Substring(0, match.Index) + Bold(attributeValue) + text.Substring(match.Index + match.Length);
        }
    }

    public string EscapeSpecialChars(string text)
    {
        if (text == null) return "";
        text = text.Replace("<", "\\<");
        text = text.Replace(">", "\\>");
        text = text.Replace("&gt;", ">");
        text = text.Replace("&lt;", "<");
        text = text.Replace("|", "\\|");
        return text.Replace(Environment.NewLine, "<br>");
    }
    public string Bold(string text)
    {
        return "**" + text + "**";
    }
    public string Link(string anchorName, string text)
    {
        return $"[{text}]({anchorName})";
    }
    public string HeadingLink(string anchorName, string text = null)
    {
        anchorName = anchorName.ToLower();
        anchorName = anchorName.Replace("<", "%5C%3C");
        anchorName = anchorName.Replace(">", "%5C%3E");
        anchorName = anchorName.Replace(",", "%2C");
        anchorName = anchorName.RegexReplace(@"[^a-zA-Z\d\(\)\% -]", "").Replace(" ", "-");
        return $"[{text ?? anchorName}](#{anchorName})";
    }
    #endregion

    public string FullText => allText.ToString();
}

}

loxsmoke commented 10 months ago

Please create the pull request. That would be faster.

robertosistemas commented 10 months ago

I created the pull request: pull request

loxsmoke commented 10 months ago

Pull request merged