PmWiki recipe that lets you embed Pastebin pastes in a wikipage.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pastebin-embed.php 8.3KB

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