GeSHI Snippet for Pivot
This is one of the snippets that that I designed for Pivot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php /* * snippet_geshi.php: inserts formatted code into the Pivot blog entries * Copyright (C) 2007 Andrew Wells <contact@pr0gr4mm3r.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Assuming a php code file called 'code.txt' in the DOC_ROOT/pivot/code directory, call like this: * [[geshi:code.txt:php]] */ function snippet_geshi($code_loc, $code_lang) { global $Paths; $code = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/pivot/code/" . $code_loc); $code = stripslashes($code); $code_lines = count(file($_SERVER['DOCUMENT_ROOT'] . "/pivot/code/" . $code_loc)); if ($code_lines < 11) { $height = ($code_lines * 15) + 40; } else { $height = 300; } // Create a GeSHi object $geshi =& new GeSHi($code, $code_lang); $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); $parsed = $geshi->parse_code(); $download_info = '<div style="color: #7F7F7F; font-size: 8pt; margin: 0 auto; margin-bottom: 15px; overflow: auto; width: 90%; "> The above code is not formatted for copying. <a href="' . PATH_WEB_HOME . 'code_download.php?loc=' . $code_loc . '"> Click here</a> for the code in plain text.</div>'; $start = '<div style="background-color: #F8F6F6; border-color: #000000; border-style: solid; border-width: 1px; height: ' . $height . 'px; margin: 0 auto; margin-bottom: 2px; margin-top: 20px; overflow: auto; width: 90%;">'; $end = '</div>'; $finished_code = $start . "n" . $parsed . "n" . $end . $download_info; #echo $finished_code; return $finished_code; } ?> |
This snippet reads a code file and displays in an entry using the GeSHi script. The above code is what you need to show code in your blog entries. There are, however, a few changes that will probably need to be made.
- See lines 28 & 30. If your code folder is in a different location, please change these paths. (You will have to create the code folder)
- See lines 46-48 & 52. If you do not decide to include a link to download the code (see second hunk of code in this entry), you will need to remove lines 46-48 and edit line 52 accordingly.
- Change “PATH_WEB_HOME” on line 47 to your site’s home path (for example “http://www.yoursite.com/”)
- Please note that the GeSHi object is declared on line 42, but the GeSHi class file is never included. That’s because I use PHP’s autoload() function. You are responsible for installing the GeSHi script and including it where necessary.
That should be it! See the code below to allow users to download the code for easier copying.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | <?php /* * code_download.php: code_download.php is designed to work with the GeSHI snippet for Pivot * Copyright (C) 2007 Andrew Wells <contact@pr0gr4mm3r.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ $filename = urldecode($_GET['loc']); /* this should only be a filename, take out any directory references */ $filename = str_replace('..', '', $filename); $filename = str_replace('/', '', $filename); header('Content-type: application/txt'); header('Content-Disposition: attachment; filename="' . $filename . '"'); if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/pivot/code/" . $filename)) { $dump = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/pivot/code/" . $filename); echo stripslashes($dump); } else { echo "File not found."; } ?> |
The only thing you may have to edit here is the path to the code files if you changed them from the default. Find all instances of ‘$_SERVER['DOCUMENT_ROOT'] . “/pivot/code/”‘ and edit accordingly.
This entry was posted on Friday, March 9th, 2007 at 3:50 pm and is filed under Internet, PHP. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.