1<?php 2 3namespace dokuwiki\test\Parsing\Markdown; 4 5use Doku_Renderer_xhtml; 6 7/** 8 * XHTML renderer tuned to emit the minimal HTML shape GFM's spec.txt uses. 9 * 10 * DokuWiki's production XHTML renderer wraps internal media in details 11 * links pointing at `/lib/exe/fetch.php?media=...` / `/lib/exe/detail.php?media=...`, 12 * rewrites internal link hrefs to `/doku.php?id=...`, and adds wiki-specific 13 * classes and attributes. All of this is correct for live wiki pages but 14 * diverges byte-for-byte from GFM's bare `<img src="...">` and 15 * `<a href="...">...</a>`. 16 * 17 * This renderer is used only by {@see GfmSpecTest} so the spec roundtrip 18 * can compare against byte-level spec HTML. Production rendering is 19 * unchanged. Methods not overridden here fall through to the XHTML 20 * renderer (paragraphs, emphasis, code spans, lists, etc.) — those render 21 * the same shape the spec expects. 22 * 23 * Note: title attributes on links/images are discarded at handle time 24 * (no DW instruction slot), so spec examples that expect `title="..."` 25 * still don't pass and stay in `skip.php`. 26 */ 27class SpecCompatRenderer extends Doku_Renderer_xhtml 28{ 29 public function table_open($maxcols = null, $numrows = null, $pos = null, $classes = null) 30 { 31 // Production DW wraps `<table>` in `<div class="table"><table class="inline">`; 32 // the spec expects bare `<table>`. 33 $this->doc .= "<table>\n"; 34 } 35 36 public function table_close($pos = null) 37 { 38 // Drop the matching `</div>` from the production wrapper. 39 $this->doc .= "</table>"; 40 } 41 42 public function tablerow_open($classes = null) 43 { 44 // Strip DW's `class="rowN"` row counter — spec rows have no class. 45 $this->doc .= "<tr>\n"; 46 } 47 48 public function tableheader_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) 49 { 50 // Production DW emits alignment as `class="...align"`; the spec uses 51 // an `align="..."` attribute. Drop the `class="colN"` counter too. 52 $this->doc .= '<th' . $this->alignAttr($align) . '>'; 53 } 54 55 public function tablecell_open($colspan = 1, $align = null, $rowspan = 1, $classes = null) 56 { 57 $this->doc .= '<td' . $this->alignAttr($align) . '>'; 58 } 59 60 private function alignAttr(?string $align): string 61 { 62 if ($align === null) return ''; 63 return ' align="' . $align . '"'; 64 } 65 66 public function internalmedia( 67 $src, 68 $title = null, 69 $align = null, 70 $width = null, 71 $height = null, 72 $cache = null, 73 $linking = null, 74 $return = false 75 ) { 76 $this->doc .= $this->specImg($src, $title, $width, $height); 77 } 78 79 public function externalmedia( 80 $src, 81 $title = null, 82 $align = null, 83 $width = null, 84 $height = null, 85 $cache = null, 86 $linking = null, 87 $return = false 88 ) { 89 $this->doc .= $this->specImg($src, $title, $width, $height); 90 } 91 92 public function internallink($id, $name = null, $search = null, $returnonly = false, $linktype = 'content') 93 { 94 $this->doc .= $this->specLink($id, $name); 95 } 96 97 public function externallink($url, $name = null, $returnonly = false) 98 { 99 $this->doc .= $this->specLink($url, $name); 100 } 101 102 public function interwikilink($match, $name, $wikiName, $wikiUri, $returnonly = false) 103 { 104 // Spec has no interwiki expectations; emit the raw `wp>Page` form as 105 // href so the mode is still visible but obviously non-standard. 106 $this->doc .= $this->specLink($match, $name); 107 } 108 109 public function emaillink($address, $name = null, $returnonly = false) 110 { 111 $this->doc .= $this->specLink('mailto:' . $address, $name ?? $address); 112 } 113 114 public function locallink($hash, $name = null, $returnonly = false) 115 { 116 $this->doc .= $this->specLink('#' . $hash, $name ?? $hash); 117 } 118 119 public function windowssharelink($url, $name = null, $returnonly = false) 120 { 121 $this->doc .= $this->specLink($url, $name); 122 } 123 124 public function code($text, $language = null, $filename = null, $options = null) 125 { 126 $this->doc .= $this->specCode($text, $language); 127 } 128 129 public function listu_open($classes = null) 130 { 131 $this->doc .= "<ul>\n"; 132 } 133 134 public function listu_close() 135 { 136 $this->doc .= "</ul>\n"; 137 } 138 139 public function listo_open($classes = null) 140 { 141 $this->doc .= "<ol>\n"; 142 } 143 144 public function listo_open_start($start = 1) 145 { 146 $start = (int) $start; 147 if ($start === 1) { 148 $this->listo_open(); 149 return; 150 } 151 $this->doc .= '<ol start="' . $start . "\">\n"; 152 } 153 154 public function listo_close() 155 { 156 $this->doc .= "</ol>\n"; 157 } 158 159 public function listitem_open($level, $node = false) 160 { 161 $this->doc .= '<li>'; 162 } 163 164 public function listitem_close() 165 { 166 $this->doc .= "</li>\n"; 167 } 168 169 public function listcontent_open() 170 { 171 // GFM has no per-item content wrapper - tight items put text directly 172 // inside <li>, loose items wrap it in <p>. The handler emits/strips 173 // p_open / p_close to drive that distinction; the wrapper itself 174 // produces no output here. 175 } 176 177 public function listcontent_close() 178 { 179 } 180 181 public function file($text, $language = null, $filename = null, $options = null) 182 { 183 $this->doc .= $this->specCode($text, $language); 184 } 185 186 public function preformatted($text) 187 { 188 // The Preformatted CallWriter rewriter collapses start/content/ 189 // newline/end into one `preformatted` call. GFM expects the body 190 // to end with a newline (spec example 104); DW's internal text 191 // loses it to `trim()`, so we re-append here. 192 $this->doc .= $this->specCode($text . "\n", null); 193 } 194 195 /** 196 * GFM shape: <pre><code class="language-xxx">...</code></pre>. The 197 * production DW renderer emits <pre class="code"> with no inner 198 * <code>, which diverges byte-for-byte. 199 */ 200 private function specCode($text, $language): string 201 { 202 $classAttr = ''; 203 if ($language !== null && $language !== '') { 204 $classAttr = ' class="language-' . hsc((string) $language) . '"'; 205 } 206 return '<pre><code' . $classAttr . '>' . hsc((string) $text) . '</code></pre>'; 207 } 208 209 private function specImg($src, $alt, $width, $height): string 210 { 211 $out = '<img src="' . hsc((string) $src) . '"'; 212 $out .= ' alt="' . hsc((string) $alt) . '"'; 213 if ($width !== null) $out .= ' width="' . (int) $width . '"'; 214 if ($height !== null) $out .= ' height="' . (int) $height . '"'; 215 $out .= ' />'; 216 return $out; 217 } 218 219 /** 220 * Emit a bare <a href="...">label</a>. If the label is a media 221 * descriptor array (the shape Media::parseMedia() returns, passed by 222 * Internallink / GfmLink when the label is `{{img}}` / ``), 223 * render the <img> inside the <a>. 224 */ 225 private function specLink($href, $label): string 226 { 227 if (is_array($label) && isset($label['type'])) { 228 $img = $this->specImg( 229 $label['src'], 230 $label['title'], 231 $label['width'] ?? null, 232 $label['height'] ?? null 233 ); 234 return '<a href="' . hsc((string) $href) . '">' . $img . '</a>'; 235 } 236 $text = ($label === null || $label === '') ? $href : $label; 237 return '<a href="' . hsc((string) $href) . '">' . hsc((string) $text) . '</a>'; 238 } 239} 240