Topic on Project:Support desk

Changing the structure of page’s table of contents

4
ZelChief (talkcontribs)

I have encountered following problem – how can we make the Mediawiki to add a dot in TOC numeration after each digit? Explanation: now the standard numeration of headings looks like:

1 Text
2 Text
  2.1 Text
  2.2 Text

And we need

1. Text
2. Text
  2.1.    Text
  2.2.    Text

If you have any ideas about solving this problem, I would be very grateful to you for any help. The task is actual because all the contents in books have dots in numeration.

Bawolff (talkcontribs)

I can't think of a good way to do this (like you could hack the source, the relavent code is in includes/Linker.php , the method tocLine of Linker class [or at least that's what it is in trunk].)

ZelChief (talkcontribs)

Thank you very much! In file includes/Linker.php I change such function

 function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
                $classes = "toclevel-$level";
                if ( $sectionIndex !== false )
                        $classes .= " tocsection-$sectionIndex";
                return "\n<li class=\"$classes\"><a href=\"#" .
                        $anchor . '"><span class="tocnumber">' .
                        $tocnumber . '</span> <span class="toctext">' .
                        $tocline . '</span></a>';
        }

to

 function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
                $tocnumber = "$tocnumber. ";
                $classes = "toclevel-$level";
                if ( $sectionIndex !== false )
                        $classes .= " tocsection-$sectionIndex";
                return "\n<li class=\"$classes\"><a href=\"#" .
                        $anchor . '"><span class="tocnumber">' .
                        $tocnumber . '</span> <span class="toctext">' .
                        $tocline . '</span></a>';
        }

As a result it works and does not even conflict with Extension:PSINoTocNum. Now I'm trying to add a dot in the numbering of sections, which I do with Extension:MagicNumberedHeadings. Where created the text of the header? Any ideas?

ZelChief (talkcontribs)

I solve my problem =) So, in file /includes/parser/Parser.php I change such function

function formatHeadings( $text, $origText, $isMain=true )

Then, after such code

			# count number of headlines for each level
			@$sublevelCount[$toclevel]++;
			$dot = 0;
			for( $i = 1; $i <= $toclevel; $i++ ) {
				if ( !empty( $sublevelCount[$i] ) ) {
					if ( $dot ) {
						$numbering .= '.';
					}
					$numbering .= $wgContLang->formatNum( $sublevelCount[$i] );
					$dot = 1;
				}
			}

I add this

$numbering .= '.';

And ... it works.

Reply to "Changing the structure of page’s table of contents"