Module: Oselvar::Var::Core::Structurer

Defined in:
lib/oselvar/var/core/structurer.rb

Overview

Group scanned blocks into Examples, tracking heading scope and orphan attachments. Port of structurer.ts.

Class Method Summary collapse

Class Method Details

.structure(path, source, blocks) ⇒ Object



14
15
16
17
18
19
20
21
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/oselvar/var/core/structurer.rb', line 14

def structure(path, source, blocks)
  examples = []
  orphan_attachments = []
  scope_stack = [] # [[level, text], ...]
  last_example_idx = -1
  attachment_open = false

  blocks.each do |block|
    case block.kind
    when 'heading'
      # Pop deeper-or-equal-level entries before pushing the new heading.
      scope_stack.pop while !scope_stack.empty? && scope_stack.last[0] >= block.level
      scope_stack << [block.level, block.text]
      attachment_open = false

    when 'paragraph', 'list_item', 'blockquote'
      # Merge a block into the previous example when that example's last
      # block is an attachment (table/fence) with no blank line between.
      if attachment_open && last_example_idx >= 0
        prev = examples[last_example_idx]
        prev_last = prev.body.last
        last_is_attachment = !prev_last.nil? && %w[table fence].include?(prev_last.kind)
        if last_is_attachment
          between = Offsets.utf16_slice(source, prev.span.end_offset, block.span.start_offset)
          unless between.match?(/\n\s*\n/)
            new_span = Offsets.span_from_offsets(source, prev.span.start_offset, block.span.end_offset)
            examples[last_example_idx] = Example.new(
              scope_stack: prev.scope_stack,
              span: new_span,
              body: prev.body + [block]
            )
            next
          end
        end
      end

      examples << Example.new(
        scope_stack: scope_stack.map { |(_, text)| text },
        span: block.span,
        body: [block]
      )
      last_example_idx = examples.length - 1
      attachment_open = true

    when 'table', 'fence'
      if attachment_open && last_example_idx >= 0
        prev = examples[last_example_idx]
        new_span = Offsets.span_from_offsets(source, prev.span.start_offset, block.span.end_offset)
        examples[last_example_idx] = Example.new(
          scope_stack: prev.scope_stack,
          span: new_span,
          body: prev.body + [block]
        )
      else
        orphan_attachments << block
      end

    when 'thematic_break'
      attachment_open = false
    end
  end

  VarDoc.new(
    path: path,
    source: source,
    examples: examples,
    orphan_attachments: orphan_attachments
  )
end