description = $f[3]; case 3: $this->extends = $f[2]; case 2: $this->filename = $f[1]; case 1: $this->name = $f[0]; default: } } } class syntax_plugin_apidoc_data_field { public $definition="", $name="", $description=""; public function __construct( $f ) { switch( min( count( $f ), 3 ) ) { case 3: $this->description = $f[2]; case 2: $this->definition = $f[1]; case 1: $this->name = $f[0]; default: } } } class syntax_plugin_apidoc_data_method { public $name="", $definition="", $description="", $parameters=array(), $returns=""; public function __construct( $f ) { switch( min( count( $f ), 4 ) ) { case 4: $this->description = $f[3]; case 3: $this->returns = $f[2]; case 2: $this->definition = $f[1]; case 1: $this->name = $f[0]; default: } } } class syntax_plugin_apidoc_data_parameter { public $name="", $type="", $defaultvalue="", $description=""; public function __construct( $f, &$c ) { switch( min( count( $f ), 4 ) ) { case 4: $this->description = $f[3]; case 3: $this->defaultvalue = $f[2]; case 2: $this->type = $f[1]; case 1: $this->name = $f[0]; default: } } } class syntax_plugin_apidoc extends DokuWiki_Syntax_Plugin { function getInfo(){ return array( 'author' => 'Marcus Runsten', 'email' => 'marcus@stilit.se', 'date' => '2015-11-12', 'name' => 'API Documentation Plugin', 'desc' => 'Adds syntax for API Documentation to DokuWiki. Version 1.1', 'url' => 'http://www.stilit.se/?page_id=107' ); } function getType(){ return 'protected';} function getPType(){ return 'block';} function getSort(){ return 100; } function connectTo($mode) { $this->Lexer->addEntryPattern( '.*?)', $mode, 'plugin_apidoc' ); } function postConnect() { $this->Lexer->addExitPattern('', 'plugin_apidoc'); } function handle($match, $state, $pos, &$handler){ switch ($state) { case DOKU_LEXER_ENTER: $this->syntax = substr($match, 1); return false; case DOKU_LEXER_UNMATCHED: file_put_contents( "/tmp/handle.txt", "Hanterar " . $match ); if( $this->syntax == 'apidoc' ) { return $this->apidoc_parse( substr( $match, 1 ) ); } } return false; } /** * Create output */ function render($mode, &$renderer, $data) { if( $data ) $renderer->doc .= $data[0]; return true; } private function apidoc_highlight( $str, $bool ) { $p = array( "/[\"\'].*?[\"\']/", "/\[\[(.*?);(.*?)\]\]/", "/\[\[(.*?)\]\]/", "/[\d]x[\d]+?|[\d]/", "/(^)([\w]+?)(\(.*?\))|(\s)([\w]+?)(\(.*?\))/", "/[()]/", "/[\\[\\]]/", "/[\\{\\}]/", '/(\$\w+?)([\s$])/', "/(?i)const\s|public|private|protected|new/", "/(?i)string|integer|boolean|bool|int|char|function/" ); $r = array( '$0', '$2', '$1', '$0', '$1$4$2$5$3$6 ', '$0', '$0', '$0', '$1$2', '$0', '$0', ); return preg_replace( $p, $r, $str ); } private function apidoc_out_html( $data ) { $html = "

"; $html .= $this->apidoc_highlight( $data->name, true ); $html .= "

Fields

Methods

"; $html .= "

" . $this->apidoc_highlight( $data->name, true ) . "

"; $html .= "

Filename : " . $data->filename . "
"; $html .= "Extends : " . $this->apidoc_highlight( $data->extends, true ) . "

"; $html .= "

Description

"; $html .= "

" . $data->description . "

"; $html .= "

Fields

"; foreach( $data->fields AS $f ) { $html .= "
"; $html .= "

" . $this->apidoc_highlight( $f->name, true ) . "

"; $html .= "

Definition : " . $this->apidoc_highlight( $f->definition, true ) . "
"; $html .= "Description
" . $f->description . "

"; $html .= "
"; } $html .= "

Methods

"; foreach( $data->methods AS $m ) { $html .= "
"; $html .= "

" . $this->apidoc_highlight( $m->name, true ) . "

"; $html .= "

Definition : " . $this->apidoc_highlight( $m->definition, true ) . "

"; $html .= "

Returns : " . $this->apidoc_highlight( $m->returns, true ) . "

"; $html .= "

Description
" . $m->description . "

"; $html .= "
"; $html .= "

Parameters

"; foreach( $m->parameters AS $p ) { $html .= "
"; $html .= "

" . $this->apidoc_highlight( $p->name, true ) . "

"; $html .= "

"; $html .= "Type : " . $this->apidoc_highlight( $p->type, true ) . "
"; $html .= "Default value : " . $this->apidoc_highlight( $p->defaultvalue, true ) . ""; $html .= "

"; $html .= "

Description
" . $p->description . "

"; $html .= "
"; } $html .= "
"; $html .= "
"; } return $html; } private function apidoc_parse( $str ) { $data = $this->apidoc_parse_data( $str ); if( !$data ) return false; $data = $this->apidoc_sort_data( $data ); if( !$data ) return false; $html = $this->apidoc_out_html( $data ); return array( $html ); } private function apidoc_parse_data( $str ) { $rData = null; $co = null; $matches = preg_split( "/(CLASS|FIELD|METHOD|PARAMETER)\|/", $str, -1, PREG_SPLIT_DELIM_CAPTURE ); for( $i=0; $i < count( $matches )-1; $i++ ) { switch( $matches[$i] ) { case 'CLASS': $rData = new syntax_plugin_apidoc_data_class( explode( "|", $matches[++$i] ) ); break; case 'FIELD': if( $rData == null ) return false; $co = $rData->fields[] = new syntax_plugin_apidoc_data_field( explode( "|", $matches[++$i] ) ); break; case 'METHOD': if( $rData == null ) return false; $co = $rData->methods[] = new syntax_plugin_apidoc_data_method( explode( "|", $matches[++$i] ) ); break; case 'PARAMETER': if( $rData == null || $co == null ) return false; $co->parameters[] = new syntax_plugin_apidoc_data_parameter( explode( "|", $matches[++$i] ) ); default: // Do nothing break; } } return $rData; } private function apidoc_sort( $arr ) { $tgt = array(); while( count( $arr ) > 0 ) { $smallest = 0; for( $i=0; $i < count( $arr ); $i++ ) if( strcasecmp( $arr[$smallest]->name, $arr[$i]->name ) > 0 ) $smallest = $i; $tgt[] = $arr[$smallest]; unset( $arr[$smallest] ); $arr = array_values( $arr ); } return $tgt; } private function apidoc_sort_data( $data ) { $data->fields = $this->apidoc_sort( $data->fields ); $data->methods = $this->apidoc_sort( $data->methods ); for( $i=0; $i < count( $data->methods ); $i++ ) $data->methods[$i]->parameters = $this->apidoc_sort( $data->methods[$i]->parameters ); return $data; } } ?>