Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to show code in forum posts?

  • Avatar of Amir Helzer
    Amir Helzer
    Participant

    @icanlocalize

    Here, on bp.org, there’s a message that says:

    `To display code, put it between backticks.`

    On my BP install, it doesn’t exist. Backticks are ignored and I can’t paste in code (HTML tags are filtered and not escaped).

    Where should I look to enable this?

Viewing 11 replies - 1 through 11 (of 11 total)
  • Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    Here’s the code that does it on buddypress.org. It’s basically repurposed from bbPress.

    I had to replace the actual backtick with the phrase `[backtick]` so that it would render properly on this forum. Make sure to correct that in your version.

    `// Borrowed from bbPress
    function bporg_encodeit( $matches ) {
    $text = trim($matches[2]);
    $text = htmlspecialchars($text, ENT_QUOTES);
    $text = str_replace(array(“rn”, “r”), “n”, $text);
    $text = preg_replace(“|nnn+|”, “nn”, $text);
    $text = str_replace(‘&’, ‘&’, $text);
    $text = str_replace(‘<', '<', $text);
    $text = str_replace(‘>’, ‘>’, $text);
    $text = “$text“;
    if ( “[backtick]” != $matches[1] )
    $text = “

    $text

    “;
    return $text;
    }

    // Borrowed from bbPress
    function bporg_decodeit( $matches ) {
    $text = $matches[2];
    $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES));
    $text = strtr($text, $trans_table);
    $text = str_replace(‘
    ‘, ”, $text);
    $text = str_replace(‘

    ‘, ”, $text);
    $text = str_replace(‘

    ‘, ”, $text);
    $text = str_replace(array(‘&’,’&’), ‘&’, $text);
    $text = str_replace(”’, “‘”, $text);
    if ( ‘

    ' == $matches[1] )
    $text = "n$textn";
    return "`$text`";
    }

    // Borrowed from bbPress. Makes code in backticks work, both in forum posts and in activity updates.
    function bporg_code_trick( $text ) {
    $text = str_replace(array("rn", "r"), "n", $text);
    $text = preg_replace_callback("|(`)(.*?)`|", 'bporg_encodeit', $text);
    $text = preg_replace_callback("!(^|n)`(.*?)`!s", 'bporg_encodeit', $text);
    return $text;
    }
    add_filter( 'bp_get_the_topic_post_content', 'bporg_code_trick', 1 );
    add_filter( 'bp_get_activity_content_body', 'bporg_code_trick', 1 );`
    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    Hi @boonebgorges

    i think there is a error in function bporg_decodeit

    `$text = str_replace(”’, “‘”, $text);`

    i also tried this code but it doen’t work for me. I added it in bp-forums-filter.php. Probably the wrong place.

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    No solution ?

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    It looks like some of the escaped characters got stripped. Here’s what it’s supposed to look like: http://pastebin.com/Mvyc8TYx

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    Sorry, it’s not ok !
    view here, i tried to insert with backtick and [code, but pre tags are missing in the example….

    http://bp-fr.net/groups/groupe-de-tests/forum/topic/code-test-code/

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    The code in the pastebin is exactly what’s running on bp.org, so it definitely works. If there’s a problem, it might be a theme-related conflict.

    Make sure that you’re only using backticks, not [ code ]

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    i give a try here, using backticks
    `
    function bporg_decodeit( $matches ) {
    $text = $matches[2];
    $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES));
    $text = strtr($text, $trans_table);
    $text = str_replace(‘
    ‘, ”, $text);
    $text = str_replace(‘

    ‘, ”, $text);
    $text = str_replace(‘

    ‘, ”, $text);
    $text = str_replace(array(‘&’,’&’), ‘&’, $text);
    $text = str_replace(”’, “‘”, $text);
    if ( ‘

    ' == $matches[1] )
    $text = "n$textn";
    return "`$text`";
    }
    `
    on my forum it is showing this
    http://bp-fr.net/groups/groupe-de-tests/forum/topic/another-test/
    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    I’m using a standard BP theme as child theme , and only for translation purpose.
    As you can compare with what is on pastebin, some code is wrong, also here on bp.org

    the return contains $text between backstick, but they are converted to < code >

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    it seems that i have a character conflict with backstick and UTF8/Mysql input/output
    & # 96; ( the html entity) is readed as a “real” backtick in the browser,
    but injecting a “real” backtick like a php code copy/paste action, is not rendered correctly.

    http://www.cs.sfu.ca/~ggbaker/reference/characters/

    Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    Yeah, it’s not totally reliable with all different kinds of characters. I took the code directly from bbPress without any real modification. One of these days I will take a closer look to see if I can improve the way it escapes special characters.

    Avatar of danbpfr
    danbpfr
    Participant

    @chouf1

    I got it at least…

    What i need is the possibility to copy/paste some code in my forum posts.
    The code on the pastebin is globally correct, but as boone says, “it’s not totally reliable”.

    The first thing, i you want to use it, is to copy paste the given code into your child theme function.php file.
    Then you add a filter to deactivate the (damned) default typographical double cote wp filter

    remove_filter( ‘bp_get_the_topic_post_content’, ‘wptexturize’ );

    here the code you find on pastebin

    in the function bporg_decodeit, retrieve <code> in the if statement
    `if ( ‘

    ' == $matches[1] )`
    Should be
    `if ( '
    ' == $matches[1] )`

    In the function bporg_code_trick, there are 4 backticks, we have to change this a little

    In the first callback, replace the FIRST backstick by & # 96 ; (without space)
    In the second callback, replace the SECOND backstick by & # 96 ; (without space)

    That's it ! Now you can copy most of php code with backstick and the right double cotes.
    view the result here:
    http://bp-fr.net/groups/groupe-de-tests/forum/topic/php-complexe-test/

Viewing 11 replies - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.