Code Snippets – Code Prettify

Another plugin, Code Prettify, leverages the Google Prettify library without having to manually link code and add classes (applies to <pre> and <code> blocks).

private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
    {
        var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

        var rand = new Random();
        int numSentences = rand.Next(maxSentences - minSentences)
            + minSentences + 1;
        int numWords = rand.Next(maxWords - minWords) + minWords + 1;

        var sb = new StringBuilder();
        for (int p = 0; p < numLines; p++)
        {
            for (int s = 0; s < numSentences; s++)
            {
                for (int w = 0; w < numWords; w++) { if (w > 0) { sb.Append(" "); }
                    sb.Append(words[rand.Next(words.Length)]);
                }
                sb.Append(". ");
            }
            sb.AppendLine();
        }
        return sb.ToString();
    }

Code Snippets – Google Prettify

Another option for syntax highlighting appears to be Google Prettify.js (assuming we can link external scripts).

Link the script <script src=”https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js”></script>.

Operates on blocks with “prettyprint” class.

Note: incompatble with Mivhak. To bypass Mivhak syntax plugin, you can wrap in <pre> block (without class) and add the class to a <code> block.

Git Page

private static string LoremIpsum(int minWords, int maxWords, int minSentences, int maxSentences, int numLines)
    {
        var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer", "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod", "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

        var rand = new Random();
        int numSentences = rand.Next(maxSentences - minSentences)
            + minSentences + 1;
        int numWords = rand.Next(maxWords - minWords) + minWords + 1;

        var sb = new StringBuilder();
        for (int p = 0; p < numLines; p++)
        {
            for (int s = 0; s < numSentences; s++)
            {
                for (int w = 0; w < numWords; w++) { if (w > 0) { sb.Append(" "); }
                    sb.Append(words[rand.Next(words.Length)]);
                }
                sb.Append(". ");
            }
            sb.AppendLine();
        }
        return sb.ToString();
    }

Code Snippets w/ Mivhak Syntax Highlighter

Using version 1.3.6 with wordpress.

Insert code with newly added “code” icon. Options can be set from the visual editor.

New plugin has options for appearance, defaults etc. on admin toolbar. Icon looks like <>.

Reference Page

static string LoremIpsum(int minWords, int maxWords,
    int minSentences, int maxSentences,
    int numParagraphs) {

    var words = new[]{"lorem", "ipsum", "dolor", "sit", "amet", "consectetuer",
        "adipiscing", "elit", "sed", "diam", "nonummy", "nibh", "euismod",
        "tincidunt", "ut", "laoreet", "dolore", "magna", "aliquam", "erat"};

    var rand = new Random();
    int numSentences = rand.Next(maxSentences - minSentences)
        + minSentences + 1;
    int numWords = rand.Next(maxWords - minWords) + minWords + 1;

    StringBuilder result = new StringBuilder();

    for(int p = 0; p < numParagraphs; p++) {
        result.Append("<p>");
        for(int s = 0; s < numSentences; s++) {
            for(int w = 0; w < numWords; w++) {
                if (w > 0) { result.Append(" "); }
                result.Append(words[rand.Next(words.Length)]);
            }
            result.Append(". ");
        }
        result.Append("</p>");
    }

    return result.ToString();
}

 

Code Snippets w/ WP-Syntax

Hello! This is a test on adding code snippets with syntax highlighting to a WP site.

Syntax highlighting can be done with the WP-Syntax plugin (currently version 1.0).

That being said, I had to make an edit to the wp-syntax.css file to get line numbering to work correctly.

.wp_syntax td.code{
	background-color: #EEE;
	background-image: -webkit-linear-gradient( transparent 50%, rgba(255, 255, 255, .9) 50% );
	background-image: -moz-linear-gradient( transparent 50%, rgba(255, 255, 255, .9) 50% );
	background-image: -ms-linear-gradient( transparent 50%, rgba(255, 255, 255, .9) 50% );
	background-image: -o-linear-gradient( transparent 50%, rgba(255, 255, 255, .9) 50% );
	background-image: linear-gradient( transparent 50%, rgba(255, 255, 255, .9) 50% );
	background-size : 1px 32px;
	line-height     : normal !important;
	white-space     : normal !important;
	width           : 100% !important;
	padding-left: 10px !important;
}

Without this edit, there is overlap between the line numbers and the code.