Sha256: 60a5d953d0c88f93992a56c1fbb687a5a24aefe99f6250ee8a4424ee4e72162e

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

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 ||=
          (property.prev_all.reverse + property.next_all).filter_map do |prop|
            DateTimeParser.new(prop.value) if prop.date_time_property?
          end.find(&:normalized_date)
      end

      # @return [String, nil]
      def attribute_value
        Helpers.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

2 entries across 2 versions & 1 rubygems

Version Path
micromicro-2.0.1 lib/micro_micro/parsers/date_time_property_parser.rb
micromicro-2.0.0 lib/micro_micro/parsers/date_time_property_parser.rb