Class: Markbridge::Processors::DiscourseMarkdown::Detectors::Event

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

Overview

Detects Discourse event blocks [event]….

Examples:

detector = Event.new
input = '[event name="Meeting" start="2025-12-15 14:00"][/event]'
match = detector.detect(input, 0)
match.node.name # => "Meeting"

Constant Summary collapse

OPEN_TAG_PATTERN =
/\[event([^\]]*)\]/i
CLOSE_TAG_PATTERN =
%r{\[/event\]}i

Instance Method Summary collapse

Instance Method Details

#detect(input, pos) ⇒ Match?

Attempt to detect an event 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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/markbridge/processors/discourse_markdown/detectors/event.rb', line 23

def detect(input, pos)
  return nil unless input[pos] == "["

  # Check for opening tag
  remaining = input[pos..]
  open_match = OPEN_TAG_PATTERN.match(remaining)
  return nil unless open_match&.begin(0)&.zero?

  # Find closing tag
  close_match = CLOSE_TAG_PATTERN.match(remaining, open_match.end(0))
  return nil unless close_match

  # Extract raw content
  end_pos = pos + close_match.end(0)
  raw = input[pos...end_pos]

  # Parse attributes from opening tag
  attrs = parse_attributes(open_match[1])

  # Validate required attributes
  return nil unless attrs["name"] && attrs["start"]

  node =
    AST::Event.new(
      name: attrs["name"],
      starts_at: attrs["start"],
      ends_at: attrs["end"],
      status: attrs["status"],
      timezone: attrs["timezone"],
      raw:,
    )

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