PmWiki recipe that lets you embed Pastebin pastes in a wikipage.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

pastebin-embed.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php if (!defined('PmWiki')) exit();
  2. /** \pastebin-embed.php
  3. * \Copyright 2017-2021 Said Achmiz
  4. * \Licensed under the MIT License
  5. * \brief Embed Pastebin pastes in a wikipage.
  6. */
  7. $RecipeInfo['PastebinEmbed']['Version'] = '2021-12-05';
  8. ## (:pastebin-embed:)
  9. Markup('pastebin-embed', '<fulltext', '/\(:pastebin-embed\s+(.+?)\s*:\)/', 'PastebinEmbed');
  10. SDV($PastebinEmbedHighlightStyle, "background-color: yellow;");
  11. function PastebinEmbed ($m) {
  12. static $id = 1;
  13. ## Parse arguments to the markup.
  14. $parsed = ParseArgs($m[1]);
  15. ## These are the ‘bare’ arguments (ones which don't require a key, just value(s)).
  16. $args = $parsed[''];
  17. $paste_id = $args[0];
  18. $noJS = in_array('no-js', $args);
  19. $noFooter = in_array('nofooter', $args);
  20. $noLineNumbers = in_array('nolinenums', $args);
  21. $raw = in_array('raw', $args);
  22. $noPre = in_array('no-pre', $args);
  23. ## Convert the comma-delimited line ranges to an array containing each line to be
  24. ## included as values.
  25. ## Note that the line numbers will be zero-indexed (for use with raw text, etc.).
  26. $line_ranges = $parsed['lines']
  27. ? explode(',', $parsed['lines'])
  28. : [ ];
  29. $line_numbers = [ ];
  30. $to_end_from = -1;
  31. foreach ($line_ranges as $key => $line_range) {
  32. if (preg_match("/([0-9]+)[-–]([0-9]+)/", $line_range, $m)) {
  33. $line_numbers = array_merge($line_numbers, range(--$m[1],--$m[2]));
  34. } else if (preg_match("/([0-9]+)[-–]$/", $line_range, $m)) {
  35. $line_numbers[] = $to_end_from = --$m[1];
  36. } else {
  37. $line_numbers[] = --$line_range;
  38. }
  39. }
  40. ## Same thing, but for highlighted line ranges.
  41. $hl_line_ranges = $parsed['hl']
  42. ? explode(',', $parsed['hl'])
  43. : [ ];
  44. $hl_line_numbers = [ ];
  45. $hl_to_end_from = -1;
  46. foreach ($hl_line_ranges as $key => $hl_line_range) {
  47. if (preg_match("/([0-9]+)[-–]([0-9]+)/", $hl_line_range, $m)) {
  48. $hl_line_numbers = array_merge($hl_line_numbers, range(--$m[1],--$m[2]));
  49. } else if (preg_match("/([0-9]+)[-–]$/", $hl_line_range, $m)) {
  50. $hl_line_numbers[] = $hl_to_end_from = --$m[1];
  51. } else {
  52. $hl_line_numbers[] = --$hl_line_range;
  53. }
  54. }
  55. $embed_js_url = "https://pastebin.com/embed_js/$paste_id";
  56. $embed_iframe_url = "https://pastebin.com/embed_iframe/$paste_id";
  57. $embed_raw_url = "https://pastebin.com/raw/$paste_id";
  58. $out = "<span class='pastebin-embed-error'>Unknown error.</span>";
  59. ## There are three ‘modes’: raw (retrieve just the text, client-side, and insert it
  60. ## into the page), no-js (retrieve the HTML, server-side, and insert it into the
  61. ## page), and default (i.e., JS-based: insert the scriptlet, and let it retrieve and
  62. ## insert the HTML into the page, client-side & async).
  63. ## The mode is set by arguments to the (:pastebin-embed:) markup:
  64. ## - if neither the ‘raw’ nor the ‘no-js’ option is given, default (JS) mode is used
  65. ## - if the ‘raw’ option is given, raw mode is used
  66. ## - if the ‘no-js’ option is given, no-js mode is used
  67. if ($raw) {
  68. $raw_text = file_get_contents($embed_raw_url);
  69. if (!$raw_text) return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
  70. $raw_lines = explode("\n", $raw_text);
  71. ## Convert HTML entities.
  72. if (!$noPre) {
  73. foreach ($raw_lines as $line)
  74. $line = PVSE($line);
  75. }
  76. ## Highlighting only works if no-pre is NOT enabled.
  77. if (!empty($hl_line_numbers) && !$noPre) {
  78. if ($hl_to_end_from >= 0)
  79. $hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($raw_lines) - 1));
  80. foreach ($hl_line_numbers as $l) {
  81. $raw_lines[$l] = "<span class='pastebin-embed-highlighted-line'>" . rtrim($raw_lines[$l]) . "</span>";
  82. }
  83. }
  84. ## Filter by line number ranges, if specified.
  85. if (!empty($line_numbers)) {
  86. if ($to_end_from >= 0)
  87. $line_numbers = array_merge($line_numbers, range($to_end_from, count($raw_lines) - 1));
  88. $raw_lines = array_intersect_key($raw_lines, array_flip($line_numbers));
  89. }
  90. $raw_text = implode("\n", $raw_lines);
  91. ## The ‘no-pre’ option means we shouldn’t wrap the text in a <pre> tag.
  92. $out = $noPre
  93. ? $raw_text
  94. : Keep("<pre class='escaped embedPastebinRaw' id='pastebinEmbed_$id'>\n" . $raw_text . "\n</pre>\n");
  95. } else if ($noJS) {
  96. include_once('simple_html_dom.php');
  97. $content_html = file_get_html($embed_iframe_url);
  98. if (!$content_html) return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
  99. $content = $content_html->find(".embedPastebin", 0);
  100. $content->id = "pastebinEmbed_$id";
  101. $styles_html = file_get_html($embed_js_url);
  102. if (!$styles_html) return Keep("<span class='pastebin-embed-error'>Could not retrieve styles!</span>");
  103. $styles = $styles_html->find("style", 0);
  104. ## Filter specified line ranges (if any have been specified via the lines=
  105. ## parameter).
  106. if (!empty($line_numbers)) {
  107. $lines = $content_html->find(".embedPastebin > ol > li");
  108. if ($to_end_from >= 0)
  109. $line_numbers = array_merge($line_numbers, range($to_end_from, count($lines) - 1));
  110. foreach ($lines as $i => $line) {
  111. if (!in_array($i, $line_numbers))
  112. $line->outertext = '';
  113. else
  114. $line->value = ++$i;
  115. }
  116. }
  117. ## Highlight specified line ranges (if any have been specified via the hl=
  118. ## parameter).
  119. if (!empty($hl_line_numbers)) {
  120. $lines = $content_html->find(".embedPastebin > ol > li");
  121. if ($hl_to_end_from >= 0)
  122. $hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($lines) - 1));
  123. foreach ($lines as $i => $line) {
  124. if (in_array($i, $hl_line_numbers)) {
  125. $line->children(0)->class .= " pastebin-embed-highlighted-line";
  126. }
  127. }
  128. }
  129. $out = Keep($styles.$content);
  130. } else {
  131. $out = Keep("<script id='pastebinEmbedScript_$id' src='$embed_js_url'></script>");
  132. if ( !empty($hl_line_numbers)
  133. || !empty($line_numbers)) {
  134. $line_numbers_js = "[ ".implode(", ",$line_numbers)." ]";
  135. $hl_line_numbers_js = "[ ".implode(", ",$hl_line_numbers)." ]";
  136. $out .= Keep("
  137. <script>
  138. var num_lines = document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelectorAll('.embedPastebin > ol > li').length;
  139. var line_numbers = $line_numbers_js;
  140. var to_end_from = $to_end_from;
  141. if (to_end_from >= 0)
  142. line_numbers = [...line_numbers, ...[...Array(num_lines - to_end_from)].map((_, i) => to_end_from + i)];
  143. var hl_line_numbers = $hl_line_numbers_js;
  144. var hl_to_end_from = $hl_to_end_from;
  145. if (hl_to_end_from >= 0)
  146. hl_line_numbers = [...hl_line_numbers, ...[...Array(num_lines - hl_to_end_from)].map((_, i) => hl_to_end_from + i)];
  147. document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelectorAll('.embedPastebin > ol > li').forEach(function (line, i) {
  148. // Highlight specified line ranges (if any have been specified via the hl= parameter).
  149. if (hl_line_numbers.indexOf(i) != -1)
  150. line.firstChild.className += ' pastebin-embed-highlighted-line';
  151. // Filter specified line ranges (if any have been specified via the lines= parameter).
  152. if (line_numbers.length > 0) {
  153. if (line_numbers.indexOf(i) == -1)
  154. line.parentElement.removeChild(line);
  155. else
  156. line.value = ++i;
  157. }
  158. });
  159. </script>
  160. ");
  161. }
  162. PastebinEmbedAppendFooter();
  163. }
  164. global $HTMLStylesFmt;
  165. if (!$raw && $noFooter) {
  166. $HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .embedFooter { display: none; }\n";
  167. }
  168. if (!$raw && $noLineNumbers) {
  169. $HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id > ol { padding-left: 5px; }\n";
  170. }
  171. PastebinEmbedInjectStyles();
  172. $id++;
  173. return $out;
  174. }
  175. function PastebinEmbedAppendFooter() {
  176. static $ran_once = false;
  177. if (!$ran_once) {
  178. global $HTMLFooterFmt;
  179. $HTMLFooterFmt[] =
  180. "<script>
  181. document.querySelectorAll('div.embedPastebin').forEach(function (embed) {
  182. if (embed.previousSibling && embed.previousSibling.tagName == 'P') {
  183. embed.id = 'pastebinEmbed_' + embed.previousSibling.firstChild.id.substring(20);
  184. }
  185. });
  186. </script>\n";
  187. }
  188. $ran_once = true;
  189. }
  190. function PastebinEmbedInjectStyles() {
  191. static $ran_once = false;
  192. if (!$ran_once) {
  193. global $HTMLStylesFmt, $PastebinEmbedHighlightStyle;
  194. $styles = "
  195. .embedPastebinRaw .pastebin-embed-highlighted-line { $PastebinEmbedHighlightStyle display: inline-block; width: calc(100% + 4px); padding-left: 4px; margin-left: -4px; }
  196. .embedPastebin li .pastebin-embed-highlighted-line { $PastebinEmbedHighlightStyle }
  197. #wikitext .embedPastebin ol { margin: 0; padding: 0 0 0 60px; }
  198. ";
  199. $HTMLStylesFmt['pastebin-embed'][] = $styles;
  200. }
  201. $ran_once = true;
  202. }