Sha256: 6f63a3e516e801b3c5d36c2204cc9633201b43ffae9926c66cdd043e2d998c90

Contents?: true

Size: 1.9 KB

Versions: 15

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module PagesCore
  class ImageEmbedder
    include ActionView::Helpers::AssetTagHelper
    include PagesCore::ImagesHelper
    include Rails.application.routes.url_helpers

    def initialize(string)
      @string = string
    end

    def embed
      parse_images(@string)
    end

    private

    def image_expression
      /\[image:(\d+)([^\]]*)?\]/
    end

    def embed_image(id, size:, class_name:, link:)
      image = Image.find(id).localize(I18n.locale)
      class_name = ["image", image_class_name(image), class_name].compact
      image_tag = dynamic_image_tag(image,
                                    size: size, crop: false, upscale: false)
      tag.figure((link ? link_to(image_tag, link) : image_tag) +
                  image_caption(image), class: class_name)
    rescue ActiveRecord::RecordNotFound
      nil
    end

    def embed_image_size(str)
      if str =~ /size="(\d*x\d*)"/
        Regexp.last_match(1)
      else
        "2000x2000"
      end
    end

    def image_caption(image)
      return unless image.caption?

      tag.figcaption(image.caption)
    end

    def image_class_name(image)
      if image.size.x == image.size.y
        "square"
      elsif image.size.x > image.size.y
        "landscape"
      else
        "portrait"
      end
    end

    def link_to(content, href)
      tag.a(content, href: href)
    end

    def parse_image(str)
      id = str.match(image_expression)[1]
      options = str.match(image_expression)[2]
      class_name = (Regexp.last_match(1) if options =~ /class="([\s\-\w]+)"/)
      link = (Regexp.last_match(1) if options =~ /link="([^"]+)"/)
      embed_image(id,
                  size: embed_image_size(options),
                  class_name: class_name,
                  link: link)
    end

    def parse_images(string)
      string.gsub(image_expression).each do |str|
        parse_image(str)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
pages_core-3.12.0 app/formatters/pages_core/image_embedder.rb
pages_core-3.11.3 app/formatters/pages_core/image_embedder.rb
pages_core-3.11.2 app/formatters/pages_core/image_embedder.rb
pages_core-3.11.1 app/formatters/pages_core/image_embedder.rb
pages_core-3.11.0 app/formatters/pages_core/image_embedder.rb
pages_core-3.10.2 app/formatters/pages_core/image_embedder.rb
pages_core-3.10.1 app/formatters/pages_core/image_embedder.rb
pages_core-3.9.2 app/formatters/pages_core/image_embedder.rb
pages_core-3.9.1 app/formatters/pages_core/image_embedder.rb
pages_core-3.9.0 app/formatters/pages_core/image_embedder.rb
pages_core-3.8.3 app/formatters/pages_core/image_embedder.rb
pages_core-3.8.2 app/formatters/pages_core/image_embedder.rb
pages_core-3.8.1 app/formatters/pages_core/image_embedder.rb
pages_core-3.8.0 app/formatters/pages_core/image_embedder.rb
pages_core-3.7.0 app/formatters/pages_core/image_embedder.rb