1e89aeebdSAndreas Gohr<?php 2e89aeebdSAndreas Gohr 3e89aeebdSAndreas Gohrnamespace dokuwiki\Parsing\ParserMode; 4e89aeebdSAndreas Gohr 5e89aeebdSAndreas Gohruse dokuwiki\Parsing\Handler; 674031e46SAndreas Gohruse dokuwiki\Parsing\Helpers\Escape; 7eb15e634SAndreas Gohruse dokuwiki\Parsing\Helpers\HtmlEntity; 81e28e406SAndreas Gohruse dokuwiki\Parsing\Helpers\Link; 91e28e406SAndreas Gohruse dokuwiki\Parsing\Helpers\Media as MediaHelper; 10e89aeebdSAndreas Gohr 11e89aeebdSAndreas Gohr/** 12e89aeebdSAndreas Gohr * GFM inline link [text](url) with optional title [text](url "title"). 13e89aeebdSAndreas Gohr * 143440a8c0SAndreas Gohr * The link text may be either plain text (the common case) or an inline 153440a8c0SAndreas Gohr * image `` — the Markdown equivalent of DW's 163440a8c0SAndreas Gohr * `[[target|{{imgUrl}}]]`. The image-as-label form emits a single link 173440a8c0SAndreas Gohr * handler call with a media descriptor array in the label slot, reusing 183440a8c0SAndreas Gohr * the same flow that `Internallink` already drives. No new handler 193440a8c0SAndreas Gohr * instructions; renderers (xhtml, odt, metadata, …) already know how to 203440a8c0SAndreas Gohr * render a link whose label is a media descriptor. 213440a8c0SAndreas Gohr * 223440a8c0SAndreas Gohr * Mirrors DW's `Internallink` architecture: a permissive outer pattern 233440a8c0SAndreas Gohr * plus handle-time parsing, rather than encoding every GFM rule at 243440a8c0SAndreas Gohr * pattern level. 253440a8c0SAndreas Gohr * 26e89aeebdSAndreas Gohr * Deliberately not supported (see skip.php for the affected spec examples): 27e89aeebdSAndreas Gohr * 28e89aeebdSAndreas Gohr * - Reference links [text][id] / [text][] / [foo] — the single-pass 29e89aeebdSAndreas Gohr * lexer cannot resolve forward references to [foo]: url definitions. 303440a8c0SAndreas Gohr * - Pointy-bracket destinations [link](<foo bar>) — the simplified 313440a8c0SAndreas Gohr * pattern will happily match, but handle() produces an internallink 323440a8c0SAndreas Gohr * with a broken src; spec tests for this stay in skip.php. 333440a8c0SAndreas Gohr * - Balanced-parens inside URLs [link](foo(bar)) — matches truncate 343440a8c0SAndreas Gohr * at first `)`, producing odd output; also in skip.php. 35e89aeebdSAndreas Gohr * - Title HTML attribute — DokuWiki link handler instructions have no 36e89aeebdSAndreas Gohr * title-attribute slot, and plumbing one through every renderer just 37e89aeebdSAndreas Gohr * for this is out of scope. The title parses cleanly but is discarded. 383440a8c0SAndreas Gohr * - Mixed text + image in the label ([prefix  suffix](url)) 393440a8c0SAndreas Gohr * — matches DW's policy: Internallink only converts the label to a 403440a8c0SAndreas Gohr * media descriptor when it matches `^{{…}}$` exactly. 41e89aeebdSAndreas Gohr */ 42e89aeebdSAndreas Gohrclass GfmLink extends AbstractMode 43e89aeebdSAndreas Gohr{ 44eb15e634SAndreas Gohr // URL slot character set: any non-paren / non-newline char, OR a 45eb15e634SAndreas Gohr // backslash-escape sequence so an escaped `\)` doesn't terminate the 46eb15e634SAndreas Gohr // URL early (spec examples 504/506/508). Backslash-unescape is 47eb15e634SAndreas Gohr // applied post-extraction; the pattern only needs to keep escaped 48eb15e634SAndreas Gohr // close-parens from prematurely ending the match. 49eb15e634SAndreas Gohr private const URL_CHAR = '(?:\\\\.|[^)\n])'; 50eb15e634SAndreas Gohr 51*0f694376SAndreas Gohr // Label character set: forbids unescaped `[` / `]` so the outer 52*0f694376SAndreas Gohr // bracket pair stays balanced, but allows `\[` / `\]` so an escaped 53*0f694376SAndreas Gohr // bracket can appear inside the label (spec example 523). The same 54*0f694376SAndreas Gohr // backslash-escape trick the URL slot already uses. 55*0f694376SAndreas Gohr private const LABEL_CHAR = '(?:\\\\.|[^\[\]\n])'; 56*0f694376SAndreas Gohr 573440a8c0SAndreas Gohr // Image sub-pattern reused for both the label alternative in the main 583440a8c0SAndreas Gohr // pattern and the image-as-label detector in handle(). No capture 593440a8c0SAndreas Gohr // groups here — the lexer wraps user patterns in a capture and 603440a8c0SAndreas Gohr // additional captures would renumber unpredictably. 61*0f694376SAndreas Gohr private const IMAGE_SUB = '!\[' . self::LABEL_CHAR . '*\]\(' . self::URL_CHAR . '+\)'; 62e89aeebdSAndreas Gohr 63e89aeebdSAndreas Gohr /** @inheritdoc */ 64e89aeebdSAndreas Gohr public function getSort() 65e89aeebdSAndreas Gohr { 66e89aeebdSAndreas Gohr return 300; 67e89aeebdSAndreas Gohr } 68e89aeebdSAndreas Gohr 69e89aeebdSAndreas Gohr /** @inheritdoc */ 70e89aeebdSAndreas Gohr public function connectTo($mode) 71e89aeebdSAndreas Gohr { 72*0f694376SAndreas Gohr // Outer shape: `[text-or-image](url)`. Text class forbids 73*0f694376SAndreas Gohr // unescaped brackets and newlines but allows `\[` / `\]`; the 74*0f694376SAndreas Gohr // image alternative explicitly matches one inline image. URL 75*0f694376SAndreas Gohr // slot is permissive — handle() does URL / title splitting 76*0f694376SAndreas Gohr // post-entry, mirroring how DW Internallink parses inside `[[...]]`. 77*0f694376SAndreas Gohr $pattern = '\[(?!\[)(?:' . self::LABEL_CHAR . '+|' . self::IMAGE_SUB . ')\]\(' . self::URL_CHAR . '+\)'; 78e89aeebdSAndreas Gohr $this->Lexer->addSpecialPattern($pattern, $mode, 'gfm_link'); 79e89aeebdSAndreas Gohr } 80e89aeebdSAndreas Gohr 81e89aeebdSAndreas Gohr /** @inheritdoc */ 82e89aeebdSAndreas Gohr public function handle($match, $state, $pos, Handler $handler) 83e89aeebdSAndreas Gohr { 843440a8c0SAndreas Gohr // Detect image-as-label `[](target)`. Parallels 853440a8c0SAndreas Gohr // Internallink's `^{{…}}$` check — when the label is exactly an 863440a8c0SAndreas Gohr // inline image, parse it into a media descriptor; otherwise 873440a8c0SAndreas Gohr // treat the label as plain text. 88eb15e634SAndreas Gohr if (preg_match('/^\[(' . self::IMAGE_SUB . ')\]\((' . self::URL_CHAR . '+)\)$/', $match, $m)) { 893440a8c0SAndreas Gohr $label = $this->parseImageDescriptor($m[1]); 903440a8c0SAndreas Gohr $targetUrl = $this->extractUrl($m[2]); 913440a8c0SAndreas Gohr } else { 923440a8c0SAndreas Gohr // Plain text label can't contain `]`, so the first `](` is 933440a8c0SAndreas Gohr // the label/target separator. 94e89aeebdSAndreas Gohr $sep = strpos($match, ']('); 9574031e46SAndreas Gohr $label = Escape::unescapeBackslashes(substr($match, 1, $sep - 1)); 963440a8c0SAndreas Gohr $targetUrl = $this->extractUrl(substr($match, $sep + 2, -1)); 973440a8c0SAndreas Gohr } 98e89aeebdSAndreas Gohr 9974031e46SAndreas Gohr // Classify on the raw URL so windowssharelink detection sees the 10074031e46SAndreas Gohr // literal `\\host\path` runs intact — GFM's `\\` → `\` collapse 10174031e46SAndreas Gohr // would otherwise destroy the share prefix. 1021e28e406SAndreas Gohr [$call, $args] = Link::classify($targetUrl, $label); 10374031e46SAndreas Gohr if ($call !== 'windowssharelink') { 10474031e46SAndreas Gohr $args[0] = Escape::unescapeBackslashes($args[0]); 10574031e46SAndreas Gohr } 1063440a8c0SAndreas Gohr $handler->addCall($call, $args, $pos); 107e89aeebdSAndreas Gohr return true; 108e89aeebdSAndreas Gohr } 1093440a8c0SAndreas Gohr 1103440a8c0SAndreas Gohr /** 1113440a8c0SAndreas Gohr * Extract the URL from a parenthesized payload: trim surrounding 112eb15e634SAndreas Gohr * whitespace, take the first whitespace-delimited token, then 113eb15e634SAndreas Gohr * apply GFM's URL-slot transformations (entity decoding; 114eb15e634SAndreas Gohr * backslash-unescape happens later, after Link::classify, because 115eb15e634SAndreas Gohr * windowssharelink detection needs the raw `\\` runs intact). 116eb15e634SAndreas Gohr * Any trailing title is discarded (no renderer slot for it). 1173440a8c0SAndreas Gohr */ 1183440a8c0SAndreas Gohr private function extractUrl(string $inside): string 1193440a8c0SAndreas Gohr { 1203440a8c0SAndreas Gohr $inside = trim($inside); 121eb15e634SAndreas Gohr $url = substr($inside, 0, strcspn($inside, " \t\n")); // remove optional title 122eb15e634SAndreas Gohr return HtmlEntity::decode($url); 1233440a8c0SAndreas Gohr } 1243440a8c0SAndreas Gohr 1253440a8c0SAndreas Gohr /** 1263440a8c0SAndreas Gohr * Parse an inline image sub-match `` into the media 1273440a8c0SAndreas Gohr * descriptor shape Media::parseMedia() returns, so the link handler 1283440a8c0SAndreas Gohr * can treat it as a media label identically to `[[page|{{img}}]]`. 1293440a8c0SAndreas Gohr */ 1303440a8c0SAndreas Gohr private function parseImageDescriptor(string $imageMatch): array 1313440a8c0SAndreas Gohr { 1323440a8c0SAndreas Gohr $sep = strpos($imageMatch, ']('); 13374031e46SAndreas Gohr $alt = Escape::unescapeBackslashes(substr($imageMatch, 2, $sep - 2)); 13474031e46SAndreas Gohr $imgUrl = Escape::unescapeBackslashes($this->extractUrl(substr($imageMatch, $sep + 2, -1))); 1353440a8c0SAndreas Gohr 1361e28e406SAndreas Gohr $p = MediaHelper::parseParameters($imgUrl); 1373440a8c0SAndreas Gohr $type = (media_isexternal($p['src']) || link_isinterwiki($p['src'])) 1383440a8c0SAndreas Gohr ? 'externalmedia' 1393440a8c0SAndreas Gohr : 'internalmedia'; 1403440a8c0SAndreas Gohr 1413440a8c0SAndreas Gohr return [ 1423440a8c0SAndreas Gohr 'type' => $type, 1433440a8c0SAndreas Gohr 'src' => $p['src'], 1443440a8c0SAndreas Gohr 'title' => $alt !== '' ? $alt : null, 1453440a8c0SAndreas Gohr 'align' => $p['align'], 1463440a8c0SAndreas Gohr 'width' => $p['width'], 1473440a8c0SAndreas Gohr 'height' => $p['height'], 1483440a8c0SAndreas Gohr 'cache' => $p['cache'], 1493440a8c0SAndreas Gohr 'linking' => $p['linking'], 1503440a8c0SAndreas Gohr ]; 1513440a8c0SAndreas Gohr } 152e89aeebdSAndreas Gohr} 153