Support » Plugins and Hacks » [Plugin: Crayon Syntax Highlighter] HOW-TO: Manually extract unformatting code?

[Plugin: Crayon Syntax Highlighter] HOW-TO: Manually extract unformatting code?

  • Hi,

    I’m trying to render a list view in which I need at most the first couple lines of a code block, probably unformatted. Is there a way with the Crayon API I can manually extract and process the content?

    get_the_content() returns a [crayon-124] id tag which I gather is being inserted via a hook somewhere as the database has the actual
    content. It’s the later I need, or even better, a way of limiting the output to a couple lines and some ellipses – something like this:

    $unformatted = get_the_unprocessed_code_content()
    $tmp = split('\n', $unformatted);
    $unformatted_shortened = implode('\n', array( $tmp[0], @$tmp[1] )) + '...';
    $formatted_shortened = CrayonWP::process($unformatted_shortened);

    Barring that, I’d be happy just to have the raw content of the post…

    Many thanks. This is stellar plugin by the way. I’ve been through a few of them too!

    Hari Karam Singh


Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author akarmenia


    Hi, replace the existing file with this:


    Then use this: var_dump(CrayonWP::post_captures());

    It returns an array of post IDs, each containing another array with the key of Crayon UID string, which contains all the info captured.

    Cool thank you. Just for clarity, I tried this in my template, mimicking CrayonWP->the_posts():

    // global $post;
    $captures = CrayonWP::capture_crayons(get_the_ID(), get_the_content());
    $content = array_pop($captures['capture']);
    $crayon = CrayonWP::instance(array(), get_the_ID());
    echo CrayonFormatter::plain_code($crayon->code());

    …but it still just gives the shortcode. When/where does Crayon convert the DB content to a shortcode?

    Plugin Author akarmenia


    You shouldn’t need to use the internals – post_captures will return the internal $post_queue array. This contains the original code you’re looking for.

    Ok, this is how I’ve accomplished it in my template file:

    // Grab the raw code and limit to N lines
    $NUM_LINES = 3;
    $tmp = CrayonWP::post_captures();
    $c = array_pop($tmp[get_the_ID()]);
    $lines = preg_split('/[\r\n]+/', $c['code']);
    $content = ''; $i = 0;
    // Combine the first $NUM_LINES lines
    while ($i < count($lines) && $i < $NUM_LINES) {
        if ($i > 0) $content .= "\n";       // combine with newline
        $content .= $lines[$i];
    if (count($lines) > $NUM_LINES) $content .= "\n...";        // add ellipse if not complete
    // Format the code and render
    $crayon = CrayonWP::instance($c['atts'], get_the_ID());
    echo $crayon->output(false);

    Feature request:
    A nice public API for outputting code on manually.

    Just want to reiterate, this is a great plugin 🙂 Thanks.

    Plugin Author akarmenia


    Thanks, definitely something to consider for the next release.

    I’m getting this on my single pages now:

    Fatal error: Undefined class constant ‘EXCERPT_STRIP’ in D:\wwwserver\club15cc.com\wp-content\plugins\crayon-syntax-highlighter\crayon_wp.class.php

    Do I need the update of crayon_settings.class.php too?

    Plugin Author akarmenia


    No, but you must have “require_once (‘crayon_settings_wp.class.php’);”

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Plugin: Crayon Syntax Highlighter] HOW-TO: Manually extract unformatting code?’ is closed to new replies.