制作docSet文檔

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, classes

  • path
    點(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 方法最后兩行去掉

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 官網(wǎng) 中文版本 好的網(wǎng)站 Content-type: text/htmlBASH Section: User ...
    不排版閱讀 4,712評論 0 5
  • 不足的地方請大家多多指正,如有其它沒有想到的常問面試題請大家多多評論,一起成長,感謝!~ String可以被繼承嗎...
    啟示錄是真的閱讀 3,069評論 3 3
  • 還曾記得皮筋栓住了你我 沙包砸中了開頭 卻沒有砸中彩票 我們跳出了這間房子 卻敗給了那座房子 一手抓住了石子 卻抓...
    雪萱草閱讀 681評論 8 29
  • 女:那邊現(xiàn)在是夏天 我住的地方走半個小時(shí)就能到海邊 男:是嗎 那挺好的 女:這次去 下次回來就是一年后了 男:等到...
    無語問大地閱讀 365評論 0 0
  • 文明禮儀是什么?文明禮儀是路上相遇時(shí)的微笑;是同學(xué)有困難時(shí)的熱情幫助;是平時(shí)與人相處時(shí)的親切;是見到...
    劉弋林閱讀 262評論 0 0

友情鏈接更多精彩內(nèi)容