rss

Self Picture

Natural Language Processing, Information Extraction and Search consultant.

 learn more   get in touch 

Logo - I Build Search
Mar 28
2009

Hacking wp-syntax plugin to show header digg

I was recently asked how I got the wp-syntax plugin to show a header like so:

test.cpp
1
2
3
int main() {
	return 0;
}

To show the test.cpp file name, I modified the wp-syntax.php file (present in /wp-content/plugins/wp-syntax/) like so:

Changed the regular expression in the wp_syntax_before_filter function from:

wp-syntax.php
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

to

wp-syntax.php
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|header=[\"']([\w-\. ]*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

And the wp_syntax_highlight function to:

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
function wp_syntax_highlight($match)
{
    global $wp_syntax_matches;
 
    $i = intval($match[1]);
    $match = $wp_syntax_matches[$i];
 
    $language = strtolower(trim($match[1]));
    $line = trim($match[2]);
    $escaped = trim($match[3]);
    $header = trim($match[4]);
    $code = wp_syntax_code_trim($match[5]);
    if ($escaped == "true") $code = htmlspecialchars_decode($code);
 
    $geshi = new GeSHi($code, $language);
    $geshi->enable_keyword_links(false);
    do_action_ref_array('wp_syntax_init_geshi', array(&$geshi));
 
    $output = "\n<div class=\"wp_syntax\">";
 
    if($header) {
        $output .= "<div class=\"wp_syn_hdr\">" . $header . "</div>";
    }

Node the addition of lines 104 and 114-116

All you have to do is add another attribute header="header-text" in your pre tag. ex. <pre lang="php" line="1" header="wp-syntax.php">

3 Responses (rss) (trackback)

  • vikram says:

    i had heard…and now i see

  • namran says:

    I think you forgot to mention ..
    additional css component to be added as below :

    /* — Code Highlighting –*/
    .wp_syntax {
    color: #100;
    background-color: #f9f9f9;
    border: 1px solid silver;
    margin: 0 0 1.5em 0;
    overflow: auto;
    }

    /* IE FIX */
    .wp_syntax {
    overflow-x: auto;
    overflow-y: hidden;
    padding-bottom: expression(this.scrollWidth > this.offsetWidth ? 15 : 0);
    _width:540px;
    }

    .wp_syntax table {
    border-collapse: collapse;
    }

    .wp_syntax .wp_syn_hdr {
    background-color:#def;
    color:#666;
    text-align:center;
    border-bottom:1px dotted silver;
    }

    .wp_syntax div, .wp_syntax td {
    vertical-align: top;
    padding: 2px 4px;
    }

    .wp_syntax .line_numbers {
    text-align: right;
    background-color: #def;
    color: gray;
    overflow: visible;
    }

    /* potential overrides for other styles */
    .wp_syntax pre {
    margin: 0;
    width: auto;
    float: none;
    clear: none;
    overflow: visible;
    }

    .right-me {
    text-align:right;
    }

    Else it will not spit the same as above also..

    Thanks anyway..

    p/s : I also copied from yours.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

This page and its contents are copyright © 2010, Pravin Paratey.