<?php
/**
 * @name:        SemanticBBCode <http://jeenaparadies.net/webdesign/jlog/>
 * @author:      Robert Bienert <robertbienert@gmx.net>
 * @version:     0.4
 * @date:        2008-07-20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * Dieses Plugin erweitert BBCode um die folgenden Tags:
 * ins:		eingefuegter Text
 * del:		geloeschter Text
 * cite:	(Zitat-) Quelle
 * xcode:	inline Code
 * q:		inline Zitat
 *
 * Im Adminbereich stehen darueber hinaus noch folgende Tags, jeweils
 * mit Parameter, zur Verfuegung:
 * q:		inline Zitat mit Quelle (URI)
 * abbr:	Abkuerzung mit Erlaeuterung
 * acronym:	Akronym mit Erlaeuterung
 */

# TODO: komfortables JavaScript fuer: q=, abbr=, acronym=

//--
// Hilfsfunktionen fuer erweiterte BBCodes
function semanticBBCode_doQ($action, $attr, $ct, $param, & $node) {
	if ($action == 'validate') {
		if (isset($attr['default'])) {
		}

		return true;	// kein Quell-URI gesetzt
	}

	$cite = isset($attr['default']) ?
		' cite="'.htmlspecialchars($attr['default']).'"' : NULL;

	return "<q{$cite}>{$ct}</q>";
}

function semanticBBCode_doAbbrAcronym($action, $attr, $ct, $param, & $node)
{
	if ($action == 'validate')
		return isset($attr['default']);

	return "<{$param['tag']} title='" .
		htmlspecialchars($attr['default']) .
		"'>{$ct}</{$param['tag']}>";
}

class SemanticBBCode extends JlogPlugin {
	//--
	// Funktionen fuer Administratoren/Autoren:
	function hook_bbcode(& $bbc) {
		$this->_private_BBCode_($bbc);

		$bbc->addCode('q', 'callback_replace', 'semanticBBCode_doQ',
			array('usecontent_param' => 'default'),
			'inline', array('block', 'inline', 'listitem', 'link'),
			array());
		$bbc->addCode('abbr', 'callback_replace',
			'semanticBBCode_doAbbrAcronym',
			array('usecontent_param' => 'default',
				'tag' => 'abbr'),
			'inline', array('block', 'inline', 'listitem', 'link'),
			array());
		$bbc->addCode('acronym', 'callback_replace',
			'semanticBBCode_doAbbrAcronym',
			array('usecontent_param' => 'default',
				'tag' => 'acronym'),
			'inline', array('block', 'inline', 'listitem', 'link'),
			array());

		return $bbc;
	}

	function hook_adminForm($form, $data) {
		return $form . $this->_private_BBCodeButtons_() .
		# TODO sehr einfache Testvariante
		"<script type='text/javascript'>
		addLoadEvent(
			function () {
				if(jlog_bbcode_br || (typeof(jlog_admin) != 'undefined'))
				if (jlog_bbcode_br.insertBefore) {
					jlog_bbcode_do_button('Q', '[q=]', '[/q]');
					jlog_bbcode_do_button('Abbr', '[abbr=]', '[/abbr]');
					jlog_bbcode_do_button('Acronym', '[acronym=]', '[/acronym]');
				}
			}
		);
		</script>";
	}

	//--
	// Funktionen fuer Kommentatoren:
	function hook_bbcomments(& $bbc) {
		$this->_private_BBCode_($bbc);

		$bbc->addCode('q', 'simple_replace', null,
			array('start_tag' => '<q>', 'end_tag' => '</q>'),
			'inline', array('block', 'inline', 'link'),
			array());

		return $bbc;
	}

	function hook_commentForm($form) {
		return $this->_private_BBCodeButtons_() . $form;
	}

	// Infodarstellung
	function hook_adminContent($output) {
		return <<<EOT
<p>Copyright &#169; 2008 Robert Bienert, <a href="http://jeenaparadies.net/projects/jlog/wiki/plugins/SemanticBBCode"><kbd>http://jeenaparadies.net/projects/jlog/wiki/plugins/SemanticBBCode</kbd></a></p>
<p>Dieses Plugin erweitert <a href="http://jeenaparadies.net/projects/jlog/wiki/help/bbcode">BBCode</a> um die folgenden Tags:</p>
<dl>
	<dt><var>ins</var></dt>		<dd>eingef&#252;gter Text</dd>
	<dt><var>del</var></dt>		<dd>gel&#246;schter Text</dd>
	<dt><var>cite</var></dt>	<dd>(Zitat-) Quelle</dd>
	<dt><var>xcode</var></dt>	<dd>inline Code</dd>
	<dt><var>q</var></dt>		<dd>inline Zitat</dd>
</dl>
<p>Im Adminbereich stehen dar&#252;ber hinaus noch folgende Tags, jeweils
mit Parameter, zur Verf&#252;gung:</p>
<dl>
	<dt><var>q</var></dt>		<dd>inline Zitat mit Quelle (URI)</dd>
	<dt><var>abbr</var></dt>	<dd>Abk&#252;rzung mit Erl&#228;uterung</dd>
	<dt><var>acronym</var></dt>	<dd>Akronym mit Erl&#228;uterung</dd>
</dl>
EOT;
	}

	//--
	// private Ressourcen fuer beide Arten von BBCode-Unterstuetzung
	// (Adminbereich und Kommentator)
	function _private_BBCode_(& $bbc) {
		$bbc->addCode('del', 'simple_replace', null,
			array('start_tag' => '<del>', 'end_tag' => '</del>'),
			'inline', array('block', 'inline', 'link'),
			array());
		$bbc->addCode('ins', 'simple_replace', null,
			array('start_tag' => '<ins>', 'end_tag' => '</ins>'),
			'inline', array('block', 'inline', 'link'),
			array());
		// Der Elementname 'xcode' ist dem MacOSXHints.ch-Forum
		// entliehen, dort heissen inline-codes ebenfalls so.
		$bbc->addCode('xcode', 'simple_replace', null,
			array('start_tag' => '<code>', 'end_tag' => '</code>'),
			'inline', array('block', 'inline', 'link'),
			array());
		$bbc->addCode('cite', 'simple_replace', null,
			array('start_tag' => '<cite>', 'end_tag' => '</cite>'),
			'inline', array('block', 'inline', 'link'),
			array());
	}

	function _private_BBCodeButtons_() {
	return "
	<script type='text/javascript'>
	addLoadEvent(
		function () {
			if(jlog_bbcode_br || (typeof(jlog_admin) != 'undefined'))
			if (jlog_bbcode_br.insertBefore) {
				jlog_bbcode_do_button('Del', '[del]', '[/del]');
				jlog_bbcode_do_button('Ins', '[ins]', '[/ins]');
				jlog_bbcode_do_button('Code', '[xcode]', '[/xcode]');
				jlog_bbcode_do_button('Q', '[q]', '[/q]');
				jlog_bbcode_do_button('Cite', '[cite]', '[/cite]');
			}
		}
	 );
	</script>";
	}
}
?>
