How to Fix Dean Lee’s Source Code Highlight Plugin

Ron Fredericks writes: In my previous post, I discuss Dean Lee’s source code syntax highlighting plugin for Wordpress. It delivers all the great features of the GeSHi open-source highlight project for Wordress bloggers.

Yet some source code displayed badly - turning a developer’s nice clean style into a chaotic and messy format. I demonstrated the display problem using a perl sendmail script I plan to discuss in a future blog post. The problem had to do with some kind of whitespace word wrap issue.

I tested various features of CSS and DIV tag attributes created by Dean’s plugin and the GeSHi php code. I am happy to say, I found a simple one-line solution. My solution is in updating Dean’s CSS container class to force white space not to wrap.

Here is my solution:
Add this property to Dean’s “ch_code_container” CSS class:

white-space: nowrap;


Here is the complete update I made to my Wordpress template:
wp-content/themes/eciWidget/style.css

Code (css)
  1.  
  2. /* Add the following CSS styles modified from Dean’s code Highlighter */
  3. /* Embedded Components, Inc. CSS classes update: version 1.0          */
  4. .ch_code_container {
  5. background-color: #f0f0f0;
  6. border: 1px solid #C3CED9;
  7. padding: 0px 0px 0 0;
  8. width: 100%;
  9. text-align: left;
  10. font-size:1.00em;
  11. overflow:auto;
  12. white-space: nowrap;
  13. }
  14. .ch_code_container .head
  15. {
  16. color: #808080;
  17. font-weight: bold;
  18. background-color: #f0f0ff;
  19. border-bottom: 1px solid #d0d0d0;
  20. padding: 2px;
  21. }
  22.  

So now let’s see how this change affects the source code highlight format using my sendmail perl script example. Compared to the image shown in my previous post, the updated highlight plugin CSS style change solved the problem. Now all lines of source code display as one line each, with the horizontal scroll bar used to see the full text:

Code (perl)
  1.  
  2. #!/usr/bin/perl
  3. ##############################################################################
  4. # FormMail                        Version 1.92                               #
  5. # Copyright 1995-2002 Matt Wright mattw@scriptarchive.com                    #
  6. # Created 06/09/95                Last Modified 04/21/02                     #
  7. # Matt’s  Script Archive, Inc.:    http://www.scriptarchive.com/             #
  8. #                                                                            #
  9. ##############################################################################
  10. # SPAM FIX                                                                   #
  11. # Embedded Components, Inc.:      http://www.embeddedcomponents.com          #
  12. # Ron Fredericks                  ronf@embeddedcomponents.com                #
  13. # Created 05/18/07                Last Modified 05/23/07                     #
  14. #                                                                            #
  15. # Logic: if number of Internet links exceed $spamChunksMax,                  #
  16. # then print a default error message instead of sending email.               #
  17. #                                                                            #
  18. # define number of links that can appear in an email                         #
  19. $spamChunksMax = 4;                                                          #
  20. # define web address to include in "too many links error message"            #
  21. $spamChunkErrorLink = "http://www.embeddedcomponents.com";                   #
  22. # define the spam error message                                              #
  23. $spamMessage = "Sorry, no spam today please…";                             #
  24. #                                                                            #
  25. # server-side spam test (search this phrase to find all script edits)        # 
  26. #                                                                            #
  27. ##############################################################################
  28. # COPYRIGHT NOTICE                                                           #
  29. # Copyright 1995-2002 Matthew M. Wright  All Rights Reserved.                #
  30. #                                                                            #
  31. # FormMail may be used and modified free of charge by anyone so long as this #
  32. # copyright notice and the comments above remain intact.  By using this      #
  33. # code you agree to indemnify Matthew M. Wright from any liability that      #
  34. # might arise from its use.                                                  #
  35. #                                                                            #
  36. # Selling the code for this program without prior written consent is         #
  37. # expressly forbidden.  In other words, please ask first before you try and  #
  38. # make money off of my program.                                              #
  39. #                                                                            #
  40. # Obtain permission before redistributing this software over the Internet or #
  41. # in any other medium. In all cases copyright and header must remain intact. #
  42. ##############################################################################
  43. # ACCESS CONTROL FIX: Peter D. Thompson Yezek                                #
  44. #                     http://www.securityfocus.com/archive/1/62033           #
  45. ##############################################################################
  46. # Define Variables                                                           #
  47. #      Detailed Information Found In README File.                            #
  48.  
  49. # $mailprog defines the location of your sendmail program on your unix       #
  50. # system. The flags -i and -t should be passed to sendmail in order to       #
  51. # have it ignore single dots on a line and to read message for recipients    #
  52.  
  53. $mailprog = ‘/usr/lib/sendmail -i -t’;
  54.  
  55. # @referers allows forms to be located only on servers which are defined     #
  56. # in this field.  This security fix from the last version which allowed      #
  57. # anyone on any server to use your FormMail script on their web site.        #
  58.  
  59. @referers = (’scriptarchive.com’,‘embeddedcomponents.com’);
  60.  
  61. # @recipients defines the e-mail addresses or domain names that e-mail can   #
  62. # be sent to.  This must be filled in correctly to prevent SPAM and allow    #
  63. # valid addresses to receive e-mail.  Read the documentation to find out how #
  64. # this variable works!!!  It is EXTREMELY IMPORTANT.                         #
  65. # @recipients = &fill_recipients(@referers);
  66. @recipients = (‘^ronf\@embeddedcomponents\.com’);
  67.  
  68. # ACCESS CONTROL FIX: Peter D. Thompson Yezek                                #
  69. # @valid_ENV allows the sysadmin to define what environment variables can    #
  70. # be reported via the env_report directive.  This was implemented to fix     #
  71. # the problem reported at http://www.securityfocus.com/bid/1187              #
  72.  
  73. @valid_ENV = (‘REMOTE_HOST’,‘REMOTE_ADDR’,‘REMOTE_USER’,‘HTTP_USER_AGENT’);
  74. #@valid_ENV = (’REMOTE_HOST’,'REMOTE_ADDR’,'REMOTE_USER’,'HTTP_USER_AGENT’,'HTTP_REFERER’);
  75.  
  76. # Done                                                                       #
  77. ##############################################################################
  78.  
  79. # Check Referring URL
  80. &check_url;
  81.  
  82. # Retrieve Date
  83. &get_date;
  84.  
  85. # Parse Form Contents
  86. &parse_form;
  87.  
  88. # Check Required Fields
  89. &check_required;
  90.  
  91. # Send E-Mail
  92. &send_mail;
  93.  
  94. # Return HTML Page or Redirect User
  95. &return_html;
  96.  
  97. # NOTE rev1.91: This function is no longer intended to stop abuse, that      #
  98. #    functionality is now embedded in the checks made on @recipients and the #
  99. #    recipient form field.                                                   #
  100.  
  101. sub check_url {
  102.  
  103.     # Localize the check_referer flag which determines if user is valid.     #
  104.     local($check_referer) = 0;
  105.  
  106.     # If a referring URL was specified, for each valid referer, make sure    #
  107.     # that a valid referring URL was passed to FormMail.                     #
  108.  
  109.     if ($ENV{‘HTTP_REFERER’}) {
  110.         foreach $referer (@referers) {
  111.             if ($ENV{‘HTTP_REFERER’} =~ m|https?://([^/]*)$referer|i) {
  112.                 $check_referer = 1;
  113.                 last;
  114.             }
  115.         }
  116.     }
  117.     else {
  118.         $check_referer = 1;
  119.     }
  120.  
  121.     # If the HTTP_REFERER was invalid, send back an error.                   #
  122.     if ($check_referer != 1) { &error(‘bad_referer’) }
  123. }
  124.  
  125. sub get_date {
  126.  
  127.     # Define arrays for the day of the week and month of the year.           #
  128.     @days   = (‘Sunday’,‘Monday’,‘Tuesday’,‘Wednesday’,
  129.                ‘Thursday’,‘Friday’,‘Saturday’);
  130.     @months = (‘January’,‘February’,‘March’,‘April’,‘May’,‘June’,‘July’,
  131.                ‘August’,‘September’,‘October’,‘November’,‘December’);
  132.  
  133.     # Get the current time and format the hour, minutes and seconds.  Add    #
  134.     # 1900 to the year to get the full 4 digit year.                         #
  135.     ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
  136.     $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
  137.     $year += 1900;
  138.  
  139.     # Format the date.                                                       #
  140.     $date = "$days[$wday], $months[$mon] $mday, $year at $time";
  141.  
  142. }
  143.  
  144. sub parse_form {
  145.  
  146.     # part of server-side spam test
  147.     # initialize our antispam counter
  148.     $spamChunkCount = 0;
  149.  
  150.     # Define the configuration associative array.                            #
  151.     %Config = (‘recipient’,,          ’subject’,,
  152.                ‘email’,,              ‘realname’,,
  153.                ‘redirect’,,           ‘bgcolor’,,
  154.                ‘background’,,         ‘link_color’,,
  155.                ‘vlink_color’,,        ‘text_color’,,
  156.                ‘alink_color’,,        ‘title’,,
  157.                ’sort’,,               ‘print_config’,,
  158.                ‘required’,,           ‘env_report’,,
  159.                ‘return_link_title’,‘return_link_url’,,
  160.                ‘print_blank_fields’,, ‘missing_fields_redirect’,);
  161.  
  162.     # Determine the form’s REQUEST_METHOD (GET or POST) and split the form   #
  163.     # fields up into their name-value pairs.  If the REQUEST_METHOD was      #
  164.     # not GET or POST, send an error.                                        #
  165.     if ($ENV{‘REQUEST_METHOD’} eq ‘GET’) {
  166.         # Split the name-value pairs
  167.         @pairs = split(/&/, $ENV{‘QUERY_STRING’});
  168.     }
  169.     elsif ($ENV{‘REQUEST_METHOD’} eq ‘POST’) {
  170.         # Get the input
  171.         read(STDIN, $buffer, $ENV{‘CONTENT_LENGTH’});
  172.  
  173.         # Split the name-value pairs
  174.         @pairs = split(/&/, $buffer);
  175.     }
  176.     else {
  177.         &error(‘request_method’);
  178.     }
  179.  
  180.     # For each name-value pair:                                              #
  181.     foreach $pair (@pairs) {
  182.  
  183.         # Split the pair up into individual variables.                       #
  184.         local($name, $value) = split(/=/, $pair);
  185.  
  186.         # Decode the form encoding on the name and value variables.          #
  187.         # v1.92: remove null bytes                                           #
  188.         $name =~ tr/+/ /;
  189.         $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  190.         $name =~ tr/\0//d;
  191.  
  192.         $value =~ tr/+/ /;
  193.         $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  194.         $value =~ tr/\0//d;
  195.        
  196.         # part of server-side spam test
  197.         # count number of accesses to internet this email message might have…
  198.         $spamChunkCount = $spamChunkCount + &count_chunks($value, "href=");
  199.         $spamChunkCount = $spamChunkCount + &count_chunks($value, "http:");
  200.         $spamChunkCount = $spamChunkCount + &count_chunks($value, "https:");
  201.         $spamChunkCount = $spamChunkCount + &count_chunks($value, "ftp:");
  202.         $spamChunkCount = $spamChunkCount + &count_chunks($value, "mailto:");
  203.  
  204.         # If the field name has been specified in the %Config array, it will #
  205.         # return a 1 for defined($Config{$name}}) and we should associate    #
  206.         # this value with the appropriate configuration variable.  If this   #
  207.         # is not a configuration form field, put it into the associative     #
  208.         # array %Form, appending the value with a ‘, ‘ if there is already a #
  209.         # value present.  We also save the order of the form fields in the   #
  210.         # @Field_Order array so we can use this order for the generic sort.  #
  211.         if (defined($Config{$name})) {
  212.             $Config{$name} = $value;
  213.         }
  214.         else {
  215.             if ($Form{$name} ne ) {
  216.                 $Form{$name} = "$Form{$name}, $value";
  217.             }
  218.             else {
  219.                 push(@Field_Order,$name);
  220.                 $Form{$name} = $value;
  221.             }
  222.         }
  223.     }
  224.  
  225.     # The next six lines remove any extra spaces or new lines from the       #
  226.     # configuration variables, which may have been caused if your editor     #
  227.     # wraps lines after a certain length or if you used spaces between field #
  228.     # names or environment variables.                                        #
  229.     $Config{‘required’} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
  230.     $Config{‘required’} =~ s/(\s+)?\n+(\s+)?//g;
  231.     $Config{‘env_report’} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
  232.     $Config{‘env_report’} =~ s/(\s+)?\n+(\s+)?//g;
  233.     $Config{‘print_config’} =~ s/(\s+|\n)?,(\s+|\n)?/,/g;
  234.     $Config{‘print_config’} =~ s/(\s+)?\n+(\s+)?//g;
  235.  
  236.     # Split the configuration variables into individual field names.         #
  237.     @Required = split(/,/,$Config{‘required’});
  238.     @Env_Report = split(/,/,$Config{‘env_report’});
  239.     @Print_Config = split(/,/,$Config{‘print_config’});
  240.  
  241.     # ACCESS CONTROL FIX: Only allow ENV variables in @valid_ENV in          #
  242.     # @Env_Report for security reasons.                                      #
  243.     foreach $env_item (@Env_Report) {
  244.         foreach $valid_item (@valid_ENV) {
  245.             if ( $env_item eq $valid_item ) { push(@temp_array, $env_item) }
  246.         }
  247.     }
  248.     @Env_Report = @temp_array;
  249. }
  250.  
  251. sub check_required {
  252.  
  253.     # Localize the variables used in this subroutine.                        #
  254.     local($require, @error);
  255.  
  256.     # The following insures that there were no newlines in any fields which  #
  257.     # will be used in the header.                                            #
  258.     if ($Config{’subject’} =~ /(\n|\r)/m || $Config{‘email’} =~ /(\n|\r)/m ||
  259.         $Config{‘realname’} =~ /(\n|\r)/m || $Config{‘recipient’} =~ /(\n|\r)/m) {
  260.         &error(‘invalid_headers’);
  261.     }
  262.  
  263.     if (!$Config{‘recipient’}) {
  264.         if (!defined(%Form)) { &error(‘bad_referer’) }
  265.         else                 { &error(‘no_recipient’) }
  266.     }
  267.     else {
  268.         # This block of code requires that the recipient address end with    #
  269.         # a valid domain or e-mail address as defined in @recipients.        #
  270.         $valid_recipient = 0;
  271.         foreach $send_to (split(/,/,$Config{‘recipient’})) {
  272.             foreach $recipient (@recipients) {
  273.                 if ($send_to =~ /$recipient$/i) {
  274.                     push(@send_to,$send_to); last;
  275.                 }
  276.             }
  277.         }
  278.         if ($#send_to < 0) { &error(’no_recipient’) }
  279.         $Config{‘recipient’} = join(‘,’,@send_to);
  280.     }
  281.  
  282.     # For each require field defined in the form:                            #
  283.     foreach $require (@Required) {
  284.  
  285.         # If the required field is the email field, the syntax of the email  #
  286.         # address if checked to make sure it passes a valid syntax.          #
  287.         if ($require eq ‘email’ && !&check_email($Config{$require})) {
  288.             push(@error,$require);
  289.         }
  290.  
  291.         # Otherwise, if the required field is a configuration field and it   #
  292.         # has no value or has been filled in with a space, send an error.    #
  293.         elsif (defined($Config{$require})) {
  294.             if ($Config{$require} eq ) { push(@error,$require); }
  295.         }