joshids / markdownsharp

Automatically exported from code.google.com/p/markdownsharp
0 stars 0 forks source link

Have the option to add a prettify class name to the <pre> tags #62

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
It would be great to be able to configure the classname to be added to the 
<pre> tags to enable prettifying of the code using a syntax highlighter such as 
google-code-prettify.

Here's my take on the solution.

Add this code to MarkdownOptions:

    /// <summary>
    /// Specifies the value set in the class attribute of the <pre> tags
    /// that are wrapped around code segments
    /// e.g. "prettyprint" for google-code-prettify 
    /// http://code.google.com/p/google-code-prettify/
    /// </summary>
    public string PrettifyClass { get; set; }

Modify Markdown constructors:

    public Markdown(bool loadOptionsFromConfigFile):
    {
        //...
        case "MarkDown.PrettifyClass":
            _prettifyClass = settings[key];
            break;
    }

And:

    public Markdown(MarkdownOptions options)
    {
        //...
        _prettifyClass = options.PrettifyClass;
    }

And modify the CodeBlockEvaluator method of the Markdown class:

    private string CodeBlockEvaluator(Match match)
    {
        string codeBlock = match.Groups[1].Value;

        codeBlock = EncodeCode(Outdent(codeBlock));
        codeBlock = _newlinesLeadingTrailing.Replace(codeBlock, "");

        return _prettifyClass.Length > 0
            ? string.Concat("\n\n<pre class=\"" + _prettifyClass + "\"><code>", codeBlock, "\n</code></pre>\n\n")
            : string.Concat("\n\n<pre><code>", codeBlock, "\n</code></pre>\n\n");
    }

Original issue reported on code.google.com by weavers...@gmail.com on 15 Oct 2013 at 1:30