<?php

class WikiTranslatorHeadingMode extends WikiTranslatorMode {



	protected $level;
	protected $toc;



	public function __construct($level, WikiTranslatorToC $toc) {
		$this->level = $level;
		$this->toc = $toc;
	}



	public function parse($ln) {
		$l = $this->level + 1;
		$title = substr($ln, $l, -$l);
		$name = htmlentities(str_replace(' ', '_', trim($title)), ENT_QUOTES);
		$this->toc->append($name, $title, $this->level);
		return "<a name=\"$name\"></a><h{$this->level}>$title</h{$this->level}>";
	}



	public static function buildIfMatch($ln, WikiTranslatorToC $toc) {
		if (substr($ln, 0, 5) == '==== ' and substr($ln, -5) == ' ====') {
			static $m3;
			$m3 or ($m3 = new WikiTranslatorHeadingMode(3, $toc));
			return $m3;
		}
		if (substr($ln, 0, 4) == '=== ' and substr($ln, -4) == ' ===') {
			static $m2;
			$m2 or ($m2 = new WikiTranslatorHeadingMode(2, $toc));
			return $m2;
		}
		if (substr($ln, 0, 3) == '== ' and substr($ln, -3) == ' ==') {
			static $m1;
			$m1 or ($m1 = new WikiTranslatorHeadingMode(1, $toc));
			return $m1;
		}
		return null;
	}



} // class WikiTranslatorHeadingMode

?>