<?php

	include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
	
	define( "PLUGIN_TEXTPARSER_FILTER_WIKI", 1 );
	define( "PLUGIN_TEXTPARSER_FILTER_TEXTILE", 2 );
	define( "PLUGIN_TEXTPARSER_FILTER_UBB", 3 );
	define( "PLUGIN_TEXTPARSER_FILTER_MARKDOWN", 4 );
	
	/**
	 * Plugin that uses the EVENT_TEXT_RENDER event to allow users to type
	 * wiki or textile markup in the posts
	 */
	class PluginTextParser extends PluginBase 
	{
	
		function PluginTextParser()
		{
			$this->PluginBase();
			
			$this->desc = "A plugin that allows users to use wiki markup in their posts.";
			$this->author = "The LifeType Project";
			$this->id = "textparser";
			$this->locales = Array( "en_UK", "zh_TW" );
			
			// register the event we are going to need
			$this->registerNotification( EVENT_TEXT_FILTER );
			
			// the two actions that will take care of updating the plugin settings
			$this->registerAdminAction( "pluginTextParserSettings", "AdminPluginTextParserSettingsAction" );
			$this->registerAdminAction( "updatePluginTextParserSettings", "AdminPluginTextParserUpdateSettingsAction" );			
			
			// and finally add an entry to the menu
			$this->addMenuEntry( "/menu/Manage/managePosts", "TextParser", "admin.php?op=pluginTextParserSettings", "" );
		}
		
		/**
		 * method that will process the EVENT_TEXT_RENDER event
		 *
		 * @params eventType We hope this will be EVENT_TEXT_RENDER
		 * @params params An array with the information we received, which we hope will be
		 * some text :)
		 * @see PluginBase
		 */
		function process( $eventType, $params ) 
		{
			// if the plugin is diabled, do nnothing
			$settings = $this->blogInfo->getSettings();
			if( !$settings->getValue( "plugin_textparser_enabled" ))
				return true;
		
			// some little check to prevent misunderstandings from happening...
			if( $eventType != EVENT_TEXT_FILTER ) {
				throw( new Exception( "WTF? The TextParser plugin should only receive EVENT_TEXT_RENDER events!" ));
				die();
			}
			
			$filter = $settings->getValue( "plugin_textparser_filter" );
			if( $filter == "" ) $filter = PLUGIN_TEXTPARSER_FILTER_WIKI;
			
			if( $filter == PLUGIN_TEXTPARSER_FILTER_WIKI ) {
				// load the wiki engine... we're doing like this here because it's about 470kb of
				// php code and that's quite a lot to load if we don't need it all!
				include_once( PLOG_CLASS_PATH. "plugins/textparser/class/data/Text/Wiki.php" );
				
				// create and configure a wiki filter object
				$wiki =& new Text_Wiki();
				$wiki->setRenderConf( "xhtml", "wikilink", "view_url", "" );
				$wiki->setRenderConf( "xhtml", "translate", HTML_SPECIALCHARS);
				
				// transform the text
				$params["text"] = $wiki->transform( $params["text"], "xhtml" );
			}
			elseif( $filter == PLUGIN_TEXTPARSER_FILTER_TEXTILE ) {
				// load the textile code... about 4500 lines of code!
				include_once( PLOG_CLASS_PATH."plugins/textparser/class/data/textile/textile2.php" );
				
				// it is adapted from the textile 2.0 plugin for WP so the do_textile function
				// will do everything for us... including using SmartyPants
				$params["text"] = do_textile( $params["text"] );
			}
			elseif( $filter == PLUGIN_TEXTPARSER_FILTER_UBB ) {
				// again, dynamic loading is the best thing we can do...
				include_once( PLOG_CLASS_PATH."plugins/textparser/class/data/ubb/BBCodeParser.php" );
				
				$parser = new HTML_BBCodeParser();
				$parser->setText( $params["text"] );
				$parser->parse();
				$params["text"] = $parser->getParsed();
			}
			elseif ( $filter == PLUGIN_TEXTPARSER_FILTER_MARKDOWN ) {
				include_once( PLOG_CLASS_PATH."plugins/textparser/class/data/markdown/markdown.php" );
				$params["text"] = Markdown( $params["text"] );
			}
			
			return true;
		}
	}
?>