Class: Decidim::ContentParsers::ProposalParser

Inherits:
BaseParser
  • Object
show all
Defined in:
decidim-proposals/lib/decidim/content_parsers/proposal_parser.rb

Overview

A parser that searches mentions of Proposals in content.

This parser accepts two ways for linking Proposals.

  • Using a standard url starting with http or https.

  • With a word starting with `~` and digits afterwards will be considered a possible mentioned proposal.

For example `~1234`, but no `~ 1234`.

Also fills a `Metadata#linked_proposals` attribute.

Defined Under Namespace

Classes: Metadata

Constant Summary collapse

URL_REGEX_SCHEME =

Matches a URL

'(?:http(s)?:\/\/)'
URL_REGEX_CONTENT =
'[\w.-]+[\w\-\._~:\/?#\[\]@!\$&\'\(\)\*\+,;=.]+'
URL_REGEX_END_CHAR =
'[\d]'
URL_REGEX =
%r{#{URL_REGEX_SCHEME}#{URL_REGEX_CONTENT}/proposals/#{URL_REGEX_END_CHAR}+}i.freeze
ID_REGEX =

Matches a mentioned Proposal ID (~(d)+ expression)

/~(\d+)/.freeze

Constants inherited from BaseParser

BaseParser::Metadata

Instance Attribute Summary collapse

Attributes inherited from BaseParser

#content, #context

Instance Method Summary collapse

Constructor Details

#initialize(content, context) ⇒ ProposalParser

Returns a new instance of ProposalParser.


30
31
32
33
# File 'decidim-proposals/lib/decidim/content_parsers/proposal_parser.rb', line 30

def initialize(content, context)
  super
  @metadata = Metadata.new([])
end

Instance Attribute Details

#metadataMetadata (readonly)

This method is abstract.

Subclass is expected to implement it

Collects and returns metadata. This metadata is accessible at parsing time so it can be acted upon (sending emails to the users) or maybe even stored at the DB for later consultation.

Examples:

Implementation for return a counter of prohibited words found

Metadata = Struct.new(:count)

def 
  Metadata.new(content.scan('foo').size)
end

Returns:

  • (Metadata)

    a Metadata object that holds extra information


46
47
48
# File 'decidim-proposals/lib/decidim/content_parsers/proposal_parser.rb', line 46

def 
  @metadata
end

Instance Method Details

#rewriteString

Replaces found mentions matching an existing Proposal with a global id for that Proposal. Other mentions found that doesn't match an existing Proposal are returned as they are.

Returns:

  • (String)

    the content with the valid mentions replaced by a global id.


40
41
42
43
# File 'decidim-proposals/lib/decidim/content_parsers/proposal_parser.rb', line 40

def rewrite
  rewrited_content = parse_for_urls(content)
  parse_for_ids(rewrited_content)
end