Extension talk:IndentSections

From mediawiki.org

how to use this?

Save it to the LocalSettings.php on your wiki. Sections should be automatically indented from then on without any more work. —Anonymous DissidentTalk 11:52, 5 October 2008 (UTC)Reply

this extension only works if you have edit permissions, i had to do this to make it work without edit permissions:

--- IndentSections.php.orig	2008-11-10 20:36:39.000000000 +0100
+++ IndentSections.php	2008-11-10 20:36:32.000000000 +0100
@@ -40,12 +40,12 @@
 function fnIndentSectionsBeforePageDisplay(&$out) {
   $text =& $out->mBodytext;
   for($i = 6; $i > 1; $i -= 1){
-    $pattern = sprintf('/(<h%d> ?<span class="editsection">.*?)(<h[1-%d]>)/ms', $i, $i-1);
+    $pattern = sprintf('/(<h%d> ?<span class="(editsection|mw-headline)">.*?)(<h[1-%d]>)/ms', $i, $i-1);
     $text = preg_replace_callback($pattern, fnIndentSectionsCallback, $text);
   }
   return true;
 }
  
 function fnIndentSectionsCallback($matches){
-  return '<blockquote>' . $matches[1] . '</blockquote>' . $matches[2];
+  return '<blockquote>' . $matches[1] . '</blockquote>' . $matches[3];
 }

should i make these changes to the code?

The changes in question don't seem to work with the latest MediaWiki.

Extension - coding changes killed the wiki.[edit]

I followed instructions and after modifying the LocalSetting.php file (as instructed), my wiki will no longer load. I am given a message that reads, "Error, Setup.php must be included from the file scope, after DefaultSettings.php"

IndentSections is incompatible with "HeaderTabs"[edit]

IndentSections worked as advertised on basic pages, but when I tried to view a page that was using the Headertabs exention ala the "<headertabs />" the article text failed to display. The page loaded all the overhead like the banner, login tools, sidebar, footer, and even the category tags... it was just the main article content in the middle that was completely missing.

Unless there is a fix for this incompatibility to be issued relatively soon, I would actually like to consider hacking the core mediawiki code to insert the <blockquote> tags manually.

Can anyone tell me which file this simple change could be made?

OR - is there a way to modify the function so that it IGNORES the level 1 (e.g. <h1>) level... this seems to be the only level that the headertabs works on.

Otherewise, this is a great and much desired extension! thanks! -Rich (revansx)

Sections not followed by "higher" sections do not indent[edit]

I have noticed that sections, without a higher level section following it, do not get indented as expected.

Expected Example 1 Example 2 Example 3
Page Name (H1) Page Name (H1) Page Name (H1) Page Name (H1)
Main Section 1 (H2) Main Section 1 (H2) Main Section 1 (H2) Main Section 1 (H2)
Sub Section 1.1 (H3)
Sub Section 1.1 (H3)
Sub Section 1.1 (H3)
Sub Section 1.1 (H3)
Sub-Sub Section 1.1.1 (H4)
Sub-Sub Section 1.1.1 (H4)
Sub-Sub Section 1.1.1 (H4)
Sub-Sub Section 1.1.1 (H4)
Sub Section 1.2 (H3)
Sub Section 1.2 (H3)
Sub Section 1.2 (H3)
Sub Section 1.2 (H3)
Main Section 2 (H2) Main Section 2 (H2) Main Section 2 (H2) Main Section 2 (H2)
Sub Section 2.1 (H3)
Sub Section 2.1 (H3) Sub Section 2.1 (H3)
Sub Section 2.1 (H3)
Sub-Sub Section 2.1.1 (H4)
Sub-Sub Section 2.1.1 (H4)
Sub-Sub Section 2.1.1 (H4)
Sub-Sub Section 2.1.1 (H4)
Sub Section 2.2 (H3)
Sub Section 2.2 (H3) Sub-Sub Section 2.1.2 (H4) Main Section 3 (H2)

I've tried to make the example as clear as possible - but ultimately any section that does not have a "higher" section following it does not get indented. Timothy.russ (talk) 00:13, 1 November 2012 (UTC)Reply

I have the same problem, plus another one: on the text written after the Sub-Sub Section H4 (see example above) if i write

'

I found on the rendered page

\'

Denis


Single quotes not escaping correctly on output[edit]

After upgrading and migrating from Windows to Ubuntu, IndentSections output appears to be leaving backslashes in front of single quote characters in sections that get indented. I have isolated this to the extension through process of elimination though I cannot rule out a change in the regex handling between the Windows/Ubuntu MySQL/PHP implementations or a change in the wiki code itself between 1.18.5 and 1.19.2.

Is anyone else seeing this behavior?

MediaWiki 1.19.2 PHP 5.3.10-1ubuntu3.4 (apache2handler) MySQL 5.5.24-0ubuntu0.12.04.1 Timothy.russ (talk) 00:50, 1 November 2012 (UTC)Reply




Alternate Solutions[edit]

I believe I have two alternate solutions that solve most, if not all, of the issues mentioned by Timothy.russ above.

ini_set('pcre.backtrack_limit', 100000000); //default 1000000
ini_set('pcre.recursion_limit', 10000000); //default 100000

function fnIndentSectionsBeforePageDisplay(&$out) {
  $text =& $out->mBodytext;
  //Section content indented
  for($i = 6; $i > 1; $i -= 1){
    $pattern = sprintf('/(<h%d> *<span[^>]+class="(editsection|mw-headline)".+?<\/h%d>)(.+?)(?=(<h[0-%d].*|$))/si', $i, $i, $i);
    $replacement = "$1\n<div style=\"margin-left:20px\">\n$3\n</div>\n";
    $temp = preg_replace($pattern, $replacement, $text);
    if(is_null($temp)){
        $error = preg_last_error();
        error_log("PREG_ERRORS: ".($error & PREG_INTERNAL_ERROR ? 'PREG_INTERNAL_ERROR, ' : '').
                                  ($error & PREG_BACKTRACK_LIMIT_ERROR ? 'PREG_BACKTRACK_LIMIT_ERROR, ' : '').
                                  ($error & PREG_RECURSION_LIMIT_ERROR ? 'PREG_RECURSION_LIMIT_ERROR, ' : '').
                                  ($error & PREG_BAD_UTF8_ERROR ? 'PREG_BAD_UTF8_ERROR, ' : '').
                                  ($error & PREG_BAD_UTF8_OFFSET_ERROR ? 'PREG_BAD_UTF8_OFFSET_ERROR' : ''));
    }else{
        $text = $temp;
    }
  }

  //Section indented
  /*for($i = 6; $i > 2; $i -= 1){
    $pattern = sprintf('/(<h%d> *<span[^>]+class="(editsection|mw-headline)".+?<\/h%d>.+?)(?=(<h[0-%d].*|$))/si', $i, $i, $i-1);
    $replacement = "\n<div style=\"margin-left:20px\">\n$1\n</div>\n";
    $temp = preg_replace($pattern, $replacement, $text);
    if(is_null($temp)){
        $error = preg_last_error();
        error_log("PREG_ERRORS: ".($error & PREG_INTERNAL_ERROR ? 'PREG_INTERNAL_ERROR, ' : '').
                                  ($error & PREG_BACKTRACK_LIMIT_ERROR ? 'PREG_BACKTRACK_LIMIT_ERROR, ' : '').
                                  ($error & PREG_RECURSION_LIMIT_ERROR ? 'PREG_RECURSION_LIMIT_ERROR, ' : '').
                                  ($error & PREG_BAD_UTF8_ERROR ? 'PREG_BAD_UTF8_ERROR, ' : '').
                                  ($error & PREG_BAD_UTF8_OFFSET_ERROR ? 'PREG_BAD_UTF8_OFFSET_ERROR' : ''));
    }else{
        $text = $temp;
    }
  }*/
  return true;
}

This function should replace the function shown above and the one on the main page. You'll notice there are two loops. The first indents the content of each section leaving the header hanging. The second indents the entire section (like the original). Comment out which ever one you don't want to use.

You'll also notice that I didn't use blockquotes. I liked the formating better this way. You could easily give the div a class and style it using CSS which would give you more flexibility than using the blockquotes.

Enjoy.


I like this solution - not using the blockquotes has the added benefit of maintaining a consistent right margin. I've also confirmed this does eliminate the unclosed indentaion issue I was seeing before.
Any idea what may be causing the issues escaping those slashes in the regex?
EDIT It appears this also fixed the escaping issue. Not sure why it didn't seem to immediately.
Shame you didn't sig your post, now I can't be sure who to get a Christmas Present for.Timothy.russ (talk) 19:44, 20 December 2012 (UTC)Reply
Since I wrote this alternate solution many months ago I've discovered a bug. When the page gets large enough it is possible that the preg_replace will fail (return null), thus blanking out your page content. The failure is due to the pcre.backtrack_limit and/or pcre.recursion_limit being met. I've over-written the code above with a solution. Basically, I just increased the limits and added code to handle any failure in a way that doesn't blank out the page content. Depending on your setup you may or may not want to increase the limits. Pantsmann (talk) 21:37, 24 June 2013 (UTC)Reply


And here's a simple solution that really works[edit]

$wgHooks['ContentGetParserOutput'][] = 'onContentGetParserOutput'; 

function onContentGetParserOutput(Content $content, Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output) {
  $text = ContentHandler::getContentText($content);
  if (!$text) return true;
  $curLevel = 1;
  $text = preg_replace_callback('/^(?:(==+).+\1)\s*\n/m', function($m) use(&$curLevel){
        $lvl = strlen($m[1]);  
        if ($lvl == $curLevel) return $m[0]; 
        $bqs = ($lvl > $curLevel ? '<blockquote>' : '</blockquote>');
        $nbq = abs($lvl - $curLevel); 
        $curLevel = $lvl;
        return str_repeat($bqs, $nbq)."\n".$m[0];
      }, $text);
  if ($curLevel > 1) $text .= "\n".str_repeat('</blockquote>', $curLevel - 1)."\n";
  
  global $wgParser, $wgTextModelsToParse; 
  $extension = ExtensionRegistry::getInstance();
  $models = $extension->getAttribute( 'SyntaxHighlightModels' );
  $model = $content->getModel();   
  if ( !$content instanceof TextContent or !in_array( $model, $wgTextModelsToParse ) ) 
      return true; // Let Wiki handle this
  $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
  return false; // Wiki shouldn't parse anymore 
}

I'll be glad to your comments.
I am a beginner, and I have not found another way to add <blockquote> to wikiText. But perhaps it exists.
Axdr (talk) 01:21, 10 January 2019 (UTC)Reply