Browse Source

add function fixMDUrls

master
filesite 3 years ago
parent
commit
c9084f4ffd
  1. 54
      lib/DirScanner.php
  2. 18
      test/DirScannerTest.php
  3. 2
      test/init_test_files.sh

54
lib/DirScanner.php

@ -76,7 +76,7 @@ Class DirScanner {
//解析描述文件内容 //解析描述文件内容
//snapshot相对路径完善,支持secure_link //snapshot相对路径完善,支持secure_link
private function parseDescriptionFiles($realpath, $dirLevel) { private function parseDescriptionFiles($realpath) {
$pathinfo = pathinfo($realpath); $pathinfo = pathinfo($realpath);
$field = preg_replace('/^.+_/', '', $pathinfo['filename']); $field = preg_replace('/^.+_/', '', $pathinfo['filename']);
$content = file_get_contents($realpath); $content = file_get_contents($realpath);
@ -92,7 +92,7 @@ Class DirScanner {
fclose($fp); fclose($fp);
$img_pathinfo = pathinfo($img_realpath); $img_pathinfo = pathinfo($img_realpath);
$extension = strtolower($img_pathinfo['extension']); $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文件 //根据路径生成文件数组,兼容URL文件
private function getFileData($realpath, $dirLevel) { private function getFileData($realpath) {
$id = $this->getId($realpath); $id = $this->getId($realpath);
$fp = fopen($realpath, 'r'); $fp = fopen($realpath, 'r');
$fstat = fstat($fp); $fstat = fstat($fp);
@ -186,7 +186,7 @@ Class DirScanner {
'ctime' => $fstat['ctime'], 'ctime' => $fstat['ctime'],
], ],
'realpath' => $realpath, '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') { if ($extension == 'url') {
@ -212,19 +212,14 @@ Class DirScanner {
} }
//根据路径和当前扫描深度获取目录名 //根据路径和当前扫描深度获取目录名
private function getDirectoryName($dirname, $dirLevel) { private function getRelativeDirname($dirname) {
if ($dirLevel <= 0) {return basename($dirname);} return str_replace($this->rootDir, '', $dirname);
$dirname = preg_replace('/\/$/', '', $dirname);
$dirname = preg_replace('/^\//', '', $dirname);
$names = explode('/', $dirname);
if ($dirLevel >= count($names)) {return $dirname;}
return implode('/', array_splice($names, -$dirLevel));
} }
//合并描述文件内容到md文件或者目录数据 //合并描述文件内容到md文件或者目录数据
private function mergeDescriptionData($realpath, $dirLevel) { private function mergeDescriptionData($realpath) {
$data = []; $data = [];
$ext = $this->parseDescriptionFiles($realpath, $dirLevel); $ext = $this->parseDescriptionFiles($realpath);
//try to find the md file //try to find the md file
$targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath); $targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath);
@ -494,10 +489,10 @@ Class DirScanner {
$extension = strtolower($pathinfo['extension']); $extension = strtolower($pathinfo['extension']);
if ( in_array($extension, $this->supportFileExtensions) ) { if ( in_array($extension, $this->supportFileExtensions) ) {
if ($extension != 'txt') { if ($extension != 'txt') {
$branch = $this->getFileData($realpath, $this->scanningDirLevel); $branch = $this->getFileData($realpath);
}else { }else {
//把描述文件内容合并到被描述的目录或md文件数据中 //把描述文件内容合并到被描述的目录或md文件数据中
$branch = $this->mergeDescriptionData($realpath, $this->scanningDirLevel); $branch = $this->mergeDescriptionData($realpath);
} }
} }
} }
@ -583,4 +578,33 @@ Class DirScanner {
return $titles; 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;
}
} }

18
test/DirScannerTest.php

@ -78,11 +78,11 @@ print_r($readme_titles);
echo "\n"; echo "\n";
echo "\n"; echo "\n";
//$menus = $scanner->getMenus(); $menus = $scanner->getMenus();
//echo "Directories:\n"; echo "Directories:\n";
//print_r($menus); print_r($menus);
//echo "\n"; echo "\n";
//echo "\n"; echo "\n";
echo "Directories and files' tree:\n"; echo "Directories and files' tree:\n";
@ -91,9 +91,15 @@ echo "\n";
echo "\n"; echo "\n";
//$scanResults = $scanner->getScanResults(); $scanResults = $scanner->getScanResults();
//print_r($scanResults); //print_r($scanResults);
//echo "\n"; //echo "\n";
//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";

2
test/init_test_files.sh

@ -41,6 +41,8 @@ tee -a "Readme.md" <<EOF
常用的小说网站。 常用的小说网站。
<img src="图片/图片搜索/谷歌图片搜索图标.ico" alt="谷歌图片搜索">
EOF EOF
mkdir -p "小说/金庸小说/" mkdir -p "小说/金庸小说/"

Loading…
Cancel
Save