Include Code Listing plugin
Written by kksou   
Monday, 28 April 2008

This plugin allows you to include code listing from external files into your content items, with syntax highlighting and line numbers as shown below:

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
<?php
# sample1.php
echo "now = ".date('F j, Y H:i:s')."\n";

for (
$i=0$i<10; ++$i) {
    print 
"$i: ".$i*$i."\n";
}
?>


Latest Version

v1.0.1 (for Joomla 1.0) and v1.5.1 (for Joomla 1.5)
Released December 10, 2008

  • Support for PHP4
  • Fixed the warning message "Notice: Undefined offset: 0 on line 57"

Key Features

  • Highlight of keywords using PHP's built-in command
  • Display of line numbers
  • Auto-wrap of long lines
  • Set the font, foreground and background color of the line numbers
  • Customize the style and layout of the code listing with CSS!

Available for both Joomla 1.0.x and native 1.5.x!


Download

How to install (Joomla 1.0.x)

  1. Download the plugin and unzip it into a folder.
  2. From the Installers menu, select mambots. In the field "Install directory", enter the folder location (where you unzipped the file) and click Install.
  3. From the Mambots menu, select Site Mambots.
  4. Make sure the plugin "Include Code Listing" is published. If you see a cross in the Published column, just click on it. It will turn into a green tick.
  5. Click on the "Include Code Listing" plugin. On the right-hand side, you will see the Plugin Parameter "Paths to your source codes". This is where you enter the list of allowed paths (for security reason). For example, if you have want to display a php file with the full pathname: c:\sample apps\php\sample1.php. In this box, you will need to enter the path c:\sample apps\php (without the last slash). You may enter multiple paths delimited by commas. And you may use either the forward slash (/) or backward slash (\) for directory path.
  6. You are now ready to use this plugin!
  7. You may want to refer to How to use the "Include Code Listing" plugin on the many tips and techniques of using this plugin.

How to install (Joomla 1.5.x)

  1. Download the plugin and unzip it into a folder.
  2. From the Extensions menu, select Install/Uninstall. In the field "Install directory", enter the folder location (where you unzipped the file) and click Install.
  3. From the Extensions menu, select Plugin Manager.
  4. Enable the plugin (Include Code Listing) - make sure there is a green tick in the Enabled column.
  5. Click on the "Include Code Listing" plugin. On the right-hand side, you will see the Plugin Parameter "Paths to your source codes". This is where you enter the list of allowed paths (for security reason). For example, if you have want to display a php file with the full pathname: c:\sample apps\php\sample1.php. In this box, you will need to enter the path c:\sample apps\php (without the last slash). You may enter multiple paths delimited by commas. And you may use either the forward slash (/) or backward slash (\) for directory path.
  6. You are now ready to use this plugin!
  7. You may want to refer to How to use the "Include Code Listing" plugin on the many tips and techniques of using this plugin.

How to use the "Include Code Listing" plugin

  1. The Simplest Form. Suppose you have a php file with the full pathname: c:\sample apps\php\sample1.php. To display the program in your content item, all you need is to include the following statement:
  2. {include_code_listing c:\sample apps\php\sample1.php}
    

    You'll get this:

     1 
     2 
     3 
     4 
     5 
     6 
     7 
     8 
    <?php
    # sample1.php
    echo "now = ".date('F j, Y H:i:s')."\n";

    for (
    $i=0$i<10; ++$i) {
        print 
    "$i: ".$i*$i."\n";
    }
    ?>

    VVIP: In case you missed it above, please make sure that you add the path of filename (in the above example, it's c:\sample apps\php) to the list of allowed paths in the property page of the plugin. Otherwise the program listing will not be displayed.

  3. Syntax Highlighting for PHP only. I used PHP's built-in highlight_string command. As such, the syntax highlighting only applies to .php files. For all other file types, you will only see plain text without syntax highlighting.
  4. {include_code_listing c:\sample apps\php\sample2.xml}
    

      1 
      2 
      3 
      4 
      5 
      6 
      7 

      8 
      9 
     10 
    <?xml version="1.0" encoding="iso-8859-1"?>
    <mosinstall type="mambot" group="content">
        <name>Include Code Listing</name>
        <author>kksou</author>
        <creationDate>April 2008</creationDate>
        <files>
            <filename mambot="include_code_listing">include_code_listing.php
              </filename>
        </files>
    </mosinstall>

  5. Word Wrap. If you have tried PHP's built-in highlight_string command before, you will know that because it uses
    &​nbsp;, long lines will stay in one whole line. This is undesirable in many cases. The "Include Code Listing" plugin processes any long text and automatically wraps it at the default of column 60.
  6. {include_code_listing c:\sample apps\php\sample3.php}
    

      1 
      2 
      3 
      4 
      5 
      6 
      7 
      8 
      9 

     10 
     11 

     12 
     13 
     14 
     15 
     16 
     17 
     18 
     19 
     20 


     21 
     22 
     23 
     24 
    <?php
    # sample3.php
    # from joomla index.php

    // Set flag that this is a parent file
    define'_VALID_MOS');

    // checks for configuration file, if none found loads installation page
    if (!file_exists'configuration.php' ) || filesize'configuration.php' ) < 10)
      {
        
    $self rtrimdirname$_SERVER['PHP_SELF'] ), '/\\' ) . '/';
        
    header("Location: http://" $_SERVER['HTTP_HOST'] . $self 
          "installation/index.php" 
    );
        exit();
    }

    require( 
    'globals.php' );
    require( 
    'configuration.php' );

    // SSL check - $http_host returns <live site url>:<port number if it is 443>
    $http_host explode(':'$_SERVER['HTTP_HOST'] );
    if( (!empty( 
    $_SERVER['HTTPS'] ) && strtolower$_SERVER['HTTPS'] ) != 'off' || 
      isset( 
    $http_host[1] ) && $http_host[1] == 443) && substr
      $mosConfig_live_site
    0) != 'https://' ) {
        
    $mosConfig_live_site 'https://'.substr$mosConfig_live_site);
    }

    ?>

  7. Specifying the wrap column. The default wrap column is 60. To change this, use the setting wrap_column. Don't forget to put this within square brackets.
  8. {include_code_listing c:\sample apps\php\sample3.1.php [wrap_column=40]}
    

      1 
      2 
      3 
      4 
      5 
      6 
      7 
      8 

      9 


     10 

     11 


     12 
     13 
     14 
     15 
    <?php
    # sample3.1.php
    # from joomla index.php

    // Set flag that this is a parent file
    define'_VALID_MOS');

    // checks for configuration file, if 
      none found loads installation page
    if (!file_exists'configuration.php' 
      || 
    filesize'configuration.php' ) < 
      10
    ) {
        
    $self rtrimdirname$_SERVER[
          'PHP_SELF'
    ] ), '/\\' ) . '/';
        
    header("Location: http://" 
          $_SERVER
    ['HTTP_HOST'] . $self 
          "installation/index.php" 
    );
        exit();
    }

    ?>

  9. Setting the appearance of overall code display. You can use css styles to change the formatting of the overall code display, such as the width, alignment, border, background color, etc. Please make sure that you enclose the styles in single quotes. Within the styles, please use only double quotes if you need to. Don't use any single quote.
  10. {include_code_listing c:\sample apps\php\sample3.1.php [wrap_column=40,
    style='background-color: #E6F1F4; border: #ddd; border-style: solid; 
    border-width: 2px; margin:0 auto; width: 60%']}
    

      1 
      2 
      3 
      4 
      5 
      6 
      7 
      8 

      9 


     10 

     11 


     12 
     13 
     14 
     15 
    <?php
    # sample3.1.php
    # from joomla index.php

    // Set flag that this is a parent file
    define'_VALID_MOS');

    // checks for configuration file, if 
      none found loads installation page
    if (!file_exists'configuration.php' 
      || 
    filesize'configuration.php' ) < 
      10
    ) {
        
    $self rtrimdirname$_SERVER[
          'PHP_SELF'
    ] ), '/\\' ) . '/';
        
    header("Location: http://" 
          $_SERVER
    ['HTTP_HOST'] . $self 
          "installation/index.php" 
    );
        exit();
    }

    ?>

More Tips & Techniques

For more tips and techniques on using this plugin, please refer to Include Code Listing plugin - More Tips and Techniques. These include:

  • Change the font and font size of the line numbers
  • Set the background color for the line numbers
  • Change the font and style of the code display
  • Suppress line numbers
  • Specifying the indent for multi-lines
  • Force wrap lines that exceed the wrap column

Links


Last Updated ( Monday, 28 April 2008 )
 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2015. kksou.com. All Rights Reserved