class Podgraph::Transformer

Attributes

images[R]

Public Class Methods

local_img?(node, attr = ['src']) click to toggle source
# File podgraph.rb, line 66
def self.local_img? node, attr = ['src'] # TODO: allow file:// scheme
  attr.any? do |k|
    node[k] && node[k].strip.size > 0 &&
      !node[k].start_with?('data:') && !URI(node[k]).scheme
  end
end
new(html) click to toggle source
# File podgraph.rb, line 11
def initialize html
  @doc = Nokogiri.HTML html
  svg!
  pre!
  @images = raster_images!
end

Public Instance Methods

html() click to toggle source
# File podgraph.rb, line 64
def html; @doc.to_s; end
pre!() click to toggle source

replace every newline in <pre> with
, because Blogger

# File podgraph.rb, line 32
def pre!
  @doc.css('pre').each do |node|
    text = node.to_s.gsub(/\n/, '<br>')
    node.replace Nokogiri::HTML.fragment text
  end
end
raster_images!() click to toggle source

return local images; relink each affecter <img>

# File podgraph.rb, line 40
def raster_images!
  images = {}
  @doc.css('img').select do |img|
    Transformer.local_img? img
  end.each do |img|
    fail "unsupported file format: #{img['src']}" if !MiniMime.lookup_by_filename img['src']

    sha1 = Digest::SHA1.hexdigest File.read img['src']
    images[sha1] ||= { name: img['src'] }
    img['src'] = "cid:#{sha1}"
  end
  images
end
subject() click to toggle source
# File podgraph.rb, line 54
def subject
  @subject ||= begin
                 node = @doc.css('h1,h2,h3,h4').first
                 return 'no subject' unless node
                 text = node.text
                 node.remove
                 text
               end
end
svg!() click to toggle source

inline svg; I've tried a straight xml insertion, but Blogger's freaked out

# File podgraph.rb, line 20
def svg!
  attr = ->(node) { ['src', 'data'].find {|k| node[k]} }
  @doc.css('img,iframe,embed,object[type="image/svg+xml"]').select do |node|
    src = attr.call node
    Transformer.local_img?(node, ['src', 'data']) && MiniMime.lookup_by_filename(node[src])&.content_type == 'image/svg+xml'
  end.each do |node|
    src = attr.call node
    node[src] = "data:image/svg+xml;base64,#{Base64.strict_encode64(File.read node[src])}"
  end
end