diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 775ee83..2726b6e 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -76,7 +76,7 @@ Class DirScanner { //解析描述文件内容 //snapshot相对路径完善,支持secure_link - private function parseDescriptionFiles($realpath, $dirLevel) { + private function parseDescriptionFiles($realpath) { $pathinfo = pathinfo($realpath); $field = preg_replace('/^.+_/', '', $pathinfo['filename']); $content = file_get_contents($realpath); @@ -92,7 +92,7 @@ Class DirScanner { fclose($fp); $img_pathinfo = pathinfo($img_realpath); $extension = strtolower($img_pathinfo['extension']); - $content = $this->getFilePath( $id, $this->getDirectoryName($img_pathinfo['dirname'], $dirLevel), $img_pathinfo['filename'], $extension, $fstat['mtime'] ); + $content = $this->getFilePath( $id, $this->getRelativeDirname($img_pathinfo['dirname']), $img_pathinfo['filename'], $extension, $fstat['mtime'] ); } } } @@ -168,7 +168,7 @@ Class DirScanner { } //根据路径生成文件数组,兼容URL文件 - private function getFileData($realpath, $dirLevel) { + private function getFileData($realpath) { $id = $this->getId($realpath); $fp = fopen($realpath, 'r'); $fstat = fstat($fp); @@ -186,7 +186,7 @@ Class DirScanner { 'ctime' => $fstat['ctime'], ], 'realpath' => $realpath, - 'path' => $this->getFilePath( $id, $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension, $fstat['mtime'] ), + 'path' => $this->getFilePath( $id, $this->getRelativeDirname($pathinfo['dirname']), $pathinfo['filename'], $extension, $fstat['mtime'] ), ]; if ($extension == 'url') { @@ -212,19 +212,14 @@ Class DirScanner { } //根据路径和当前扫描深度获取目录名 - private function getDirectoryName($dirname, $dirLevel) { - if ($dirLevel <= 0) {return basename($dirname);} - $dirname = preg_replace('/\/$/', '', $dirname); - $dirname = preg_replace('/^\//', '', $dirname); - $names = explode('/', $dirname); - if ($dirLevel >= count($names)) {return $dirname;} - return implode('/', array_splice($names, -$dirLevel)); + private function getRelativeDirname($dirname) { + return str_replace($this->rootDir, '', $dirname); } //合并描述文件内容到md文件或者目录数据 - private function mergeDescriptionData($realpath, $dirLevel) { + private function mergeDescriptionData($realpath) { $data = []; - $ext = $this->parseDescriptionFiles($realpath, $dirLevel); + $ext = $this->parseDescriptionFiles($realpath); //try to find the md file $targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath); @@ -494,10 +489,10 @@ Class DirScanner { $extension = strtolower($pathinfo['extension']); if ( in_array($extension, $this->supportFileExtensions) ) { if ($extension != 'txt') { - $branch = $this->getFileData($realpath, $this->scanningDirLevel); + $branch = $this->getFileData($realpath); }else { //把描述文件内容合并到被描述的目录或md文件数据中 - $branch = $this->mergeDescriptionData($realpath, $this->scanningDirLevel); + $branch = $this->mergeDescriptionData($realpath); } } } @@ -583,4 +578,33 @@ Class DirScanner { return $titles; } + //替换.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); + 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); + } + } + + return $html; + } + } diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php index 69d535a..b51de07 100644 --- a/test/DirScannerTest.php +++ b/test/DirScannerTest.php @@ -78,11 +78,11 @@ print_r($readme_titles); echo "\n"; echo "\n"; -//$menus = $scanner->getMenus(); -//echo "Directories:\n"; -//print_r($menus); -//echo "\n"; -//echo "\n"; +$menus = $scanner->getMenus(); +echo "Directories:\n"; +print_r($menus); +echo "\n"; +echo "\n"; echo "Directories and files' tree:\n"; @@ -91,9 +91,15 @@ echo "\n"; echo "\n"; -//$scanResults = $scanner->getScanResults(); +$scanResults = $scanner->getScanResults(); //print_r($scanResults); //echo "\n"; //echo "\n"; +$readmeFile = $scanResults[$readme_id]; +$html = file_get_contents($readmeFile['realpath']); +$html = $scanner->fixMDUrls($readmeFile['realpath'], $html); +echo "{$html}\n"; +echo "\n"; +echo "\n"; diff --git a/test/init_test_files.sh b/test/init_test_files.sh index 614d97f..3cfdd65 100755 --- a/test/init_test_files.sh +++ b/test/init_test_files.sh @@ -41,6 +41,8 @@ tee -a "Readme.md" < + EOF mkdir -p "小说/金庸小说/"