2007-06-29 04:33:14 +00:00
|
|
|
<?php
|
|
|
|
|
2014-06-01 17:50:34 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
class SyntaxHighlight_GeSHi {
|
|
|
|
/**
|
|
|
|
* Has GeSHi been initialised this session?
|
|
|
|
*/
|
|
|
|
private static $initialised = false;
|
2008-01-11 09:05:18 +00:00
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
/**
|
|
|
|
* List of languages available to GeSHi
|
2012-03-09 23:33:42 +00:00
|
|
|
* @var array
|
2007-06-29 04:33:14 +00:00
|
|
|
*/
|
|
|
|
private static $languages = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parser hook
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @param array $args
|
|
|
|
* @param Parser $parser
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function parserHook( $text, $args = array(), $parser ) {
|
2014-10-13 23:35:49 +00:00
|
|
|
global $wgSyntaxHighlightDefaultLang, $wgUseTidy;
|
2009-06-24 05:25:32 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2007-06-29 04:33:14 +00:00
|
|
|
self::initialise();
|
2008-07-11 18:12:48 +00:00
|
|
|
$text = rtrim( $text );
|
|
|
|
// Don't trim leading spaces away, just the linefeeds
|
|
|
|
$text = preg_replace( '/^\n+/', '', $text );
|
2011-09-16 17:52:28 +00:00
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
// Validate language
|
2009-11-19 20:37:17 +00:00
|
|
|
if( isset( $args['lang'] ) && $args['lang'] ) {
|
2009-05-18 20:28:50 +00:00
|
|
|
$lang = $args['lang'];
|
2007-06-29 04:33:14 +00:00
|
|
|
} else {
|
2009-05-17 15:21:37 +00:00
|
|
|
// language is not specified. Check if default exists, if yes, use it.
|
2009-05-18 20:28:50 +00:00
|
|
|
if ( !is_null( $wgSyntaxHighlightDefaultLang ) ) {
|
|
|
|
$lang = $wgSyntaxHighlightDefaultLang;
|
|
|
|
} else {
|
2011-05-13 13:27:29 +00:00
|
|
|
$error = self::formatLanguageError( $text );
|
2009-06-24 05:25:32 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $error;
|
2009-05-18 20:28:50 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
2009-05-18 20:28:50 +00:00
|
|
|
$lang = strtolower( $lang );
|
2009-06-24 05:25:32 +00:00
|
|
|
if( !preg_match( '/^[a-z_0-9-]*$/', $lang ) ) {
|
2011-05-13 13:27:29 +00:00
|
|
|
$error = self::formatLanguageError( $text );
|
2009-06-24 05:25:32 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $error;
|
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi = self::prepare( $text, $lang );
|
2009-06-24 05:25:32 +00:00
|
|
|
if( !$geshi instanceof GeSHi ) {
|
2011-05-13 13:27:29 +00:00
|
|
|
$error = self::formatLanguageError( $text );
|
2009-06-24 05:25:32 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $error;
|
|
|
|
}
|
2008-09-28 15:30:45 +00:00
|
|
|
|
|
|
|
$enclose = self::getEncloseType( $args );
|
|
|
|
|
2008-07-11 18:12:48 +00:00
|
|
|
// Line numbers
|
|
|
|
if( isset( $args['line'] ) ) {
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi->enable_line_numbers( GESHI_FANCY_LINE_NUMBERS );
|
|
|
|
}
|
2008-07-10 20:55:02 +00:00
|
|
|
// Highlighting specific lines
|
2008-07-10 12:45:20 +00:00
|
|
|
if( isset( $args['highlight'] ) ) {
|
2008-07-10 20:55:02 +00:00
|
|
|
$lines = self::parseHighlightLines( $args['highlight'] );
|
2011-12-02 17:00:19 +00:00
|
|
|
if ( count( $lines ) ) {
|
2010-08-09 06:56:38 +00:00
|
|
|
$geshi->highlight_lines_extra( $lines );
|
|
|
|
}
|
2008-07-10 12:45:20 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
// Starting line number
|
2010-08-09 06:56:38 +00:00
|
|
|
if( isset( $args['start'] ) ) {
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi->start_line_numbers_at( $args['start'] );
|
2010-08-09 06:56:38 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi->set_header_type( $enclose );
|
|
|
|
// Strict mode
|
2010-08-09 06:56:38 +00:00
|
|
|
if( isset( $args['strict'] ) ) {
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi->enable_strict_mode();
|
2010-08-09 06:56:38 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
// Format
|
|
|
|
$out = $geshi->parse_code();
|
2011-05-13 13:27:29 +00:00
|
|
|
if ( $geshi->error == GESHI_ERROR_NO_SUCH_LANG ) {
|
|
|
|
// Common error :D
|
|
|
|
$error = self::formatLanguageError( $text );
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $error;
|
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
$err = $geshi->error();
|
|
|
|
if( $err ) {
|
2011-05-13 13:27:29 +00:00
|
|
|
// Other unknown error!
|
2009-06-24 05:25:32 +00:00
|
|
|
$error = self::formatError( $err );
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $error;
|
|
|
|
}
|
|
|
|
// Armour for Parser::doBlockLevels()
|
2011-12-02 17:00:19 +00:00
|
|
|
if( $enclose === GESHI_HEADER_DIV ) {
|
2009-06-24 05:25:32 +00:00
|
|
|
$out = str_replace( "\n", '', $out );
|
2011-12-02 17:00:19 +00:00
|
|
|
}
|
2013-12-02 16:28:37 +00:00
|
|
|
// HTML Tidy will convert tabs to spaces incorrectly (bug 30930).
|
|
|
|
// But the conversion from tab to space occurs while reading the input,
|
|
|
|
// before the conversion from 	 to tab, so we can armor it that way.
|
|
|
|
if( $wgUseTidy ) {
|
|
|
|
$out = str_replace( "\t", '	', $out );
|
|
|
|
}
|
2009-06-24 05:25:32 +00:00
|
|
|
// Register CSS
|
2014-10-13 23:35:49 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( array( "ext.geshi.language.$lang", 'ext.geshi.local' ) );
|
2011-11-20 18:07:20 +00:00
|
|
|
|
2012-04-14 04:08:20 +00:00
|
|
|
$encloseTag = $enclose === GESHI_HEADER_NONE ? 'span' : 'div';
|
2011-02-10 01:59:14 +00:00
|
|
|
$attribs = Sanitizer::validateTagAttributes( $args, $encloseTag );
|
|
|
|
|
|
|
|
//lang is valid in HTML context, but also used on GeSHi
|
|
|
|
unset( $attribs['lang'] );
|
|
|
|
|
2009-06-24 05:25:32 +00:00
|
|
|
if ( $enclose === GESHI_HEADER_NONE ) {
|
2012-05-10 23:12:27 +00:00
|
|
|
$attribs = self::addAttribute( $attribs, 'class', 'mw-geshi ' . $lang . ' source-' . $lang );
|
2007-06-29 04:33:14 +00:00
|
|
|
} else {
|
2012-01-05 19:46:42 +00:00
|
|
|
// Default dir="ltr" (but allow dir="rtl", although unsure if needed)
|
|
|
|
$attribs['dir'] = isset( $attribs['dir'] ) && $attribs['dir'] === 'rtl' ? 'rtl' : 'ltr';
|
2012-04-14 04:08:20 +00:00
|
|
|
$attribs = self::addAttribute( $attribs, 'class', 'mw-geshi mw-code mw-content-' . $attribs['dir'] );
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
2012-04-14 04:08:20 +00:00
|
|
|
$out = Html::rawElement( $encloseTag, $attribs, $out );
|
2011-02-10 01:59:14 +00:00
|
|
|
|
2009-06-24 05:25:32 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return $out;
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
2010-08-09 06:56:38 +00:00
|
|
|
|
2011-12-02 17:00:19 +00:00
|
|
|
/**
|
|
|
|
* @param $attribs array
|
|
|
|
* @param $name string
|
|
|
|
* @param $value string
|
|
|
|
* @return array
|
|
|
|
*/
|
2011-02-10 01:59:14 +00:00
|
|
|
private static function addAttribute( $attribs, $name, $value ) {
|
|
|
|
if( isset( $attribs[$name] ) ) {
|
|
|
|
$attribs[$name] = $value . ' ' . $attribs[$name];
|
|
|
|
} else {
|
|
|
|
$attribs[$name] = $value;
|
|
|
|
}
|
|
|
|
return $attribs;
|
|
|
|
}
|
|
|
|
|
2008-07-10 20:55:02 +00:00
|
|
|
/**
|
|
|
|
* Take an input specifying a list of lines to highlight, returning
|
|
|
|
* a raw list of matching line numbers.
|
|
|
|
*
|
|
|
|
* Input is comma-separated list of lines or line ranges.
|
|
|
|
*
|
2011-12-02 17:00:19 +00:00
|
|
|
* @param $arg string
|
2008-07-10 20:55:02 +00:00
|
|
|
* @return array of ints
|
|
|
|
*/
|
|
|
|
protected static function parseHighlightLines( $arg ) {
|
|
|
|
$lines = array();
|
|
|
|
$values = array_map( 'trim', explode( ',', $arg ) );
|
|
|
|
foreach ( $values as $value ) {
|
|
|
|
if ( ctype_digit($value) ) {
|
|
|
|
$lines[] = (int) $value;
|
|
|
|
} elseif ( strpos( $value, '-' ) !== false ) {
|
|
|
|
list( $start, $end ) = array_map( 'trim', explode( '-', $value ) );
|
|
|
|
if ( self::validHighlightRange( $start, $end ) ) {
|
|
|
|
for ($i = intval( $start ); $i <= $end; $i++ ) {
|
|
|
|
$lines[] = $i;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wfDebugLog( 'geshi', "Invalid range: $value\n" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wfDebugLog( 'geshi', "Invalid line: $value\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $lines;
|
|
|
|
}
|
2010-08-09 06:56:38 +00:00
|
|
|
|
2008-07-10 20:55:02 +00:00
|
|
|
/**
|
|
|
|
* Validate a provided input range
|
2011-12-02 17:00:19 +00:00
|
|
|
* @param $start
|
|
|
|
* @param $end
|
|
|
|
* @return bool
|
2008-07-10 20:55:02 +00:00
|
|
|
*/
|
2008-07-11 18:12:48 +00:00
|
|
|
protected static function validHighlightRange( $start, $end ) {
|
2008-07-10 20:55:02 +00:00
|
|
|
// Since we're taking this tiny range and producing a an
|
|
|
|
// array of every integer between them, it would be trivial
|
|
|
|
// to DoS the system by asking for a huge range.
|
|
|
|
// Impose an arbitrary limit on the number of lines in a
|
|
|
|
// given range to reduce the impact.
|
|
|
|
$arbitrarilyLargeConstant = 10000;
|
|
|
|
return
|
|
|
|
ctype_digit($start) &&
|
|
|
|
ctype_digit($end) &&
|
|
|
|
$start > 0 &&
|
|
|
|
$start < $end &&
|
|
|
|
$end - $start < $arbitrarilyLargeConstant;
|
|
|
|
}
|
2008-01-11 09:05:18 +00:00
|
|
|
|
2011-12-02 17:00:19 +00:00
|
|
|
/**
|
|
|
|
* @param $args array
|
|
|
|
* @return int
|
|
|
|
*/
|
2008-09-28 15:30:45 +00:00
|
|
|
static function getEncloseType( $args ) {
|
|
|
|
// "Enclose" parameter
|
2014-05-05 10:32:56 +00:00
|
|
|
$enclose = GESHI_HEADER_PRE_VALID;
|
2008-09-28 15:30:45 +00:00
|
|
|
if ( isset( $args['enclose'] ) ) {
|
|
|
|
if ( $args['enclose'] === 'div' ) {
|
|
|
|
$enclose = GESHI_HEADER_DIV;
|
|
|
|
} elseif ( $args['enclose'] === 'none' ) {
|
|
|
|
$enclose = GESHI_HEADER_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $enclose;
|
|
|
|
}
|
|
|
|
|
2012-10-16 10:33:26 +00:00
|
|
|
/**
|
|
|
|
* Hook into Content::getParserOutput to provide syntax highlighting for
|
|
|
|
* script content.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since MW 1.21
|
|
|
|
*/
|
|
|
|
public static function renderHook( Content $content, Title $title,
|
2014-05-10 21:28:07 +00:00
|
|
|
$revId, ParserOptions $options, $generateHtml, ParserOutput &$output
|
2012-10-16 10:33:26 +00:00
|
|
|
) {
|
|
|
|
|
2014-07-30 19:09:05 +00:00
|
|
|
global $wgSyntaxHighlightModels, $wgUseSiteCss,
|
|
|
|
$wgParser, $wgTextModelsToParse;
|
2012-10-16 10:33:26 +00:00
|
|
|
|
|
|
|
// Determine the language
|
|
|
|
$model = $content->getModel();
|
|
|
|
if ( !isset( $wgSyntaxHighlightModels[$model] ) ) {
|
|
|
|
// We don't care about this model, carry on.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$generateHtml ) {
|
2014-07-30 19:09:05 +00:00
|
|
|
// Nothing special for us to do, let MediaWiki handle this.
|
|
|
|
return true;
|
2012-10-16 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hope that $wgSyntaxHighlightModels does not contain silly types.
|
2014-09-15 04:50:20 +00:00
|
|
|
$text = ContentHandler::getContentText( $content );
|
2012-10-16 10:33:26 +00:00
|
|
|
|
|
|
|
if ( $text === null || $text === false ) {
|
2014-07-30 19:09:05 +00:00
|
|
|
// Oops! Non-text content? Let MediaWiki handle this.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse using the standard parser to get links etc. into the database, HTML is replaced below.
|
|
|
|
// We could do this using $content->fillParserOutput(), but alas it is 'protected'.
|
|
|
|
if ( $content instanceof TextContent && in_array( $model, $wgTextModelsToParse ) ) {
|
|
|
|
$output = $wgParser->parse( $text, $title, $options, true, true, $revId );
|
2012-10-16 10:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$lang = $wgSyntaxHighlightModels[$model];
|
|
|
|
|
|
|
|
// Attempt to format
|
|
|
|
$geshi = self::prepare( $text, $lang );
|
|
|
|
if( $geshi instanceof GeSHi ) {
|
|
|
|
|
|
|
|
$out = $geshi->parse_code();
|
|
|
|
if( !$geshi->error() ) {
|
|
|
|
// Done
|
2014-04-16 20:23:12 +00:00
|
|
|
$output->addModuleStyles( "ext.geshi.language.$lang" );
|
2012-10-16 10:33:26 +00:00
|
|
|
$output->setText( "<div dir=\"ltr\">{$out}</div>" );
|
|
|
|
|
|
|
|
if( $wgUseSiteCss ) {
|
|
|
|
$output->addModuleStyles( 'ext.geshi.local' );
|
2014-09-17 19:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inform MediaWiki that we have parsed this page and it shouldn't mess with it.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottle out
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook to provide syntax highlighting for API pretty-printed output
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @param string $text
|
|
|
|
* @param string $mime
|
|
|
|
* @param string $format
|
|
|
|
* @since MW 1.24
|
|
|
|
*/
|
|
|
|
public static function apiFormatHighlight( IContextSource $context, $text, $mime, $format ) {
|
|
|
|
switch ( $mime ) {
|
|
|
|
case 'text/javascript':
|
|
|
|
case 'application/json':
|
|
|
|
$lang = 'javascript';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'text/xml':
|
|
|
|
$lang = 'xml';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Don't know how to handle this
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$geshi = self::prepare( $text, $lang );
|
|
|
|
if( $geshi instanceof GeSHi ) {
|
|
|
|
$out = $geshi->parse_code();
|
|
|
|
if( !$geshi->error() ) {
|
|
|
|
$output = $context->getOutput();
|
2014-10-13 23:35:49 +00:00
|
|
|
$output->addModuleStyles( array( "ext.geshi.language.$lang", 'ext.geshi.local' ) );
|
2014-09-17 19:32:35 +00:00
|
|
|
$output->addHTML( "<div dir=\"ltr\">{$out}</div>" );
|
2014-07-30 19:09:05 +00:00
|
|
|
|
|
|
|
// Inform MediaWiki that we have parsed this page and it shouldn't mess with it.
|
2012-10-16 10:33:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bottle out
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2011-08-14 06:59:35 +00:00
|
|
|
public static function prepare( $text, $lang ) {
|
2013-09-05 16:31:53 +00:00
|
|
|
|
|
|
|
global $wgSyntaxHighlightKeywordLinks;
|
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
self::initialise();
|
|
|
|
$geshi = new GeSHi( $text, $lang );
|
2010-08-09 06:56:38 +00:00
|
|
|
if( $geshi->error() == GESHI_ERROR_NO_SUCH_LANG ) {
|
2007-06-29 04:33:14 +00:00
|
|
|
return null;
|
2010-08-09 06:56:38 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
$geshi->set_encoding( 'UTF-8' );
|
|
|
|
$geshi->enable_classes();
|
|
|
|
$geshi->set_overall_class( "source-$lang" );
|
2013-09-05 16:31:53 +00:00
|
|
|
$geshi->enable_keyword_links( $wgSyntaxHighlightKeywordLinks );
|
2013-02-20 10:22:46 +00:00
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-13 20:15:37 +00:00
|
|
|
/**
|
2014-05-04 20:50:14 +00:00
|
|
|
* 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.
|
2013-02-13 20:15:37 +00:00
|
|
|
*
|
|
|
|
* The hack is documented in MediaWiki core under
|
|
|
|
* docs/uidesign/monospace.html and in bug 33496.
|
|
|
|
*/
|
2014-05-04 20:50:14 +00:00
|
|
|
// 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;',
|
|
|
|
/* preserve defaults = */ true
|
|
|
|
);
|
|
|
|
|
|
|
|
// No need to preserve default (which is just "font-family: monospace;")
|
|
|
|
// outputting both is unnecessary
|
|
|
|
$geshi->set_overall_style(
|
|
|
|
'font-family: monospace, monospace;',
|
|
|
|
/* preserve defaults = */ false
|
|
|
|
);
|
2013-02-13 20:15:37 +00:00
|
|
|
|
2014-04-16 20:23:12 +00:00
|
|
|
return $geshi;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepare a CSS snippet suitable for use as a ParserOutput/OutputPage
|
|
|
|
* head item.
|
|
|
|
*
|
2014-05-10 21:28:07 +00:00
|
|
|
* Not used anymore, kept for backwards-compatibility with other extensions.
|
|
|
|
*
|
2014-04-16 20:23:12 +00:00
|
|
|
* @deprecated
|
|
|
|
* @param GeSHi $geshi
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function buildHeadItem( $geshi ) {
|
2014-05-10 21:28:07 +00:00
|
|
|
wfDeprecated( __METHOD__ );
|
2011-01-23 09:38:33 +00:00
|
|
|
$css = array();
|
2007-06-29 04:33:14 +00:00
|
|
|
$css[] = '<style type="text/css">/*<![CDATA[*/';
|
2014-04-16 20:23:12 +00:00
|
|
|
$css[] = self::getCSS( $geshi );
|
|
|
|
$css[] = '/*]]>*/';
|
|
|
|
$css[] = '</style>';
|
|
|
|
return implode( "\n", $css );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the complete CSS code necessary to display styles for given GeSHi instance.
|
|
|
|
*
|
|
|
|
* @param GeSHi $geshi
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getCSS( $geshi ) {
|
|
|
|
$lang = $geshi->language;
|
|
|
|
$css = array();
|
2007-07-18 21:43:54 +00:00
|
|
|
$css[] = ".source-$lang {line-height: normal;}";
|
2008-07-11 18:12:48 +00:00
|
|
|
$css[] = ".source-$lang li, .source-$lang pre {";
|
|
|
|
$css[] = "\tline-height: normal; border: 0px none white;";
|
|
|
|
$css[] = "}";
|
2014-04-16 20:23:12 +00:00
|
|
|
$css[] = $geshi->get_stylesheet( /*$economy_mode*/ false );
|
2007-06-29 04:33:14 +00:00
|
|
|
return implode( "\n", $css );
|
|
|
|
}
|
2008-01-11 09:05:18 +00:00
|
|
|
|
2011-05-13 13:27:29 +00:00
|
|
|
/**
|
|
|
|
* Format an 'unknown language' error message and append formatted
|
|
|
|
* plain text to it.
|
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return string HTML fragment
|
|
|
|
*/
|
|
|
|
private static function formatLanguageError( $text ) {
|
2012-08-16 20:43:42 +00:00
|
|
|
$msg = wfMessage( 'syntaxhighlight-err-language' )->inContentLanguage()->escaped();
|
|
|
|
$error = self::formatError( $msg, $text );
|
2011-05-13 13:27:29 +00:00
|
|
|
return $error . '<pre>' . htmlspecialchars( $text ) . '</pre>';
|
|
|
|
}
|
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
/**
|
|
|
|
* Format an error message
|
|
|
|
*
|
|
|
|
* @param string $error
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function formatError( $error = '' ) {
|
|
|
|
$html = '';
|
2010-08-09 06:56:38 +00:00
|
|
|
if( $error ) {
|
2007-06-29 04:33:14 +00:00
|
|
|
$html .= "<p>{$error}</p>";
|
2010-08-09 06:56:38 +00:00
|
|
|
}
|
2012-08-16 20:43:42 +00:00
|
|
|
$html .= '<p>' . wfMessage( 'syntaxhighlight-specify')->inContentLanguage()->escaped()
|
2008-11-21 08:41:06 +00:00
|
|
|
. ' <samp><source lang="html4strict">...</source></samp></p>'
|
2012-08-16 20:43:42 +00:00
|
|
|
. '<p>' . wfMessage( 'syntaxhighlight-supported' )->inContentLanguage()->escaped()
|
|
|
|
. '</p>' . self::formatLanguages();
|
2007-06-29 04:33:14 +00:00
|
|
|
return "<div style=\"border: solid red 1px; padding: .5em;\">{$html}</div>";
|
|
|
|
}
|
2008-01-11 09:05:18 +00:00
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
/**
|
|
|
|
* Format the list of supported languages
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private static function formatLanguages() {
|
|
|
|
$langs = self::getSupportedLanguages();
|
|
|
|
$list = array();
|
|
|
|
if( count( $langs ) > 0 ) {
|
2007-07-13 18:07:52 +00:00
|
|
|
foreach( $langs as $lang ) {
|
|
|
|
$list[] = '<samp>' . htmlspecialchars( $lang ) . '</samp>';
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
2011-02-25 16:38:23 +00:00
|
|
|
return '<p class="mw-collapsible mw-collapsed" style="padding: 0em 1em;">' . implode( ', ', $list ) . '</p><br style="clear: all"/>';
|
2007-06-29 04:33:14 +00:00
|
|
|
} else {
|
2012-08-16 20:43:42 +00:00
|
|
|
return '<p>' . wfMessage( 'syntaxhighlight-err-loading' )->inContentLanguage()->escaped() . '</p>';
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
|
|
|
}
|
2008-01-11 09:05:18 +00:00
|
|
|
|
2007-06-29 04:33:14 +00:00
|
|
|
/**
|
|
|
|
* Get the list of supported languages
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function getSupportedLanguages() {
|
2014-11-19 00:32:46 +00:00
|
|
|
global $wgGeSHiSupportedLanguages;
|
|
|
|
return $wgGeSHiSupportedLanguages;
|
2007-06-29 04:33:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialise messages and ensure the GeSHi class is loaded
|
2011-12-02 17:00:19 +00:00
|
|
|
* @return bool
|
2007-06-29 04:33:14 +00:00
|
|
|
*/
|
|
|
|
private static function initialise() {
|
|
|
|
if( !self::$initialised ) {
|
2010-08-09 06:56:38 +00:00
|
|
|
if( !class_exists( 'GeSHi' ) ) {
|
2013-01-21 18:26:05 +00:00
|
|
|
require( dirname( __FILE__ ) . '/geshi/geshi.php' );
|
2010-08-09 06:56:38 +00:00
|
|
|
}
|
2007-06-29 04:33:14 +00:00
|
|
|
self::$initialised = true;
|
|
|
|
}
|
2007-07-21 01:26:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-07-01 19:34:01 +00:00
|
|
|
|
2014-11-19 16:27:25 +00:00
|
|
|
/**
|
|
|
|
* Get the GeSHI's version information while Special:Version is read.
|
|
|
|
* @param $extensionTypes
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function extensionTypes( &$extensionTypes ) {
|
|
|
|
global $wgExtensionCredits;
|
|
|
|
self::initialise();
|
|
|
|
$wgExtensionCredits['parserhook']['SyntaxHighlight_GeSHi']['version'] = GESHI_VERSION;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-16 20:23:12 +00:00
|
|
|
/**
|
|
|
|
* Register a ResourceLoader module providing styles for each supported language.
|
|
|
|
*
|
|
|
|
* @param ResourceLoader $resourceLoader
|
|
|
|
* @return bool true
|
|
|
|
*/
|
|
|
|
public static function resourceLoaderRegisterModules( &$resourceLoader ) {
|
|
|
|
$modules = array();
|
|
|
|
|
|
|
|
foreach ( self::getSupportedLanguages() as $lang ) {
|
|
|
|
$modules["ext.geshi.language.$lang" ] = array(
|
|
|
|
'class' => 'ResourceLoaderGeSHiModule',
|
|
|
|
'lang' => $lang,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$resourceLoader->register( $modules );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-14 06:59:35 +00:00
|
|
|
}
|