1be906b56SAndreas Gohr<?php 2be906b56SAndreas Gohr 3be906b56SAndreas Gohrnamespace dokuwiki\Parsing\ParserMode; 4be906b56SAndreas Gohr 571096e46SAndreas Gohruse dokuwiki\Parsing\Handler; 671096e46SAndreas Gohruse dokuwiki\Parsing\Handler\Preformatted as PreformattedHandler; 77958e698SAndreas Gohr 8be906b56SAndreas Gohrclass Preformatted extends AbstractMode 9be906b56SAndreas Gohr{ 10be906b56SAndreas Gohr /** @inheritdoc */ 1171096e46SAndreas Gohr public function getSort() 1271096e46SAndreas Gohr { 1371096e46SAndreas Gohr return 20; 1471096e46SAndreas Gohr } 1571096e46SAndreas Gohr 16b1c59bedSAndreas Gohr /** 17b1c59bedSAndreas Gohr * Number of leading spaces that trigger a preformatted block. 18b1c59bedSAndreas Gohr * 19*47a02a10SAndreas Gohr * DokuWiki's historical value is 2 spaces; Markdown uses 4. When the 20*47a02a10SAndreas Gohr * active parse prefers Markdown (md or md+dw) we flip to 4 so indented 21*47a02a10SAndreas Gohr * code blocks match GFM. A single tab is always a trigger regardless 22*47a02a10SAndreas Gohr * of the space threshold. 23b1c59bedSAndreas Gohr */ 24b1c59bedSAndreas Gohr protected function getIndentWidth(): int 25b1c59bedSAndreas Gohr { 26*47a02a10SAndreas Gohr return $this->registry->isMdPreferred() ? 4 : 2; 27b1c59bedSAndreas Gohr } 28b1c59bedSAndreas Gohr 2971096e46SAndreas Gohr /** @inheritdoc */ 30be906b56SAndreas Gohr public function connectTo($mode) 31be906b56SAndreas Gohr { 32b1c59bedSAndreas Gohr $indent = str_repeat(' ', $this->getIndentWidth()); 33b1c59bedSAndreas Gohr 3496d096f1SAndreas Gohr $this->Lexer->addEntryPattern('\n' . $indent, $mode, 'preformatted'); 3596d096f1SAndreas Gohr $this->Lexer->addEntryPattern('\n\t', $mode, 'preformatted'); 36be906b56SAndreas Gohr 37259e91d9SAndreas Gohr // match continuation lines inside the preformatted block 38b1c59bedSAndreas Gohr $this->Lexer->addPattern('\n' . $indent, 'preformatted'); 39be906b56SAndreas Gohr $this->Lexer->addPattern('\n\t', 'preformatted'); 40be906b56SAndreas Gohr } 41be906b56SAndreas Gohr 42be906b56SAndreas Gohr /** @inheritdoc */ 43be906b56SAndreas Gohr public function postConnect() 44be906b56SAndreas Gohr { 45f57da51cSAndreas Gohr // Two exits: a zero-width lookahead when the next line starts with 46f57da51cSAndreas Gohr // non-whitespace content (so the boundary \n stays in the stream 47f57da51cSAndreas Gohr // and downstream block-level modes like GfmHr or GfmHeader can 48f57da51cSAndreas Gohr // anchor on it), and a consuming \n fall-through for blank lines 49f57da51cSAndreas Gohr // and end-of-input. The lookahead-only branch is registered first 50f57da51cSAndreas Gohr // so PCRE's leftmost-first alternation prefers it whenever it 51f57da51cSAndreas Gohr // applies; the consuming branch handles the cases where it cannot. 52f57da51cSAndreas Gohr $this->Lexer->addExitPattern('(?=\n[^ \t\n])', 'preformatted'); 53be906b56SAndreas Gohr $this->Lexer->addExitPattern('\n', 'preformatted'); 54be906b56SAndreas Gohr } 55be906b56SAndreas Gohr 56be906b56SAndreas Gohr /** @inheritdoc */ 5771096e46SAndreas Gohr public function handle($match, $state, $pos, Handler $handler) 58be906b56SAndreas Gohr { 5971096e46SAndreas Gohr switch ($state) { 6071096e46SAndreas Gohr case DOKU_LEXER_ENTER: 6171096e46SAndreas Gohr $handler->setCallWriter(new PreformattedHandler($handler->getCallWriter())); 6271096e46SAndreas Gohr $handler->addCall('preformatted_start', [], $pos); 6371096e46SAndreas Gohr break; 6471096e46SAndreas Gohr case DOKU_LEXER_EXIT: 6571096e46SAndreas Gohr $handler->addCall('preformatted_end', [], $pos); 6671096e46SAndreas Gohr /** @var PreformattedHandler $reWriter */ 6771096e46SAndreas Gohr $reWriter = $handler->getCallWriter(); 6871096e46SAndreas Gohr $handler->setCallWriter($reWriter->process()); 6971096e46SAndreas Gohr break; 7071096e46SAndreas Gohr case DOKU_LEXER_MATCHED: 7171096e46SAndreas Gohr $handler->addCall('preformatted_newline', [], $pos); 7271096e46SAndreas Gohr break; 7371096e46SAndreas Gohr case DOKU_LEXER_UNMATCHED: 7471096e46SAndreas Gohr $handler->addCall('preformatted_content', [$match], $pos); 7571096e46SAndreas Gohr break; 7671096e46SAndreas Gohr } 7771096e46SAndreas Gohr 7871096e46SAndreas Gohr return true; 79be906b56SAndreas Gohr } 80be906b56SAndreas Gohr} 81