Crayon Syntax Highlighter
HOW-TO: Manually extract unformatting code? (9 posts)

  1. harikaram
    Posted 3 years ago #


    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


  2. akarmenia
    Plugin Author

    Posted 3 years ago #

    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.

  3. harikaram
    Posted 3 years ago #

    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?

  4. akarmenia
    Plugin Author

    Posted 3 years ago #

    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.

  5. harikaram
    Posted 3 years ago #

    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);
  6. harikaram
    Posted 3 years ago #

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

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

  7. akarmenia
    Plugin Author

    Posted 3 years ago #

    Thanks, definitely something to consider for the next release.

  8. harikaram
    Posted 3 years ago #

    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?

  9. akarmenia
    Plugin Author

    Posted 3 years ago #

    No, but you must have "require_once ('crayon_settings_wp.class.php');"

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Crayon Syntax Highlighter
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic