Module: Coradoc::AsciiDoc::Parser::Block

Defined in:
lib/coradoc/asciidoc/parser/block.rb

Instance Method Summary collapse

Instance Method Details

#block(n_deep = 3) ⇒ Object

AsciiDoc Block Delimiter Patterns:

  • “4+” means 4 or more identical characters

  • “2+” means 2 or more identical characters

Block types by delimiter character:

  • “=” (4+): Example block (====, =====, etc.)

  • “-” (4+): Source/listing block (—-, —–, etc.)

  • “-” (2): Open block (–)

  • “_” (4+): Quote block (__, _, etc.)

  • “*” (4+): Sidebar block (****, *****, etc.)

  • “+” (4+): Pass block (++++, +++++, etc.)

  • “.” (4+): Literal block (.…, .…., etc.)

Table delimiters:

  • “|===” defines table boundaries

  • “|” separates cells within the table



24
25
26
27
28
29
30
31
# File 'lib/coradoc/asciidoc/parser/block.rb', line 24

def block(n_deep = 3)
  (example_block(n_deep) |
  sidebar_block(n_deep) |
  source_block(n_deep) |
  quote_block(n_deep) |
  pass_block(n_deep) |
  open_block(n_deep)).as(:block)
end

#block_content(n_deep = 3) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/coradoc/asciidoc/parser/block.rb', line 76

def block_content(n_deep = 3)
  c = block_image |
      list |
      text_line(false, unguarded: true) |
      empty_line.as(:line_break)
  c |= block(n_deep - 1) if n_deep.positive?
  c.repeat(1)
end

#block_delimiterObject

Block delimiter: 4+ identical characters (or 2 for open block) NOTE: repeat(4,) means 4 or more (not exactly 4)



87
88
89
90
91
92
93
94
95
96
# File 'lib/coradoc/asciidoc/parser/block.rb', line 87

def block_delimiter
  line_start? >>
    ((str('*') |
      str('=') |
      str('_') |
      str('+') |
      str('-')).repeat(4) | # 4+ characters for most blocks
      str('-').repeat(2, 2)) >> # Exactly 2 for open block
    newline
end

#block_style(n_deep = 3, delimiter = '*', repeater = 4, type = nil, verbatim: false) ⇒ Object

Block style parser with variable delimiter length

Parameters:

  • n_deep (Integer) (defaults to: 3)

    Nesting depth for nested blocks

  • delimiter (String) (defaults to: '*')

    The delimiter character (“=”, “-”, “_”, “*”, “+”)

  • repeater (Integer) (defaults to: 4)

    Minimum number of delimiter characters (default: 4)

  • type (Symbol) (defaults to: nil)

    Block type for special handling (e.g., :pass)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/coradoc/asciidoc/parser/block.rb', line 113

def block_style(n_deep = 3, delimiter = '*', repeater = 4, type = nil, verbatim: false)
  # repeat(repeater,) means repeater or more characters
  current_delimiter = str(delimiter).repeat(repeater).capture(:delimit)
  closing_delimiter = dynamic do |_s, c|
    str(c.captures[:delimit].to_s.strip)
  end

  # Create a block content parser that respects the closing delimiter
  # This prevents nested blocks from consuming the closing delimiter
  block_content_with_closing = dynamic do |_s, c|
    delim_str = c.captures[:delimit].to_s.strip
    closing_pattern = str(delim_str) >> newline

    # Build content that doesn't match the closing delimiter
    content = block_image | list | text_line(false, unguarded: true,
                                                    verbatim: verbatim) | empty_line.as(:line_break)
    if n_deep.positive?
      # For nested blocks, also prevent them from consuming the closing delimiter
      content |= block(n_deep - 1)
    end

    # Each content element must not start with the closing delimiter
    (closing_pattern.absent? >> content).repeat(1)
  end

  element_attributes >>
    (line_start? >> attribute_list >> newline).maybe >>
    line_start? >>
    current_delimiter.as(:delimiter) >> newline >>
    if type == :pass
      (text_line(false, unguarded: true, verbatim: verbatim) | empty_line.as(:line_break)).repeat(1).as(:lines)
    else
      # Use dynamic block content that respects closing delimiter
      block_content_with_closing.as(:lines)
    end >>
    line_start? >>
    closing_delimiter >> newline
end

#block_style_exact(n_deep = 3, delimiter = '-', exact_chars = 2, type = nil) ⇒ Object

Block style parser with EXACT delimiter length (for open blocks) Open blocks use exactly 2 dashes and cannot nest within themselves



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/coradoc/asciidoc/parser/block.rb', line 154

def block_style_exact(n_deep = 3, delimiter = '-', exact_chars = 2, type = nil)
  current_delimiter = str(delimiter).repeat(exact_chars, exact_chars).capture(:delimit)
  closing_delimiter = dynamic do |_s, c|
    str(c.captures[:delimit].to_s.strip)
  end

  # Create a block content parser that respects the closing delimiter
  block_content_with_closing = dynamic do |_s, c|
    delim_str = c.captures[:delimit].to_s.strip
    closing_pattern = str(delim_str) >> newline

    content = block_image | list | text_line(false, unguarded: true) | empty_line.as(:line_break)
    content |= block(n_deep - 1) if n_deep.positive?

    (closing_pattern.absent? >> content).repeat(1)
  end

  element_attributes >>
    (line_start? >> attribute_list >> newline).maybe >>
    line_start? >>
    current_delimiter.as(:delimiter) >> newline >>
    if type == :pass
      (text_line(false, unguarded: true) | empty_line.as(:line_break)).repeat(1).as(:lines)
    else
      block_content_with_closing.as(:lines)
    end >>
    line_start? >>
    closing_delimiter >> newline
end

#block_titleObject



66
67
68
# File 'lib/coradoc/asciidoc/parser/block.rb', line 66

def block_title
  str('.') >> space.absent? >> text.as(:title) >> newline
end

#block_type(type) ⇒ Object



70
71
72
73
74
# File 'lib/coradoc/asciidoc/parser/block.rb', line 70

def block_type(type)
  (line_start? >> str('[') >> str('[').absent? >>
    str(type).as(:type) >>
    str(']')) >> newline # |
end

#element_attributesObject



98
99
100
101
102
103
104
105
106
# File 'lib/coradoc/asciidoc/parser/block.rb', line 98

def element_attributes
  block_title.maybe >>
    element_id.maybe >>
    (attribute_list >> newline).maybe >>
    block_title.maybe >>
    newline.maybe >>
    (attribute_list >> newline).maybe >>
    element_id.maybe
end

#example_block(n_deep) ⇒ Object



41
42
43
# File 'lib/coradoc/asciidoc/parser/block.rb', line 41

def example_block(n_deep)
  block_style(n_deep, '=', 4)
end

#open_block(n_deep) ⇒ Object

Open block: exactly 2 dashes (cannot nest within itself)



62
63
64
# File 'lib/coradoc/asciidoc/parser/block.rb', line 62

def open_block(n_deep)
  block_style_exact(n_deep, '-', 2)
end

#pass_block(n_deep) ⇒ Object



45
46
47
# File 'lib/coradoc/asciidoc/parser/block.rb', line 45

def pass_block(n_deep)
  block_style(n_deep, '+', 4, :pass)
end

#quote_block(n_deep) ⇒ Object



49
50
51
# File 'lib/coradoc/asciidoc/parser/block.rb', line 49

def quote_block(n_deep)
  block_style(n_deep, '_', 4)
end

#reviewer_note_block(_n_deep = 3) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/coradoc/asciidoc/parser/block.rb', line 33

def reviewer_note_block(_n_deep = 3)
  # Match blocks with reviewer attribute
  # This should only match when attribute_list contains reviewer=
  # For now, we'll make it not match anything specific
  # The block() method will handle these cases
  str('').absent? # Never matches - placeholder for future implementation
end


53
54
55
# File 'lib/coradoc/asciidoc/parser/block.rb', line 53

def sidebar_block(n_deep)
  block_style(n_deep, '*', 4)
end

#source_block(n_deep) ⇒ Object



57
58
59
# File 'lib/coradoc/asciidoc/parser/block.rb', line 57

def source_block(n_deep)
  block_style(n_deep, '-', 4, verbatim: true)
end