瀏覽代碼

Fix various bugs; add simplehtmldom

master
achmizs 4 年之前
父節點
當前提交
eba80a1c5b
共有 3 個檔案被更改,包括 2421 行新增54 行删除
  1. 47
    54
      gist-embed.php
  2. 21
    0
      simplehtmldom/LICENSE
  3. 2353
    0
      simplehtmldom/simple_html_dom.php

+ 47
- 54
gist-embed.php 查看文件

* \brief Embed Gists in a wikipage. * \brief Embed Gists in a wikipage.
*/ */


$RecipeInfo['GistEmbed']['Version'] = '2018-10-19';
$RecipeInfo['GistEmbed']['Version'] = '2021-12-11';


## (:gist-embed:) ## (:gist-embed:)
Markup('gist-embed', '<fulltext', '/\\(:gist-embed\\s+(.+?)\\s*:\\)/', 'GistEmbed');
Markup('gist-embed', '<fulltext', '/\(:gist-embed\s+(.+?)\s*:\)/', 'GistEmbed');


SDV($GistEmbedHighlightStyle, "background-color: yellow;"); SDV($GistEmbedHighlightStyle, "background-color: yellow;");


## 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[''];
$gist_id = $args[0]; $gist_id = $args[0];
$noJS = in_array('no-js', $args); $noJS = in_array('no-js', $args);
if ($raw) { if ($raw) {
## If no filenames have been specified, we'll have to retrieve the file list from ## If no filenames have been specified, we'll have to retrieve the file list from
## the server; otherwise, we'll have no idea what files to request, and just ## the server; otherwise, we'll have no idea what files to request, and just
## retrieving the 'raw' URL for a multi-file gist (with no file specified) gets
## retrieving the ‘raw’ URL for a multi-file gist (with no file specified) gets
## the first file only... ## the first file only...
if (empty($files)) { if (empty($files)) {
$full_gist_data = json_decode(file_get_contents($embed_json_url),true); $full_gist_data = json_decode(file_get_contents($embed_json_url),true);
foreach ($raw_lines as $line) foreach ($raw_lines as $line)
$line = PVSE($line); $line = PVSE($line);
} }
## Highlighting only works if no-pre is NOT enabled AND if we're displaying a
## Highlighting only works if no-pre is NOT enabled AND if were displaying a
## single file only. ## single file only.
if (!empty($hl_line_numbers) && !$noPre && count($files) == 1) {
if ( !empty($hl_line_numbers)
&& !$noPre
&& count($files) == 1) {
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) {
$raw_lines[$l] = "<span class='gist-embed-highlighted-line'>" . rtrim($raw_lines[$l]) . "</span>"; $raw_lines[$l] = "<span class='gist-embed-highlighted-line'>" . rtrim($raw_lines[$l]) . "</span>";
} }
} }
## Specifying line numbers only works if we're displaying a single file only.
if (!empty($line_numbers) && count($files) == 1) {
## Specifying line numbers only works if we’re displaying a single file only.
if ( !empty($line_numbers)
&& count($files) == 1) {
if ($to_end_from >= 0) if ($to_end_from >= 0)
$line_numbers = array_merge($line_numbers, range($to_end_from, count($raw_lines) - 1)); $line_numbers = array_merge($line_numbers, range($to_end_from, count($raw_lines) - 1));
$raw_lines = array_intersect_key($raw_lines, array_flip($line_numbers)); $raw_lines = array_intersect_key($raw_lines, array_flip($line_numbers));
} }
$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 ? $raw_text : Keep("<pre class='escaped gistRaw' id='gistEmbed_$id_$filename'>\n" . $raw_text . "\n</pre>\n"); $out[] = $noPre ? $raw_text : Keep("<pre class='escaped gistRaw' id='gistEmbed_$id_$filename'>\n" . $raw_text . "\n</pre>\n");
} }
$out = implode($noPre ? "\n\n" : "", $out); $out = implode($noPre ? "\n\n" : "", $out);
} else if ($noJS) { } else if ($noJS) {
include_once('simple_html_dom.php');
include_once('simplehtmldom/simple_html_dom.php');
$json_content = json_decode(file_get_contents($embed_json_url),true); $json_content = json_decode(file_get_contents($embed_json_url),true);
$content->id = "gistEmbed_$id"; $content->id = "gistEmbed_$id";
## If specific files are specified, we simply delete the div.gist-file containers ## If specific files are specified, we simply delete the div.gist-file containers
## that contain files we don't want.
## that contain files we dont want.
if (!empty($files)) { if (!empty($files)) {
$file_ids = preg_replace("/\./", "-", $files); $file_ids = preg_replace("/\./", "-", $files);
$gist_file_blocks = $content_html->find("div.gist-file"); $gist_file_blocks = $content_html->find("div.gist-file");
} }
} }
## Specifying line numbers only works if we're displaying a single file only.
## Specifying line numbers only works if were displaying a single file only.
$displayed_gist_files = array_filter($content_html->find("div.gist-file"), function ($d) { return $d->outertext; }); $displayed_gist_files = array_filter($content_html->find("div.gist-file"), function ($d) { return $d->outertext; });
if (!empty($line_numbers) && count($displayed_gist_files) == 1) {
if ( !empty($line_numbers)
&& count($displayed_gist_files) == 1) {
$lines = reset($displayed_gist_files)->find(".js-file-line-container tr"); $lines = reset($displayed_gist_files)->find(".js-file-line-container tr");
if ($to_end_from >= 0) if ($to_end_from >= 0)
$line_numbers = array_merge($line_numbers, range($to_end_from, count($lines) - 1)); $line_numbers = array_merge($line_numbers, range($to_end_from, count($lines) - 1));
} }
} }
## Highlighting specific line numbers only works if we're display a single file
## only.
if (!empty($hl_line_numbers) && count($displayed_gist_files) == 1) {
## Highlighting specific line numbers only works if we’re displaying
## a single file only.
if ( !empty($hl_line_numbers)
&& count($displayed_gist_files) == 1) {
$lines = reset($displayed_gist_files)->find(".js-file-line-container tr"); $lines = reset($displayed_gist_files)->find(".js-file-line-container tr");
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($lines) - 1)); $hl_line_numbers = array_merge($hl_line_numbers, range($hl_to_end_from, count($lines) - 1));
$out = Keep($content); $out = Keep($content);
} else { } else {
$out = Keep("<script id='gistEmbedScript_$id' src='$embed_js_url'></script>"); $out = Keep("<script id='gistEmbedScript_$id' src='$embed_js_url'></script>");
$out .= Keep("
<script>
document.querySelector('#gistEmbedScript_$id').parentElement.nextSibling.id = 'gistEmbed_$id';
</script>
");
## If specific files are specified, we'll delete the div.gist-file containers
## that contain files we don't want (this script will run right after the script
## If specific files are specified, well delete the div.gist-file containers
## that contain files we dont want (this script will run right after the script
## that adds the content in the first place). ## that adds the content in the first place).
if (!empty($files)) { if (!empty($files)) {
$files_js = preg_replace("/\./", "-", "[ '".implode("', '",$files)."' ]");
$files_js = preg_replace("/\./", "-", "[ '" . implode("', '", $files) . "' ]");
$out .= Keep(" $out .= Keep("
<script>
var files = $files_js;
document.querySelector('#gistEmbedScript_$id').parentElement.nextSibling.querySelectorAll('div.gist-file').forEach(function (gist_file_block) {
<script>{
let files = $files_js;
document.querySelector('#gistEmbed_$id').querySelectorAll('div.gist-file').forEach(function (gist_file_block) {
if (files.indexOf(gist_file_block.querySelector('div.file').id.substring(5)) == -1) if (files.indexOf(gist_file_block.querySelector('div.file').id.substring(5)) == -1)
gist_file_block.parentElement.removeChild(gist_file_block); gist_file_block.parentElement.removeChild(gist_file_block);
}); });
</script>
}</script>
"); ");
} }
## Specifying line numbers only works if we're displaying a single file only.
if (!empty($line_numbers) || !empty($hl_line_numbers)) {
$line_numbers_js = "[ ".implode(", ",$line_numbers)." ]";
$hl_line_numbers_js = "[ ".implode(", ",$hl_line_numbers)." ]";
## Specifying line numbers only works if we’re displaying a single file only.
if ( !empty($line_numbers)
|| !empty($hl_line_numbers)) {
$line_numbers_js = "[ " . implode(", " , $line_numbers) . " ]";
$hl_line_numbers_js = "[ " . implode(", " , $hl_line_numbers) . " ]";
$out .= Keep(" $out .= Keep("
<script>
if (document.querySelector('#gistEmbedScript_$id').parentElement.nextSibling.querySelectorAll('div.gist-file').length == 1) {
var num_lines = document.querySelector('#gistEmbedScript_$id').parentElement.nextSibling.querySelector('div.gist-file').querySelectorAll('.js-file-line-container tr').length;
<script>{
if (document.querySelector('#gistEmbed_$id').querySelectorAll('div.gist-file').length == 1) {
let num_lines = document.querySelector('#gistEmbed_$id').querySelector('div.gist-file').querySelectorAll('.js-file-line-container tr').length;


var line_numbers = $line_numbers_js;
var to_end_from = $to_end_from;
let line_numbers = $line_numbers_js;
let 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)];


var hl_line_numbers = $hl_line_numbers_js;
var hl_to_end_from = $hl_to_end_from;
let hl_line_numbers = $hl_line_numbers_js;
let 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('#gistEmbedScript_$id').parentElement.nextSibling.querySelector('div.gist-file').querySelectorAll('.js-file-line-container tr').forEach(function (line, i) {
document.querySelector('#gistEmbed_$id').querySelector('div.gist-file').querySelectorAll('.js-file-line-container tr').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.children[1].className += ' gist-embed-highlighted-line'; line.children[1].className += ' gist-embed-highlighted-line';
line.parentElement.removeChild(line); line.parentElement.removeChild(line);
}); });
} }
</script>
}</script>
"); ");
} }
GistEmbedAppendFooter();
} }
global $HTMLStylesFmt; global $HTMLStylesFmt;
return $out; return $out;
} }


function GistEmbedAppendFooter() {
static $ran_once = false;
if (!$ran_once) {
global $HTMLFooterFmt;
$HTMLFooterFmt[] =
"<script>
document.querySelectorAll('div.gist').forEach(function (embed) {
if (embed.previousSibling && embed.previousSibling.tagName == 'P') {
embed.id = 'gistEmbed_' + embed.previousSibling.firstChild.id.substring(16);
}
});
</script>\n";
}
$ran_once = true;
}

function GistEmbedInjectStyles() { function GistEmbedInjectStyles() {
static $ran_once = false; static $ran_once = false;
if (!$ran_once) { if (!$ran_once) {

+ 21
- 0
simplehtmldom/LICENSE 查看文件

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.

+ 2353
- 0
simplehtmldom/simple_html_dom.php
文件差異過大導致無法顯示
查看文件


Loading…
取消
儲存