forked from sNKS/rhodes-api-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
136 lines (112 loc) · 3.35 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'rubygems'
require 'bundler'
Bundler.setup
require './environment'
desc 'Start a development server'
task :server do
if which('shotgun')
exec 'shotgun -O config.ru'
else
warn 'warn: shotgun not installed; reloading is disabled.'
exec 'rackup config.ru -p 9393'
end
end
desc 'Index documentation'
task :index do
puts "indexing now:"
client = IndexTank::Client.new(ENV['HEROKUTANK_API_URL'])
index = client.indexes(AppConfig['index'])
index.delete rescue nil
index.add rescue nil
print "Waiting to initialize #{AppConfig['index']}..."
while not index.running?
print "."
sleep 0.5
$stdout.flush
end
Topic.all_topics.each do |doc|
if File.exist?(doc)
name = name_for(doc)
puts "...indexing #{name}"
source = File.read(doc)
topic = Topic.load(name, source)
topic.text_only
result = indextank_document = index.document(name).add(:title => topic.title, :text => topic.body)
puts "=> #{result}"
end
end
puts "finished indexing"
end
desc 'Sample search'
task :search, :query do |t, args|
client = IndexTank::Client.new(ENV['HEROKUTANK_API_URL'])
index = client.indexes(AppConfig['index'])
results = index.search(args[:query], :fetch => 'title', :snippet => 'text')
puts "#{results['matches']} results."
puts results.inspect
end
desc 'Load doc files from config.yml dirs'
task :load do
AppConfig['dirs'].each do |name,dir|
if File.exist?(dir) and not dir == 'docs/'
puts "Copying #{dir}*.txt to docs/#{name}"
`rm -rf docs/#{name}`
`mkdir -p docs/#{name}`
`cp #{dir}*.txt docs/#{name}`
end
end
end
desc 'Create offline archive'
task :archive do
serverpid = Process.fork { Rake::Task['server'].invoke }
puts "Waiting 3 seconds for server to start..."
sleep 3
`wget -mirror -k -E -nH -nv -P archive http://127.0.0.1:9393/`
puts "Mirroring complete. Killing server."
Process.kill(9,serverpid)
Rake::Task['process_archive'].invoke
puts "Done"
end
desc 'Apply inline CSS styling to offline archive files'
task :process_archive do
downloadedHTML = File.join("archive","**","*.html")
puts "\nHighlighting unclickable links..."
htmlFiles = Dir.glob(downloadedHTML)
# Links that go to 127.0.0.1:9393 (where no server is running) get styled dark red
# Links that go to external sites (may not be reachable if user is truly offline) get italics
htmlFiles.each do |fileName|
puts "Processing " + fileName
fileContents = IO.read(fileName)
fileContents.gsub!(/href="(http:\/\/127.0.0.1:9393\/.*?)"/) do |match|
#puts "Red: " + $1
"style='color:darkred;cursor:pointer' title='" + $1 + " was unreachable'"
end
fileContents.gsub!(/href="http.*?"/) do |match|
#puts "Italic: " + match
match + ' style="font-style:italic"'
end
#puts ""
File.open(fileName,"w") do |fd|
fd.write(fileContents)
end
end
end
desc 'Load latest docs and publish to edge'
task :publish_edge => :load do
`git add .`
`git commit -m "cijoe auto-commit"`
`git push origin master`
`git push edge master`
end
desc 'Alias for server'
task :start => :server
def which(command)
ENV['PATH'].
split(':').
map { |p| "#{p}/#{command}" }.
find { |p| File.executable?(p) }
end
def name_for(doc)
parts = doc.split('/')
parts.size == 3 ? parts[1..-1].join('/').gsub(/\.txt/,'') : File.basename(doc, '.txt')
end