Skip to content

Instantly share code, notes, and snippets.

@killme2008
Last active December 15, 2015 14:39
Show Gist options
  • Save killme2008/5276420 to your computer and use it in GitHub Desktop.
Save killme2008/5276420 to your computer and use it in GitHub Desktop.
A ruby script to generate docset for dash from your public gists.
#!/usr/bin/ruby
## Author : dennis (killme2008@gmail.com)
## Description : A ruby script to generate docset for dash from your public gists.Test on ruby-2.0.0-p0.
## Dependencies : sudo gem install sqlite3
## Useage : Type command : ruby gen_docset_for_dash.rb [docset_name] [github_account]
## Then it will create a folder named '[docset_name].docset' in current folder,you can add the generated
## docset to dash in dash Preferences menu.
require 'rubygems'
require 'fileutils'
require 'open-uri'
require 'sqlite3'
def mkdir(doc_name)
path = "#{doc_name}.docset/Contents/Resources/Documents/"
FileUtils.mkdir_p path
return path
end
def wget_info_plist(doc_name, path)
str = open("http://kapeli.com/Dash/Info.plist") do |f|
f.read
end
File.open("#{path}/../../Info.plist", "w") do |f|
f.puts str.gsub(/<string>(.*?)<\/string>/, "<string>#{doc_name}</string>")
end
end
def fetch_gists(account)
page_no = 1
rt = []
while true
open("https://gist.github.com/#{account}?page=#{page_no}") do |f|
gists = f.read.scan(/<a href=\"\/killme2008\/(\d+)\" title=\"Files\"/)
return rt.flatten if gists.empty?
rt << gists
end
page_no = page_no.succ
end
end
def clone_gists(path, gists)
puts "cd #{path}"
gists.each do |gist|
puts "git clone https://gist.github.com/#{gist}.git"
system("cd #{path} && git clone https://gist.github.com/#{gist}.git")
end
end
def init_sqlite_db(path, gists)
begin
db = SQLite3::Database.open "#{path}/../docSet.dsidx"
db.execute "CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)"
gists.each do |gist|
Dir.glob("#{path}/#{gist}/*") do |f|
if File.file?(f)
name = File.basename(f)
file_path = "http://gist.github.com/#{gist}"
puts "INSERT INTO searchIndex(name,type,path) VALUES ('#{File.basename(f)}','File','#{file_path}')"
db.execute "INSERT INTO searchIndex(name,type,path) VALUES ('#{File.basename(f)}','File','#{file_path}')"
end
end
end
rescue SQLite3::Exception => e
puts "Exception occured when init sqlite database"
puts e
ensure
db.close if db
end
end
if __FILE__ == $0
if ARGV.size != 2
STDERR.puts " Useage : ruby #{$0} [doc_name] [gist_account]"
STDERR.puts " For example : ruby #{$0} MyGist killme2008"
exit 1
end
doc_name = ARGV[0]
gist_account = ARGV[1]
path = mkdir(doc_name)
puts "mkdir #{path}"
puts "Begin to fetch gists for account #{gist_account}"
gists =fetch_gists(gist_account)
puts "Fetched #{gists.size} gists for account #{gist_account}"
if gists.empty?
STDERR.puts "Empty gists for account #{gist_account},exit."
exit 1
end
clone_gists(path, gists)
puts "wget http://kapeli.com/Dash/Info.plist"
wget_info_plist(doc_name, path)
puts "Create sqlite database..."
init_sqlite_db(path, gists)
puts "Done!"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment