WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. harikaram
    Member
    Posted 1 year ago #

    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
    <pre>....</pre>
    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!

    Gratefully,
    Hari Karam Singh
    http://club15cc.com

    http://wordpress.org/extend/plugins/crayon-syntax-highlighter/

  2. akarmenia
    Member
    Plugin Author

    Posted 1 year ago #

    Hi, replace the existing file with this:

    https://dl.dropbox.com/u/33811812/Crayon/minor/crayon-syntax-highlighter-1.9.8-minor-1.zip

    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
    Member
    Posted 1 year 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']);
    //print_r($content);
    $crayon = CrayonWP::instance(array(), get_the_ID());
    $crayon->code($content);
    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
    Member
    Plugin Author

    Posted 1 year 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
    Member
    Posted 1 year 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];
        $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());
    $crayon->code($content);
    echo $crayon->output(false);
  6. harikaram
    Member
    Posted 1 year ago #

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

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

  7. akarmenia
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks, definitely something to consider for the next release.

  8. harikaram
    Member
    Posted 1 year 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
    Member
    Plugin Author

    Posted 1 year 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

About this Topic