From: <za...@us...> - 2003-06-30 02:44:40
|
Update of /cvsroot/tikiwiki/tiki/lib/wiki-plugins In directory sc8-pr-cvs1:/tmp/cvs-serv8282/wiki-plugins Modified Files: wikiplugin_code.php Log Message: Optional line numbering added, background parameter removed (CSS should be used insted) Index: wikiplugin_code.php =================================================================== RCS file: /cvsroot/tikiwiki/tiki/lib/wiki-plugins/wikiplugin_code.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- wikiplugin_code.php 29 Jun 2003 17:38:39 -0000 1.4 +++ wikiplugin_code.php 30 Jun 2003 02:44:35 -0000 1.5 @@ -1,34 +1,45 @@ <?php - +// $Header$ // Displays a snippet of code -// Parameters: bgcolor (optional background color) +// Parameters: ln => line numbering (default false) // Example: // {CODE()} -// print("foo"); +// print("foo"); // {CODE} -function wikiplugin_code_help() { - return 'Displays a snippet of code'; +function wikiplugin_code_help() +{ + return "Displays a snippet of code.\nSet optional paramater -+ln+- to 1 if you need line numbering feature."; } -function wikiplugin_code($data,$params) { - global $tikilib; - global $style; - +function wikiplugin_code($data,$params) +{ extract($params); - $code=htmlspecialchars(trim($data)); - if(!isset($bgcolor)) {$bgcolor='#EEEEEE';} - //If you want line numbering use something like this: - //$lines = explode("\n",$code); - //print_r($lines); - - if (strstr($style, "zaufi")) - $codestyle="class='codelisting'"; - else - $codestyle="style='border: 1px solid #CCCCCC;margin: 4px;padding-left: 10px;background-color:$bgcolor;'"; + if(isset($ln)&&$ln==1) { + $lines = explode("\n",$data); + $i=1; // current line number + $code=''; + // Will skip leading and trailing empty lines + // to make snippet look better :) + $fl=0; // 'first code line printed' flag + $ae=''; // + foreach($lines as $line) { + $len=strlen($line); + if(!($len||$fl)) continue; // skip leading empty lines + if($len) { + // OK len >0 + $code.=$ae.($fl?"\n":'').sprintf("%3d",$i).': '.$line; + $fl=1; // first line already printed + $ae=''; + } else $ae.="\n".sprintf("%3d",$i).': '.$line; + $i++; + } + $code=rtrim($code); + } else $code=$data; + // Wrap listing into div - $data = "<div $codestyle><pre>".$code."</pre></div>"; + $data = "<div class='codelisting'><pre>".$code."</pre></div>"; return $data; } |