Topic on Project:Support desk

How to customize auto-numbering of TOCs

7
PatPeter (talkcontribs)

I read the manual, but neither it nor any extension told me or does what I want to do. I want to customize the Table of Contents so that it uses a different numbering scheme. For instance, instead of decimal/decimal/decimal/decimal/decimal, something like upper-roman/upper-latin/decimal/lower-latin/lower-roman (one of Microsoft Word's default lists), etc. or any other combination of my choosing based on list-style-type, even if I wanted Hebrew or Hiragana.

Wargo (talkcontribs)
Bawolff (talkcontribs)

Due to the way the TOC is currently programmed, its kind of cumbersome to customize the numbering scheme of the ToC. The numbers are rather hardcoded in Linker::tocLine which is unfortunate.

What you can do is add some css like follows to your mediawiki:Common.css (haven't tested much, you may need to play with it a little bit)

html #toc ul, .toc ul {
list-style-image:none;
list-style-position:inside;
list-style-type:armenian /*replace this with the type you want*/;
}

span.tocnumber {
display:none;
} 
PatPeter (talkcontribs)

Thanks for pointing me to the function's location. I just rewrote the function for my wiki:

	// http://www.go4expert.com/forums/showthread.php?t=4948
	private static function numberToRoman($num) {
		$n = intval($num);
		$result = '';

		$romans = array(
			'M' => 1000, 
			'CM' => 900, 
			'D' => 500, 
			'CD' => 400,
			'C' => 100, 
			'XC' => 90, 
			'L' => 50, 
			'XL' => 40,
			'X' => 10, 
			'IX' => 9, 
			'V' => 5, 
			'IV' => 4, 
			'I' => 1
		);
		
		foreach ($romans as $roman => $latin) {
			$matches = intval($n / $latin);
			$result .= str_repeat($roman, $matches);
			$n = $n % $latin;
		}
		
		return $result;
	}
	
	static function tocLine( $anchor, $tocline, $tocnumber, $level, $sectionIndex = false ) {
		$classes = "toclevel-$level";
		if ( $sectionIndex !== false ) {
			$classes .= " tocsection-$sectionIndex";
		}
		$numbers = explode('.', $tocnumber);
		$tocnumbers = "";
		
		for ($i = 0; $i < count($numbers); $i++) {
			switch ($i) {
				case 0: # upper-roman
					$tocnumbers .= self::numberToRoman($numbers[$i]);
					break;
				
				case 1: # upper-latin
					$tocnumbers .= chr(64 + $numbers[$i]);
					break;
				
				case 2: # decimal
					$tocnumbers .= $numbers[$i];
					break;
				
				case 3: # lower-latin
					$tocnumbers .= strtolower(chr(64 + $numbers[$i]));
					break;
				
				case 4: # lower-roman
					$tocnumbers .= strtolower(self::numberToRoman($numbers[$i]));
					break;
				
				case 5: # lower-greek
					$tocnumbers .= "&#" . ($numbers[$i] + 944) . ";";
					break;
				
				default:
					$tocnumbers .= $numbers[$i];
			}
			$tocnumbers .= ". ";
		}
		
		return "\n<li class=\"$classes\"><a href=\"#" .
			$anchor . '"><span class="tocnumber"> ' .
			$tocnumbers . '</span> <span class="toctext">' .
			$tocline . '</span></a>';
	}
PatPeter (talkcontribs)

Does anyone know if it is possible to grab css in PHP? If I could grab MediaWiki:Common.css and check for list-style-type in toclevel or tocsection I could make this code dynamic instead of static.

Bawolff (talkcontribs)

I don't understand what you mean by "grab css". You can certainly add more css on the php side (See resource loader docs for how to add custom css as part of an extension).

If you mean read the current css and do something based on that - In theory you can retrieve the current contents of MediaWiki:Common.css on the php side (just like you can do for any other message), parse the css, and see what's there, but that would be a large amount of work (to write your own css parser), not to mention css can come from other places too, so I really do not reccomend doing that, if that is what you are planning. A better way to be able to customize would be to define a new system message, get its contents with wfMsgForContent, and do stuff based on its contents (or rewrite the toc to use <li> which can be styled by css).

[As an aside, note that making custom patches to mediawiki is not recomended/supported yadda yadda standard disclaimer... Although in this case there's really no hooks for an extension to use]

Reply to "How to customize auto-numbering of TOCs"