Sha256: 03a7b6e0e69f7ab5e369efd91e7ca738fe0a47efae95d87fb3e00e20dbc61cc9

Contents?: true

Size: 1.85 KB

Versions: 18

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
      govspeak_to_html(description) if description.present?
    end

    def has_block?
      !!block
    end

    def has_embed?
      !!embed
    end

  private

    def govspeak_to_html(govspeak)
      Govspeak::Document.new(govspeak).to_html
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
govuk_publishing_components-16.13.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.12.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.11.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.10.1 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.10.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.9.2 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.9.1 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.9.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.8.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.7.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.6.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.5.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.4.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.3.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.2.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.1.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-16.0.0 app/models/govuk_publishing_components/component_example.rb
govuk_publishing_components-15.3.0 app/models/govuk_publishing_components/component_example.rb