Class: Markbridge::Processors::DiscourseMarkdown::Detectors::Poll

Inherits:
Base
  • Object
show all
Defined in:
lib/markbridge/processors/discourse_markdown/detectors/poll.rb

Overview

Detects Discourse poll blocks [poll]….

Examples:

detector = Poll.new
input = "[poll type=\"regular\"]\n* A\n* B\n[/poll]"
match = detector.detect(input, 0)
match.node.type # => "regular"

Constant Summary collapse

TAG_PATTERN =
%r{\A\[poll(?<attrs>[^\]]*)\](?<content>.*?)\[/poll\]}im

Instance Method Summary collapse

Instance Method Details

#detect(input, pos) ⇒ Match?

Attempt to detect a poll at the given position.

Parameters:

  • input (String)

    the full input string

  • pos (Integer)

    current position to check

Returns:

  • (Match, nil)

    match result or nil if no match



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/markbridge/processors/discourse_markdown/detectors/poll.rb', line 22

def detect(input, pos)
  match = TAG_PATTERN.match(input[pos..])
  return nil unless match

  attrs = parse_attributes(match[:attrs])
  node =
    AST::Poll.new(
      name: attrs["name"] || "poll",
      type: attrs["type"],
      results: attrs["results"],
      public: attrs["public"] == "true",
      chart_type: attrs["charttype"],
      options: extract_options(match[:content]),
      raw: match[0],
    )

  Match.new(start_pos: pos, end_pos: pos + match.end(0), node:)
end