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
# 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) |
  literal_block(n_deep) |
  open_block(n_deep)).as(:block)
end

#block_content(n_deep = 3) ⇒ Object



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

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 for open block) Used by paragraph.rb to reject lines that look like block delimiters. NOTE: repeat(4,) means 4 or more (not exactly 4)



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

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
    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)



103
104
105
106
107
108
109
110
111
112
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
# File 'lib/coradoc/asciidoc/parser/block.rb', line 103

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 and cannot nest within themselves



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

def block_style_exact(n_deep = 3, delimiter = '-', exact_chars = 2)
  capture_key = :"delimit_#{delimiter}_exact_#{exact_chars}_#{n_deep}"
  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

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

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

    (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_titleObject



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

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

#block_type(type) ⇒ Object



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

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

#example_block(n_deep) ⇒ Object



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

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

#literal_block(n_deep) ⇒ Object



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

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

#open_block(n_deep) ⇒ Object

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



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

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

#pass_block(n_deep) ⇒ Object



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

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

#quote_block(n_deep) ⇒ Object



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

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


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

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

#source_block(n_deep) ⇒ Object



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

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