Extension:H1 CSS

What can this extension do?
This extension allows to specify an initial number for &lt;h1&gt; tags, together with an optional text prefix. This allows subsequently consistent numbering &lt;h1&gt; through &lt;h5&gt; (for if it's supported in a future), regardless autonumbering is enabled or not. This is based in CSS 2 counters, and has been proved successfully in both Firefox and konqueror. IE shows no numbering at all, though the chapter prefix is shown ok. The tag is intended to be called once right before the keyword.

Usage
 This starts numbering from "5" on.  where prefix additionaly prefixes the &lt;h1&gt; tags with the word "Chapter", so a wiki code of =My custom chapter= would be shown as Chapter 5. My custom chapter and the table of contents will show the numbering starting from 5. Of course, subsequent &lt;h1&gt; tags will follow consecutive numbering (6, 7, etc). The example is exactly the same that  which means that the parameter full is parsed, assigning the last word to the chapter, and the rest of the text before it, to the prefix. As an extra option, you can enclose the chapter title, so My custom chapter allows to do it all in one line.

Installation
Copy the H1_CSS.php file into the extensions folder of your MediaWiki installation. Then add the following line to your LocalSettings.php file (near the end):

require_once( "$IP/extensions/H1_CSS.php" );

Parameters

 * full: The final string to be used as prefix and initial chapter number. This is the preferred way if you wish both to add a prefix and have an inititial chapter number different from 1. Currently, if any of the other two parameters is explicitily set, it take precedence.
 * chapter: Sets the initial number for &lt;h1&gt; headings. Meant to be used when you don't want nor need a prefix.
 * prefix: Sets the prefix for &lt;h1&gt; heading. Meant to be used when you don't want nor need an initial chapter number different from 1.

Changes to LocalSettings.php
require_once("$IP/extensions/H1_CSS.php");

Code
 (remember that "prefix" is optional), * which starts numbering from "5", and prefix it with the word "Chapter", * so a wiki code of "=My custom chapter=" would be shown as "Chapter 5. * My custom chapter", and the table of contents will show the numbering starting from 5. * The example is exactly the same that , which means that * the parameter "full" is parsed, assigning the last word to the chapter, and the rest, to the prefix. * As an extra option, you can enclose the chapter title, so My custom chapter * allows to do it all in one line. * * This is based in CSS 2 counters, and has been proved successfully in both Firefox and konqueror. * IE shows no numbering at all, though the chapter prefix is shown ok. * * @author Oscar Manuel Gómez Senovilla  * @copyright Copyright © 2007, Oscar Manuel Gómez Senovilla * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later */

$wgExtensionFunctions[] = array( 'H1Hook', 'setup' ); $wgExtensionCredits['parserhook'][] = array(	'name' => 'H1 CSS',	'description' => 'Parser hook to init H1 tags at a custom number using CSS 2',	'author' => 'Oscar Manuel Gómez Senovilla' );

class H1Hook {

public static function setup { global $wgParser; $wgParser->setHook( 'H1_CSS', array( 'H1Hook', 'parse' ) ); }	/**	 * @param string $content wiki content, maybe not necessary * @param array $args, being processed "chapter", "prefix" and "full". */	public static function parse($content, array $args, Parser &$parser ) { $h1 = htmlspecialchars( trim( Sanitizer::checkCss( $content ) ) );

// Force disable numbering, to use our numbering $parser->mOptions->setNumberHeadings(false);

if (!$args['full']) {			$chapter=($args['chapter']?$args['chapter']-1:"0"); $prefix=($args['prefix']?trim($args['prefix'])." ":""); }		else {			// We strip the string, to assign the last word to $chapter, // and the rest, to $prefix $full= explode(" ", $args['full']); if ($numargs=count($full)) {				// Let's preserve explicit chapter and prefix parameters, for backwards compatibility if ($args['chapter']) {					$chapter=$args['chapter']-1; }				else {					$chapter=$full[$numargs-1]-1; }				if ($args['prefix']) {					trim($args['prefix']). " ";				}				else {					unset($_prefix); for ($c=0;$c<($numargs-1);$c++) {						$_prefix .= " ". $full[$c]; }					$prefix=trim($_prefix). " ";				}			}		}

// Add inline style to head secion $parser->mOutput->addHeadItem( << /*<![CDATA[*/

counter-reset: chapter {$chapter} toc_chapter {$chapter}; }
 * 1) bodyContent {

content: "{$prefix}" counter(chapter) ". "; }
 * 1) bodyContent h1:before{

counter-reset: sub_h2; counter-increment: chapter; }
 * 1) bodyContent h1 {

content: counter(chapter) "." counter(sub_h2) " "; }
 * 1) bodyContent h2 span.mw-headline:before {

counter-increment: sub_h2; counter-reset: sub_h3; }
 * 1) bodyContent h2 {

content: counter(chapter) "." counter(sub_h2) "." counter(sub_h3) " "; }
 * 1) bodyContent h3 span.mw-headline:before{

counter-increment: sub_h3; counter-reset: sub_h4; }
 * 1) bodyContent h3 {

content: counter(chapter) "." counter(sub_h2) "." counter(sub_h3) "." counter(sub_h4) " "; }
 * 1) bodyContent h4 span.mw-headline:before{

counter-increment: sub_h4; counter-reset: sub_h5; }
 * 1) bodyContent h4 {

/* While there's no support for "=====", no span here */ content: counter(chapter) "." counter(sub_h2) "." counter(sub_h3) "." counter(sub_h4) "." counter(sub_h5) " "; }
 * 1) bodyContent h5:before{

counter-increment: sub_h5; }
 * 1) bodyContent h5 {

.tocnumber { display: none; }

content: counter(toc_chapter) ". "; }
 * 1) bodyContent li.toclevel-1 .toctext:before {

counter-increment: toc_chapter; counter-reset: toc_h2; }
 * 1) bodyContent li.toclevel-1 {

content: counter(toc_chapter) "." counter(toc_h2) " "; }
 * 1) bodyContent li.toclevel-2 .toctext:before {

counter-increment: toc_h2; counter-reset: toc_h3; }
 * 1) bodyContent li.toclevel-2 {

content: counter(toc_chapter) "." counter(toc_h2) "." counter(toc_h3) " "; counter-increment: toc_h3; }
 * 1) bodyContent li.toclevel-3 .toctext:before {

counter-reset: toc_h4; }
 * 1) bodyContent li.toclevel-3 {

content: counter(toc_chapter) "." counter(toc_h2) "." counter(toc_h3) "." counter(toc_h4) " "; counter-increment: toc_h4; }
 * 1) bodyContent li.toclevel-4 .toctext:before {

counter-reset: toc_h5; }
 * 1) bodyContent li.toclevel-4 {

content: counter(toc_chapter) "." counter(toc_h2) "." counter(toc_h3) "." counter(toc_h4) "." counter(toc_h5) " "; counter-increment: toc_h5; }
 * 1) bodyContent li.toclevel-5 .toctext:before {

/*]]>*/ EOT );		// If there's a content in the call, return it as h1, 		if (!empty($h1))		{			return " $h1 ";		}	} }

Styling
This extension, when called, adds some inline CSS code, trying to fit to explicit predefined css classes in TOC and headings tags, so necessary actions are done. No font style is changed, just specific counter handling.