cvs: pres2 / objects.php

From: Date: Thu, 21 Nov 2002 20:39:20 +0000
Subject: cvs: pres2 / objects.php
Groups: php.pres 
Request: Send a blank email to pres+get-545@lists.php.net to get a copy of this message
andrei Thu Nov 21 15:39:20 2002 EDT Modified files: /pres2 objects.php Log: Add line numbering capability to examples. Index: pres2/objects.php diff -u pres2/objects.php:1.125 pres2/objects.php:1.126 --- pres2/objects.php:1.125 Sat Nov 2 02:06:11 2002 +++ pres2/objects.php Thu Nov 21 15:39:20 2002 @@ -127,6 +127,18 @@ } // }}} +function add_line_numbers($text) +{ + $lines = preg_split ('!$\n!m', $text); + $lnwidth = strlen(count($lines)); + $format = '%'.$lnwidth."d: %s\n"; + $lined_text = ''; + while (list ($num, $line) = each ($lines)) { + $lined_text .= sprintf($format, $num + 1, $line); + } + return $lined_text; +} + // {{{ strip_markups function strip_markups($str) { $ret = preg_replace('/\*([\S ]+?)\*/','\1',$str); @@ -845,6 +857,7 @@ $this->iheight = '80%'; $this->localhost = false; $this->effect = ''; + $this->linenumbers = false; } function display() { @@ -876,7 +889,15 @@ case 'embed': case 'flash': case 'system': - highlight_file($_html_filename); + if ($this->linenumbers) { + ob_start(); + highlight_file($_html_filename); + $contents = ob_get_contents(); + ob_end_clean(); + echo add_line_numbers($contents); + } else { + highlight_file($_html_filename); + } break; case 'c': $prog = trim(which c2html); @@ -923,7 +944,12 @@ } else { switch($this->type) { case 'php': - highlight_string($this->text); + if ($this->linenumbers) { + $text = add_line_numbers($this->text); + highlight_string($text); + } else { + highlight_string($this->text); + } break; case 'shell': echo '<pre>'.htmlspecialchars($this->text)."</pre>\n";

« previous php.pres (#545) next »