Sha256: 01e19e780a35037d2752e1f113dbc575485dd0760956a89b34c6087b1d76514c

Contents?: true

Size: 1.85 KB

Versions: 13

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module Decidim
  module ContentParsers
    # A parser that searches specific tags in the content. This is an abstract
    # class that provides some helper methods for parsing the content and the
    # implementation has to be defined by all the classes that inherit from this
    # class.
    #
    # @see BaseParser Examples of how to use a content parser
    class TagParser < BaseParser
      # Replaces tags name with new or existing tags models global ids.
      #
      # The actual tags depend on the context, these can be hashtags, user
      # mentions, user group mentions, etc.
      #
      # @return [String] the content with the tags replaced by global ids
      def rewrite
        if html_content?
          html_fragment.search("span[data-type='#{tag_data_type}']").each do |el|
            el.replace replace_tags(element_tag(el))
          end
          html_fragment.to_s
        else
          replace_tags(content)
        end
      end

      protected

      def tag_data_type
        raise NotImplementedError, "#{self.class.name} does not define tag_data_type"
      end

      def replace_tags(text)
        raise NotImplementedError, "#{self.class.name} does not define replace_tags"
      end

      def scan_tags(text)
        raise NotImplementedError, "#{self.class.name} does not define scan_tags"
      end

      def element_tag(element)
        element["data-id"] || element["data-label"] || element.content
      end

      def content_tags
        @content_tags ||= begin
          scannables =
            if html_content?
              html_fragment.search("span[data-type='#{tag_data_type}']").map do |el|
                element_tag(el)
              end
            else
              [content]
            end

          scannables.map { |str| scan_tags(str) }.flatten.uniq
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.4 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.29.0 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.3 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.29.0.rc4 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.29.0.rc3 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.29.0.rc2 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.29.0.rc1 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.2 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.1 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.0 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.0.rc5 lib/decidim/content_parsers/tag_parser.rb
decidim-core-0.28.0.rc4 lib/decidim/content_parsers/tag_parser.rb