/
ochook.rb
180 lines (135 loc) · 3.69 KB
/
ochook.rb
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
require 'fileutils'
require 'nokogiri'
module Chook
class Ochook
attr_accessor :id
attr_reader :invalidity
def self.from_zhook(path, id_length)
ook = new
ook.id = ook.find_unique_id(id_length)
ook.from_zhook(path)
ook
end
def self.from_id(id)
ook = new
ook.id = id.to_s
ook
end
def find_unique_id(id_length)
100.times {
id = generate_id(id_length)
return id unless File.directory?(system_path(id))
}
raise "Cannot find a unique id"
rescue => e
self.invalidity = e
end
def from_zhook(path)
FileUtils.mkdir_p(system_path)
`unzip #{path} -d #{system_path}`
raise "Not a zip file" unless $?.success?
unless File.exists?(system_path(@id, "index.html"))
raise "index.html not found"
end
unless File.exists?(system_path(@id, "cover.png"))
raise "cover.png not found"
end
generate_manifest
insert_manifest_attribute
rescue => e
self.invalidity = e
end
def system_path(id = @id, *args)
pave('public', 'books', id, args)
end
def public_path(id = @id, *args)
"/#{pave('books', id, args)}"
end
def index_document
return @doc if @doc
File.open(system_path(@id, "index.html"), 'r') { |f|
return @doc = Nokogiri::HTML::Document.parse(f)
}
end
def metadata(name)
doc = index_document
if node = doc.at_css("meta[name=#{name}]")
node['content']
else
nil
end
end
def valid?
@invalidity ? false : true
end
def exists?
File.directory?(system_path)
end
def invalidity=(exception)
if Sinatra::Application.environment == :development
raise exception
else
@invalidity = exception
#puts "Ochook invalid: #{@invalidity.inspect}"
end
end
def destroy
FileUtils.rm_rf(system_path) if @id
end
def toc_html
link_to_section = lambda { |sxn, heading|
sid = sxn.heading['id'] || sxn.node['id']
return heading unless sid && !sid.empty?
'<a href="#'+sid+'">'+heading+'</a>'
}
outliner = Chook::Outliner.new(index_document)
outliner.process(index_document.root)
outline_html = outliner.to_html { |sxn, below|
heading = sxn.heading_text
if heading
heading = link_to_section.call(sxn, heading)
elsif sxn.respond_to?(:container) && sxn.container && !below.empty?
heading = '<br class="anon" />'
end
heading
}
end
protected
def generate_id(len = 4)
require 'digest/sha1'
s = Digest::SHA1.new
s << Time.now.to_s
s << String(Time.now.usec)
s << String(rand(0))
s << String($$)
str = s.hexdigest
str.slice(rand(str.size - len), len)
end
def generate_manifest
manifest = [
"CACHE MANIFEST",
"NETWORK:",
"*",
"CACHE:",
"/read/#{@id}/"
]
Dir.glob(File.join(system_path, "**", "*")).each { |path|
manifest << path.gsub(/^public/, '') unless File.directory?(path)
}
File.open(system_path(@id, "ochook.manifest"), 'w') { |f|
f.write(manifest.join("\n"))
}
end
def insert_manifest_attribute
doc = index_document
doc.at_css('html').set_attribute('manifest', 'ochook.manifest')
File.open(system_path(@id, "index.html"), "w") { |f|
f.write(doc.to_html)
}
end
# A simple File.join shortcut
def pave(*args)
File.join(*(args.flatten.compact))
end
end
end