PmWiki recipe that lets you embed Pastebin pastes in a wikipage.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

pastebin-embed.php 8.2KB

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