<?php
/*
 * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
 *
 * This file is released under the terms of the MIT license. You can find the
 * complete text in the attached LICENSE file or online at:
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

class sly_Controller_Devutilsdocs_Doc extends sly_Controller_Devutilsdocs {
	public function indexAction() {
		$this->init();

		$doc  = sly_get('doc', 'string');
		$docs = $this->getDocFiles(false);

		if (!isset($docs[$doc])) {
			sly_Util_HTTP::redirect('index.php?page=devutilsdocs');
		}

		$title = $this->getDocTitle($doc.'.textile');
		$html  = $this->getParsedDocFile($doc.'.textile');
		$html  = $this->prepareForSally($html);

		print $this->render('doc.phtml', compact('html', 'title'));
	}

	private function prepareForSally($html) {
		$html = preg_replace('#<p( .*?)?>#', '<p$2>', $html);
		$html = preg_replace('#<pre>#', '<pre style="font-family:monospace;margin:0 10px 5px;padding:3px;border:1px solid #4D4646;line-height:16px">', $html);
		$html = preg_replace('#<h1(.*?)>(.*?)</h1>#', '', $html);
		$html = preg_replace('#<h3(.*?)>#', '<h4$1>', $html);
		$html = preg_replace('#<h2(.*?)>#', '<h3$1>', $html);
		$html = preg_replace('#<ul(.*?)>#', '<ul$1>', $html);
		$html = preg_replace('#<ol(.*?)>#', '<ol$1>', $html);
		$html = preg_replace('#<code(.*?)>#', '<code style="font-family:monospace"$1>', $html);
		$html = preg_replace('#<em(.*?)>#', '<em style="font-style:italic"$1>', $html);
		$html = preg_replace('#<blockquote(.*?)>#', '<blockquote style="font-style:italic;margin:15px 10px 15px 20px"$1>', $html);
		$html = preg_replace('#</h3>#', '</h4>', $html);
		$html = preg_replace('#</h2>#', '</h3>', $html);
		$html = preg_replace('#</h1>#', '</h2>', $html);

		return $html;
	}
}
