2006-06-23 21:22:25 +00:00
|
|
|
<?php
|
|
|
|
# MediaWiki Poem extension v1.0cis
|
|
|
|
#
|
|
|
|
# Based on example code from
|
2009-12-05 15:03:20 +00:00
|
|
|
# http://www.mediawiki.org/wiki/Manual:Extending_wiki_markup
|
2006-06-23 21:22:25 +00:00
|
|
|
#
|
|
|
|
# All other code is copyright © 2005 Nikola Smolenski <smolensk@eunet.yu>
|
|
|
|
# (with modified parser callback and attribute additions)
|
|
|
|
#
|
|
|
|
# Anyone is allowed to use this code for any purpose.
|
2007-12-16 18:27:23 +00:00
|
|
|
#
|
2006-06-23 21:22:25 +00:00
|
|
|
# To install, copy the extension to your extensions directory and add line
|
|
|
|
# include("extensions/Poem.php");
|
|
|
|
# to the bottom of your LocalSettings.php
|
|
|
|
#
|
|
|
|
# To use, put some text between <poem></poem> tags
|
|
|
|
#
|
|
|
|
# For more information see its page at
|
2009-12-05 15:03:20 +00:00
|
|
|
# http://www.mediawiki.org/wiki/Extension:Poem
|
2006-06-23 21:22:25 +00:00
|
|
|
|
2009-09-04 22:22:12 +00:00
|
|
|
$wgHooks['ParserFirstCallInit'][] = 'wfPoemExtension';
|
2006-10-29 21:41:25 +00:00
|
|
|
$wgExtensionCredits['parserhook'][] = array(
|
2009-04-27 04:45:10 +00:00
|
|
|
'path' => __FILE__,
|
2008-02-18 18:42:20 +00:00
|
|
|
'name' => 'Poem',
|
|
|
|
'author' => array( 'Nikola Smolenski', 'Brion Vibber', 'Steve Sanbeg' ),
|
|
|
|
'url' => 'http://www.mediawiki.org/wiki/Extension:Poem',
|
|
|
|
'descriptionmsg' => 'poem-desc',
|
2006-10-29 21:41:25 +00:00
|
|
|
);
|
2008-11-30 03:15:22 +00:00
|
|
|
$wgParserTestFiles[] = dirname( __FILE__ ) . "/poemParserTests.txt";
|
|
|
|
$wgExtensionMessagesFiles['Poem'] = dirname(__FILE__) . '/Poem.i18n.php';
|
2006-06-23 21:22:25 +00:00
|
|
|
|
2009-09-04 22:22:12 +00:00
|
|
|
function wfPoemExtension( &$parser ) {
|
2011-06-07 09:45:41 +00:00
|
|
|
$parser->setHook( 'poem', 'wfRenderPoemTag' );
|
2008-06-06 20:38:04 +00:00
|
|
|
return true;
|
2006-06-23 21:22:25 +00:00
|
|
|
}
|
|
|
|
|
2011-01-23 10:33:37 +00:00
|
|
|
/**
|
|
|
|
* @param $in
|
|
|
|
* @param array $param
|
|
|
|
* @param $parser Parser
|
|
|
|
* @param bool $frame
|
|
|
|
* @return string
|
|
|
|
*/
|
2011-06-07 09:45:41 +00:00
|
|
|
function wfRenderPoemTag( $in, $param=array(), $parser=null, $frame=false ) {
|
2006-12-14 21:40:24 +00:00
|
|
|
|
2008-02-20 23:08:49 +00:00
|
|
|
/* using newlines in the text will cause the parser to add <p> tags,
|
2011-06-07 09:45:41 +00:00
|
|
|
* which may not be desired in some cases
|
2008-02-20 23:08:49 +00:00
|
|
|
*/
|
2008-03-26 20:58:20 +00:00
|
|
|
$nl = isset( $param['compact'] ) ? '' : "\n";
|
2011-06-07 09:45:41 +00:00
|
|
|
|
2011-02-10 01:50:15 +00:00
|
|
|
$tag = $parser->insertStripItem( "<br />", $parser->mStripState );
|
2011-06-07 09:45:41 +00:00
|
|
|
|
2011-02-10 01:50:15 +00:00
|
|
|
$text = preg_replace(
|
2011-06-07 09:45:41 +00:00
|
|
|
array( "/^\n/", "/\n$/D", "/\n/" ),
|
|
|
|
array( "", "", "$tag\n" ),
|
2011-02-10 01:50:15 +00:00
|
|
|
$in );
|
2011-06-07 09:45:41 +00:00
|
|
|
$text = preg_replace_callback( '/^( +)/m', 'wfPoemReplaceSpaces', $text );
|
|
|
|
$text = $parser->recursiveTagParse( $text, $frame );
|
2008-02-18 18:42:20 +00:00
|
|
|
|
2010-01-16 16:28:06 +00:00
|
|
|
$attribs = Sanitizer::validateTagAttributes( $param, 'div' );
|
2008-02-18 18:42:20 +00:00
|
|
|
|
2006-06-23 21:22:25 +00:00
|
|
|
// Wrap output in a <div> with "poem" class.
|
|
|
|
if( isset( $attribs['class'] ) ) {
|
|
|
|
$attribs['class'] = 'poem ' . $attribs['class'];
|
|
|
|
} else {
|
|
|
|
$attribs['class'] = 'poem';
|
|
|
|
}
|
2008-02-18 18:42:20 +00:00
|
|
|
|
2011-06-07 09:47:46 +00:00
|
|
|
return Html::rawElement( 'div', $attribs, $nl . trim( $text ) . $nl );
|
2011-06-07 09:45:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for preg_replace_callback()
|
|
|
|
*/
|
|
|
|
function wfPoemReplaceSpaces( $m ) {
|
|
|
|
return str_replace( ' ', ' ', $m[1] );
|
2006-06-23 21:22:25 +00:00
|
|
|
}
|