xref: /dokuwiki/_test/tests/Parsing/Markdown/SpecCompatRenderer.php (revision 3dabe4e0a0d70b79a7aced8ac8a36d4b37a61024)
13440a8c0SAndreas Gohr<?php
23440a8c0SAndreas Gohr
33440a8c0SAndreas Gohrnamespace dokuwiki\test\Parsing\Markdown;
43440a8c0SAndreas Gohr
53440a8c0SAndreas Gohruse Doku_Renderer_xhtml;
63440a8c0SAndreas Gohr
73440a8c0SAndreas Gohr/**
83440a8c0SAndreas Gohr * XHTML renderer tuned to emit the minimal HTML shape GFM's spec.txt uses.
93440a8c0SAndreas Gohr *
103440a8c0SAndreas Gohr * DokuWiki's production XHTML renderer wraps internal media in details
113440a8c0SAndreas Gohr * links pointing at `/lib/exe/fetch.php?media=...` / `/lib/exe/detail.php?media=...`,
123440a8c0SAndreas Gohr * rewrites internal link hrefs to `/doku.php?id=...`, and adds wiki-specific
133440a8c0SAndreas Gohr * classes and attributes. All of this is correct for live wiki pages but
143440a8c0SAndreas Gohr * diverges byte-for-byte from GFM's bare `<img src="...">` and
153440a8c0SAndreas Gohr * `<a href="...">...</a>`.
163440a8c0SAndreas Gohr *
173440a8c0SAndreas Gohr * This renderer is used only by {@see GfmSpecTest} so the spec roundtrip
183440a8c0SAndreas Gohr * can compare against byte-level spec HTML. Production rendering is
193440a8c0SAndreas Gohr * unchanged. Methods not overridden here fall through to the XHTML
203440a8c0SAndreas Gohr * renderer (paragraphs, emphasis, code spans, lists, etc.) — those render
213440a8c0SAndreas Gohr * the same shape the spec expects.
223440a8c0SAndreas Gohr *
233440a8c0SAndreas Gohr * Note: title attributes on links/images are discarded at handle time
243440a8c0SAndreas Gohr * (no DW instruction slot), so spec examples that expect `title="..."`
253440a8c0SAndreas Gohr * still don't pass and stay in `skip.php`.
263440a8c0SAndreas Gohr */
273440a8c0SAndreas Gohrclass SpecCompatRenderer extends Doku_Renderer_xhtml
283440a8c0SAndreas Gohr{
29*3dabe4e0SAndreas Gohr    public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null)
30*3dabe4e0SAndreas Gohr    {
31*3dabe4e0SAndreas Gohr        // Production DW wraps `<table>` in `<div class="table"><table class="inline">`;
32*3dabe4e0SAndreas Gohr        // the spec expects bare `<table>`.
33*3dabe4e0SAndreas Gohr        $this->doc .= "<table>\n";
34*3dabe4e0SAndreas Gohr    }
35*3dabe4e0SAndreas Gohr
36*3dabe4e0SAndreas Gohr    public function table_close($pos = null)
37*3dabe4e0SAndreas Gohr    {
38*3dabe4e0SAndreas Gohr        // Drop the matching `</div>` from the production wrapper.
39*3dabe4e0SAndreas Gohr        $this->doc .= "</table>";
40*3dabe4e0SAndreas Gohr    }
41*3dabe4e0SAndreas Gohr
42*3dabe4e0SAndreas Gohr    public function tablerow_open($classes = null)
43*3dabe4e0SAndreas Gohr    {
44*3dabe4e0SAndreas Gohr        // Strip DW's `class="rowN"` row counter — spec rows have no class.
45*3dabe4e0SAndreas Gohr        $this->doc .= "<tr>\n";
46*3dabe4e0SAndreas Gohr    }
47*3dabe4e0SAndreas Gohr
48*3dabe4e0SAndreas Gohr    public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
49*3dabe4e0SAndreas Gohr    {
50*3dabe4e0SAndreas Gohr        // Production DW emits alignment as `class="...align"`; the spec uses
51*3dabe4e0SAndreas Gohr        // an `align="..."` attribute. Drop the `class="colN"` counter too.
52*3dabe4e0SAndreas Gohr        $this->doc .= '<th' . $this->alignAttr($align) . '>';
53*3dabe4e0SAndreas Gohr    }
54*3dabe4e0SAndreas Gohr
55*3dabe4e0SAndreas Gohr    public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null)
56*3dabe4e0SAndreas Gohr    {
57*3dabe4e0SAndreas Gohr        $this->doc .= '<td' . $this->alignAttr($align) . '>';
58*3dabe4e0SAndreas Gohr    }
59*3dabe4e0SAndreas Gohr
60*3dabe4e0SAndreas Gohr    private function alignAttr(?string $align): string
61*3dabe4e0SAndreas Gohr    {
62*3dabe4e0SAndreas Gohr        if ($align === null) return '';
63*3dabe4e0SAndreas Gohr        return ' align="' . $align . '"';
64*3dabe4e0SAndreas Gohr    }
65b1c59bedSAndreas Gohr
663440a8c0SAndreas Gohr    public function internalmedia(
673440a8c0SAndreas Gohr        $src,
683440a8c0SAndreas Gohr        $title = null,
693440a8c0SAndreas Gohr        $align = null,
703440a8c0SAndreas Gohr        $width = null,
713440a8c0SAndreas Gohr        $height = null,
723440a8c0SAndreas Gohr        $cache = null,
733440a8c0SAndreas Gohr        $linking = null,
743440a8c0SAndreas Gohr        $return = false
753440a8c0SAndreas Gohr    ) {
763440a8c0SAndreas Gohr        $this->doc .= $this->specImg($src, $title, $width, $height);
773440a8c0SAndreas Gohr    }
783440a8c0SAndreas Gohr
793440a8c0SAndreas Gohr    public function externalmedia(
803440a8c0SAndreas Gohr        $src,
813440a8c0SAndreas Gohr        $title = null,
823440a8c0SAndreas Gohr        $align = null,
833440a8c0SAndreas Gohr        $width = null,
843440a8c0SAndreas Gohr        $height = null,
853440a8c0SAndreas Gohr        $cache = null,
863440a8c0SAndreas Gohr        $linking = null,
873440a8c0SAndreas Gohr        $return = false
883440a8c0SAndreas Gohr    ) {
893440a8c0SAndreas Gohr        $this->doc .= $this->specImg($src, $title, $width, $height);
903440a8c0SAndreas Gohr    }
913440a8c0SAndreas Gohr
923440a8c0SAndreas Gohr    public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content')
933440a8c0SAndreas Gohr    {
943440a8c0SAndreas Gohr        $this->doc .= $this->specLink($id, $name);
953440a8c0SAndreas Gohr    }
963440a8c0SAndreas Gohr
973440a8c0SAndreas Gohr    public function externallink($url, $name = null, $returnonly = false)
983440a8c0SAndreas Gohr    {
993440a8c0SAndreas Gohr        $this->doc .= $this->specLink($url, $name);
1003440a8c0SAndreas Gohr    }
1013440a8c0SAndreas Gohr
1023440a8c0SAndreas Gohr    public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false)
1033440a8c0SAndreas Gohr    {
1043440a8c0SAndreas Gohr        // Spec has no interwiki expectations; emit the raw `wp>Page` form as
1053440a8c0SAndreas Gohr        // href so the mode is still visible but obviously non-standard.
1063440a8c0SAndreas Gohr        $this->doc .= $this->specLink($match, $name);
1073440a8c0SAndreas Gohr    }
1083440a8c0SAndreas Gohr
1093440a8c0SAndreas Gohr    public function emaillink($address, $name = null, $returnonly = false)
1103440a8c0SAndreas Gohr    {
1113440a8c0SAndreas Gohr        $this->doc .= $this->specLink('mailto:' . $address, $name ?? $address);
1123440a8c0SAndreas Gohr    }
1133440a8c0SAndreas Gohr
1143440a8c0SAndreas Gohr    public function locallink($hash, $name = null, $returnonly = false)
1153440a8c0SAndreas Gohr    {
1163440a8c0SAndreas Gohr        $this->doc .= $this->specLink('#' . $hash, $name ?? $hash);
1173440a8c0SAndreas Gohr    }
1183440a8c0SAndreas Gohr
1193440a8c0SAndreas Gohr    public function windowssharelink($url, $name = null, $returnonly = false)
1203440a8c0SAndreas Gohr    {
1213440a8c0SAndreas Gohr        $this->doc .= $this->specLink($url, $name);
1223440a8c0SAndreas Gohr    }
1233440a8c0SAndreas Gohr
124b1c59bedSAndreas Gohr    public function code($text, $language = null, $filename = null, $options = null)
125b1c59bedSAndreas Gohr    {
126b1c59bedSAndreas Gohr        $this->doc .= $this->specCode($text, $language);
127b1c59bedSAndreas Gohr    }
128b1c59bedSAndreas Gohr
129685560ebSAndreas Gohr    public function listu_open($classes = null)
130685560ebSAndreas Gohr    {
131685560ebSAndreas Gohr        $this->doc .= "<ul>\n";
132685560ebSAndreas Gohr    }
133685560ebSAndreas Gohr
134685560ebSAndreas Gohr    public function listu_close()
135685560ebSAndreas Gohr    {
136685560ebSAndreas Gohr        $this->doc .= "</ul>\n";
137685560ebSAndreas Gohr    }
138685560ebSAndreas Gohr
139685560ebSAndreas Gohr    public function listo_open($classes = null, $start = 1)
140685560ebSAndreas Gohr    {
141685560ebSAndreas Gohr        if ((int) $start !== 1) {
142685560ebSAndreas Gohr            $this->doc .= '<ol start="' . (int) $start . "\">\n";
143685560ebSAndreas Gohr        } else {
144685560ebSAndreas Gohr            $this->doc .= "<ol>\n";
145685560ebSAndreas Gohr        }
146685560ebSAndreas Gohr    }
147685560ebSAndreas Gohr
148685560ebSAndreas Gohr    public function listo_close()
149685560ebSAndreas Gohr    {
150685560ebSAndreas Gohr        $this->doc .= "</ol>\n";
151685560ebSAndreas Gohr    }
152685560ebSAndreas Gohr
153685560ebSAndreas Gohr    public function listitem_open($level, $node = false)
154685560ebSAndreas Gohr    {
155685560ebSAndreas Gohr        $this->doc .= '<li>';
156685560ebSAndreas Gohr    }
157685560ebSAndreas Gohr
158685560ebSAndreas Gohr    public function listitem_close()
159685560ebSAndreas Gohr    {
160685560ebSAndreas Gohr        $this->doc .= "</li>\n";
161685560ebSAndreas Gohr    }
162685560ebSAndreas Gohr
163685560ebSAndreas Gohr    public function listcontent_open()
164685560ebSAndreas Gohr    {
165685560ebSAndreas Gohr        // GFM has no per-item content wrapper - tight items put text directly
166685560ebSAndreas Gohr        // inside <li>, loose items wrap it in <p>. The handler emits/strips
167685560ebSAndreas Gohr        // p_open / p_close to drive that distinction; the wrapper itself
168685560ebSAndreas Gohr        // produces no output here.
169685560ebSAndreas Gohr    }
170685560ebSAndreas Gohr
171685560ebSAndreas Gohr    public function listcontent_close()
172685560ebSAndreas Gohr    {
173685560ebSAndreas Gohr    }
174685560ebSAndreas Gohr
175b1c59bedSAndreas Gohr    public function file($text, $language = null, $filename = null, $options = null)
176b1c59bedSAndreas Gohr    {
177b1c59bedSAndreas Gohr        $this->doc .= $this->specCode($text, $language);
178b1c59bedSAndreas Gohr    }
179b1c59bedSAndreas Gohr
180b1c59bedSAndreas Gohr    public function preformatted($text)
181b1c59bedSAndreas Gohr    {
182b1c59bedSAndreas Gohr        // The Preformatted CallWriter rewriter collapses start/content/
183b1c59bedSAndreas Gohr        // newline/end into one `preformatted` call. GFM expects the body
184b1c59bedSAndreas Gohr        // to end with a newline (spec example 104); DW's internal text
185b1c59bedSAndreas Gohr        // loses it to `trim()`, so we re-append here.
186b1c59bedSAndreas Gohr        $this->doc .= $this->specCode($text . "\n", null);
187b1c59bedSAndreas Gohr    }
188b1c59bedSAndreas Gohr
189b1c59bedSAndreas Gohr    /**
190b1c59bedSAndreas Gohr     * GFM shape: <pre><code class="language-xxx">...</code></pre>. The
191b1c59bedSAndreas Gohr     * production DW renderer emits <pre class="code"> with no inner
192b1c59bedSAndreas Gohr     * <code>, which diverges byte-for-byte.
193b1c59bedSAndreas Gohr     */
194b1c59bedSAndreas Gohr    private function specCode($text, $language): string
195b1c59bedSAndreas Gohr    {
196b1c59bedSAndreas Gohr        $classAttr = '';
197b1c59bedSAndreas Gohr        if ($language !== null && $language !== '') {
198b1c59bedSAndreas Gohr            $classAttr = ' class="language-' . hsc((string) $language) . '"';
199b1c59bedSAndreas Gohr        }
200b1c59bedSAndreas Gohr        return '<pre><code' . $classAttr . '>' . hsc((string) $text) . '</code></pre>';
201b1c59bedSAndreas Gohr    }
202b1c59bedSAndreas Gohr
2033440a8c0SAndreas Gohr    private function specImg($src, $alt, $width, $height): string
2043440a8c0SAndreas Gohr    {
2053440a8c0SAndreas Gohr        $out = '<img src="' . hsc((string) $src) . '"';
2063440a8c0SAndreas Gohr        $out .= ' alt="' . hsc((string) $alt) . '"';
2073440a8c0SAndreas Gohr        if ($width !== null)  $out .= ' width="' . (int) $width . '"';
2083440a8c0SAndreas Gohr        if ($height !== null) $out .= ' height="' . (int) $height . '"';
2093440a8c0SAndreas Gohr        $out .= ' />';
2103440a8c0SAndreas Gohr        return $out;
2113440a8c0SAndreas Gohr    }
2123440a8c0SAndreas Gohr
2133440a8c0SAndreas Gohr    /**
2143440a8c0SAndreas Gohr     * Emit a bare <a href="...">label</a>. If the label is a media
2153440a8c0SAndreas Gohr     * descriptor array (the shape Media::parseMedia() returns, passed by
2163440a8c0SAndreas Gohr     * Internallink / GfmLink when the label is `{{img}}` / `![alt](img)`),
2173440a8c0SAndreas Gohr     * render the <img> inside the <a>.
2183440a8c0SAndreas Gohr     */
2193440a8c0SAndreas Gohr    private function specLink($href, $label): string
2203440a8c0SAndreas Gohr    {
2213440a8c0SAndreas Gohr        if (is_array($label) && isset($label['type'])) {
2223440a8c0SAndreas Gohr            $img = $this->specImg(
2233440a8c0SAndreas Gohr                $label['src'],
2243440a8c0SAndreas Gohr                $label['title'],
2253440a8c0SAndreas Gohr                $label['width'] ?? null,
2263440a8c0SAndreas Gohr                $label['height'] ?? null
2273440a8c0SAndreas Gohr            );
2283440a8c0SAndreas Gohr            return '<a href="' . hsc((string) $href) . '">' . $img . '</a>';
2293440a8c0SAndreas Gohr        }
2303440a8c0SAndreas Gohr        $text = ($label === null || $label === '') ? $href : $label;
2313440a8c0SAndreas Gohr        return '<a href="' . hsc((string) $href) . '">' . hsc((string) $text) . '</a>';
2323440a8c0SAndreas Gohr    }
2333440a8c0SAndreas Gohr}
234