书签生成


需求分析

  • 离线浏览器书签,加入版本控制
  • 缓存书签内容,弥补书签被删除,书签在国外打不开的问题
  • 书签全文搜索功能

软件设计

// 数据初始化
$this->init();

// 读取书签文件
$this->getBookmarkContent();

// 解析书签结构
$crawler = new Crawler($this->bookmarkContent);
$this->analysis($crawler);

编码

$children->each(function ($crawler, $i) use ($parentDir, &$bookmarkDir) {
    if ($crawler->nodeName() == 'dt') {
        $h3 = $crawler->filter('dt > h3');
        if ($h3->count()) {
            $bookmarkDir = $parentDir.trim($h3->text()).'/';
            Storage::disk('bookmark')->makeDirectory($bookmarkDir);
            Storage::disk('docs')->makeDirectory($bookmarkDir);
        }
        $a = $crawler->filter('dt > a');
        if ($a->count()) {
            $crawler->filter('a')->each(function ($crawler, $i) use ($parentDir, &$bookmarkDir) {
                if (empty($parentDir)) {
                    $parentDir = '其他书签/';
                }
                $filename = $parentDir.md5($crawler->attr('href')).'.html';
                $content = view('bookmark.default')->with('title', $crawler->text())->with('url', $crawler->attr('href'))->render();
                $this->debug && $this->comment("\t".$filename);
                Storage::disk('bookmark')->put($filename, $content);
                Storage::disk('docs')->put($filename, $content);
            });
        }
    } elseif ($crawler->nodeName() == 'dl') {
        $this->debug && $this->comment($bookmarkDir);
        $this->analysis($crawler, $bookmarkDir);
    }
});