Sha256: c116bac2488b4ac7abe2960a34647f471a36314a7be7b7e54f6f23fff2d2826f

Contents?: true

Size: 1.94 KB

Versions: 24

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module PagesCore
  class HtmlFormatter
    class << self
      def to_html(string, options = {})
        new(string, options).to_html
      end
    end

    def initialize(string, options = {})
      @string = string
      @options = options
    end

    def to_html
      string = shorten(parse_images(parse_files(parse_attachments(@string))))
      string += " #{@options[:append]}" if @options[:append]
      fix_markup(RedCloth.new(string).to_html).html_safe
    end

    private

    def attachment_expression
      /\[attachment:([\d,]+)\]/
    end

    def file_expression
      /\[file:([\d,]+)\]/
    end

    def find_attachment(id)
      Attachment.find(id).localize(I18n.locale)
    rescue ActiveRecord::RecordNotFound
      nil
    end

    def find_attachments(str)
      str.match(attachment_expression)[1]
         .split(",")
         .map { |id| find_attachment(id) }
         .compact
    end

    def find_file(id)
      PageFile.find(id).localize(I18n.locale)
    rescue ActiveRecord::RecordNotFound
      nil
    end

    def find_files(str)
      str.match(file_expression)[1]
         .split(",")
         .map { |id| find_file(id) }
         .compact
    end

    def fix_markup(str)
      Nokogiri::HTML.fragment(str).to_html
    end

    def parse_attachments(string)
      string.gsub(attachment_expression).each do |str|
        PagesCore.config.attachment_embedder.new(
          find_attachments(str)
        ).to_html
      end
    end

    def parse_files(string)
      string.gsub(file_expression).each do |str|
        PagesCore.config.attachment_embedder.new(
          find_files(str).map(&:attachment)
        ).to_html
      end
    end

    def parse_images(string)
      PagesCore::ImageEmbedder.new(string).embed
    end

    def shorten(string)
      unless @options[:shorten] && string.length > @options[:shorten]
        return string
      end

      "#{string[0..@options[:shorten]]}..."
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
pages_core-3.14.0 app/formatters/pages_core/html_formatter.rb
pages_core-3.13.0 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.7 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.6 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.5 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.4 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.3 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.2 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.1 app/formatters/pages_core/html_formatter.rb
pages_core-3.12.0 app/formatters/pages_core/html_formatter.rb
pages_core-3.11.3 app/formatters/pages_core/html_formatter.rb
pages_core-3.11.2 app/formatters/pages_core/html_formatter.rb
pages_core-3.11.1 app/formatters/pages_core/html_formatter.rb
pages_core-3.11.0 app/formatters/pages_core/html_formatter.rb
pages_core-3.10.2 app/formatters/pages_core/html_formatter.rb
pages_core-3.10.1 app/formatters/pages_core/html_formatter.rb
pages_core-3.9.2 app/formatters/pages_core/html_formatter.rb
pages_core-3.9.1 app/formatters/pages_core/html_formatter.rb
pages_core-3.9.0 app/formatters/pages_core/html_formatter.rb
pages_core-3.8.3 app/formatters/pages_core/html_formatter.rb