wiki:xref:dokuwiki:inc:parser:handler.php
- handler.php
- <?php
- use dokuwiki\Extension\Event;
- use dokuwiki\Extension\SyntaxPlugin;
- use dokuwiki\Parsing\Handler\Block;
- use dokuwiki\Parsing\Handler\CallWriter;
- use dokuwiki\Parsing\Handler\CallWriterInterface;
- use dokuwiki\Parsing\Handler\Lists;
- use dokuwiki\Parsing\Handler\Nest;
- use dokuwiki\Parsing\Handler\Preformatted;
- use dokuwiki\Parsing\Handler\Quote;
- use dokuwiki\Parsing\Handler\Table;
- /**
- * Class Doku_Handler
- */
- class Doku_Handler {
- /** @var CallWriterInterface */
- protected $callWriter = null;
- /** @var array The current CallWriter will write directly to this list of calls, Parser reads it */
- /** @var array internal status holders for some modes */
- 'section' => false,
- 'doublequote' => 0,
- );
- /** @var bool should blocks be rewritten? FIXME seems to always be true */
- protected $rewriteBlocks = true;
- /**
- * @var bool are we in a footnote already?
- */
- protected $footnote;
- /**
- * Doku_Handler constructor.
- */
- public function __construct() {
- $this->callWriter = new CallWriter($this);
- }
- /**
- * Add a new call by passing it to the current CallWriter
- *
- * @param string $handler handler method name (see mode handlers below)
- * @param mixed $args arguments for this call
- * @param int $pos byte position in the original source file
- */
- public function addCall($handler, $args, $pos) {
- $this->callWriter->writeCall($call);
- }
- /**
- * Accessor for the current CallWriter
- *
- * @return CallWriterInterface
- */
- public function getCallWriter() {
- return $this->callWriter;
- }
- /**
- * Set a new CallWriter
- *
- * @param CallWriterInterface $callWriter
- */
- public function setCallWriter($callWriter) {
- $this->callWriter = $callWriter;
- }
- /**
- * Return the current internal status of the given name
- *
- * @param string $status
- * @return mixed|null
- */
- public function getStatus($status) {
- return $this->status[$status];
- }
- /**
- * Set a new internal status
- *
- * @param string $status
- * @param mixed $value
- */
- public function setStatus($status, $value) {
- $this->status[$status] = $value;
- }
- /** @deprecated 2019-10-31 use addCall() instead */
- public function _addCall($handler, $args, $pos) {
- dbg_deprecated('addCall');
- $this->addCall($handler, $args, $pos);
- }
- /**
- * Similar to addCall, but adds a plugin call
- *
- * @param string $plugin name of the plugin
- * @param mixed $args arguments for this call
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @param string $match matched syntax
- */
- public function addPluginCall($plugin, $args, $state, $pos, $match) {
- $this->callWriter->writeCall($call);
- }
- /**
- * Finishes handling
- *
- * Called from the parser. Calls finalise() on the call writer, closes open
- * sections, rewrites blocks and adds document_start and document_end calls.
- *
- * @triggers PARSER_HANDLER_DONE
- */
- public function finalize(){
- $this->callWriter->finalise();
- if ( $this->status['section'] ) {
- }
- if ( $this->rewriteBlocks ) {
- $B = new Block();
- $this->calls = $B->process($this->calls);
- }
- Event::createAndTrigger('PARSER_HANDLER_DONE',$this);
- }
- /**
- * fetch the current call and advance the pointer to the next one
- *
- * @fixme seems to be unused?
- * @return bool|mixed
- */
- public function fetch() {
- if($call !== false) {
- return $call;
- }
- return false;
- }
- /**
- * Internal function for parsing highlight options.
- * $options is parsed for key value pairs separated by commas.
- * A value might also be missing in which case the value will simple
- * be set to true. Commas in strings are ignored, e.g. option="4,56"
- * will work as expected and will only create one entry.
- *
- * @param string $options space separated list of key-value pairs,
- * e.g. option1=123, option2="456"
- * @return array|null Array of key-value pairs $array['key'] = 'value';
- * or null if no entries found
- */
- protected function parse_highlight_options($options) {
- preg_match_all('/(\w+(?:="[^"]*"))|(\w+(?:=[^\s]*))|(\w+[^=\s\]])(?:\s*)/', $options, $matches, PREG_SET_ORDER);
- foreach ($matches as $match) {
- if ($equal_sign === false) {
- $result [$key] = 1;
- } else {
- $result [$key] = $value;
- } else {
- $result [$key] = 1;
- }
- }
- }
- // Check for supported options
- $result,
- 'enable_line_numbers',
- 'start_line_numbers_at',
- 'highlight_lines_extra',
- 'enable_keyword_links')
- )
- );
- // Sanitize values
- if($result['enable_line_numbers'] === 'false') {
- $result['enable_line_numbers'] = false;
- }
- $result['enable_line_numbers'] = (bool) $result['enable_line_numbers'];
- }
- }
- $result['start_line_numbers_at'] = (int) $result['start_line_numbers_at'];
- }
- if($result['enable_keyword_links'] === 'false') {
- $result['enable_keyword_links'] = false;
- }
- $result['enable_keyword_links'] = (bool) $result['enable_keyword_links'];
- }
- return null;
- }
- return $result;
- }
- /**
- * Simplifies handling for the formatting tags which all behave the same
- *
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @param string $name actual mode name
- */
- protected function nestingTag($match, $state, $pos, $name) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- break;
- case DOKU_LEXER_EXIT:
- break;
- case DOKU_LEXER_UNMATCHED:
- break;
- }
- }
- /**
- * The following methods define the handlers for the different Syntax modes
- *
- * The handlers are called from dokuwiki\Parsing\Lexer\Lexer\invokeParser()
- *
- * @todo it might make sense to move these into their own class or merge them with the
- * ParserMode classes some time.
- */
- // region mode handlers
- /**
- * Special plugin handler
- *
- * This handler is called for all modes starting with 'plugin_'.
- * An additional parameter with the plugin name is passed. The plugin's handle()
- * method is called here
- *
- * @author Andreas Gohr <andi@splitbrain.org>
- *
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @param string $pluginname name of the plugin
- * @return bool mode handled?
- */
- public function plugin($match, $state, $pos, $pluginname){
- /** @var SyntaxPlugin $plugin */
- $plugin = plugin_load('syntax',$pluginname);
- if($plugin != null){
- $data = $plugin->handle($match, $state, $pos, $this);
- }
- if ($data !== false) {
- $this->addPluginCall($pluginname,$data,$state,$pos,$match);
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function base($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_UNMATCHED:
- return true;
- break;
- }
- return false;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- // get level and title
- if($level < 1) $level = 1;
- $this->status['section'] = true;
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function notoc($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function nocache($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function linebreak($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function eol($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function hr($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function strong($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'strong');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function emphasis($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'emphasis');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function underline($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'underline');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function monospace($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'monospace');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function subscript($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'subscript');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function superscript($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'superscript');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function deleted($match, $state, $pos) {
- $this->nestingTag($match, $state, $pos, 'deleted');
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function footnote($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- // footnotes can not be nested - however due to limitations in lexer it can't be prevented
- // we will still enter a new footnote mode, we just do nothing
- if ($this->footnote) {
- break;
- }
- $this->footnote = true;
- $this->callWriter = new Nest($this->callWriter, 'footnote_close');
- break;
- case DOKU_LEXER_EXIT:
- // check whether we have already exitted the footnote mode, can happen if the modes were nested
- if (!$this->footnote) {
- break;
- }
- $this->footnote = false;
- /** @var Nest $reWriter */
- $reWriter = $this->callWriter;
- $this->callWriter = $reWriter->process();
- break;
- case DOKU_LEXER_UNMATCHED:
- break;
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function listblock($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- $this->callWriter = new Lists($this->callWriter);
- break;
- case DOKU_LEXER_EXIT:
- /** @var Lists $reWriter */
- $reWriter = $this->callWriter;
- $this->callWriter = $reWriter->process();
- break;
- case DOKU_LEXER_MATCHED:
- break;
- case DOKU_LEXER_UNMATCHED:
- break;
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function unformatted($match, $state, $pos) {
- if ( $state == DOKU_LEXER_UNMATCHED ) {
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function preformatted($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- $this->callWriter = new Preformatted($this->callWriter);
- break;
- case DOKU_LEXER_EXIT:
- /** @var Preformatted $reWriter */
- $reWriter = $this->callWriter;
- $this->callWriter = $reWriter->process();
- break;
- case DOKU_LEXER_MATCHED:
- break;
- case DOKU_LEXER_UNMATCHED:
- break;
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function quote($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- $this->callWriter = new Quote($this->callWriter);
- break;
- case DOKU_LEXER_EXIT:
- /** @var Lists $reWriter */
- $reWriter = $this->callWriter;
- $this->callWriter = $reWriter->process();
- break;
- case DOKU_LEXER_MATCHED:
- break;
- case DOKU_LEXER_UNMATCHED:
- break;
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- return $this->code($match, $state, $pos, 'file');
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @param string $type either 'code' or 'file'
- * @return bool mode handled?
- */
- public function code($match, $state, $pos, $type='code') {
- if ( $state == DOKU_LEXER_UNMATCHED ) {
- $matches = sexplode('>',$match,2,'');
- // Cut out variable options enclosed in []
- }
- // We shortcut html here.
- if ($param[0] == 'html') $param[0] = 'html4strict';
- if ($param[0] == '-') $param[0] = null;
- $param [] = $this->parse_highlight_options ($options[0]);
- }
- $this->addCall($type, $param, $pos);
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function acronym($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function smiley($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function wordblock($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function entity($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function multiplyentity($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function singlequoteopening($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function singlequoteclosing($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function apostrophe($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function doublequoteopening($match, $state, $pos) {
- $this->status['doublequote']++;
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function doublequoteclosing($match, $state, $pos) {
- if ($this->status['doublequote'] <= 0) {
- $this->doublequoteopening($match, $state, $pos);
- } else {
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function camelcaselink($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function internallink($match, $state, $pos) {
- // Strip the opening and closing markup
- // Split title from URL
- $link = sexplode('|',$link,2);
- if ( $link[1] === null ) {
- $link[1] = null;
- // If the title is an image, convert it to an array containing the image details
- $link[1] = Doku_Handler_Parse_Media($link[1]);
- }
- //decide which kind of link it is
- if ( link_isinterwiki($link[0]) ) {
- // Interwiki
- $interwiki = sexplode('>',$link[0],2,'');
- $this->addCall(
- 'interwikilink',
- $pos
- );
- // Windows Share
- $this->addCall(
- 'windowssharelink',
- $pos
- );
- // external link (accepts all protocols)
- $this->addCall(
- 'externallink',
- $pos
- );
- // E-Mail (pattern above is defined in inc/mail.php)
- $this->addCall(
- 'emaillink',
- $pos
- );
- // local link
- $this->addCall(
- 'locallink',
- $pos
- );
- }else{
- // internal link
- $this->addCall(
- 'internallink',
- $pos
- );
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function filelink($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function windowssharelink($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function media($match, $state, $pos) {
- $p = Doku_Handler_Parse_Media($match);
- $this->addCall(
- $p['type'],
- $p['height'], $p['cache'], $p['linking']),
- $pos
- );
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function rss($match, $state, $pos) {
- // get params
- $p['max'] = $match[1];
- }else{
- $p['max'] = 8;
- }
- } else {
- $p['refresh'] = 14400; // default to 4 hours
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function externallink($match, $state, $pos) {
- $url = $match;
- $title = null;
- // add protocol on simple short URLs
- $title = $url;
- $url = 'ftp://'.$url;
- }
- $title = $url;
- $url = 'http://'.$url;
- }
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function emaillink($match, $state, $pos) {
- return true;
- }
- /**
- * @param string $match matched syntax
- * @param int $state a LEXER_STATE_* constant
- * @param int $pos byte position in the original source file
- * @return bool mode handled?
- */
- public function table($match, $state, $pos) {
- switch ( $state ) {
- case DOKU_LEXER_ENTER:
- $this->callWriter = new Table($this->callWriter);
- } else {
- }
- break;
- case DOKU_LEXER_EXIT:
- /** @var Table $reWriter */
- $reWriter = $this->callWriter;
- $this->callWriter = $reWriter->process();
- break;
- case DOKU_LEXER_UNMATCHED:
- }
- break;
- case DOKU_LEXER_MATCHED:
- if ( $match == ' ' ){
- } else if ( $match == "\n|" ) {
- } else if ( $match == "\n^" ) {
- } else if ( $match == '|' ) {
- } else if ( $match == '^' ) {
- }
- break;
- }
- return true;
- }
- // endregion modes
- }
- //------------------------------------------------------------------------
- function Doku_Handler_Parse_Media($match) {
- // Strip the opening and closing markup
- // Split title from URL
- $link = sexplode('|', $link, 2);
- // Check alignment
- // Logic = what's that ;)...
- if ( $lalign & $ralign ) {
- $align = 'center';
- } else if ( $ralign ) {
- $align = 'right';
- } else if ( $lalign ) {
- $align = 'left';
- } else {
- $align = null;
- }
- // The title...
- $link[1] = null;
- }
- //remove aligning spaces
- //split into src and parameters (using the very last questionmark)
- if($pos !== false){
- }else{
- $src = $link[0];
- $param = '';
- }
- //parse width and height
- } else {
- $w = null;
- $h = null;
- }
- //get linking command
- $linking = 'nolink';
- $linking = 'direct';
- $linking = 'linkonly';
- }else{
- $linking = 'details';
- }
- //get caching command
- $cache = $cachemode[1];
- }else{
- $cache = 'cache';
- }
- // Check whether this is a local or remote image or interwiki
- if (media_isexternal($src) || link_isinterwiki($src)){
- $call = 'externalmedia';
- } else {
- $call = 'internalmedia';
- }
- 'type'=>$call,
- 'src'=>$src,
- 'title'=>$link[1],
- 'align'=>$align,
- 'width'=>$w,
- 'height'=>$h,
- 'cache'=>$cache,
- 'linking'=>$linking,
- );
- return $params;
- }
Только авторизованные участники могут оставлять комментарии.
wiki/xref/dokuwiki/inc/parser/handler.php.txt · Последнее изменение: — 127.0.0.1