Site:Developer stuff/Transclusions/lsth.php
From Feast upon the Word (http://feastupontheword.org). Copyright, Feast upon the Word.
< Site:Developer stuff | Transclusions
Revision as of 01:32, 10 October 2007 by Matthewfaulconer (Talk | contribs)
<?php
if ( ! defined( 'MEDIAWIKI' ) )
die();
/**#@+
*
* A parser extension that further extends labeled section transclusion,
* adding a function, #lsth for transcluding marked sections of text,
*
* This calls internal functions from lst.php. It will not work if that
* extension is not enabled, and may not work if the two files are not in
* sync.
*
* @addtogroup Extensions
*
* @link http://www.mediawiki.org/wiki/Extension:Labeled_Section_Transclusion Documentation
*
* @author Steve Sanbeg
* @copyright Copyright © 2006, Steve Sanbeg
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*/
##
# Standard initialisation code
##
$wgExtensionFunctions[]="wfLabeledSectionTransclusionHeading";
$wgHooks['LanguageGetMagic'][] = 'wfLabeledSectionTransclusionHeadingMagic';
$wgParserTestFiles[] = dirname( __FILE__ ) . "/lsthParserTests.txt";
function wfLabeledSectionTransclusionHeading()
{
global $wgParser;
$wgParser->setFunctionHook( 'lsth', 'wfLstIncludeHeading' );
}
function wfLabeledSectionTransclusionHeadingMagic( &$magicWords, $langCode ) {
// Add the magic words
$magicWords['lsth'] = array( 0, 'lsth', 'section-h');
return true;
}
///section inclusion - include all matching sections
function wfLstIncludeHeading($parser, $page='', $sec='', $to='', $length='')
{
if (wfLst_text_($parser, $page, $title, $text) == false)
return $text;
//Generate a regex to match the === classical heading section(s) === we're
//interested in.
if ($sec == '') {
$begin_off = 0;
$head_len = 6;
} else {
$pat = '^(={1,6})\s*' . preg_quote($sec, '/') . '\s*\1\s*($)' ;
if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE) ) {
$begin_off = $m[0][1]; //By changing from $m[2][1] to $m[0][1] we capture the heading as part of the included text.
//head_len tracks the level of the heading. E.g. for ==heading== head_len=2 ; ===heading=== head_len=3
$head_len = strlen($m[1][0]);
//wfDebug( "LSTH: offset is $begin_off" );
} else {
//wfDebug( "LSTH: match failed: '$pat'" );
return '';
}
}
if ($to != '') {
//if $to is supplied, try and match it. If we don't match, just
//ignore it.
$pat = '^(={1,6})\s*' . preg_quote($to, '/') . '\s*\1\s*$';
if (preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off))
$end_off = $m[0][1]-1;
}
if (! isset($end_off)) {
$pat = '^(={1,'.$head_len.'})(?!=).*?\1\s*$';
if (preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off))
$end_off = $m[0][1]-1;
else
wfDebug("LSTH: fail end match: '$pat'");
//wfDebug("LSTH:head len is $head_len, pat is $pat, head is '.$m[1][0]'";
}
$nhead = wfLst_count_headings_($text, $begin_off);
wfDebug( "LSTH: head offset = $nhead" );
if (isset($end_off))
$result = shorten(substr($text, $begin_off, $end_off - $begin_off), $length);
else
$result = shorten(substr($text, $begin_off), $length);
return wfLst_parse_($parser,$title,$result, "#lsth:${page}|${sec}", $nhead);
}
function shorten($text, $length)
{
if ($length != '')
$text=implode('
',array_slice(explode("\n",$text),0,$length));
return $text;
}