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
32
33
# File 'lib/coradoc/asciidoc/parser/block.rb', line 24

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

#block_content(n_deep = 3) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/coradoc/asciidoc/parser/block.rb', line 107

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

#block_delimiterObject

Block delimiter: 4+ identical characters, or 2 dashes for open blocks, or 3+ backticks for Markdown-style code fences. Used by paragraph.rb to reject lines that look like block delimiters. NOTE: repeat(4,) means 4 or more (not exactly 4)



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/coradoc/asciidoc/parser/block.rb', line 120

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

#block_style(n_deep = 3, delimiter = '*', repeater = 4, 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)

  • verbatim (Boolean) (defaults to: false)

    Treat body as raw text (no substitutions, no nested blocks)



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/coradoc/asciidoc/parser/block.rb', line 138

def block_style(n_deep = 3, delimiter = '*', repeater = 4, verbatim: false)
  capture_key = :"delimit_#{delimiter}_#{n_deep}"
  current_delimiter = str(delimiter).repeat(repeater).capture(capture_key)
  closing_delimiter = dynamic do |_s, c|
    str(c.captures[capture_key].to_s.strip)
  end

  block_content_with_closing = dynamic do |_s, c|
    delim_str = c.captures[capture_key].to_s.strip
    closing_pattern = str(delim_str) >> newline

    # Verbatim blocks (source/listing/literal/pass) treat their body
    # as literal text per the AsciiDoc spec — no substitutions, no
    # nested blocks. Allowing nested block parsing here would consume
    # shorter inner delimiters (e.g. `----` inside `------`) and
    # strip the original structure when serializing back.
    content = if verbatim
                text_line(false, unguarded: true, verbatim: true) |
                  empty_line.as(:line_break)
              else
                c = block_image
                c |= block(n_deep - 1) if n_deep.positive?
                c |= list
                c |= text_line(false, unguarded: true)
                c |= empty_line.as(:line_break)
                c
              end

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

  block_header >>
    line_start? >>
    current_delimiter.as(:delimiter) >> newline >>
    block_content_with_closing.as(:lines) >>
    line_start? >>
    closing_delimiter >> newline
end

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

Block style parser with EXACT delimiter length (for open blocks). Open blocks use exactly 2 dashes. A ‘[source]`/``/`[literal]` positional attribute casts the body to verbatim — block macros like `image::` must survive byte-for-byte, same as delimited source blocks.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/coradoc/asciidoc/parser/block.rb', line 181

def block_style_exact(n_deep = 3, delimiter = '-', exact_chars = 2)
  capture_key = :"delimit_#{delimiter}_exact_#{exact_chars}_#{n_deep}"
  attr_capture_key = :"#{capture_key}_attrs"
  current_delimiter = str(delimiter).repeat(exact_chars, exact_chars).capture(capture_key)
  closing_delimiter = dynamic do |_s, c|
    str(c.captures[capture_key].to_s.strip)
  end

  # Closure so the call bypasses Parslet's method_missing inside the
  # dynamic block. capture() stashes the parsed AST (a nested Hash for
  # a `[source,ruby]` header). Inspect the structure directly so we
  # don't depend on Ruby's Hash#to_s format (it changed in 3.4).
  verbatim_cast = lambda do |raw|
    values = []
    queue = [raw]
    while (node = queue.shift)
      case node
      when Hash then queue.concat(node.values)
      when Array then queue.concat(node)
      else values << node
      end
    end
    castable = %w[source listing literal]
    values.any? { |v| castable.include?(v.to_s) }
  end

  block_content_with_closing = dynamic do |_s, c|
    delim_str = c.captures[capture_key].to_s.strip
    raw_header = c.captures[attr_capture_key]
    closing_pattern = str(delim_str) >> newline

    content = if verbatim_cast.call(raw_header)
                text_line(false, unguarded: true, verbatim: true) |
                  empty_line.as(:line_break)
              else
                alt = block_image
                alt |= block(n_deep - 1) if n_deep.positive?
                alt |= list
                alt |= text_line(false, unguarded: true)
                alt |= empty_line.as(:line_break)
                alt
              end

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

  block_header.capture(attr_capture_key) >>
    line_start? >>
    current_delimiter.as(:delimiter) >> newline >>
    block_content_with_closing.as(:lines) >>
    line_start? >>
    closing_delimiter >> newline
end

#block_titleObject



96
97
98
99
# File 'lib/coradoc/asciidoc/parser/block.rb', line 96

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

#block_type(type) ⇒ Object



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

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

#example_block(n_deep) ⇒ Object



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

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

#literal_block(n_deep) ⇒ Object



55
56
57
# File 'lib/coradoc/asciidoc/parser/block.rb', line 55

def literal_block(n_deep)
  block_style(n_deep, '.', 4, verbatim: true)
end

#markdown_code_block(n_deep = 3) ⇒ Object

Markdown-style fenced code block: triple-backtick (or longer) fence with an optional language tag on the opening line. Behaves as a verbatim source block — same model as ‘[source,lang]n—-`. Pragmatic permissiveness for content that originates from (or is edited alongside) Markdown; not standard AsciiDoc but widely accepted (GitHub’s renderer treats “‘ as a listing delimiter).



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/coradoc/asciidoc/parser/block.rb', line 70

def markdown_code_block(n_deep = 3)
  capture_key = :"md_fence_#{n_deep}"
  opening_fence = str('`').repeat(3).capture(capture_key)
  closing_fence = dynamic do |_s, c|
    str(c.captures[capture_key].to_s.strip)
  end
  language = (space? >> match("[A-Za-z0-9_+.-]").repeat(1).as(:language)).maybe

  block_content_with_closing = dynamic do |_s, c|
    fence_str = c.captures[capture_key].to_s.strip
    closing_pattern = closing_fence >> space? >> newline

    content = text_line(false, unguarded: true, verbatim: true) |
                empty_line.as(:line_break)

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

  block_header >>
    line_start? >>
    opening_fence.as(:delimiter) >> language >> newline >>
    block_content_with_closing.as(:lines) >>
    line_start? >>
    closing_fence >> space? >> newline
end

#open_block(n_deep) ⇒ Object

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



60
61
62
# File 'lib/coradoc/asciidoc/parser/block.rb', line 60

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

#pass_block(n_deep) ⇒ Object



39
40
41
# File 'lib/coradoc/asciidoc/parser/block.rb', line 39

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

#quote_block(n_deep) ⇒ Object



43
44
45
# File 'lib/coradoc/asciidoc/parser/block.rb', line 43

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


47
48
49
# File 'lib/coradoc/asciidoc/parser/block.rb', line 47

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

#source_block(n_deep) ⇒ Object



51
52
53
# File 'lib/coradoc/asciidoc/parser/block.rb', line 51

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