1<?php 2 3// implementation of 4// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite 5 6// must be run within Dokuwiki 7use ComboStrap\StringUtility; 8use ComboStrap\Tag; 9use ComboStrap\PluginUtility; 10 11require_once(__DIR__ . '/../class/StringUtility.php'); 12 13if (!defined('DOKU_INC')) die(); 14 15 16class syntax_plugin_combo_cite extends DokuWiki_Syntax_Plugin 17{ 18 const TAG = "cite"; 19 20 21 function getType() 22 { 23 return 'container'; 24 } 25 26 /** 27 * How Dokuwiki will add P element 28 * 29 * * 'normal' - The plugin can be used inside paragraphs 30 * * 'block' - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs 31 * * 'stack' - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs 32 * 33 * @see DokuWiki_Syntax_Plugin::getPType() 34 */ 35 function getPType() 36 { 37 return 'normal'; 38 } 39 40 /** 41 * @return array 42 * Allow which kind of plugin inside 43 * 44 * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs') 45 * because we manage self the content and we call self the parser 46 * 47 * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php 48 */ 49 function getAllowedTypes() 50 { 51 return array('baseonly', 'container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs'); 52 } 53 54 function getSort() 55 { 56 /** 57 * Should be less than the cite syntax plugin 58 **/ 59 return 200; 60 } 61 62 63 function connectTo($mode) 64 { 65 66 $pattern = PluginUtility::getContainerTagPattern(self::TAG); 67 $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent())); 68 69 } 70 71 72 function postConnect() 73 { 74 75 $this->Lexer->addExitPattern('</' . syntax_plugin_combo_cite::TAG . '>', PluginUtility::getModeForComponent($this->getPluginComponent())); 76 77 } 78 79 /** 80 * 81 * The handle function goal is to parse the matched syntax through the pattern function 82 * and to return the result for use in the renderer 83 * This result is always cached until the page is modified. 84 * @param string $match 85 * @param int $state 86 * @param int $pos - byte position in the original source file 87 * @param Doku_Handler $handler 88 * @return array|bool 89 * @see DokuWiki_Syntax_Plugin::handle() 90 * 91 */ 92 function handle($match, $state, $pos, Doku_Handler $handler) 93 { 94 95 switch ($state) { 96 97 case DOKU_LEXER_ENTER : 98 $tagAttributes = PluginUtility::getTagAttributes($match); 99 $node = new Tag(self::TAG, $tagAttributes, $state, $handler->calls); 100 $parent = ""; 101 if ($node->hasParent()) { 102 $parent = $node->getParent()->getName(); 103 } 104 return array( 105 PluginUtility::STATE => $state, 106 PluginUtility::ATTRIBUTES => $tagAttributes, 107 PluginUtility::PARENT_TAG => $parent); 108 109 case DOKU_LEXER_UNMATCHED : 110 return array( 111 PluginUtility::STATE => $state, 112 PluginUtility::PAYLOAD => $match 113 ); 114 115 case DOKU_LEXER_EXIT : 116 // Important otherwise we don't get an exit in the render 117 $node = new Tag(self::TAG, array(), $state, $handler->calls); 118 $parentName = ""; 119 if ($node->hasParent()) { 120 $parentName = $node->getParent()->getName(); 121 } 122 return array( 123 PluginUtility::STATE => $state, 124 PluginUtility::PARENT_TAG => $parentName); 125 126 127 } 128 return array(); 129 130 } 131 132 /** 133 * Render the output 134 * @param string $format 135 * @param Doku_Renderer $renderer 136 * @param array $data - what the function handle() return'ed 137 * @return boolean - rendered correctly? (however, returned value is not used at the moment) 138 * @see DokuWiki_Syntax_Plugin::render() 139 * 140 * 141 */ 142 function render($format, Doku_Renderer $renderer, $data) 143 { 144 145 if ($format == 'xhtml') { 146 147 /** @var Doku_Renderer_xhtml $renderer */ 148 $state = $data [PluginUtility::STATE]; 149 switch ($state) { 150 case DOKU_LEXER_ENTER : 151 152 $attributes = $data[PluginUtility::ATTRIBUTES]; 153 $parent = $data[PluginUtility::PARENT_TAG]; 154 if (!empty($parent) && $parent == syntax_plugin_combo_blockquote::TAG) { 155 StringUtility::addEolIfNotPresent($renderer->doc); 156 $renderer->doc .= "<footer class=\"blockquote-footer\"><cite"; 157 if (sizeof($attributes) > 0) { 158 $inlineAttributes = PluginUtility::array2HTMLAttributes($attributes); 159 $renderer->doc .= " $inlineAttributes>"; 160 } else { 161 $renderer->doc .= '>'; 162 } 163 164 } else { 165 $renderer->doc .= "<cite"; 166 if (sizeof($attributes) > 0) { 167 $inlineAttributes = PluginUtility::array2HTMLAttributes($attributes); 168 $renderer->doc .= " $inlineAttributes"; 169 } 170 $renderer->doc .= ">"; 171 } 172 break; 173 174 case DOKU_LEXER_UNMATCHED : 175 $renderer->doc .= PluginUtility::escape($data[PluginUtility::PAYLOAD]); 176 break; 177 178 case DOKU_LEXER_EXIT : 179 180 $renderer->doc .= '</cite>'; 181 $parent = $data[PluginUtility::PARENT_TAG]; 182 if (!empty($parent) && in_array($parent, ["card", "blockquote"])) { 183 $renderer->doc .= '</footer>' . DOKU_LF; 184 } else { 185 $renderer->doc .= DOKU_LF; 186 } 187 break; 188 189 } 190 return true; 191 } 192 193 // unsupported $mode 194 return false; 195 } 196 197 198} 199 200