Sha256: 38ca8524aedb291bd81311a1af3de9cba5999ffac0a0a4ec8bb8c168c6c8e759

Contents?: true

Size: 902 Bytes

Versions: 6

Compression:

Stored size: 902 Bytes

Contents

# frozen_string_literal: true

require 'cgi'

module HS
  module ElementPreview
    def text_preview(text)
      return unless text
      CGI.escapeHTML(text)
    end

    def element_preview(tag_name, params, children)
      [
        element_start(tag_name, params, children),
        element_content(children),
        element_end(tag_name, children)
      ].join
    end

    private

    def element_start(tag, params, children)
      start = "<#{tag}"

      unless params.empty?
        params_str = params.map { |k, v| "#{k}=\"#{v}\"" }.join(' ')
        start = "#{start} #{params_str}"
      end

      return "#{start} />" if children.empty?
      "#{start}>"
    end

    def element_content(children)
      return if children.empty?
      children.map(&:preview).compact.join
    end

    def element_end(tag, children)
      return if children.empty?

      "</#{tag}>"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hs-cli-0.3.1 lib/hs/preview/element_preview.rb
hs-cli-0.3.0 lib/hs/preview/element_preview.rb
hs-cli-0.2.2 lib/hs/preview/element_preview.rb
hs-cli-0.2.1 lib/hs/preview/element_preview.rb
hs-cli-0.2.0 lib/hs/preview/element_preview.rb
hs-cli-0.1.2 lib/hs/preview/element_preview.rb