PmWiki recipe that lets you embed Pastebin pastes in a wikipage.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pastebin-embed.php 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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-11';
  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, server-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)
  70. return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
  71. $raw_lines = explode("\n", $raw_text);
  72. ## Convert HTML entities.
  73. if (!$noPre) {
  74. foreach ($raw_lines as $line)
  75. $line = PVSE($line);
  76. }
  77. ## Highlighting only works if no-pre is NOT enabled.
  78. if ( !empty($hl_line_numbers)
  79. && !$noPre) {
  80. if ($hl_to_end_from >= 0)
  81. $hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($raw_lines) - 1));
  82. foreach ($hl_line_numbers as $l) {
  83. $raw_lines[$l] = "<span class='pastebin-embed-highlighted-line'>" . rtrim($raw_lines[$l]) . "</span>";
  84. }
  85. }
  86. ## Filter by line number ranges, if specified.
  87. if (!empty($line_numbers)) {
  88. if ($to_end_from >= 0)
  89. $line_numbers = array_merge($line_numbers, range($to_end_from, count($raw_lines) - 1));
  90. $raw_lines = array_intersect_key($raw_lines, array_flip($line_numbers));
  91. }
  92. $raw_text = implode("\n", $raw_lines);
  93. ## The ‘no-pre’ option means we shouldn’t wrap the text in a <pre> tag.
  94. $out = $noPre
  95. ? $raw_text
  96. : Keep("<pre class='escaped embedPastebinRaw' id='pastebinEmbed_$id'>\n" . $raw_text . "\n</pre>\n");
  97. } else if ($noJS) {
  98. include_once('simple_html_dom.php');
  99. $content_html = file_get_html($embed_iframe_url);
  100. if (!$content_html)
  101. return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
  102. $content = $content_html->find(".embedPastebin", 0);
  103. $content->id = "pastebinEmbed_$id";
  104. $styles_html = file_get_html($embed_js_url);
  105. if (!$styles_html)
  106. return Keep("<span class='pastebin-embed-error'>Could not retrieve styles!</span>");
  107. $styles = $styles_html->find("style", 0);
  108. ## Filter specified line ranges (if any have been specified via the lines=
  109. ## parameter).
  110. if (!empty($line_numbers)) {
  111. $lines = $content_html->find(".embedPastebin > ol > li");
  112. if ($to_end_from >= 0)
  113. $line_numbers = array_merge($line_numbers, range($to_end_from, count($lines) - 1));
  114. foreach ($lines as $i => $line) {
  115. if (!in_array($i, $line_numbers))
  116. $line->outertext = '';
  117. else
  118. $line->value = ++$i;
  119. }
  120. }
  121. ## Highlight specified line ranges (if any have been specified via the hl=
  122. ## parameter).
  123. if (!empty($hl_line_numbers)) {
  124. $lines = $content_html->find(".embedPastebin > ol > li");
  125. if ($hl_to_end_from >= 0)
  126. $hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($lines) - 1));
  127. foreach ($lines as $i => $line) {
  128. if (in_array($i, $hl_line_numbers)) {
  129. $line->children(0)->class .= " pastebin-embed-highlighted-line";
  130. }
  131. }
  132. }
  133. $out = Keep($styles.$content);
  134. } else {
  135. $out = Keep("<script id='pastebinEmbedScript_$id' src='$embed_js_url'></script>");
  136. $out .= Keep("
  137. <script>
  138. document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelector('.embedPastebin').id = 'pastebinEmbed_$id';
  139. </script>
  140. ");
  141. if ( !empty($hl_line_numbers)
  142. || !empty($line_numbers)) {
  143. $line_numbers_js = "[ " . implode(", ", $line_numbers) . " ]";
  144. $hl_line_numbers_js = "[ " . implode(", ", $hl_line_numbers) . " ]";
  145. $out .= Keep("
  146. <script>{
  147. let num_lines = document.querySelector('#pastebinEmbed_$id').querySelectorAll('.embedPastebin > ol > li').length;
  148. let line_numbers = $line_numbers_js;
  149. let to_end_from = $to_end_from;
  150. if (to_end_from >= 0)
  151. line_numbers = [...line_numbers, ...[...Array(num_lines - to_end_from)].map((_, i) => to_end_from + i)];
  152. let hl_line_numbers = $hl_line_numbers_js;
  153. let hl_to_end_from = $hl_to_end_from;
  154. if (hl_to_end_from >= 0)
  155. hl_line_numbers = [...hl_line_numbers, ...[...Array(num_lines - hl_to_end_from)].map((_, i) => hl_to_end_from + i)];
  156. document.querySelector('#pastebinEmbed_$id').querySelectorAll('.embedPastebin .source > ol > li').forEach(function (line, i) {
  157. // Highlight specified line ranges (if any have been specified via the hl= parameter).
  158. if (hl_line_numbers.indexOf(i) != -1)
  159. line.firstChild.className += ' pastebin-embed-highlighted-line';
  160. // Filter specified line ranges (if any have been specified via the lines= parameter).
  161. if (line_numbers.length > 0) {
  162. if (line_numbers.indexOf(i) == -1)
  163. line.parentElement.removeChild(line);
  164. else
  165. line.value = ++i;
  166. }
  167. });
  168. }</script>
  169. ");
  170. }
  171. }
  172. global $HTMLStylesFmt;
  173. if (!$raw && $noFooter) {
  174. $HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .embedFooter { display: none; }\n";
  175. }
  176. if (!$raw && $noLineNumbers) {
  177. $HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .source > ol { padding-left: 5px; }\n";
  178. }
  179. PastebinEmbedInjectStyles();
  180. $id++;
  181. return $out;
  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. }