mirror of https://github.com/freeCodeCamp/devdocs
parent
6fc48db8af
commit
a6855329e8
@ -0,0 +1,24 @@
|
||||
verbose: false
|
||||
skip_missing_workers: true
|
||||
allow_lossy: true
|
||||
advpng: false
|
||||
gifsicle:
|
||||
interlace: false
|
||||
level: 3
|
||||
careful: true
|
||||
jhead: false
|
||||
jpegoptim:
|
||||
strip: all
|
||||
max_quality: 100
|
||||
jpegrecompress: false
|
||||
jpegtran: false
|
||||
optipng:
|
||||
level: 3
|
||||
interlace: false
|
||||
strip: true
|
||||
pngcrush: false
|
||||
pngout: false
|
||||
pngquant:
|
||||
quality: !ruby/range 80..99
|
||||
speed: 3
|
||||
svgo: false
|
@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Docs
|
||||
class ImagesFilter < Filter
|
||||
include Instrumentable
|
||||
|
||||
def self.optimize_image_data(data)
|
||||
@image_optim ||= ImageOptim.new
|
||||
@image_optim.optimize_image_data(data)
|
||||
end
|
||||
|
||||
def call
|
||||
@@cache ||= {}
|
||||
|
||||
doc.css('img[src]').each do |node|
|
||||
src = node['src']
|
||||
|
||||
if @@cache.key?(src)
|
||||
node['src'] = @@cache[src] unless @@cache[src] == false
|
||||
next
|
||||
end
|
||||
|
||||
@@cache[src] = false
|
||||
|
||||
url = Docs::URL.parse(src)
|
||||
url.scheme = 'https' if url.scheme.nil?
|
||||
next unless url.scheme == 'http' || url.scheme == 'https'
|
||||
|
||||
begin
|
||||
Request.run(url) do |response|
|
||||
unless response.success?
|
||||
instrument 'broken.image', url: url, status: response.code
|
||||
next
|
||||
end
|
||||
|
||||
unless response.mime_type.start_with?('image/')
|
||||
instrument 'invalid.image', url: url, content_type: response.mime_type
|
||||
next
|
||||
end
|
||||
|
||||
image = response.body
|
||||
|
||||
unless context[:optimize_images] == false
|
||||
image = self.class.optimize_image_data(image) || image
|
||||
end
|
||||
|
||||
size = image.bytesize
|
||||
|
||||
if size > max_size
|
||||
instrument 'too_big.image', url: url, size: size
|
||||
next
|
||||
end
|
||||
|
||||
image = Base64.strict_encode64(image)
|
||||
image.prepend "data:#{response.mime_type};base64,"
|
||||
node['src'] = @@cache[src] = image
|
||||
end
|
||||
rescue => exception
|
||||
instrument 'error.image', url: url, exception: exception
|
||||
end
|
||||
end
|
||||
|
||||
doc
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def max_size
|
||||
@max_size ||= context[:max_image_size] || 100.kilobytes
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Docs
|
||||
class ImageSubscriber < Subscriber
|
||||
self.namespace = 'image'
|
||||
|
||||
def broken(event)
|
||||
log "Skipped broken image (#{event.payload[:code]}): #{event.payload[:url]}"
|
||||
end
|
||||
|
||||
def invalid(event)
|
||||
log "Skipped invalid image (#{event.payload[:content_type]}): #{event.payload[:url]}"
|
||||
end
|
||||
|
||||
def too_big(event)
|
||||
log "Skipped large image (#{(event.payload[:size] / 1.kilobyte.to_f).round} KB): #{event.payload[:url]}"
|
||||
end
|
||||
|
||||
def error(event)
|
||||
exception = event.payload[:exception]
|
||||
log "ERROR: #{event.payload[:url]}"
|
||||
puts " #{exception.class}: #{exception.message.gsub("\n", "\n ")}"
|
||||
puts exception.backtrace.select { |line| line.start_with?(Docs.root_path) }.join("\n ").prepend("\n ")
|
||||
puts "\n"
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in new issue