Sha256: f6444349a2ebacabf0e4b36cc2ac45c35a360f7e0b0ea0124e2d3a1fac01a262

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module MicroMicro
  module Parsers
    class DateTimePropertyParser < BasePropertyParser
      HTML_ATTRIBUTES_MAP = {
        'datetime' => %w[del ins time],
        'title'    => %w[abbr],
        'value'    => %w[data input]
      }.freeze

      # @see https://microformats.org/wiki/microformats2-parsing#parsing_a_dt-_property
      #
      # @return [String]
      def value
        @value ||= resolved_value || attribute_value || super
      end

      private

      # @see https://microformats.org/wiki/value-class-pattern#microformats2_parsers_implied_date
      #
      # @return [MicroMicro::Parsers::DateTimeParser, nil]
      def adopted_date_time_parser
        @adopted_date_time_parser ||= begin
          date_time_siblings = (property.prev_all.reverse + property.next_all).select { |prop| prop.prefix == 'dt' }

          date_time_siblings.map { |prop| DateTimeParser.new(prop.value) }.find(&:normalized_date)
        end
      end

      # @return [String, nil]
      def attribute_value
        self.class.attribute_value_from(node, HTML_ATTRIBUTES_MAP)
      end

      # @return [MicroMicro::Parsers::DateTimeParser]
      def date_time_parser
        @date_time_parser ||= DateTimeParser.new(ValueClassPatternParser.new(node, ' ').value)
      end

      # @see https://microformats.org/wiki/value-class-pattern#microformats2_parsers_implied_date
      #
      # @return [Boolean]
      def imply_date?
        date_time_parser.normalized_time && !date_time_parser.normalized_date && adopted_date_time_parser
      end

      # @return [String]
      def resolved_value
        return "#{adopted_date_time_parser.normalized_date} #{date_time_parser.value}" if imply_date?

        date_time_parser.value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micromicro-1.1.0 lib/micro_micro/parsers/date_time_property_parser.rb