From cfffa2a2af6e9f5202b1d9c2349402f35ef5fb65 Mon Sep 17 00:00:00 2001 From: filesite Date: Thu, 31 Mar 2022 21:31:44 +0800 Subject: [PATCH] function fixMDUrls improved --- lib/DirScanner.php | 52 ++++++++++++++++++++++++++--------------- test/init_test_files.sh | 19 ++++++++------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 2726b6e..2838f87 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -580,27 +580,41 @@ Class DirScanner { //替换.md文件解析之后的HTML中的静态文件URL为相对路径path public function fixMDUrls($realpath, $html) { - $reg_urls = '/src="([^"]+)"/i'; - preg_match_all($reg_urls, $html, $matches); - if (empty($matches[1])) { - return $html; + $pathinfo = pathinfo($realpath); + + $matches = []; + + //匹配图片地址 + $reg_imgs = '/src="([^"]+)"/i'; + preg_match_all($reg_imgs, $html, $img_matches); + if (!empty($img_matches[1])) { + $matches = $img_matches[1]; } - $pathinfo = pathinfo($realpath); - foreach ($matches[1] as $url) { - if (preg_match('/^http(s)?:\/\//i', $url)) {continue;} - - $img_realpath = "{$pathinfo['dirname']}/{$url}"; - if (file_exists($img_realpath)) { - $id = $this->getId($img_realpath); - $fp = fopen($img_realpath, 'r'); - $fstat = fstat($fp); - fclose($fp); - $img_pathinfo = pathinfo($img_realpath); - $extension = strtolower($img_pathinfo['extension']); - $img_path = $this->getFilePath( $id, $this->getRelativeDirname($img_pathinfo['dirname']), $img_pathinfo['filename'], $extension, $fstat['mtime'] ); - - $html = str_replace($url, $img_path, $html); + //匹配a链接 + $reg_links = '/href="([^"]+)"/i'; + preg_match_all($reg_links, $html, $link_matches); + if (!empty($link_matches[1])) { + $matches = array_merge($matches, $link_matches[1]); + } + + if (!empty($matches)) { + foreach ($matches as $url) { + if (preg_match('/^http(s)?:\/\//i', $url)) {continue;} + + $src_realpath = "{$pathinfo['dirname']}/{$url}"; + if (file_exists($src_realpath)) { + $id = $this->getId($src_realpath); + $fp = fopen($src_realpath, 'r'); + $fstat = fstat($fp); + fclose($fp); + $src_pathinfo = pathinfo($src_realpath); + $extension = strtolower($src_pathinfo['extension']); + + $src_path = $this->getFilePath( $id, $this->getRelativeDirname($src_pathinfo['dirname']), $src_pathinfo['filename'], $extension, $fstat['mtime'] ); + + $html = str_replace("\"{$url}\"", "\"{$src_path}\"", $html); + } } } diff --git a/test/init_test_files.sh b/test/init_test_files.sh index 3cfdd65..01da4ed 100755 --- a/test/init_test_files.sh +++ b/test/init_test_files.sh @@ -28,7 +28,7 @@ rm -rf "小说/" rm -rf "图片/" touch "Readme.md" -tee -a "Readme.md" <谷歌图片搜索 +Bing图片搜索 ## 小说 常用的小说网站。 -谷歌图片搜索 +金庸小说 +古龙小说 EOF @@ -52,7 +55,7 @@ mkdir -p "图片/必应图片搜索/" touch "小说/金庸小说/最爱金庸网站图标.ico" touch "小说/金庸小说/最爱金庸.url" -tee -a "小说/金庸小说/最爱金庸.url" < "小说/金庸小说/snapshot.txt" touch "小说/古龙小说/最爱古龙网站图标.ico" touch "小说/古龙小说/最爱古龙.url" -tee -a "小说/金庸小说/最爱古龙.url" < "图片/图片搜索/snapshot.txt" touch "图片/必应图片搜索/bing图标.ico" touch "图片/必应图片搜索/bing.url" -tee -a "图片/必应图片搜索/bing.url" <