Sha256: 2284f37fd50f3e69ad0dfc3b6d065233b944414545fae8837b806f6ec55d84d8

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module Kitchen
  # An element for a note
  #
  class NoteElement < ElementBase

    # Creates a new +NoteElement+
    #
    # @param node [Nokogiri::XML::Node] the node this element wraps
    # @param document [Document] this element's document
    #
    def initialize(node:, document: nil)
      super(node: node,
            document: document,
            enumerator_class: NoteElementEnumerator)
    end

    # Returns the short type
    # @return [Symbol]
    #
    def self.short_type
      :note
    end

    # Returns the note's title element
    #
    # @return [Element, nil]
    #
    def title
      block_error_if(block_given?)
      first("[data-type='title']")
    end

    # Returns true if the note's title is autogenerated
    #
    # @return [Boolean]
    #
    def indicates_autogenerated_title?
      detected_note_title_key != 0 && detected_note_title_key.present?
    end

    # Get the autogenerated title for this note
    #
    # @return [String]
    #
    def autogenerated_title
      if indicates_autogenerated_title?
        I18n.t(:"notes.#{detected_note_title_key}")
      else
        "unknown title for note with classes #{classes}"
      end
    end

    protected

    def detected_note_title_key
      @detected_note_title_key ||= begin
        return 0 if classes.empty? || !I18n.t('.').key?(:notes)

        possible_keys = I18n.t(:notes).keys.map(&:to_s)
        keys = possible_keys & classes

        raise("too many translation keys: #{keys.join(', ')}") if keys.many?
        return 0 if keys.empty?

        keys.first
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
openstax_kitchen-9.2.0 lib/kitchen/note_element.rb
openstax_kitchen-9.1.0 lib/kitchen/note_element.rb
openstax_kitchen-9.0.0 lib/kitchen/note_element.rb
openstax_kitchen-8.0.1 lib/kitchen/note_element.rb
openstax_kitchen-8.0.0 lib/kitchen/note_element.rb
openstax_kitchen-7.0.0 lib/kitchen/note_element.rb
openstax_kitchen-6.1.0 lib/kitchen/note_element.rb
openstax_kitchen-6.0.0 lib/kitchen/note_element.rb
openstax_kitchen-5.0.0 lib/kitchen/note_element.rb
openstax_kitchen-4.1.1 lib/kitchen/note_element.rb
openstax_kitchen-4.1.0 lib/kitchen/note_element.rb
openstax_kitchen-4.0.0 lib/kitchen/note_element.rb