Difference between revisions of "Site:Developer stuff/Transclusions/lsth.php"

From Feast upon the Word (http://feastupontheword.org). Copyright, Feast upon the Word.
Jump to: navigation, search
m (Original content of file)
 
(changes)
Line 4: Line 4:
 
if ( ! defined( 'MEDIAWIKI' ) )
 
if ( ! defined( 'MEDIAWIKI' ) )
 
die();
 
die();
 
 
/**#@+  
 
/**#@+  
 
  *
 
  *
Line 22: Line 21:
 
  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 
  * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 
  */
 
  */
 
 
##
 
##
 
# Standard initialisation code
 
# Standard initialisation code
 
##
 
##
 
 
$wgExtensionFunctions[]="wfLabeledSectionTransclusionHeading";
 
$wgExtensionFunctions[]="wfLabeledSectionTransclusionHeading";
 
$wgHooks['LanguageGetMagic'][] = 'wfLabeledSectionTransclusionHeadingMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfLabeledSectionTransclusionHeadingMagic';
 
$wgParserTestFiles[] = dirname( __FILE__ ) . "/lsthParserTests.txt";
 
$wgParserTestFiles[] = dirname( __FILE__ ) . "/lsthParserTests.txt";
 
 
function wfLabeledSectionTransclusionHeading()  
 
function wfLabeledSectionTransclusionHeading()  
 
{
 
{
Line 36: Line 32:
 
   $wgParser->setFunctionHook( 'lsth', 'wfLstIncludeHeading' );
 
   $wgParser->setFunctionHook( 'lsth', 'wfLstIncludeHeading' );
 
}
 
}
 
 
function wfLabeledSectionTransclusionHeadingMagic( &$magicWords, $langCode ) {
 
function wfLabeledSectionTransclusionHeadingMagic( &$magicWords, $langCode ) {
 
   // Add the magic words
 
   // Add the magic words
   $magicWords['lsth'] = array( 0, 'lsth', 'section-h' );
+
   $magicWords['lsth'] = array( 0, 'lsth', 'section-h');
 
   return true;
 
   return true;
 
}
 
}
 
 
///section inclusion - include all matching sections
 
///section inclusion - include all matching sections
function wfLstIncludeHeading($parser, $page='', $sec='', $to='')
+
function wfLstIncludeHeading($parser, $page='', $sec='', $to='', $length='')
 
{
 
{
 
   if (wfLst_text_($parser, $page, $title, $text) == false)
 
   if (wfLst_text_($parser, $page, $title, $text) == false)
 
     return $text;
 
     return $text;
 
 
   //Generate a regex to match the === classical heading section(s) === we're
 
   //Generate a regex to match the === classical heading section(s) === we're
 
   //interested in.
 
   //interested in.
Line 66: Line 59:
 
      
 
      
 
   }
 
   }
 
 
   if ($to != '') {
 
   if ($to != '') {
 
     //if $to is supplied, try and match it.  If we don't match, just
 
     //if $to is supplied, try and match it.  If we don't match, just
Line 74: Line 66:
 
       $end_off = $m[0][1]-1;
 
       $end_off = $m[0][1]-1;
 
   }
 
   }
 
 
 
   if (! isset($end_off)) {
 
   if (! isset($end_off)) {
 
     $pat = '^(={1,'.$head_len.'})(?!=).*?\1\s*$';
 
     $pat = '^(={1,'.$head_len.'})(?!=).*?\1\s*$';
Line 82: Line 72:
 
     else  
 
     else  
 
       wfDebug("LSTH: fail end match: '$pat'");
 
       wfDebug("LSTH: fail end match: '$pat'");
 
 
     //wfDebug("LSTH:head len is $head_len, pat is $pat, head is '.$m[1][0]'";
 
     //wfDebug("LSTH:head len is $head_len, pat is $pat, head is '.$m[1][0]'";
 
   }  
 
   }  
 
 
   $nhead = wfLst_count_headings_($text, $begin_off);
 
   $nhead = wfLst_count_headings_($text, $begin_off);
 
   wfDebug( "LSTH: head offset = $nhead" );
 
   wfDebug( "LSTH: head offset = $nhead" );
 
 
   if (isset($end_off))
 
   if (isset($end_off))
     $result = substr($text, $begin_off, $end_off - $begin_off);
+
     $result = shorten(substr($text, $begin_off, $end_off - $begin_off), $length);
 
   else
 
   else
     $result = substr($text, $begin_off);
+
     $result = shorten(substr($text, $begin_off), $length) ;
 
    
 
    
 
   return wfLst_parse_($parser,$title,$result, "#lsth:${page}|${sec}", $nhead);
 
   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;
 
}
 
}
  
 
</nowiki></pre>
 
</nowiki></pre>

Revision as of 03:14, 9 October 2007


<?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[2][1];
      $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;
}