Sha256: 876c94b72fffb3be4a6b624cf6db4928454a7348b83a8382b2c5e2f2743988e6

Contents?: true

Size: 1.85 KB

Versions: 17

Compression:

Stored size: 1.85 KB

Contents

require 'rouge'

module GovukPublishingComponents
  class ComponentExample
    attr_reader :id,
                :data,
                :context,
                :description,
                :block,
                :embed

    def initialize(id, example)
      @id = id
      @data = example["data"] || {}
      @context = example["context"] || {}
      @description = example["description"] || false
      @block = @data.delete(:block) || false
      @embed = example["embed"]
    end

    def name
      id.humanize
    end

    def highlight_code(string_to_format = '')
      formatter = Rouge::Formatters::HTML.new
      lexer = Rouge::Lexers::ERB.new
      formatter.format(lexer.lex(string_to_format)).html_safe
    end

    def pretty_data
      JSON.pretty_generate(data).gsub('\\n', "\n    ").gsub(/"(\w*)":/, '\1:').strip
    end

    def data?
      data.any?
    end

    def html_safe_data
      html_safe_strings(data.dup)
    end

    # Iterate through data object and recursively mark
    # any found string as html_safe
    #
    # Safe HTML can be passed to components, simulate
    # by marking any string that comes from YAML as safe
    def html_safe_strings(obj)
      if obj.is_a?(String)
        obj.html_safe
      elsif obj.is_a?(Hash)
        obj.each do |key, value|
          obj[key] = html_safe_strings(value)
        end
      elsif obj.is_a?(Array)
        obj.map! { |e| html_safe_strings(e) }
      else
        obj
      end
    end

    def right_to_left?
      !!context['right_to_left']
    end

    def dark_background?
      !!context['dark_background']
    end

    def html_description
      markdown_to_html(description) if description.present?
    end

    def has_block?
      !!block
    end

    def has_embed?
      !!embed
    end

  private

    def markdown_to_html(markdown)
      Kramdown::Document.new(markdown).to_html
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
govuk_publishing_components-16.27.1 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.27.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.26.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.25.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.24.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.23.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.22.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.21.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.20.1 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.20.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.19.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.18.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.17.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.16.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.15.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.14.1 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.14.0 app/models/govuk_publishing_components/component_example.rb