• Resolved alanzucconi

    (@alanzucconi)


    Hi,

    For years I have used Crayon Syntax Highlighter for my WordPress website. I recently switched to Enlighter when I started using Gutenberg for the new posts.

    I would like to use Englither for the previous posts, so I have enabled the compatibility options (Compatibility mode: enabled, the_content: enabled, the_excerpt: enabled, Crayon: enabled, all other options off). This seems to be working fine on all pieces of code, except the inline ones.

    If I disable Crayon, the inline code is visualised as normal text. I looked at the HTML code previously created by Crayon, and I can see that the inline code looks something like this:

    <span class=”lang:c# decode:true crayon-inline”>xxx</span>

    which is not recognised by Enlighter.

    Is there any way to ensure that Enlighter will render the inline code generated by Crayon? I cannot disable Crayon until this is resolved, as I have hundreds of articles with thousands of pieces of inline code that would otherwise render as text!

    Any suggestion would be really appreciated!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Andi Dittrich

    (@andi-dittrich)

    Hi,

    i didn’t know yet that “span” tags were supported: https://github.com/aramk/crayon-syntax-highlighter ?

    At the moment only “pre” tags are recognized by the crayon compat mode: https://github.com/EnlighterJS/Plugin.WordPress/blob/master/modules/compatibility/Crayon.php

    You could simply add your own conversion filter (examples for filter functions are available on GitHub).

    Thread Starter alanzucconi

    (@alanzucconi)

    Thank you Ani, that was such an incredibly fast reply!

    Would you be able to point me towards some resources for the conversion filter you mentioned? I have not done anything like that before, so any advice would be very appreciated!

    Plugin Author Andi Dittrich

    (@andi-dittrich)

    was the “inline” code officially supported by crayon ? do you have any references ?

    related GitHub thread: https://github.com/EnlighterJS/Plugin.WordPress/issues/136
    currently used compat code: https://github.com/EnlighterJS/Plugin.WordPress/blob/master/modules/compatibility/Crayon.php

    Thread Starter alanzucconi

    (@alanzucconi)

    Yes, I can confirm that the “inline” option is officially supported by Crayon.
    You can see in this screenshot here (from the plugin page) that there is an “Inline” checkbox at the very top right.

    It does not add any “pre” or “code” tag, but wraps the selected text into a span with class: ”lang:c# decode:true crayon-inline”.

    This is a page in which I have used the inline function extensively.

    Unfortunately I am not experienced enough with plugin development to point you towards the Crayon file where the inline functionality is actually implemented.

    It would be pretty handy if Enlighter could support this: it appears to be the last unsupported Crayon functionality, and I imagine there must be other people with a similar need.

    If you need any additional information, I would be happy to help!

    Thread Starter alanzucconi

    (@alanzucconi)

    For anyone who is interested, I finally took some time to make a quick WordPress plugin that converts Crayon Syntax Highlighter inline code to Enlighter. This way, I could deactivate Crayon for good!

    If you are unfamiliar with plugin development, just put the following code in a PHP file in wp-content/plugins/crayon-to-enlighter/crayon-to-enlighter.php. It should be automatically recognised as a plugin by WordPress which you can enabled.

    It does not alter your posts, so it can be safely disabled without any permanent change being made. It is far from perfect, but I have not have any issue (and I have hundreds of posts using both Crayon and Enlighter heavily).

    Let me know if you have any suggestion/improvement!

    <?php
       /*
       Plugin Name: Crayon to Enlighter
       Plugin URI: 
       Description: A plugin that converts inline code from Crayon Syntax Highlighter to Enlighter format.
       Version: 1.0
       Author: Alan Zucconi
       Author URI: https://www.alanzucconi.com/
       License: 
       */
    
       // Crayon inline code:
       // <span class="lang:c# decode:true crayon-inline">xxx</span>
       //
       // Enlighter inline equivalent code:
       //
       // <code data-enlighter-language="csharp" class="EnlighterJSRAW">float[] Xs</code>
       add_filter('the_content',
          function($content)
          {
             // crayon filter regex
             $regex = 
                // <span class="lang:c# decode:true crayon-inline">xxx</span>
                '/<span\s+class="lang:([a-z#+]+?)\s+decode:true\s+crayon-inline\s*"\s*>' .
    
                // content of the span tag
                // (+? captures as little as possible)
                '(.+?)' .
    
                // closing tag
                '<\/span>' .
             
                // case insensitive, multiline
                '/im';
    
             // <code data-enlighter-language="csharp" class="EnlighterJSRAW">xxx</code>
             $replacement = '<code data-enlighter-language="${1}" class="EnlighterJSRAW">${2}</code>';
    
             return preg_replace($regex, $replacement, $content);
          },
       1);
    
    ?>
    Plugin Author Andi Dittrich

    (@andi-dittrich)

    Hi @alanzucconi ,

    sry. it didn’t get a notice about your previous post.
    Thanks for the docs, i’ll add inline support to one of the next releases.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Crayon inline code not converted’ is closed to new replies.