Compare commits
No commits in common. "e211db1ddd786384e58d1938a0f044ee6fc26c95" and "03ecce520fae7c69b034d2e0dad07741dd986a21" have entirely different histories.
e211db1ddd
...
03ecce520f
@ -4,10 +4,10 @@
|
|||||||
* \Licensed under the MIT License
|
* \Licensed under the MIT License
|
||||||
* \brief Embed Pastebin pastes in a wikipage.
|
* \brief Embed Pastebin pastes in a wikipage.
|
||||||
*/
|
*/
|
||||||
$RecipeInfo['PastebinEmbed']['Version'] = '2021-12-11';
|
$RecipeInfo['PastebinEmbed']['Version'] = '2018-10-19';
|
||||||
|
|
||||||
## (:pastebin-embed:)
|
## (:pastebin-embed:)
|
||||||
Markup('pastebin-embed', '<fulltext', '/\(:pastebin-embed\s+(.+?)\s*:\)/', 'PastebinEmbed');
|
Markup('pastebin-embed', '<fulltext', '/\\(:pastebin-embed\\s+(.+?)\\s*:\\)/', 'PastebinEmbed');
|
||||||
|
|
||||||
SDV($PastebinEmbedHighlightStyle, "background-color: yellow;");
|
SDV($PastebinEmbedHighlightStyle, "background-color: yellow;");
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ function PastebinEmbed ($m) {
|
|||||||
## Parse arguments to the markup.
|
## Parse arguments to the markup.
|
||||||
$parsed = ParseArgs($m[1]);
|
$parsed = ParseArgs($m[1]);
|
||||||
|
|
||||||
## These are the ‘bare’ arguments (ones which don't require a key, just value(s)).
|
## These are the "bare" arguments (ones which don't require a key, just value(s)).
|
||||||
$args = $parsed[''];
|
$args = $parsed[''];
|
||||||
$paste_id = $args[0];
|
$paste_id = $args[0];
|
||||||
$noJS = in_array('no-js', $args);
|
$noJS = in_array('no-js', $args);
|
||||||
@ -25,17 +25,12 @@ function PastebinEmbed ($m) {
|
|||||||
$noLineNumbers = in_array('nolinenums', $args);
|
$noLineNumbers = in_array('nolinenums', $args);
|
||||||
$raw = in_array('raw', $args);
|
$raw = in_array('raw', $args);
|
||||||
$noPre = in_array('no-pre', $args);
|
$noPre = in_array('no-pre', $args);
|
||||||
|
|
||||||
## Dark theme, if specified.
|
|
||||||
$theme = $parsed['theme'] ?: "";
|
|
||||||
|
|
||||||
## Convert the comma-delimited line ranges to an array containing each line to be
|
## Convert the comma-delimited line ranges to an array containing each line to be
|
||||||
## included as values.
|
## included as values.
|
||||||
## Note that the line numbers will be zero-indexed (for use with raw text, etc.).
|
## Note that the line numbers will be zero-indexed (for use with raw text, etc.).
|
||||||
$line_ranges = $parsed['lines']
|
$line_ranges = $parsed['lines'] ? explode(',', $parsed['lines']) : array();
|
||||||
? explode(',', $parsed['lines'])
|
$line_numbers = array();
|
||||||
: [ ];
|
|
||||||
$line_numbers = [ ];
|
|
||||||
$to_end_from = -1;
|
$to_end_from = -1;
|
||||||
foreach ($line_ranges as $key => $line_range) {
|
foreach ($line_ranges as $key => $line_range) {
|
||||||
if (preg_match("/([0-9]+)[-–]([0-9]+)/", $line_range, $m)) {
|
if (preg_match("/([0-9]+)[-–]([0-9]+)/", $line_range, $m)) {
|
||||||
@ -48,10 +43,8 @@ function PastebinEmbed ($m) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
## Same thing, but for highlighted line ranges.
|
## Same thing, but for highlighted line ranges.
|
||||||
$hl_line_ranges = $parsed['hl']
|
$hl_line_ranges = $parsed['hl'] ? explode(',', $parsed['hl']) : array();
|
||||||
? explode(',', $parsed['hl'])
|
$hl_line_numbers = array();
|
||||||
: [ ];
|
|
||||||
$hl_line_numbers = [ ];
|
|
||||||
$hl_to_end_from = -1;
|
$hl_to_end_from = -1;
|
||||||
foreach ($hl_line_ranges as $key => $hl_line_range) {
|
foreach ($hl_line_ranges as $key => $hl_line_range) {
|
||||||
if (preg_match("/([0-9]+)[-–]([0-9]+)/", $hl_line_range, $m)) {
|
if (preg_match("/([0-9]+)[-–]([0-9]+)/", $hl_line_range, $m)) {
|
||||||
@ -63,24 +56,23 @@ function PastebinEmbed ($m) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$embed_js_url = "https://pastebin.com/embed_js/$paste_id" . ($theme ? "?theme={$theme}" : "");
|
$embed_js_url = "https://pastebin.com/embed_js/$paste_id";
|
||||||
$embed_iframe_url = "https://pastebin.com/embed_iframe/$paste_id" . ($theme ? "?theme={$theme}" : "");
|
$embed_iframe_url = "https://pastebin.com/embed_iframe/$paste_id";
|
||||||
$embed_raw_url = "https://pastebin.com/raw/$paste_id";
|
$embed_raw_url = "https://pastebin.com/raw/$paste_id";
|
||||||
|
|
||||||
$out = "<span class='pastebin-embed-error'>Unknown error.</span>";
|
$out = "<span class='pastebin-embed-error'>Unknown error.</span>";
|
||||||
|
|
||||||
## There are three ‘modes’: raw (retrieve just the text, server-side, and insert it
|
## There are three 'modes': raw (retrieve just the text, client-side, and insert it
|
||||||
## into the page), no-js (retrieve the HTML, server-side, and insert it into the
|
## into the page), no-js (retrieve the HTML, server-side, and insert it into the
|
||||||
## page), and default (i.e., JS-based: insert the scriptlet, and let it retrieve and
|
## page), and default (i.e., JS-based - insert the scriptlet, and let it retrieve and
|
||||||
## insert the HTML into the page, client-side & async).
|
## insert the HTML into the page, client-side & async).
|
||||||
## The mode is set by arguments to the (:pastebin-embed:) markup:
|
## The mode is set by arguments to the (:pastebin-embed:) markup:
|
||||||
## - if neither the ‘raw’ nor the ‘no-js’ option is given, default (JS) mode is used
|
## - if neither the 'raw' nor the 'no-js' option is given, default (JS) mode is used
|
||||||
## - if the ‘raw’ option is given, raw mode is used
|
## - if the 'raw' option is given, raw mode is used
|
||||||
## - if the ‘no-js’ option is given, no-js mode is used
|
## - if the 'no-js' option is given, no-js mode is used
|
||||||
if ($raw) {
|
if ($raw) {
|
||||||
$raw_text = file_get_contents($embed_raw_url);
|
$raw_text = file_get_contents($embed_raw_url);
|
||||||
if (!$raw_text)
|
if (!$raw_text) return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
|
||||||
return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
|
|
||||||
|
|
||||||
$raw_lines = explode("\n", $raw_text);
|
$raw_lines = explode("\n", $raw_text);
|
||||||
## Convert HTML entities.
|
## Convert HTML entities.
|
||||||
@ -89,8 +81,7 @@ function PastebinEmbed ($m) {
|
|||||||
$line = PVSE($line);
|
$line = PVSE($line);
|
||||||
}
|
}
|
||||||
## Highlighting only works if no-pre is NOT enabled.
|
## Highlighting only works if no-pre is NOT enabled.
|
||||||
if ( !empty($hl_line_numbers)
|
if (!empty($hl_line_numbers) && !$noPre) {
|
||||||
&& !$noPre) {
|
|
||||||
if ($hl_to_end_from >= 0)
|
if ($hl_to_end_from >= 0)
|
||||||
$hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($raw_lines) - 1));
|
$hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($raw_lines) - 1));
|
||||||
foreach ($hl_line_numbers as $l) {
|
foreach ($hl_line_numbers as $l) {
|
||||||
@ -105,22 +96,18 @@ function PastebinEmbed ($m) {
|
|||||||
}
|
}
|
||||||
$raw_text = implode("\n", $raw_lines);
|
$raw_text = implode("\n", $raw_lines);
|
||||||
|
|
||||||
## The ‘no-pre’ option means we shouldn’t wrap the text in a <pre> tag.
|
## The 'no-pre' option means we shouldn't wrap the text in a <pre> tag.
|
||||||
$out = $noPre
|
$out = $noPre ? $raw_text : Keep("<pre class='escaped embedPastebinRaw' id='pastebinEmbed_$id'>\n" . $raw_text . "\n</pre>\n");
|
||||||
? $raw_text
|
|
||||||
: Keep("<pre class='escaped embedPastebinRaw' id='pastebinEmbed_$id'>\n" . $raw_text . "\n</pre>\n");
|
|
||||||
} else if ($noJS) {
|
} else if ($noJS) {
|
||||||
include_once('simplehtmldom/simple_html_dom.php');
|
include_once('simple_html_dom.php');
|
||||||
|
|
||||||
$content_html = file_get_html($embed_iframe_url);
|
$content_html = file_get_html($embed_iframe_url);
|
||||||
if (!$content_html)
|
if (!$content_html) return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
|
||||||
return Keep("<span class='pastebin-embed-error'>Could not retrieve paste!</span>");
|
|
||||||
$content = $content_html->find(".embedPastebin", 0);
|
$content = $content_html->find(".embedPastebin", 0);
|
||||||
$content->id = "pastebinEmbed_$id";
|
$content->id = "pastebinEmbed_$id";
|
||||||
|
|
||||||
$styles_html = file_get_html($embed_js_url);
|
$styles_html = file_get_html($embed_js_url);
|
||||||
if (!$styles_html)
|
if (!$styles_html) return Keep("<span class='pastebin-embed-error'>Could not retrieve styles!</span>");
|
||||||
return Keep("<span class='pastebin-embed-error'>Could not retrieve styles!</span>");
|
|
||||||
$styles = $styles_html->find("style", 0);
|
$styles = $styles_html->find("style", 0);
|
||||||
|
|
||||||
## Filter specified line ranges (if any have been specified via the lines=
|
## Filter specified line ranges (if any have been specified via the lines=
|
||||||
@ -153,31 +140,25 @@ function PastebinEmbed ($m) {
|
|||||||
$out = Keep($styles.$content);
|
$out = Keep($styles.$content);
|
||||||
} else {
|
} else {
|
||||||
$out = Keep("<script id='pastebinEmbedScript_$id' src='$embed_js_url'></script>");
|
$out = Keep("<script id='pastebinEmbedScript_$id' src='$embed_js_url'></script>");
|
||||||
$out .= Keep("
|
|
||||||
<script>
|
|
||||||
document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelector('.embedPastebin').id = 'pastebinEmbed_$id';
|
|
||||||
</script>
|
|
||||||
");
|
|
||||||
|
|
||||||
if ( !empty($hl_line_numbers)
|
if (!empty($hl_line_numbers) || !empty($line_numbers)) {
|
||||||
|| !empty($line_numbers)) {
|
$line_numbers_js = "[ ".implode(", ",$line_numbers)." ]";
|
||||||
$line_numbers_js = "[ " . implode(", ", $line_numbers) . " ]";
|
$hl_line_numbers_js = "[ ".implode(", ",$hl_line_numbers)." ]";
|
||||||
$hl_line_numbers_js = "[ " . implode(", ", $hl_line_numbers) . " ]";
|
|
||||||
$out .= Keep("
|
$out .= Keep("
|
||||||
<script>{
|
<script>
|
||||||
let num_lines = document.querySelector('#pastebinEmbed_$id').querySelectorAll('.embedPastebin > ol > li').length;
|
var num_lines = document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelectorAll('.embedPastebin > ol > li').length;
|
||||||
|
|
||||||
let line_numbers = $line_numbers_js;
|
var line_numbers = $line_numbers_js;
|
||||||
let to_end_from = $to_end_from;
|
var to_end_from = $to_end_from;
|
||||||
if (to_end_from >= 0)
|
if (to_end_from >= 0)
|
||||||
line_numbers = [...line_numbers, ...[...Array(num_lines - to_end_from)].map((_, i) => to_end_from + i)];
|
line_numbers = [...line_numbers, ...[...Array(num_lines - to_end_from)].map((_, i) => to_end_from + i)];
|
||||||
|
|
||||||
let hl_line_numbers = $hl_line_numbers_js;
|
var hl_line_numbers = $hl_line_numbers_js;
|
||||||
let hl_to_end_from = $hl_to_end_from;
|
var hl_to_end_from = $hl_to_end_from;
|
||||||
if (hl_to_end_from >= 0)
|
if (hl_to_end_from >= 0)
|
||||||
hl_line_numbers = [...hl_line_numbers, ...[...Array(num_lines - hl_to_end_from)].map((_, i) => hl_to_end_from + i)];
|
hl_line_numbers = [...hl_line_numbers, ...[...Array(num_lines - hl_to_end_from)].map((_, i) => hl_to_end_from + i)];
|
||||||
|
|
||||||
document.querySelector('#pastebinEmbed_$id').querySelectorAll('.embedPastebin .source > ol > li').forEach(function (line, i) {
|
document.querySelector('#pastebinEmbedScript_$id').parentElement.nextSibling.querySelectorAll('.embedPastebin > ol > li').forEach(function (line, i) {
|
||||||
// Highlight specified line ranges (if any have been specified via the hl= parameter).
|
// Highlight specified line ranges (if any have been specified via the hl= parameter).
|
||||||
if (hl_line_numbers.indexOf(i) != -1)
|
if (hl_line_numbers.indexOf(i) != -1)
|
||||||
line.firstChild.className += ' pastebin-embed-highlighted-line';
|
line.firstChild.className += ' pastebin-embed-highlighted-line';
|
||||||
@ -190,9 +171,11 @@ function PastebinEmbed ($m) {
|
|||||||
line.value = ++i;
|
line.value = ++i;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}</script>
|
</script>
|
||||||
");
|
");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PastebinEmbedAppendFooter();
|
||||||
}
|
}
|
||||||
|
|
||||||
global $HTMLStylesFmt;
|
global $HTMLStylesFmt;
|
||||||
@ -200,7 +183,7 @@ function PastebinEmbed ($m) {
|
|||||||
$HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .embedFooter { display: none; }\n";
|
$HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .embedFooter { display: none; }\n";
|
||||||
}
|
}
|
||||||
if (!$raw && $noLineNumbers) {
|
if (!$raw && $noLineNumbers) {
|
||||||
$HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id .source > ol { padding-left: 5px; }\n";
|
$HTMLStylesFmt['pastebin-embed'][] = "#pastebinEmbed_$id > ol { padding-left: 5px; }\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
PastebinEmbedInjectStyles();
|
PastebinEmbedInjectStyles();
|
||||||
@ -209,6 +192,22 @@ function PastebinEmbed ($m) {
|
|||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function PastebinEmbedAppendFooter() {
|
||||||
|
static $ran_once = false;
|
||||||
|
if (!$ran_once) {
|
||||||
|
global $HTMLFooterFmt;
|
||||||
|
$HTMLFooterFmt[] =
|
||||||
|
"<script>
|
||||||
|
document.querySelectorAll('div.embedPastebin').forEach(function (embed) {
|
||||||
|
if (embed.previousSibling && embed.previousSibling.tagName == 'P') {
|
||||||
|
embed.id = 'pastebinEmbed_' + embed.previousSibling.firstChild.id.substring(20);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>\n";
|
||||||
|
}
|
||||||
|
$ran_once = true;
|
||||||
|
}
|
||||||
|
|
||||||
function PastebinEmbedInjectStyles() {
|
function PastebinEmbedInjectStyles() {
|
||||||
static $ran_once = false;
|
static $ran_once = false;
|
||||||
if (!$ran_once) {
|
if (!$ran_once) {
|
||||||
|
|||||||
1721
simple_html_dom.php
Normal file
1721
simple_html_dom.php
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,21 +0,0 @@
|
|||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2019 S.C. Chen, John Schlick, logmanoriginal
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
SOFTWARE.
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user