summaryrefslogtreecommitdiff
path: root/inc/parserutils.php
diff options
context:
space:
mode:
authorandi <andi@splitbrain.org>2005-05-21 23:04:30 +0200
committerandi <andi@splitbrain.org>2005-05-21 23:04:30 +0200
commit107b01d60353b74aebd1901b6f19e70a998b2594 (patch)
tree5584f0b9c2eedf2050e01aac79ffe81cf6021da2 /inc/parserutils.php
parent5233912627d370d8a346a810cc95c46554a4a8e1 (diff)
downloadrpg-107b01d60353b74aebd1901b6f19e70a998b2594.tar.gz
rpg-107b01d60353b74aebd1901b6f19e70a998b2594.tar.bz2
sorting support for syntax plugins, info plugin now actually does something
Syntax plugins now need to implement getSort() which should return a number. This number is used to add all syntax modes in the correct order. To see a list in which order current modes are loaded you can use the info plugin like this: ~~INFO:syntaxmodes~~ lists all known modes (includes existing plugins) with their sorting score ~~INFO:syntaxtypes~~ lists all syntax types and their registered modes (useful for implementing the getType() function. darcs-hash:20050521210430-9977f-2baaf6043afc6ea3fed41cdca97564218fb519c2.gz
Diffstat (limited to 'inc/parserutils.php')
-rw-r--r--inc/parserutils.php157
1 files changed, 97 insertions, 60 deletions
diff --git a/inc/parserutils.php b/inc/parserutils.php
index 9c0e4fd9c..bd5317e1b 100644
--- a/inc/parserutils.php
+++ b/inc/parserutils.php
@@ -154,86 +154,123 @@ function p_cached_instructions($file,$cacheonly=false){
* @author Andreas Gohr <andi@splitbrain.org>
*/
function p_get_instructions($text){
+
+ $modes = p_get_parsermodes();
+
+ // Create the parser
+ $Parser = & new Doku_Parser();
+
+ // Add the Handler
+ $Parser->Handler = & new Doku_Handler();
+
+ //add modes to parser
+ foreach($modes as $mode){
+ $Parser->addMode($mode['mode'],$mode['obj']);
+ }
+
+ // Do the parsing
+ $p = $Parser->parse($text);
+// dbg($p);
+ return $p;
+}
+
+/**
+ * returns all available parser syntax modes in correct order
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function p_get_parsermodes(){
global $conf;
+ //reuse old data
+ static $modes = null;
+ if($modes != null){
+ return $modes;
+ }
+
//import parser classes and mode definitions
require_once DOKU_INC . 'inc/parser/parser.php';
- // load syntax plugins
+ // we now collect all syntax modes and their objects, then they will
+ // be sorted and added to the parser in correct order
+ $modes = array();
+
+ // add syntax plugins
$pluginlist = plugin_list('syntax');
if(count($pluginlist)){
global $PARSER_MODES;
- $plugins = array();
+ $obj = null;
foreach($pluginlist as $p){
- plugin_load('syntax',$p,$plugin[$p]); //load plugin into $plugin array
- $PARSER_MODES[$plugin[$p]->getType()][] = "plugin_$p"; //register mode type
+ plugin_load('syntax',$p,$obj); //load plugin into $obj
+ $PARSER_MODES[$obj->getType()][] = "plugin_$p"; //register mode type
+ //add to modes
+ $modes[] = array(
+ 'sort' => $obj->getSort(),
+ 'mode' => "plugin_$p",
+ 'obj' => $obj,
+ );
}
}
-
- // Create the parser
- $Parser = & new Doku_Parser();
-
- // Add the Handler
- $Parser->Handler = & new Doku_Handler();
- // Load all the modes
- $Parser->addMode('listblock',new Doku_Parser_Mode_ListBlock());
- $Parser->addMode('preformatted',new Doku_Parser_Mode_Preformatted());
- $Parser->addMode('notoc',new Doku_Parser_Mode_NoToc());
- $Parser->addMode('nocache',new Doku_Parser_Mode_NoCache());
- $Parser->addMode('header',new Doku_Parser_Mode_Header());
- $Parser->addMode('table',new Doku_Parser_Mode_Table());
-
- $formats = array (
- 'strong', 'emphasis', 'underline', 'monospace',
- 'subscript', 'superscript', 'deleted',
- );
- foreach ( $formats as $format ) {
- $Parser->addMode($format,new Doku_Parser_Mode_Formatting($format));
+ // add default modes
+ $std_modes = array('listblock','preformatted','notoc','nocache',
+ 'header','table','linebreak','footnote','hr',
+ 'unformatted','php','html','code','file','quote',
+ 'multiplyentity','quotes','internallink','rss',
+ 'media','externallink','emaillink','windowssharelink',
+ 'eol');
+ foreach($std_modes as $m){
+ $class = "Doku_Parser_Mode_$m";
+ $obj = new $class();
+ $modes[] = array(
+ 'sort' => $obj->getSort(),
+ 'mode' => $m,
+ 'obj' => $obj
+ );
}
- $Parser->addMode('linebreak',new Doku_Parser_Mode_Linebreak());
- $Parser->addMode('footnote',new Doku_Parser_Mode_Footnote());
- $Parser->addMode('hr',new Doku_Parser_Mode_HR());
-
- $Parser->addMode('unformatted',new Doku_Parser_Mode_Unformatted());
- $Parser->addMode('php',new Doku_Parser_Mode_PHP());
- $Parser->addMode('html',new Doku_Parser_Mode_HTML());
- $Parser->addMode('code',new Doku_Parser_Mode_Code());
- $Parser->addMode('file',new Doku_Parser_Mode_File());
- $Parser->addMode('quote',new Doku_Parser_Mode_Quote());
+ // add formatting modes
+ $fmt_modes = array('strong','emphasis','underline','monospace',
+ 'subscript','superscript','deleted');
+ foreach($fmt_modes as $m){
+ $obj = new Doku_Parser_Mode_formatting($m);
+ $modes[] = array(
+ 'sort' => $obj->getSort(),
+ 'mode' => $m,
+ 'obj' => $obj
+ );
+ }
+
+ // add modes which need files
+ $obj = new Doku_Parser_Mode_smiley(array_keys(getSmileys()));
+ $modes[] = array('sort' => $obj->getSort(), 'mode' => 'smiley','obj' => $obj );
+ $obj = new Doku_Parser_Mode_acronym(array_keys(getAcronyms()));
+ $modes[] = array('sort' => $obj->getSort(), 'mode' => 'acronym','obj' => $obj );
+ $obj = new Doku_Parser_Mode_entity(array_keys(getEntities()));
+ $modes[] = array('sort' => $obj->getSort(), 'mode' => 'entity','obj' => $obj );
- $Parser->addMode('smiley',new Doku_Parser_Mode_Smiley(array_keys(getSmileys())));
- $Parser->addMode('acronym',new Doku_Parser_Mode_Acronym(array_keys(getAcronyms())));
- #$Parser->addMode('wordblock',new Doku_Parser_Mode_Wordblock($Modes,getBadWords()));
- $Parser->addMode('entity',new Doku_Parser_Mode_Entity(array_keys(getEntities())));
- $Parser->addMode('multiplyentity',new Doku_Parser_Mode_MultiplyEntity());
- $Parser->addMode('quotes',new Doku_Parser_Mode_Quotes());
-
- if($conf['camelcase']){
- $Parser->addMode('camelcaselink',new Doku_Parser_Mode_CamelCaseLink());
+ // add optional camelcase mode
+ if($conf['camelcase']){
+ $obj = new Doku_Parser_Mode_camelcaselink();
+ $modes[] = array('sort' => $obj->getSort(), 'mode' => 'camelcaselink','obj' => $obj );
}
- //add plugins FIXME since order is important we, need to find a better way!
- foreach ( array_keys($plugin) as $p ) {
- $Parser->addMode("plugin_$p",$plugin[$p]);
- }
+ //sort modes
+ usort($modes,'p_sort_modes');
- $Parser->addMode('internallink',new Doku_Parser_Mode_InternalLink());
- $Parser->addMode('rss',new Doku_Parser_Mode_RSS());
- $Parser->addMode('media',new Doku_Parser_Mode_Media());
- $Parser->addMode('externallink',new Doku_Parser_Mode_ExternalLink());
- $Parser->addMode('emaillink',new Doku_Parser_Mode_EmailLink());
- $Parser->addMode('windowssharelink',new Doku_Parser_Mode_WindowsShareLink());
- //$Parser->addMode('filelink',new Doku_Parser_Mode_FileLink()); //FIXME ???
- $Parser->addMode('eol',new Doku_Parser_Mode_Eol());
+ return $modes;
+}
- // Do the parsing
- $p = $Parser->parse($text);
-// dbg($p);
- return $p;
-}
+/**
+ * Callback function for usort
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ */
+function p_sort_modes($a, $b){
+ if($a['sort'] == $b['sort']) return 0;
+ return ($a['sort'] < $b['sort']) ? -1 : 1;
+}
/**
* Renders a list of instruction to the specified output mode