Class: Decidim::ContentParsers::HashtagParser

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/decidim/content_parsers/hashtag_parser.rb

Overview

A parser that searches hashtags in content.

A word starting with ‘#` will be considered as a hashtag if it only contains letters, numbers or underscores. If the `#` is followed with an underscore, then it is not considered.

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

HASHTAG_REGEX =

Matches a hashtag if it starts with a letter or number and only contains letters, numbers or underscores.

/(?:\A|\s\K)\B#([[:alnum:]](?:[[:alnum:]]|_)*)\b/i

Constants inherited from BaseParser

BaseParser::Metadata

Instance Attribute Summary

Attributes inherited from BaseParser

#content, #context

Instance Method Summary collapse

Methods inherited from BaseParser

#initialize

Constructor Details

This class inherits a constructor from Decidim::ContentParsers::BaseParser

Instance Method Details

#metadataObject



32
33
34
# File 'lib/decidim/content_parsers/hashtag_parser.rb', line 32

def 
  Metadata.new(content_hashtags.map { |content_hashtag| hashtag(content_hashtag) }.uniq)
end

#rewriteString

Replaces hashtags name with new or existing hashtags models global ids.

Returns:

  • (String)

    the content with the hashtags replaced by global ids



26
27
28
29
30
# File 'lib/decidim/content_parsers/hashtag_parser.rb', line 26

def rewrite
  content.gsub(HASHTAG_REGEX) do |match|
    "#{hashtag(match[1..-1]).to_global_id}/#{extra_hashtags? ? "_" : ""}#{match[1..-1]}"
  end
end