コード例 #1
0
ファイル: code.php プロジェクト: stevestreza/code-feather
 public function format_code($text, $post = null)
 {
     if (isset($post)) {
         $code = $text;
         if (preg_match("/\\[gist: ([0-9]+)\\]/i", $code, $matches)) {
             return '<script src="http://gist.github.com/' . $matches[1] . '.js"></script>';
         }
         $post->code_unformatted = $post->code;
         $languages = getLanguages();
         $geshi = new GeSHi($code, $languages[$post->language]);
         if ($geshi->error() !== false) {
             return "<pre id='geshi_code'>" . $geshi->error() . "</pre>";
         }
         $geshi->set_overall_id('geshi_code');
         $return = $geshi->parse_code();
         return $return;
     } else {
         return "<pre id='geshi_code'>" . $text . "</pre>";
     }
 }
 /**
  * The main method of the Plugin
  *
  * @param	string		$content: The PlugIn content
  * @param	array		$conf: The PlugIn configuration
  * @return	The content that is displayed on the website
  */
 function main($content, $config)
 {
     if (!t3lib_extMgm::isLoaded('geshilib')) {
         return "Geshi library not loaded";
     }
     // get content
     $this->pi_initPIflexForm();
     $config['content.']['lang'] = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'cLang', 'sVIEW');
     $config['content.']['code'] = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'cCode', 'sVIEW');
     $config['content.']['highlight'] = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'cHighlight', 'sOPTIONS');
     $config['content.']['startnumber'] = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'cStartnumber', 'sOPTIONS');
     // init geshi library
     $this->geshi = new GeSHi($config['content.']['code'], $config['content.']['lang']);
     // defaults
     $this->geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
     // set highlighted lines
     if ($config['content.']['highlight'] !== '') {
         $this->geshi->highlight_lines_extra(split(',', $config['content.']['highlight']));
     }
     // set startnumber
     if (isset($config['content.']['startnumber'])) {
         $this->geshi->start_line_numbers_at($config['content.']['startnumber']);
     }
     // style
     if (isset($config['default.'])) {
         $this->_styleSubjects($config['default.']);
     }
     if (isset($config[$config['content.']['lang'] . '.'])) {
         $this->_styleSubjects($config[$config['content.']['lang'] . '.']);
     }
     // external stylesheets
     if (isset($config['global.']['external']) && $config['global.']['external'] == 0) {
         // do not use external style sheets
     } else {
         // mtness.net modification: I love stylesheets!
         $this->geshi->enable_classes();
         // Echo out the stylesheet for this code block And continue echoing the page
         $this->geshiCSS = '<style type="text/css"><!--' . $this->geshi->get_stylesheet() . '--></style>';
         // additional headerdata to include the styles
         $GLOBALS['TSFE']->additionalHeaderData['sema_sourcecode:' . $config['content.']['lang']] = $this->geshiCSS;
     }
     // xhtml compliance
     if (isset($config['global.']['xhtmlcompliant']) && $config['global.']['xhtmlcompliant'] == 1) {
         $this->geshi->force_xhtml_compliance = true;
     }
     // check for errors
     if ($this->geshi->error() !== false) {
         // log an error, this happens if the language file is missing or non-readable. Other input
         // specific errors can also occour, eg. if a non-existing container type is set for the engine.
         $GLOBALS['BE_USER']->simplelog($this->geshi->error(), $extKey = 'sema_sourcecode', 1);
     }
     // render
     return $this->pi_wrapInBaseClass($this->geshi->parse_code());
 }
コード例 #3
0
ファイル: source_color.php プロジェクト: laiello/xiv
 public function code(&$buff)
 {
     $geshi = new GeSHi($buff, $this->lang_alias($this->lang));
     $geshi->set_header_type(GESHI_HEADER_PRE);
     $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
     $geshi->code_style = $geshi->line_style1 = '';
     $geshi->keyword_links = false;
     $error = $geshi->error();
     if ($error) {
         debug($error);
     }
     return '<span class="code">' . ($error ? nl2br(htmlspecialchars($buff)) : $geshi->parse_code()) . '</span>';
 }
コード例 #4
0
ファイル: bbcode.php プロジェクト: RazorMarx/izend
function bbcode_highlite($s, $language = false)
{
    $s = base64_decode($s);
    if (!$language) {
        return '<code>' . htmlspecialchars($s, ENT_COMPAT, 'UTF-8') . '</code>';
    }
    $geshi = new GeSHi($s, $language);
    $geshi->enable_classes(true);
    $geshi->set_header_type(GESHI_HEADER_DIV);
    $geshi->enable_keyword_links(false);
    $geshi->set_tab_width(4);
    $output = $geshi->parse_code();
    if ($geshi->error()) {
        return false;
    }
    head('stylesheet', 'geshi/' . $language, 'screen');
    return '<div class="geshi">' . $output . '</div>';
}
コード例 #5
0
 function highlight($code, $lang, $path)
 {
     if (empty($lang)) {
         $lang = 'c';
     }
     $geshi = new GeSHi($code, $lang);
     $geshi->enable_classes();
     $geshi->set_overall_class('highlight');
     $geshi->set_header_type(GESHI_HEADER_NONE);
     /* Now this feature works, maybe activate it? */
     /* $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 1); */
     $result = $geshi->parse_code();
     /* This thing fixes styles when line numbers are disabled */
     $result = '<div class="highlight ' . $lang . '">' . $result . '</div>';
     if ($geshi->error()) {
         $result = $code;
     }
     return $result;
 }
コード例 #6
0
ファイル: prettyfile.php プロジェクト: RazorMarx/izend
function pretty_file($file, $language, $startline = 0, $endline = 0, $number = false)
{
    if (!$file) {
        return false;
    }
    $s = read_file($file, $startline, $endline);
    if (!$s) {
        return false;
    }
    if (!$language) {
        return $s;
    }
    $output = false;
    switch ($language) {
        case 'plain':
            $s = preg_replace("/\\]\\=\\>\n(\\s+)/m", "] => ", $s);
            $s = htmlentities($s, ENT_COMPAT, 'UTF-8');
            $output = '<pre class="plain">' . PHP_EOL . $s . '</pre>' . PHP_EOL;
            break;
        default:
            $geshi = new GeSHi($s, $language);
            $geshi->enable_classes(true);
            $geshi->set_header_type(GESHI_HEADER_DIV);
            if ($number) {
                $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
                $geshi->start_line_numbers_at($startline > 0 ? $startline : 1);
            }
            $geshi->enable_keyword_links(false);
            $geshi->set_tab_width(4);
            //			echo '<pre>' . PHP_EOL .$geshi->get_stylesheet( ). '</pre>' . PHP_EOL;
            $output = $geshi->parse_code();
            if ($geshi->error()) {
                return false;
            }
    }
    return $output;
}
コード例 #7
0
ファイル: geshi.php プロジェクト: carriercomm/pastebin
 /**
  * Easy way to highlight stuff. Behaves just like highlight_string
  * 
  * @param string The code to highlight
  * @param string The language to highlight the code in
  * @param string The path to the language files. You can leave this blank if you need
  *               as from version 1.0.7 the path should be automatically detected
  * @param boolean Whether to return the result or to echo
  * @return string The code highlighted (if $return is true)
  * @since 1.0.2
  */
 function geshi_highlight($string, $language, $path, $return = false)
 {
     $geshi = new GeSHi($string, $language, $path);
     $geshi->set_header_type(GESHI_HEADER_NONE);
     if ($return) {
         return '<code>' . $geshi->parse_code() . '</code>';
     }
     echo '<code>' . $geshi->parse_code() . '</code>';
     if ($geshi->error()) {
         return false;
     }
     return true;
 }
コード例 #8
0
ファイル: geshi.php プロジェクト: badboy/devbird
 /**
  * Easy way to highlight stuff. Behaves just like highlight_string
  *
  * @param string The code to highlight
  * @param string The language to highlight the code in
  * @param string The path to the language files. You can leave this blank if you need
  *               as from version 1.0.7 the path should be automatically detected
  * @param boolean Whether to return the result or to echo
  * @return string The code highlighted (if $return is true)
  * @since 1.0.2
  */
 function geshi_highlight($string, $language, $path = null, $return = false)
 {
     $geshi = new GeSHi($string, $language, $path);
     $geshi->set_header_type(GESHI_HEADER_NONE);
     $code = $geshi->parse_code();
     /**
      * changed some code here to properly work with my blogsystem (BadBoy_, 05/23/2008, http://badboy.pytalhost.de)
      */
     if ($geshi->error()) {
         return $string;
         #$geshi->error();
     }
     if ($return) {
         return '<code>' . $code . '</code>';
     }
     echo '<code>' . $code . '</code>';
     return true;
 }
コード例 #9
0
 /**
  * Initialise a GeSHi object to format some code, performing
  * common setup for all our uses of it
  *
  * @param string $text
  * @param string $lang
  * @return GeSHi
  */
 private static function prepare($text, $lang)
 {
     self::initialise();
     $geshi = new GeSHi($text, $lang);
     if ($geshi->error() == GESHI_ERROR_NO_SUCH_LANG) {
         return null;
     }
     $geshi->set_encoding('UTF-8');
     $geshi->enable_classes();
     $geshi->set_overall_class("source-{$lang}");
     $geshi->enable_keyword_links(false);
     return $geshi;
 }
コード例 #10
0
 /**
  * Easy way to highlight stuff. Behaves just like highlight_string
  *
  * @param string The code to highlight
  * @param string The language to highlight the code in
  * @param string The path to the language files. You can leave this blank if you need
  *               as from version 1.0.7 the path should be automatically detected
  * @param boolean Whether to return the result or to echo
  * @return string The code highlighted (if $return is true)
  * @since 1.0.2
  */
 function geshi_highlight($string, $language, $path = null, $return = false)
 {
     $geshi = new GeSHi($string, $language, $path);
     $geshi->set_header_type(GESHI_HEADER_PRE_VALID);
     $geshi->enable_classes();
     $geshi->enable_keyword_links(false);
     $geshi->highlight_lines_extra(array(8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 24, 32, 80));
     if ($return) {
         return '<code>' . $geshi->parse_code() . '</code>';
     }
     echo '<code>' . $geshi->parse_code() . '</code>';
     if ($geshi->error()) {
         return false;
     }
     return true;
 }
コード例 #11
0
 /**
  * Initialise a GeSHi object to format some code, performing
  * common setup for all our uses of it
  *
  * @param string $text
  * @param string $lang
  * @return GeSHi
  */
 public static function prepare($text, $lang)
 {
     global $wgTitle, $wgOut;
     self::initialise();
     $geshi = new GeSHi($text, $lang);
     if ($geshi->error() == GESHI_ERROR_NO_SUCH_LANG) {
         return null;
     }
     $geshi->set_encoding('UTF-8');
     $geshi->enable_classes();
     $geshi->set_overall_class("source-{$lang}");
     $geshi->enable_keyword_links(false);
     // Wikia change start
     if ($wgTitle instanceof Title && EditPageLayoutHelper::isCodeSyntaxHighlightingEnabled($wgTitle)) {
         $theme = 'solarized-light';
         if (SassUtil::isThemeDark()) {
             $theme = 'solarized-dark';
         }
         $geshi->set_language_path(GESHI_ROOT . $theme . DIRECTORY_SEPARATOR);
         $geshi->set_overall_id('theme-' . $theme);
         $wgOut->addStyle(AssetsManager::getInstance()->getSassCommonURL('extensions/SyntaxHighlight_GeSHi/styles/solarized.scss'));
     }
     // Wikia change end
     return $geshi;
 }
コード例 #12
0
	/**
	 * Initialise a GeSHi object to format some code, performing
	 * common setup for all our uses of it
	 *
	 * @note Used only until MW 1.20
	 *
	 * @param string $text
	 * @param string $lang
	 * @return GeSHi
	 */
	public static function prepare( $text, $lang ) {

		global $wgSyntaxHighlightKeywordLinks;

		self::initialise();
		$geshi = new GeSHi( $text, $lang );
		if( $geshi->error() == GESHI_ERROR_NO_SUCH_LANG ) {
			return null;
		}
		$geshi->set_encoding( 'UTF-8' );
		$geshi->enable_classes();
		$geshi->set_overall_class( "source-$lang" );
		$geshi->enable_keyword_links( $wgSyntaxHighlightKeywordLinks );

		// If the source code is over 100 kB, disable higlighting of symbols.
		// If over 200 kB, disable highlighting of strings too.
		$bytes = strlen( $text );
		if ( $bytes > 102400 ) {
			$geshi->set_symbols_highlighting( false );
			if ( $bytes > 204800 ) {
				$geshi->set_strings_highlighting( false );
			}
		}

		return $geshi;
	}
コード例 #13
0
 /**
  * Initialise a GeSHi object to format some code, performing
  * common setup for all our uses of it
  *
  * @param string $text
  * @param string $lang
  * @return GeSHi
  */
 public static function prepare($text, $lang)
 {
     global $wgSyntaxHighlightKeywordLinks;
     self::initialise();
     $geshi = new GeSHi($text, $lang);
     if ($geshi->error() == GESHI_ERROR_NO_SUCH_LANG) {
         return null;
     }
     $geshi->set_encoding('UTF-8');
     $geshi->enable_classes();
     $geshi->set_overall_class("source-{$lang}");
     $geshi->enable_keyword_links($wgSyntaxHighlightKeywordLinks);
     // If the source code is over 100 kB, disable higlighting of symbols.
     // If over 200 kB, disable highlighting of strings too.
     $bytes = strlen($text);
     if ($bytes > 102400) {
         $geshi->set_symbols_highlighting(false);
         if ($bytes > 204800) {
             $geshi->set_strings_highlighting(false);
         }
     }
     /**
      * GeSHi comes by default with a font-family set to monospace, which
      * causes the font-size to be smaller than one would expect.
      * We append a CSS hack to the default GeSHi styles: specifying 'monospace'
      * twice "resets" the browser font-size specified for monospace.
      *
      * The hack is documented in MediaWiki core under
      * docs/uidesign/monospace.html and in bug 33496.
      */
     // Preserve default since we don't want to override the other style
     // properties set by geshi (padding, font-size, vertical-align etc.)
     $geshi->set_code_style('font-family: monospace, monospace;', true);
     // No need to preserve default (which is just "font-family: monospace;")
     // outputting both is unnecessary
     $geshi->set_overall_style('font-family: monospace, monospace;', false);
     return $geshi;
 }
コード例 #14
0
ファイル: bbcode.php プロジェクト: GamerSource/Infected-CMS-2
 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>';
     }
 }
コード例 #15
0
ファイル: ext.geshify.php プロジェクト: neverpanic/GeSHify
 /**
  * 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;
 }
コード例 #16
0
ファイル: highlighter.php プロジェクト: yszar/linuxwp
/**
 * 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;
}
コード例 #17
0
ファイル: geshi.php プロジェクト: VictorSproot/AtomXCMS-2
 /**
  * Easy way to highlight stuff. Behaves just like highlight_string
  *
  * @param string The code to highlight
  * @param string The language to highlight the code in
  * @param string The path to the language files. You can leave this blank if you need
  *               as from version 1.0.7 the path should be automatically detected
  * @param boolean Whether to return the result or to echo
  * @return string The code highlighted (if $return is true)
  * @since 1.0.2
  */
 function geshi_highlight($string, $language, $path = null, $return = false)
 {
     $geshi = new GeSHi($string, $language, $path);
     $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
     $geshi->set_header_type(GESHI_HEADER_PRE_TABLE);
     if ($return) {
         return '<code>' . $geshi->parse_code() . '</code>';
     }
     echo '<code>' . $geshi->parse_code() . '</code>';
     if ($geshi->error()) {
         return false;
     }
     return true;
 }
コード例 #18
0
function geshi_format_code($text, $ext)
{
    $str = '';
    $geshi = new GeSHi($text, '');
    $lang = $ext == 'diff' ? $ext : $geshi->get_language_name_from_extension($ext);
    $geshi->set_language($lang);
    if ($geshi->error() == GESHI_ERROR_NO_SUCH_LANG) {
        return null;
    }
    $geshi->set_encoding('UTF-8');
    //$geshi->enable_classes();
    //$geshi->set_overall_class( "source-$lang" );
    //$geshi->enable_keyword_links( false );
    $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
    $geshi->set_header_type(GESHI_HEADER_DIV);
    $str .= $geshi->parse_code($text, 1);
    if ($geshi->error()) {
        $str .= $geshi->error();
    }
    return $str;
}
コード例 #19
0
ファイル: geshi.php プロジェクト: ookwudili/chisimba
 /**
  * function: geshi_highlight
  * -------------------------
  * Easy way to highlight stuff. Behaves just like highlight_string
  */
 function geshi_highlight($string, $language, $path, $return = false)
 {
     $geshi = new GeSHi($string, $language, $path);
     $geshi->set_header_type(GESHI_HEADER_DIV);
     if ($return) {
         return str_replace('<div>', '<code>', str_replace('</div>', '</code>', $geshi->parse_code()));
     }
     echo str_replace('<div>', '<code>', str_replace('</div>', '</code>', $geshi->parse_code()));
     if ($geshi->error()) {
         return false;
     }
     return true;
 }
コード例 #20
0
ファイル: ui.inc.php プロジェクト: AzerothShard/thebuggenie
/**
 * Easy way to highlight stuff. Behaves just like highlight_string
 *
 * @param string The code to highlight
 * @param string The language to highlight the code in
 * @param string The path to the language files. You can leave this blank if you need
 *               as from version 1.0.7 the path should be automatically detected
 * @param boolean Whether to return the result or to echo
 * @return string The code highlighted (if $return is true)
 * @since 1.0.2
 */
function geshi_highlight($string, $language, $path = null, $return = false)
{
    defined('GESHI_ROOT') || define('GESHI_ROOT', THEBUGGENIE_CORE_PATH . 'geshi' . DS);
    $geshi = new GeSHi($string, $language, $path);
    $geshi->set_header_type(GESHI_HEADER_NONE);
    if ($return) {
        return '<code>' . $geshi->parse_code() . '</code>';
    }
    echo '<code>' . $geshi->parse_code() . '</code>';
    if ($geshi->error()) {
        return false;
    }
    return true;
}