Class: Decidim::ContentParsers::ParagraphParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- Decidim::ContentParsers::ParagraphParser
- Defined in:
- lib/decidim/content_parsers/paragraph_parser.rb
Overview
A parser that searches mentions of Paragraphs in content.
This parser accepts two ways for linking Paragraphs.
-
Using a standard url starting with http or https.
-
With a word starting with `~` and digits afterwards will be considered a possible mentioned paragraph.
For example `~1234`, but no `~ 1234`.
Also fills a `Metadata#linked_paragraphs` 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}/paragraphs/#{URL_REGEX_END_CHAR}+}i.freeze
- ID_REGEX =
Matches a mentioned Paragraph ID (~(d)+ expression)
/~(\d+)/.freeze
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#initialize(content, context) ⇒ ParagraphParser
constructor
A new instance of ParagraphParser.
-
#rewrite ⇒ String
Replaces found mentions matching an existing Paragraph with a global id for that Paragraph.
Constructor Details
#initialize(content, context) ⇒ ParagraphParser
Returns a new instance of ParagraphParser.
30 31 32 33 |
# File 'lib/decidim/content_parsers/paragraph_parser.rb', line 30 def initialize(content, context) super @metadata = Metadata.new([]) end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
46 47 48 |
# File 'lib/decidim/content_parsers/paragraph_parser.rb', line 46 def @metadata end |
Instance Method Details
#rewrite ⇒ String
Replaces found mentions matching an existing Paragraph with a global id for that Paragraph. Other mentions found that doesn't match an existing Paragraph are returned as they are.
40 41 42 43 |
# File 'lib/decidim/content_parsers/paragraph_parser.rb', line 40 def rewrite rewrited_content = parse_for_urls(content) parse_for_ids(rewrited_content) end |