Top Level Namespace
Defined Under Namespace
Modules: Jekyll
Classes: PaspagonConfig
Instance Method Summary
collapse
-
#default_content_type(format) ⇒ Object
-
#format_paid?(post, format_config) ⇒ Boolean
-
#format_xattrs(format, format_config) ⇒ Object
-
#generate_doc(post, format, format_config, dest_path) ⇒ Object
-
#generation_command(post, format, _format_config, dest_path) ⇒ Object
-
#hash_to_ini(hash, section = nil, section_nested = nil) ⇒ Object
-
#maybe_generate_doc(post, format) ⇒ Object
-
#pandoc_input_format(site) ⇒ Object
-
#sync_payment_attributes(path, attrs) ⇒ Object
Instance Method Details
#default_content_type(format) ⇒ Object
85
86
87
88
89
90
91
92
93
|
# File 'lib/jekyll-paspagon/hooks.rb', line 85
def default_content_type(format)
content_types =
{'epub' => 'application/epub+zip',
'html' => 'text/html',
'mobi' => 'application/x-mobipocket-ebook',
'pdf' => 'application/pdf'}
content_types.default = 'application/octet-stream'
content_types[format]
end
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/jekyll-paspagon/hooks.rb', line 176
def format_paid?(post, format_config)
too_late =
if format_config['paid_before']
Date.today - Date.parse(post.date.to_s) >= format_config['paid_before'].to_i
else
false
end
too_soon =
if format_config['paid_after']
Date.today - Date.parse(post.date.to_s) <= format_config['paid_after'].to_i
else
false
end
!too_late && !too_soon
end
|
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/jekyll-paspagon/hooks.rb', line 71
def format_xattrs(format, format_config)
attrs = {}
format_config['prices'].each do |currency, price|
attrs["user.x-amz-meta-price-#{currency}"] = price if price
end
format_config['addresses'].each do |currency, address|
attrs["user.x-amz-meta-address-#{currency}"] = address if address
end
attrs['user.x-amz-meta-link-expiration-time'] = format_config['link_expiration_time'] if format_config['link_expiration_time']
attrs['user.content-disposition'] = format_config['content_disposition'] if format_config['content_disposition']
attrs['user.content-type'] = format_config['content_type'] || default_content_type(format)
attrs
end
|
#generate_doc(post, format, format_config, dest_path) ⇒ Object
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/jekyll-paspagon/hooks.rb', line 135
def generate_doc(post, format, format_config, dest_path)
case format
when 'html'
if format_config['paid']
free_dest = post.destination
system("mv #{free_dest} #{dest_path}")
post.content = post.data['excerpt'].output
post.data['excerpt_only'] = true
post.output = Jekyll::Renderer.new(post.site, post, post.site.site_payload).run
File.open(free_dest, 'wb') do |f|
f.write(post.output)
end
end else
command = generation_command(post, format, format_config, dest_path)
raise "Failed to execute: #{command}" unless system command
end
end
|
#generation_command(post, format, _format_config, dest_path) ⇒ Object
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/jekyll-paspagon/hooks.rb', line 155
def generation_command(post, format, _format_config, dest_path)
input_format = pandoc_input_format(post.site)
case format
when 'azw3', 'mobi', 'pdf'
intermediary_path = File.join(Dir.tmpdir, Digest::SHA256.hexdigest(dest_path) + '.epub')
"pandoc #{post.path} -f #{input_format} -t epub -o #{intermediary_path} && ebook-convert #{intermediary_path} #{dest_path} --start-reading-at \"//h:h1[not(contains (@class, 'title'))]\" > /dev/null && rm #{intermediary_path}"
else
"pandoc #{post.path} -f #{input_format} -t #{format} -o #{dest_path}"
end
end
|
#hash_to_ini(hash, section = nil, section_nested = nil) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/jekyll-paspagon/config.rb', line 60
def hash_to_ini(hash, section = nil, section_nested = nil)
sections, entries = hash.partition { |_, v| v.is_a? Hash }
lines = []
lines += ["[#{section}]"] if section && !section_nested
section_prefix = section_nested ? "#{section}-" : ''
lines += entries.map { |e| section_prefix + e.join(' = ') }
lines += sections.map do |inner_section, inner_section_entries|
hash_to_ini(inner_section_entries, section_prefix + inner_section, section)
end
lines.flatten.join("\n")
end
|
#maybe_generate_doc(post, format) ⇒ Object
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
|
# File 'lib/jekyll-paspagon/hooks.rb', line 107
def maybe_generate_doc(post, format)
format_config = post.data['formats'][format]
dest_path = format_config['path']
FileUtils.mkdir_p(File.dirname(dest_path))
if File.exist?(dest_path)
source_path = post.path
if File.ctime(source_path) > File.ctime(dest_path)
temp_path = File.join(Dir.tmpdir, Digest::SHA256.hexdigest(dest_path) + '.' + format)
generate_doc(post, format, format_config, temp_path)
temp_hash = Digest::SHA256.file(temp_path)
dest_hash = Digest::SHA256.file(dest_path)
if temp_hash == dest_hash
File.delete(temp_path)
false
else
FileUtils.mv(temp_path, dest_path)
true
end
else
false
end
else
generate_doc(post, format, format_config, dest_path)
true
end
end
|
166
167
168
169
170
171
172
173
174
|
# File 'lib/jekyll-paspagon/hooks.rb', line 166
def pandoc_input_format(site)
(site.config['pandoc'] || {})['input'] ||
begin
kramdown = site.config['kramdown'] || {}
format = 'markdown_github+yaml_metadata_block'
format += '-hard_line_breaks' unless kramdown['hard_wrap']
format
end
end
|
#sync_payment_attributes(path, attrs) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/jekyll-paspagon/hooks.rb', line 95
def sync_payment_attributes(path, attrs)
xattr = Xattr.new(path)
old = xattr.list.select { |a| a.start_with?('user.') }
new = attrs.keys
(old - new).each do |a|
xattr.remove a
end
new.each do |a|
xattr[a] = attrs[a] unless xattr[a] == attrs[a].to_s
end
end
|