Module: Coradoc::AsciiDoc::Transformer::ListRules

Defined in:
lib/coradoc/asciidoc/transformer/list_rules.rb

Overview

Module containing list transformation rules

Class Method Summary collapse

Class Method Details

.apply(transformer_class) ⇒ Object



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
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
176
177
178
179
180
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 121

def self.apply(transformer_class)
  transformer_class.class_eval do
    # List item
    rule(list_item: subtree(:list_item)) do
      marker = list_item[:marker]
      id = list_item[:id]
      lines = list_item[:lines]
      content = if lines
                  Transformer.lines_to_text_elements(lines)
                else
                  list_item[:text].to_s
                end
      attached = list_item[:attached]
      nested = list_item[:nested]
      line_break = list_item[:line_break]

      # Convert nested array to proper List object if needed
      if nested.is_a?(Array) && nested.any?
        nested = if nested.all?(Model::List::Core)
                   nested.first
                 elsif nested.all?(Model::List::Item)
                   first_marker = nested.first.marker
                   if first_marker.to_s.lstrip.start_with?('.', '1', 'a', 'A', 'i', 'I')
                     Model::List::Ordered.new(items: nested)
                   else
                     Model::List::Unordered.new(items: nested)
                   end
                 else
                   nested
                 end
      end

      Model::List::Item.new(
        content: content, id:, marker:, attached:, nested:, line_break:
      )
    end

    # List passthrough
    rule(list: simple(:list)) do
      list
    end

    # Unordered list
    rule(unordered: sequence(:list_items)) do
      Model::List::Unordered.new(
        items: list_items
      )
    end

    rule(
      attribute_list: simple(:attribute_list),
      unordered: sequence(:list_items)
    ) do
      Model::List::Unordered.new(
        items: list_items,
        attrs: attribute_list
      )
    end

    # Ordered list
    rule(ordered: sequence(:list_items)) do
      Model::List::Ordered.new(
        items: list_items
      )
    end

    rule(
      attribute_list: simple(:attribute_list),
      ordered: sequence(:list_items)
    ) do
      Model::List::Ordered.new(
        items: list_items,
        attrs: attribute_list
      )
    end

    # Definition list term (with optional anchor)
    rule(dlist_term: subtree(:term_data), delimiter: simple(:delim)) do
      case term_data
      when Hash
        text = term_data[:text]
        text = text.to_s if text.is_a?(Parslet::Slice) || text.is_a?(String)
        text = text.content.to_s if text.is_a?(Model::TextElement)
        id = term_data[:id]
        id = id.to_s if id.is_a?(Parslet::Slice)
        { text: text.to_s, id: id, delimiter: delim.to_s }
      when Model::TextElement
        { text: term_data.content.to_s, id: term_data.id, delimiter: delim.to_s }
      else
        { text: term_data.to_s, id: nil, delimiter: delim.to_s }
      end
    end

    # Definition list item. The parser's dlist_definition rule
    # always emits :lines (one entry per source line of the dd,
    # including the single-line case as a one-element array).
    # Join via the shared lines_to_text_elements helper used by
    # ulist/olist — single source of truth for line joining.
    # `attached:` carries any `+`-continuation blocks captured by
    # the parser; pass through to the model so downstream stages
    # can render them as additional dd children.
    rule(
      definition_list_item: subtree(:item_data)
    ) do
      data = item_data.is_a?(Hash) ? item_data : { terms: Array(item_data), lines: [] }

      item_id = nil
      item_delim = '::'
      terms_data = data[:terms]
      # Split lines on hard_line_break so each source line is one
      # entry. Without this, text_any greedy-matches across
      # newlines via hard_line_break, joining what should be
      # separate source lines into one entry (and swallowing the
      # `+` line-continuation marker).
      split_lines = Transformer.split_lines_on_hard_break(data[:lines] || [])
      definition = Transformer.lines_to_text_elements(split_lines)
      attached = Array(data[:attached])

      terms = Array(terms_data).map do |t|
        case t
        when Hash
          item_id ||= t[:id].to_s if t[:id]
          item_delim = t[:delimiter].to_s if t[:delimiter]
          t[:text].to_s
        else
          t.to_s
        end
      end

      Model::List::DefinitionItem.new(terms: terms, contents: definition,
                                      id: item_id, delimiter: item_delim,
                                      attached: attached)
    end

    rule(definition_list: sequence(:list_items)) do
      ListRules.build_dlist_tree(list_items)
    end

    # Definition list with attribute_list (e.g., [%key])
    rule(
      attribute_list: simple(:attribute_list),
      definition_list: sequence(:list_items)
    ) do
      tree = ListRules.build_dlist_tree(list_items)
      tree.attrs = attribute_list if attribute_list
      tree
    end
  end
end

.attached_present?(item) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 86

def attached_present?(item)
  !item.attached.nil? && item.attached.any?
end

.build_dlist_tree(items) ⇒ Object

Build a nested DefinitionList tree from a flat list of items.

AsciiDoc's dlist syntax uses delimiter length to express nesting depth (:: = 1, ::: = 2, :::: = 3). Consecutive term-only items at the SAME depth are merged into a single multi-term <dt> sharing the next item's <dd>:

term1::            →   <dt>term1</dt>
term2::                <dt>term2</dt>
def                    <dd>def</dd>

Stack-based walk in source order. Each item carries its own delimiter; depth is derived via #dlist_depth.



22
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
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 22

def build_dlist_tree(items)
  root = Model::List::Definition.new(items: [])
  stack = [[root, 0]]

  items.each do |item|
    depth = dlist_depth(item.delimiter)
    stack.pop while stack.last[1] >= depth

    parent_list = stack.last[0]
    last = parent_list.items.last

    if merge_with_predecessor?(last, item)
      merge_term_only_item(last, item)
      # Stack stays — deeper items still nest under `last`.
      # Replace the stack top so deeper items land in `last`'s
      # current nested list (already on the stack from when
      # `last` was first added).
    else
      parent_list.items << item
      item.nested << Model::List::Definition.new(items: [])
      stack.push([item.nested.last, depth])
    end
  end

  prune_empty_nested(root)
  root
end

.contents_present?(item) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 82

def contents_present?(item)
  !item.contents.nil? && !item.contents.empty?
end

.dlist_depth(delimiter) ⇒ Object



104
105
106
107
108
109
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 104

def dlist_depth(delimiter)
  delim = delimiter.to_s
  return 1 if delim == ';;' || delim.empty?

  [delim.count(':') - 1, 1].max
end

.merge_term_only_item(last, current) ⇒ Object



97
98
99
100
101
102
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 97

def merge_term_only_item(last, current)
  last.terms.concat(current.terms)
  # contents and attached are arrays; concat is safe (no-op for empty)
  last.contents.concat(current.contents.to_a)
  last.attached.concat(current.attached.to_a)
end

.merge_with_predecessor?(last, current) ⇒ Boolean

Two consecutive items at the same depth become a multi-term <dt> sharing one <dd> when the PREVIOUS item is term-only (no def, no attached blocks). The previous item is the accumulating dt; the current item contributes either another term (if it's also term-only) or the terminal dt + the shared dd (if it has a def).

Once an item has its own def/attached, it's the terminal dt of any in-progress multi-term group; the next same-depth item starts a fresh entry.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 60

def merge_with_predecessor?(last, current)
  return false unless last
  return false unless last.delimiter == current.delimiter
  return false unless term_only?(last)

  true
end

.nested_has_items?(item) ⇒ Boolean

item.nested is an Array; each Definition has its own items Array. An item with populated nested items (deeper dlist children) is not term-only.

Returns:

  • (Boolean)


93
94
95
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 93

def nested_has_items?(item)
  Array(item.nested).any? { |list| list.is_a?(Model::List::Definition) && list.items.any? }
end

.prune_empty_nested(list) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 111

def prune_empty_nested(list)
  list.items.each do |item|
    item.nested.select! do |n|
      n.is_a?(Model::List::Definition) && n.items.any?
    end
    item.nested.each { |n| prune_empty_nested(n) }
  end
end

.term_only?(item) ⇒ Boolean

"Term-only" means the item carries no dd content of its own (no inline def, no +-attached blocks, no nested child items). Such items are eligible to merge with a following same-depth item to form a multi-term <dt> sharing one dd. Items with nested children have already "claimed" their dd slot for the nested content and can't merge.

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/coradoc/asciidoc/transformer/list_rules.rb', line 74

def term_only?(item)
  return false if contents_present?(item)
  return false if attached_present?(item)
  return false if nested_has_items?(item)

  true
end