docSet 文檔可用于 zeal dash 軟件中。 zeal 在win 下 和 Linux 均有可用版本 dash 則只在 Mac 可用
制作 dcocSet 文檔主要分 3 步
鏡像文檔網(wǎng)站
做鏡像網(wǎng)站就是把整個網(wǎng)站爬下來,并且把 css 和 js 圖片等靜態(tài)資源文件轉(zhuǎn)換成本地的路徑, 主要使用工具是 wget
以 vue 中文文檔 為例:
wget -r -p -np -k https://cn.vuejs.org/
制作索引文件
zeal 可以快速的搜索文檔主要利用了 sqlite 數(shù)據(jù)庫,在數(shù)據(jù)庫中有一張 serchIndex 表, 這張表常用的字段有三個,分別是 name , type, path
name
關(guān)鍵詞type
關(guān)鍵詞的類型,代表該關(guān)鍵詞是函數(shù),還是類,等等可選的字段有Sections,Fun,classespath
點(diǎn)擊關(guān)鍵詞要跳轉(zhuǎn)的路徑
所以,關(guān)鍵是制作一個這種合理的索引表
下面是使用 ruby 實(shí)現(xiàn)制作索引表的功能,以及一些目錄的生成
require 'nokogiri'
require "sqlite3"
require "fileutils"
class HtmlToDoc
def initialize(html_dir, docset_name)
@html_path = html_dir
@docset_name = "#{docset_name}.docset"
@name = docset_name
mkdir_file
create_plist
@con = SQLite3::Database.new(@dsidx)
@con.execute("CREATE TABLE IF NOT EXISTS searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)");
end
# 插入數(shù)據(jù)
def update_db(name, path, type = 'Classes')
@con.execute('INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (?,?,?)',[name,type,path])
puts name,path
end
# 提取url,根據(jù)你的需求更改提取規(guī)則
def add_urls(html_path)
doc = Nokogiri::HTML(File.open(html_path).read)
doc.css("h3>a").each do |tag|
name = tag.parent.text.strip
if name.size > 0 && tag[:href]
path = tag[:href].strip.split("#").last
update_db(name,html_path + "#" + path)
end
end
end
# 生成目錄
def mkdir_file
FileUtils.rm_r(@docset_name) if File.exists?(@docset_name)
@doc_dir = "#{@docset_name}/Contents/Resources/Documents"
FileUtils.mkdir_p(@doc_dir)
@dsidx = "#{@docset_name}/Contents/Resources/docSet.dsidx"
FileUtils.touch(@dsidx)
@plist = "#{@docset_name}/Contents/info.plist"
FileUtils.touch(@plist)
puts "目錄創(chuàng)建成功"
end
# 制作plist 文件
# 各種key 的意思請參考 dash 官方文檔
def create_plist
plist = <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>#{@name}</string>
<key>CFBundleName</key>
<string>#{@name}</string>
<key>DashDocSetFamily</key>
<string>#{@name}</string>
<key>DocSetPlatformFamily</key>
<string>requests</string>
<key>isDashDocset</key>
<true/>
<key>isJavaScriptEnabled</key>
<true/>
<key>dashIndexFilePath</key>
<string>#{@name}</string>
</dict>
</plist>
EOF
File.open(@plist,"w").write(plist)
end
# 移動文檔
def copy_files
FileUtils.cp_r(@html_path.split("/").first, @doc_dir)
# 將docSet 文檔移動到 zeal 目錄下
# local_doc_dir = "/home/dccmmtop/.local/share/Zeal/Zeal/docsets"
# FileUtils.cp_r(@docset_name, local_doc_dir)
end
def start
Dir.open(@html_path).each do |file|
next unless file =~ /.html$/
add_urls(File.join(@html_path, file))
end
copy_files
end
end
if ARGV[0] == "-h"
puts 'ruby ./convert.rb "要生成文檔的html地址(要包含整個網(wǎng)站的根目錄)" "生成文檔的名字"'
puts "例子: ruby convert.rb cn.vuejs.org/v2/guide vue"
else
HtmlToDoc.new(ARGV[0],ARGV[1]).start
end
移動docSet目錄
最后將 制作好的 docSet 文件夾移動到 zeal 的文檔目錄下, 也可以將上面腳本中 copy_files 方法最后兩行去掉