Geshi Syntax Highlighting Helper for CakePHP
Geshi is a very powerful highlighting tools. I searched the geshi helper for this CakePHP site, and found it on Mark Story blog. Thanks to Mark that have written the useful helper.
Later I found that I have to tweak Mark's helper because there are some problem with my environment. I want to add the helper ability to auto generate these things:
- Built-in geshi CSS for the highlighting text
- JavaScript for code/highlight switching
Also, I need to strip all "<br/>" to newline (\n) inside highlighting code that generated by my text editor (tinyMCE).
Here is my modification on file: app/views/helper/geshi.php
I added 3 private functions to get CSS, javascript and convert <br/> to new line
Show Plain Text
- private function getCSS() {
- if(!$this->_geshi) return;
- $view =& ClassRegistry::getObject('view');
- $view->addScript('<style type="text/css">'.$this->_geshi->get_stylesheet().'</style>');
- }
- private function getScript() {
- if(!$this->_geshi) return;
- $view =& ClassRegistry::getObject('view');
- $script = "";
- $view->addScript($script);
- }
assign var $script above with this string, don't forget to add mootools library on your page header.
Show Plain Text
- <script type="text/javascript">
- window.addEvent('domready', function() {
- var codeBlocks = $$('div.code');
- codeBlocks.each(function(block){
- var switchButton = block.getPrevious()
- var blockText = block.get('text');
- var plainTextEl = new Element('pre', {
- 'text' : blockText,
- 'class' : 'plain-text'
- });
- var htmlText = block.get('html');
- block.store('state', 'code');
- switchButton.addEvent('click', function(e) {
- e.stop();
- var state = block.retrieve('state');
- var _blockText = plainTextEl;
- var _htmlText = htmlText;
- if (state == 'code') {
- this.set('text', 'Show Highlighted Code');
- block.store('state', 'text');
- block.empty().adopt(_blockText);
- } else {
- this.set('text', 'Show Plain Text');
- block.set('html', _htmlText);
- block.store('state', 'code');
- }
- });
- }, this);
- });
- </script>
The last function is to replace all '<br/>' with '\n'
Show Plain Text
- private function convertBR($str) {
- return $out;
- }
OK, we now ready to add those new functions into the main functions, goto function highlight() and insert this line just before return
Show Plain Text- $this->getScript(); // ------ add this line
- return $this->output( $html );
Then goto function _processCodeBlock() add these 2 lines
Show Plain Text
- $code = $this->convertBR($code); //------------add this line
- if ((bool)$lang) {
- //get instance or use stored instance
- if ($this->_geshi == null) {
- $geshi = new GeSHI($code, $lang);
- $this->_geshi = $geshi;
- $this->__configureInstance($this->_geshi);
- } else {
- $this->_geshi->set_language($lang);
- }
- $this->getCSS(); //--------- add this line
- $highlighted = $this->_geshi->parse_code();
- return $openTag . $highlighted . $closeTag;
- }
That's all, here is the completed zip file to download, helper file only, please refer to Mark website for the completed instruction to use the helper.