function AdvancedCodeTag($source, $settings) { global $languages, $languagesPath, $codeTag; $language = array_shift($settings); // [arg1] // [arg1] if ($language == '') { $language = 'text'; } if ($language == "list") { // list all languages supported return "<br>List of supported languages for <b>Geshi " . GESHI_VERSION . "</b>:<br>" . implode("<br>", $languages); } if ($language != "" && !in_array($language, $languages)) { // list languages if invalid argument return "<br>Invalid language argument, \"<b>" . $language . "</b>\", select one from the list:<br>" . implode("<br>", $languages); } // set geshi $geshi = new GeSHi(trim($source), $language, $languagesPath); $geshi->enable_strict_mode($codeTag["advanced"]["strict"]); // [arg2 or more] if (in_array('n', $settings)) { // display line numbers $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); } /* Add more GeSHi features from [ http://qbnz.com/highlighter/geshi-doc.html ] template: if( in_array( '<PARAMETER NAME>', $settings ) ) { $geshi-><GESHI FUNCTION CALL> } */ // removes newlines replaces with <br /> return str_replace("\n", '<br />', $geshi->parse_code()); }
public static function _code($action, $attributes, $content, $params, &$node_object) { if ($action == 'validate') { if (isset($attributes['default'])) { $highlighter = new GeSHi($content, $attributes['default']); if ($highlighter->error() === false) { $highlighter->enable_strict_mode(GESHI_MAYBE); $highlighter->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); $node_object->highlighter =& $highlighter; return true; } return false; } return true; } else { $content = trim($content, "\r\n"); if (isset($attributes['default'])) { $content = $node_object->highlighter->parse_code(); } else { $content = htmlspecialchars($content); $content = '<pre><ol><li>' . preg_replace("/\\s*\n/", '</li><li>', $content) . '</li></ol></div>'; } /** * STATISCHE SPRACHE */ return '<div class="code"><h4>' . 'Code-Ansicht' . ':</h4>' . $content . '</div>'; } }
protected function highlightSourceFile($filename) { if ($this->geshipath) { require_once $this->geshipath . '/geshi.php'; $source = file_get_contents($filename); $geshi = new GeSHi($source, 'php', $this->geshilanguagespath); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->enable_strict_mode(true); $geshi->enable_classes(true); $geshi->set_url_for_keyword_group(3, ''); $html = $geshi->parse_code(); $lines = split("<li>|</li>", $html); // skip first and last line array_pop($lines); array_shift($lines); $lines = array_filter($lines); $lines = array_map(array($this, 'stripDiv'), $lines); return $lines; } else { $lines = file($filename); for ($i = 0; $i < count($lines); $i++) { $line = $lines[$i]; $line = rtrim($line); if (function_exists('mb_convert_encoding')) { $lines[$i] = mb_convert_encoding($line, 'UTF-8'); } else { $lines[$i] = utf8_encode($line); } } return $lines; } }
/** * Highlights the content parts marked as source code using the GeSHi * class. * @author Björn Detert <*****@*****.**> * @version 20. 9. 2006 * @param string $content The text to be parsed * @param object $parent The calling object (regulary of type tx_mmforum_pi1), so this * object inherits all configuration and language options from the * calling object. * @param array $conf The calling plugin's configuration vars * @return string The parsed string */ function syntaxhighlighting($content, $parent, $conf) { /* Path to Geshi Syntax-Highlighting files. */ $path = GeneralUtility::getFileAbsFileName('EXT:mm_forum/res/geshi/geshi/', $onlyRelative = 1, $relToTYPO3_mainDir = 0); $conf['postparser.']['tsrefUrl'] ? define('GESHI_TS_REF', $conf['postparser.']['tsrefUrl']) : define('GESHI_TS_REF', 'www.typo3.net'); $res = $this->databaseHandle->exec_SELECTquery('lang_title,lang_pattern,lang_code', 'tx_mmforum_syntaxhl', 'deleted=0'); while ($data = $this->databaseHandle->sql_fetch_assoc($res)) { preg_match_all($data['lang_pattern'], $content, $source_arr); while (list($key, $value) = each($source_arr[1])) { $value = trim($this->decode_entities($value)); if ($data['lang_title'] == 'php') { if (!preg_match("/<\\?/", trim(substr($value, 0, 6)))) { $value = "<?\n" . $value . "\n?>"; } } $geshi = new GeSHi($value, $data['lang_code'], $path); $geshi->set_header_type(GESHI_HEADER_PRE); #$geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_line_style('background: ' . $conf['postparser.']['sh_linestyle_bg'] . ';', 'background: ' . $conf['postparser.']['sh_linestyle_bg2'] . ';', true); $geshi->set_overall_style('margin:0px;', true); $geshi->enable_classes(); $style = '<style type="text/css"><!--'; $style .= $geshi->get_stylesheet(); $style .= '--></style>'; $geshi->enable_strict_mode('FALSE'); $replace = $geshi->parse_code(); $time = $geshi->get_time(); $CodeHead = '<div class="tx-mmforum-pi1-codeheader">' . strtoupper($data['lang_title']) . '</div>'; // $code_header , check this out?? I get confused ^^ $replace = '###DONT_PARSE_AGAIN_START###' . $CodeHead . '<div class="tx-mmforum-pi1-codeblock">' . $style . $replace . '</div>###DONT_PARSE_AGAIN_ENDE###'; $content = str_replace($source_arr[0][$key], $replace, $content); } } return $content; }
function wp_codebox_highlight_geshi ($match) { global $codeid, $post; $codeid ++; //get option from DB $cb_plain_txt = get_option("cb_plain_txt"); $cb_line = get_option("cb_line"); $cb_colla = get_option("cb_colla"); $cb_wrap_over = get_option("cb_wrap_over"); $cb_highlight = get_option("cb_highlight"); $cb_strict = get_option("cb_strict"); $cb_caps = get_option("cb_caps"); $cb_tab_width = intval(get_option("cb_tab_width")); $cb_keywords_link = get_option("cb_keywords_link"); if ($match[1]) { $language = strtolower(trim($match[1])); } else { $language = "text"; } $line = trim($match[4]); $file = trim($match[2]); $colla = trim($match[3]); $code = wp_codebox_code_trim($match[5]); $is_windowsie = wp_codebox_is_windowsie(); $geshi = new GeSHi($code, $language); $geshi->enable_keyword_links($cb_keywords_link); $geshi->set_case_keywords($cb_caps); $geshi->set_tab_width($cb_tab_width); $geshi->enable_strict_mode($cb_strict); do_action_ref_array('wp_codebox_init_geshi', array(&$geshi)); $output = "\n"; if (! ($cb_plain_txt)) { $output .= "<div class=\"wp_codebox_msgheader"; if (($cb_colla && (! ($colla == "+"))) || ($colla == "-")) { $output .= " wp_codebox_hide"; } $output .= "\">"; $output .= "<span class=\"right\">"; $output .= "<sup><a href=\"http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples\" target=\"_blank\" title=\"WP-CodeBox HowTo?\"><span style=\"color: #99cc00\">?</span></a></sup>"; if ($is_windowsie) { $output .= "<a href=\"javascript:;\" onclick=\"copycode('p" . $post->ID . "code" . $codeid . "');\">" . __('[Copy to clipboard]', 'wp-codebox') . "</a>"; } /* $output .= "<a href=\"javascript:;\" onclick=\"toggle_collapse('p".$post->ID.$codeid."');\">[<span id=\"p".$post->ID.$codeid."_symbol\">"; if (($cb_colla && (!($colla == "+"))) || ($colla == "-")){$output .= "+";} else {$output.= "-";} $output .= "</span>]</a>"; */ $output .= "</span>"; if ($file) { $output .= "<span class=\"left2\">" . __('Download', 'wp-codebox') . ' <a href="' . get_bloginfo('wpurl') . '/wp-content/plugins/wp-codebox/wp-codebox.php?p=' . $post->ID . '&download=' . wp_specialchars($file) . '">' . wp_specialchars($file) . '</a>'; } else { $output .= "<span class=\"left\">" . "<a href=\"javascript:;\" onclick=\"javascript:showCodeTxt('p" . $post->ID . "code" . $codeid . "'); return false;\">" . __('View Code', 'wp-codebox') . "</a> " . strtoupper($language); } $output .= "</span><div class=\"codebox_clear\"></div></div>"; } $output .= "<div class=\"wp_codebox\">"; $output .= "<table>"; $output .= "<tr "; $output .= "id=\"p" . $post->ID . $codeid . "\">"; if ($cb_line && (! ($line))) $line = "1"; if (($line) && (! ($line == "n"))) { $output .= "<td class=\"line_numbers\">"; $output .= wp_codebox_line_numbers($code, $line); $output .= "</td>"; } $output .= "<td class=\"code\" id=\"p" . $post->ID . "code" . $codeid . "\">"; $output .= $geshi->parse_code(); $output .= "</td></tr></table></div>\n"; return $output; }
/** * Loads data for this template */ protected function LoadData() { $head = $this->GetProject()->GetHeadCommit(); $this->tpl->assign('head', $head); $commit = $this->GetProject()->GetCommit($this->params['hashbase']); $this->tpl->assign('commit', $commit); if (!isset($this->params['hash']) && isset($this->params['file'])) { $this->params['hash'] = $commit->GetTree()->PathToHash($this->params['file']); if (empty($this->params['hash'])) { throw new GitPHP_FileNotFoundException($this->params['file']); } } $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']); if ($this->params['file']) { $blob->SetPath($this->params['file']); } $blob->SetCommit($commit); $this->tpl->assign('blob', $blob); $blame = new GitPHP_FileBlame($this->GetProject(), $commit, $this->params['file'], $this->exe); $this->tpl->assign('blame', $blame->GetBlame()); if (isset($this->params['output']) && $this->params['output'] == 'js') { return; } $this->tpl->assign('tree', $commit->GetTree()); if ($this->config->GetValue('geshi')) { include_once GITPHP_GESHIDIR . "geshi.php"; if (class_exists('GeSHi')) { $geshi = new GeSHi("", 'php'); if ($geshi) { $lang = GitPHP_Util::GeshiFilenameToLanguage($blob->GetName()); if (empty($lang)) { $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(), '.'), 1)); } if (!empty($lang)) { $geshi->enable_classes(); $geshi->enable_strict_mode(GESHI_MAYBE); $geshi->set_source($blob->GetData()); $geshi->set_language($lang); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $output = $geshi->parse_code(); $bodystart = strpos($output, '<td'); $bodyend = strrpos($output, '</tr>'); if ($bodystart !== false && $bodyend !== false) { $geshihead = substr($output, 0, $bodystart); $geshifoot = substr($output, $bodyend); $geshibody = substr($output, $bodystart, $bodyend - $bodystart); $this->tpl->assign('geshihead', $geshihead); $this->tpl->assign('geshibody', $geshibody); $this->tpl->assign('geshifoot', $geshifoot); $this->tpl->assign('geshicss', $geshi->get_stylesheet()); $this->tpl->assign('geshi', true); } } } } } }
function ch_highlight_code($matches) { global $ch_options; // undo nl and p formatting $plancode = $matches[2]; $plancode = $this->entodec($plancode); $geshi = new GeSHi($plancode, strtolower($matches[1])); $geshi->set_encoding('utf-8'); $geshi->set_header_type(GESHI_HEADER_DIV); $geshi->enable_classes(true); $language = $geshi->get_language_name(); if ($language == 'PHP') { $geshi->add_keyword(2, 'protected'); $geshi->add_keyword(2, 'private'); $geshi->add_keyword(2, 'abstract'); $geshi->add_keyword(2, 'static'); $geshi->add_keyword(2, 'final'); $geshi->add_keyword(2, 'implements'); } elseif ($language == 'Bash') { $geshi->add_keyword(2, 'convert'); $geshi->add_keyword(2, 'git'); } elseif ($language == 'Vim Script') { $geshi->add_keyword(1, 'endfunction'); } if (ch_go('ch_b_linenumber')) { $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); } $geshi->enable_strict_mode(ch_go('ch_b_strict_mode')); $geshi->set_tab_width(ch_go('ch_in_tab_width')); $geshi->set_overall_class('dean_ch'); $overall_style = ''; if (!ch_go("ch_b_wrap_text")) { $overall_style .= 'white-space: nowrap;'; } else { $overall_style .= 'white-space: wrap;'; } if ($overall_style != '') { $geshi->set_overall_style($overall_style, false); } return $geshi->parse_code(); }
/** * Loads data for this template */ protected function LoadData() { $commit = $this->GetProject()->GetCommit($this->params['hashbase']); $this->tpl->assign('commit', $commit); $tree = $commit->GetTree(); $this->tpl->assign('tree', $commit->GetTree()); if (!isset($this->params['hash']) && isset($this->params['file'])) { $this->params['hash'] = $tree->PathToHash($this->params['file']); if (empty($this->params['hash'])) { throw new GitPHP_FileNotFoundException($this->params['file']); } } $blob = $this->GetProject()->GetObjectManager()->GetBlob($this->params['hash']); if (!empty($this->params['file'])) { $blob->SetPath($this->params['file']); } $blob->SetCommit($commit); $this->tpl->assign('blob', $blob); if ($this->Plain()) { return; } $head = $this->GetProject()->GetHeadCommit(); $this->tpl->assign('head', $head); if ($this->config->GetValue('filemimetype')) { $mimeReader = new GitPHP_FileMimeTypeReader($blob, $this->GetMimeStrategy()); $mimetype = $mimeReader->GetMimeType(true); if ($mimetype == 'image') { $this->tpl->assign('datatag', true); $this->tpl->assign('mime', $mimeReader->GetMimeType()); $this->tpl->assign('data', base64_encode($blob->GetData())); return; } } if ($this->config->GetValue('geshi')) { include_once GITPHP_GESHIDIR . "geshi.php"; if (class_exists('GeSHi')) { $geshi = new GeSHi("", 'php'); if ($geshi) { $lang = GitPHP_Util::GeshiFilenameToLanguage($blob->GetName()); if (empty($lang)) { $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(), '.'), 1)); } if (!empty($lang)) { $geshi->enable_classes(); $geshi->enable_strict_mode(GESHI_MAYBE); $geshi->set_source($blob->GetData()); $geshi->set_language($lang); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_overall_id('blobData'); $this->tpl->assign('geshiout', $geshi->parse_code()); $this->tpl->assign('geshicss', $geshi->get_stylesheet()); $this->tpl->assign('geshi', true); return; } } } } $this->tpl->assign('bloblines', $blob->GetData(true)); }
/** * Function called by the pre_typography extension hook before the text will be parsed by EE * @param string $str text that will be parsed * @param object $typo Typography object * @param array $prefs Preferences sent to $TYPE->parse_type * @return string text where the code has been stripped and code positions marked with an MD5-ID * @access public * @global $EXT Extension-Object to support multiple calls to the same extension hook * @global $OUT could be used to display errors - it isn't at the moment * @todo Display error using $OUT */ function pre_typography($str, $typo, $prefs) { // we don't need the DB, nor IN, nor DSP // should probably use OUT to display user_error messages global $EXT, $OUT; // here we're doing the actual work if ($EXT->last_call !== FALSE) { // A different extension has run before us $str = $EXT->last_call; } $cache_dir = dirname(__FILE__) . '/' . $this->settings['cache_dir']; $rllen = strlen($this->rlimit); $pos = array(); preg_match_all($this->llimit, $str, $matches, PREG_OFFSET_CAPTURE); foreach ($matches[0] as $key => $match) { $pos[$match[1]] = array(); $pos[$match[1]]['match'] = $match[0]; // lang (called type internally for historical reasons) if (!empty($matches[1][$key][0])) { // strip slashes for filesystem security and quotes because the value might be quoted $pos[$match[1]]['type'] = str_replace(array('/', '"', "'"), '', substr($matches[1][$key][0], 5)); } else { $pos[$match[1]]['type'] = NULL; } // strict if (!empty($matches[2][$key][0])) { switch (str_replace(array('"', "'"), '', strtolower(substr($matches[2][$key][0], 7)))) { case 'true': case '1': $pos[$match[1]]['strict'] = TRUE; break; case 'false': case '0': $pos[$match[1]]['strict'] = FALSE; break; default: $pos[$match[1]]['strict'] = NULL; break; } } else { $pos[$match[1]]['strict'] = NULL; } // line $pos[$match[1]]['line'] = !empty($matches[3][$key][0]) ? str_replace(array('"', "'"), '', substr($matches[3][$key][0], 5)) : NULL; // start $pos[$match[1]]['start'] = !empty($matches[4][$key][0]) ? str_replace(array('"', "'"), '', substr($matches[4][$key][0], 6)) : NULL; // keyword_links if (!empty($matches[5][$key][0])) { switch (str_replace(array("'", '"'), '', strtolower(substr($matches[5][$key][0], 14)))) { case 'true': case '1': $pos[$match[1]]['keyword_links'] = TRUE; break; case 'false': case '0': $pos[$match[1]]['keyword_links'] = FALSE; break; default: $pos[$match[1]]['keyword_links'] = NULL; break; } } else { $pos[$match[1]]['keyword_links'] = NULL; } // overall_class $pos[$match[1]]['overall_class'] = !empty($matches[6][$key][0]) ? str_replace(array("'", '"'), '', substr($matches[6][$key][0], 14)) : NULL; // overall_id $pos[$match[1]]['overall_id'] = !empty($matches[7][$key][0]) ? str_replace(array("'", '"'), '', substr($matches[7][$key][0], 11)) : NULL; } // clean variables used in the loop unset($matches, $key, $match); // krsort the array so we can use substr stuff and won't mess future replacements krsort($pos); // Check for the cache dir if (file_exists($cache_dir) && is_dir($cache_dir)) { $cache_dir = realpath($cache_dir) . '/'; if (!is_writable($cache_dir)) { // try to chmod it @chmod($cache_dir, 0777); if (!is_writable($cache_dir)) { // still not writable? display a warning print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> is not writable! This will cause severe performance problems, so I suggest you chmod that dir.'; } } } else { if (!mkdir($cache_dir, 0777)) { print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> could not be created! This will cause severe performance problems, so I suggest you create and chmod that dir.'; } else { // create an index.html so the contents will not be listed. @touch($cache_dir . 'index.html'); } } if (mt_rand(0, 10) == 10) { // on every 10th visit do the garbage collection $cur = time(); $d = dir($cache_dir); while (($f = $d->read()) !== FALSE) { if ($f != 'index.html' && $f[0] != '.') { if ($cur - filemtime($cache_dir . $f) > $this->settings['cache_cutoff']) { // File is older than cutoff, delete it. @unlink($cache_dir . $f); } } } } // loop through the code snippets $i = 0; foreach ($pos as $code_pos => $match) { $error = FALSE; if (($code_end_pos = strpos($str, $this->rlimit, (int) $code_pos + strlen($match['match']))) !== FALSE) { // we have a matching end tag. // make sure cache is regenerated when changing options, too! $md5 = md5(($not_geshified = substr($str, $code_pos + strlen($match['match']), $code_end_pos - $code_pos - strlen($match['match']))) . print_r($match, TRUE) . print_r($this->settings, TRUE)); // check whether we already have this in a cache file if (is_file($cache_dir . $md5) && is_readable($cache_dir . $md5)) { if (is_callable('file_get_contents')) { $geshified = file_get_contents($cache_dir . $md5); // this is for the garbage collection touch($cache_dir . $md5); } else { // screw PHP4! $f = fopen($cache_dir . $md5, 'r'); $geshified = fread($f, filesize($cache_dir . $md5)); fclose($f); touch($cache_dir . $md5); } } else { // no cache so do the GeSHi thing if ($this->settings['geshi_version'] == '1.1') { // use GeSHi 1.1 include_once dirname(__FILE__) . '/geshi-1.1/class.geshi.php'; // highlight code according to type setting, default to setting $geshi = new GeSHi($not_geshified, $match['type'] !== NULL ? $match['type'] : $this->settings['default_type']); $str_error = $geshi->error(); if (empty($str_error)) { // neither line numbers, nor strict mode is supported in GeSHi 1.1 yet. // in fact it does pretty much nothing // there used to be a rather large block of code here, testing wether methods were callable and call them if // that's the case. // parse the code $geshified = $geshi->parseCode(); } else { $error = TRUE; } } else { // use GeSHi 1.0 include_once dirname(__FILE__) . '/geshi-1.0/geshi.php'; // highlight code according to type setting, default to php $geshi = new GeSHi($not_geshified, $match['type'] !== NULL ? $match['type'] : $this->settings['default_type']); $str_error = $geshi->error(); if (empty($str_error)) { // enable line numbers $number_style = !empty($match['line']) ? $match['line'] : $this->settings['default_line']; switch (strtolower(preg_replace('/\\d+/', '', $number_style))) { case 'normal': $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); break; case 'fancy': $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, (int) preg_replace('/[^\\d]*/', '', $number_style)); break; case 'none': $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS); break; } // set first line number if ($match['start']) { $geshi->start_line_numbers_at($match['start']); } // set strict mode if ($match['strict']) { $geshi->enable_strict_mode(TRUE); } // enable or disable keyword links $geshi->enable_keyword_links((bool) ($match['keyword_links'] !== NULL) ? $match['keyword_links'] : $this->settings['keyword_links']); // set overall class name if ($match['overall_class'] != NULL) { $geshi->set_overall_class($match['overall_class']); } // set overall id if ($match['overall_id'] != NULL) { $geshi->set_overall_id($match['overall_id']); } // set header type switch ($this->settings['geshi_header_type']) { case 'div': $geshi->set_header_type(GESHI_HEADER_DIV); break; case 'pre': $geshi->set_header_type(GESHI_HEADER_PRE); break; case 'pre-valid': $geshi->set_header_type(GESHI_HEADER_PRE_VALID); break; case 'pre-table': $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); break; case 'none': $geshi->set_header_type(GESHI_HEADER_NONE); break; } // set encoding (for legacy reasons afaik) $geshi->set_encoding($this->settings['geshi_encoding']); // parse the source code $geshified = $geshi->parse_code(); } else { $error = TRUE; } } if (!file_exists($cache_dir . $md5) && is_writable($cache_dir) || file_exists($cache_dir . $md5) && is_writable($cache_dir . $md5)) { if (!$error) { // we can write to the cache file if (is_callable('file_put_contents')) { file_put_contents($cache_dir . $md5, $geshified); @chmod($cache_dir . $md5, 0777); } else { // when will you guys finally drop PHP4 support? $f = fopen($cache_dir . $md5, 'w'); fwrite($f, $geshified); fclose($f); @chmod($cache_dir . $md5, 0777); } } } else { // We could ignore that, but for performance reasons better warn the user. print '<b>Warning</b>: Your <i>' . $this->name . '</i> cache directory <b>' . $cache_dir . '</b> is not writable! This will cause severe performance problems, so I suggest you chmod that dir.'; } } // save replacement to cache and mark location with an identifier for later replacement if (!isset($_SESSION['cache']['ext.geshify'])) { $_SESSION['cache']['ext.geshify'] = array(); } if (!$error) { $_SESSION['cache']['ext.geshify'][$md5] = $geshified; $str = substr($str, 0, $code_pos) . $md5 . substr($str, $code_end_pos + $rllen); } } // unset used variables, so we don't get messed up unset($code_pos, $code_end_pos, $md5, $geshified, $not_geshified, $geshi, $match, $ident, $error); } return $str; }
/** * Function handler for coloring shortcodes. It's used in comments and for [filesyntax tag] * * @param string $atts * @param string $content * @return string */ function fr_codesyntax_handler($atts, $content = null, $cleanHTML = true, $commentProcessing = false) { global $wp_sh_styling_type; if (empty($content)) { return '<font color="red"><b>' . __('WP-SYNHIGHLIGHT PLUGIN: NOTHING TO HIGHLIGHT! PLEASE READ README.TXT IN PLUGIN FOLDER!', 'wp-synhighlighter') . '</b></font>'; } //Parsing paramters $params = shortcode_atts(array('title' => get_option('wp_synhighlight_default_codeblock_title') ? get_option('wp_synhighlight_default_codeblock_title') : __("Code block", 'wp-synhighlighter'), 'bookmarkname' => '', 'lang' => 'pascal', 'lines' => get_option('wp_synhighlight_default_lines') ? get_option('wp_synhighlight_default_lines') : 'fancy', 'lines_start' => get_option('wp_synhighlight_default_lines_start_with') ? get_option('wp_synhighlight_default_lines_start_with') : '1', 'container' => get_option('wp_synhighlight_default_container') ? get_option('wp_synhighlight_default_container') : 'pre', 'capitalize' => get_option('wp_synhighlight_default_capitalize_keywords') ? get_option('wp_synhighlight_default_capitalize_keywords') : 'no', 'tab_width' => get_option('wp_synhighlight_default_tab_width') ? get_option('wp_synhighlight_default_tab_width') : 4, 'strict' => get_option('wp_synhighlight_default_strict_mode') ? get_option('wp_synhighlight_default_strict_mode') : 'always', 'blockstate' => get_option('wp_synhighlight_default_blockstate') ? get_option('wp_synhighlight_default_blockstate') : 'default', 'highlight_lines' => "", 'doclinks' => !get_option('wp_synhighlight_doclinks_off')), $atts); if ($cleanHTML) { //Clearing all other HTML code $content = strip_tags($content); //Converting HTML entities $content = html_entity_decode($content, ENT_QUOTES); } //Trimming first and last incorrect newlines $content = trim($content); //Windows Live Writer patch foreach ($params as &$param) { $param = trim(html_entity_decode($param, ENT_QUOTES), '"'); } //Highlighting $geshi = new GeSHi($content, $params['lang']); if (!$commentProcessing and ($wp_sh_styling_type == 'theme' or $wp_sh_styling_type == 'embedbody')) { $geshi->enable_classes(); } //Setting Geshi options //Lines switch ($params['lines']) { case 'normal': $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); break; case 'fancy': $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); break; case 'no': $geshi->enable_line_numbers(GESHI_NO_LINE_NUMBERS); break; } $geshi->start_line_numbers_at($params['lines_start']); //Container switch ($params['container']) { case 'pre': $geshi->set_header_type(GESHI_HEADER_PRE); break; case 'div': $geshi->set_header_type(GESHI_HEADER_DIV); break; case 'pre_valid': $geshi->set_header_type(GESHI_HEADER_PRE_VALID); break; case 'pre_table': $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); break; case 'none': $geshi->set_header_type(GESHI_HEADER_NONE); break; } //Keywords capitalization switch ($params['capitalize']) { case 'no': $geshi->set_case_keywords(GESHI_CAPS_NO_CHANGE); break; case 'upper': $geshi->set_case_keywords(GESHI_CAPS_UPPER); break; case 'lower': $geshi->set_case_keywords(GESHI_CAPS_LOWER); break; } //Tab width $geshi->set_tab_width($params['tab_width']); //Strict mode switch ($params['strict']) { case 'always': $geshi->enable_strict_mode(GESHI_ALWAYS); break; case 'maybe': $geshi->enable_strict_mode(GESHI_MAYBE); break; case 'never': $geshi->enable_strict_mode(GESHI_NEVER); break; } //Block state switch ($params['blockstate']) { case 'collapsed': $initiallyHidden = true; break; case 'default': case 'expanded': default: $initiallyHidden = false; break; } //Controlling doclinks $geshi->enable_keyword_links($params['doclinks']); static $instanceNumber = 0; $instanceNumber++; $bookmarkName = empty($params['bookmarkname']) ? "codesyntax_" . $instanceNumber : $params['bookmarkname']; //Highlighting lines if (!empty($params['highlight_lines'])) { $geshi->highlight_lines_extra(explode(',', $params['highlight_lines'])); } //Checking for geshi errors if ($geshi->error()) { return '<font color="red"><b>' . $geshi->error() . '</b></font>'; } //Styling codeblock $header = wp_synhighlight_get_tpl_header($instanceNumber, $params['title'], $bookmarkName, $initiallyHidden); //Embedding only one copy of each used language style static $embeddedStylesheets = array(); if ($wp_sh_styling_type == 'embedbody' and !in_array($params['lang'], $embeddedStylesheets)) { $header = '<style type="text/css"><!--' . "\r\n" . $geshi->get_stylesheet() . "\r\n" . '-->' . "\r\n" . '</style>' . $header; $embeddedStylesheets[] = $params['lang']; } $footer = wp_synhighlight_get_tpl_footer(); $result = $header . $geshi->parse_code() . $footer; return $result; }
/** * LoadData * * Loads data for this template * * @access protected */ protected function LoadData() { $commit = $this->project->GetCommit($this->params['hashbase']); $this->tpl->assign('commit', $commit); if (!isset($this->params['hash']) && isset($this->params['file'])) { $this->params['hash'] = $commit->PathToHash($this->params['file']); } $blob = $this->project->GetBlob($this->params['hash']); if (!empty($this->params['file'])) { $blob->SetPath($this->params['file']); } $blob->SetCommit($commit); $this->tpl->assign('blob', $blob); if (isset($this->params['plain']) && $this->params['plain']) { return; } $head = $this->project->GetHeadCommit(); $this->tpl->assign('head', $head); $this->tpl->assign('tree', $commit->GetTree()); if (GitPHP_Config::GetInstance()->GetValue('filemimetype', true)) { $mime = $blob->FileMime(); if ($mime) { $mimetype = strtok($mime, '/'); if ($mimetype == 'image') { $this->tpl->assign('datatag', true); $this->tpl->assign('mime', $mime); $this->tpl->assign('data', base64_encode($blob->GetData())); return; } } } if (GitPHP_Config::GetInstance()->GetValue('geshi', true)) { include_once GitPHP_Util::AddSlash(GitPHP_Config::GetInstance()->GetValue('geshiroot', 'lib/geshi/')) . "geshi.php"; if (class_exists('GeSHi')) { $geshi = new GeSHi("", 'php'); if ($geshi) { $lang = $geshi->get_language_name_from_extension(substr(strrchr($blob->GetName(), '.'), 1)); if (!empty($lang)) { $geshi->enable_classes(); $geshi->enable_strict_mode(GESHI_MAYBE); $geshi->set_source($blob->GetData()); $geshi->set_language($lang); $geshi->set_header_type(GESHI_HEADER_PRE_TABLE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->set_overall_id('blobData'); $this->tpl->assign('geshiout', $geshi->parse_code()); $this->tpl->assign('geshicss', $geshi->get_stylesheet()); $this->tpl->assign('geshi', true); return; } } } } $this->tpl->assign('bloblines', $blob->GetData(true)); }
function AdvancedCodeTag($source, $settings) { global $languages, $languagesPath, $codeTag; $language = array_shift($settings); // [arg1] $isNumbered = array_shift($settings); // [arg2] // [arg1] if ($language == '') { $language = 'text'; // bugfix: to work for existing <code> tags, simply use "text" } if ($language == "list") { // list all languages supported return "<br>List of supported languages for <b>Geshi " . GESHI_VERSION . "</b>:<br>" . implode("<br>", $languages); } if ($language != "" && !in_array($language, $languages)) { // list languages if invalid argument return "<br>Invalid language argument, \"<b>" . $language . "</b>\", select one from the list:<br>" . implode("<br>", $languages); } // set geshi $geshi = new GeSHi(trim($source), $language, $languagesPath); $geshi->enable_strict_mode($codeTag["advanced"]["strict"]); // [arg2] if ($isNumbered == "n") { // display line numbers $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); } /* Add more GeSHi features below http://qbnz.com/highlighter/geshi-doc.html */ return $geshi->parse_code(); }