Sha256: 2a93df9b171b15888ca23a2a6dc6fd49dec32ab120bad82079c12df59d0e40f6

Contents?: true

Size: 1.36 KB

Versions: 25

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'yaml'

module PlatformosCheck
  module PlatformosLiquid
    module Tag
      extend self

      LABELS_NOT_IN_SOURCE_INDEX = %w[elsif ifchanged catch ensure when]

      def labels
        @labels ||= tags_file_contents
                    .map { |x| to_label(x) }
                    .to_set
      end

      def end_labels
        @end_labels ||= tags_file_contents
                        .select { |x| x.is_a?(Hash) }
                        .map { |x| x.values[0] }
      end

      def tag_regex(tag)
        return unless labels.include?(tag)

        @tag_regexes ||= {}
        @tag_regexes[tag] ||= /\A#{Liquid::TagStart}-?\s*#{tag}/m
      end

      def liquid_tag_regex(tag)
        return unless labels.include?(tag)

        @tag_liquid_regexes ||= {}
        @tag_liquid_regexes[tag] ||= /^\s*#{tag}/m
      end

      private

      def to_label(label)
        return label if label.is_a?(String)

        label.keys[0]
      end

      def tags_file_contents
        @tags_file_contents ||= SourceIndex.tags.map do |tag|
          opening_tag = tag.name
          closing_tag = "end#{opening_tag}"
          if /#{opening_tag}.+#{closing_tag}/m.match?(tag.hash['syntax'])
            { opening_tag => closing_tag }
          else
            opening_tag
          end
        end + LABELS_NOT_IN_SOURCE_INDEX
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
platformos-check-0.4.14 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.13 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.12 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.11 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.10 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.9 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.8 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.7 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.6 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.5 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.4 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.3 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.2 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.1 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.4.0 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.3.3 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.3.1 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.3.0 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.2.2 lib/platformos_check/platformos_liquid/tag.rb
platformos-check-0.2.1 lib/platformos_check/platformos_liquid/tag.rb