75

prettyfile

pretty_file
SYNOPSIS

pretty_file($file, $language, $startline=0, $endline=0, $number=false)

DESCRIPTION
CODE
  1. require_once 'geshi.php';
  1. function pretty_file($file, $language, $startline=0, $endline=0, $number=false) {
  2.     if (!$file) {
  3.         return false;
  4.     }
  5.  
  6.     $s=read_file($file, $startline, $endline);
  7.  
  8.     if (!$s) {
  9.         return false;
  10.     }
  11.  
  12.     if (!$language) {
  13.         return $s;
  14.     }
  15.  
  16.     $output = false;
  17.  
  18.     switch ($language) {
  19.         case 'plain':
  20.             $s = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $s);
  21.             $s = htmlentities($s, ENT_COMPAT, 'UTF-8');
  22.  
  23.             $output = '<pre class="plain">' . PHP_EOL . $s . '</pre>' . PHP_EOL;
  24.             break;
  25.         default:
  26.             $geshi = new GeSHi($s, $language);
  27.             $geshi->enable_classes(true);
  28.             $geshi->set_header_type(GESHI_HEADER_DIV);
  29.             if ($number) {
  30.                 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
  31.                 $geshi->start_line_numbers_at($startline > 0 ? $startline : 1);
  32.             }
  33.             $geshi->enable_keyword_links(false);
  34.             $geshi->set_tab_width(4);
  35.  
  36. //          echo '<pre>' . PHP_EOL .$geshi->get_stylesheet( ). '</pre>' . PHP_EOL;
  37.  
  38.             $output = $geshi->parse_code();
  39.  
  40.             if ($geshi->error()) {
  41.                 return false;
  42.             }
  43.     }
  44.  
  45.     return $output;
  46. }

The HTML returned by pretty_file doesn't contain any CSS. To properly apply the style to a page, ask to generate the style sheet by activating the line 74 of the code and save it in a file in the folder css/geshi.

EXAMPLE

To add the CSS for the Python language, call pretty_file with as first argument a file hello.py and 'python' as second argument. Copy from the output, before the code in HTML, the CSS for the class .python, and save all the lines in the file css/geshi/python.css at the root of website.

read_file
SYNOPSIS

read_file($file, $startline=0, $endline=0)

DESCRIPTION
CODE
  1. function read_file($file, $startline=0, $endline=0) {
  2.     $lines = @file($file);
  3.  
  4.     if ($lines === false) {
  5.         return false;
  6.     }
  7.  
  8.     if ($startline or $endline) {
  9.         $offset=$startline ? $startline-1 : 0;
  10.  
  11.         if ($endline) {
  12.             $length=$startline ? $endline - $startline + 1 : $endline;
  13.             $lines = array_slice($lines, $offset, $length);
  14.         }
  15.         else {
  16.             $lines = array_slice($lines, $offset);
  17.         }
  18.     }
  19.  
  20.     $s=implode('', $lines);
  21.  
  22.     $s=rtrim($s);
  23.  
  24.     return $s;
  25. }
SEE ALSO

bbcode

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].