Links on this page…

Plugins! feed.feed icon

TXP Plugins! is powered by Textpattern and most happily hosted by DreamHost.

sed_display_code.

A plugin to format and display a text file—specifically a file of code—in a convenient manner.

There are other plugins out there that do this too, but this was my first shot an a TXP plugin—a project on which to cut my php teeth—and I wanted a code listing with cleaner layout than glx_code and a lot leaner output than GeSHi. This is the result.

All of the listings on this page are generated with this plugin.

Examples

The following listings demonstrate the features of this plugin.

Listing 1: snip.php
#Code
0001function sed_count_initial_tabs ( $string ) {
0002$out_tabs = 0; // output variable...
0003 
0004// private locals...
0005$pv_len = strlen($string);
0006$pv_pos = 0;
0007$pv_continue = 1;
0008 
0009while( (1==$pv_continue) && ($pv_pos<$pv_len) ) {

Download this code
[Copies Grabbed: 962]

Tag Used To Generate Listing 1.

<txp:sed_display_code file="snip.php" list_no="1" show_count='Copies Grabbed: '/>

Listing 1 Notes

  1. No Syntax highlighting.
    That’s right, no syntax highlighting. The bulk of Dunstan’s original code to do this was to recognise and mark up comment blocks. To correctly handle comments from a variety of possible source languages requires quite a lot of parsing. In the end I decided that I could simply do without comment highlighting—in fact, I could do without any form of syntax highlighting. Doing without it really simplifies the code. If you simply must have syntax highlighting then go grab the GeSHi plugin.
  2. Integration with TXP File Management
    By default, v0.5 will use TXP’s files directory for storing your files and the download link can show how many times a file has been grabbed.
  3. Customisable download link.
    If you want a download link—you can have it! You can then change the text and tooltip displayed. If you don’t want a link—you can surpress it.
    Easy!
  4. Alternate row styling.
    The styles for odd and even rows can be defined in your css file.
Listing 2: snip.php
#Code
0001function sed_count_initial_tabs ( $string ) {
0002$out_tabs = 0; // output variable...
0003 
0004// private locals...
0005$pv_len = strlen($string);
0006$pv_pos = 0;
0007$pv_continue = 1;
0008 
0009while( (1==$pv_continue) && ($pv_pos<$pv_len) ) {

Tag Used To Generate Listing 2.

<txp:sed_display_code file="snip.php" link_text='' highlight1="5,6,7" list_no="2" />

Listing 2 Notes

  1. Download link surpressed.
    This listing uses link_text='' to prevent the download link from being output.
  2. Line highlighting.
    You can define up to three sets of line numbers to be highlighted in your listing. Useful if you want to draw attention to certain aspects of your code. Each highlight set is customisable via your CSS file.
  3. Robust tabulation at the start of lines.
    Correctly displays lines from the source file that have tabs embedded in the middle of them. Check out the highlighted lines and also the source (from listing 1) to see the cleanup.
  4. Preserves blank lines.
    Any line of the source file that is blank produces a corresponding blank line in the screen output.
Listing 3: sed_display_code-0.2.php
#Code
0251function sed_generate_row ( $wrap, $line_no, $line, $row_class, $line_cell_class ) {
0252$pv_space = '&#160;';
0253 
0254$pv_line_no_cell = doTag( $line_no, $wrap, '' );
0255$pv_line_cell = doTag( $line, $wrap, $line_cell_class );
0256 
0257return doTag( $pv_line_no_cell.$pv_line_cell, 'tr', $row_class )."\n";
0258}

Tag Used To Generate Listing 3.

<txp:sed_display_code dir='code' file="sed_display_code-0.2.php" start="251" end="258" link_text='' highlight3="251,257" list_no="3" />

Listing 3 Notes

  1. Specific Directory
    You can re-direct the plugin to get your code from a specific directory if you prefer not to use TXP file management.
  2. Customisable start & end lines.
    You can list just a portion of a file by defining the start line, end line or both.
  3. Tabulation on wrapped lines.
    Check out lines 251 and 257 in the listing. Unlike code listers that use ordered lists as the basis for the output, this plugin also indents the wrapped portion of lines.

files/doesnt-exist.php.txt could not be located!

Tag Used To Generate Listing 4.

<txp:sed_display_code file="doesnt-exist.php" list_no="4" err_text="could not be located!"/>

Listing 4 Notes

  1. Error Message Styling
    You can specify custom error messages and an error style for CSS markup if a file cannot be found.

Downloads

The latest version is available here—as are any prior versions.

Manual

For detailed instructions and example tags, please refer to the manual.