Module: Coradoc::AsciiDoc::Transformer::BlockRules

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

Overview

Module containing block element transformation rules

Class Method Summary collapse

Class Method Details

.apply(transformer_class) ⇒ Object



8
9
10
11
12
13
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/coradoc/asciidoc/transformer/block_rules.rb', line 8

def self.apply(transformer_class)
  transformer_class.class_eval do
    # Generic block
    rule(block: subtree(:block)) do
      id = block[:id]
      title = block[:title]
      attribute_list = block[:attribute_list]
      delimiter = block[:delimiter].to_s
      delimiter_c = delimiter[0]
      lines = block[:lines]
      ordering = block.keys.select do |k|
        %i[id title attribute_list attribute_list2].include?(k)
      end

      opts = {
        id: id,
        title: title,
        delimiter_len: delimiter.size,
        lines: lines,
        ordering: ordering
      }
      opts[:attributes] = attribute_list if attribute_list
      delimiter_len = opts[:delimiter_len]

      if delimiter_c == '*'
        if attribute_list
          if attribute_list.positional == [] &&
             attribute_list.named.first&.name == 'reviewer'
            Model::Block::ReviewerComment.new(
              id:,
              title:,
              lines:,
              delimiter_len:,
              attributes: attribute_list
            )
          else
            Model::Block::Side.new(id:, title:, lines:, delimiter_len:,
                                   attributes: attribute_list)
          end
        else
          Model::Block::Side.new(id:, title:, lines:, delimiter_len:,
                                 attributes: attribute_list)
        end
      elsif delimiter_c == '='
        Model::Block::Example.new(id:, title:, lines:, delimiter_len:,
                                  attributes: attribute_list)
      elsif delimiter_c == '+'
        Model::Block::Pass.new(id:, title:, lines:, delimiter_len:,
                               attributes: attribute_list)
      elsif delimiter_c == '-' && delimiter.size == 2
        Model::Block::Open.new(id:, title:, lines:, delimiter_len:,
                               attributes: attribute_list)
      elsif delimiter_c == '-' && delimiter.size >= 4
        Model::Block::SourceCode.new(id:, title:, lines:, delimiter_len:,
                                     attributes: attribute_list)
      elsif delimiter_c == '_'
        Model::Block::Quote.new(id:, title:, lines:, delimiter_len:,
                                attributes: attribute_list)
      end
    end

    # Example
    rule(example: sequence(:example)) do
      Model::Block::Example.new(title: '', lines: example)
    end

    # Admonition
    rule(
      admonition_type: simple(:admonition_type),
      content: sequence(:content)
    ) do
      Model::Admonition.new(content: content, type: admonition_type.to_s)
    end

    # Block image
    rule(block_image: subtree(:block_image)) do
      id = block_image[:id]
      title = block_image[:title]
      path = block_image[:path]
      attrs = block_image[:attribute_list]
      Model::Image::BlockImage.new(
        title: title,
        id: id,
        src: path,
        attributes: attrs,
        line_break: "\n"
      )
    end
  end
end