Module: Coradoc::AsciiDoc::Transformer::InlineRules

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

Overview

Module containing inline element transformation rules

Constant Summary collapse

FORMATTING_VARIANTS =

Inline formatting variants that share the same rule shape: constrained and unconstrained forms of the same model class. ‘span` is excluded because it carries `text:` + `attributes:` rather than `content:`, so it gets its own pair of rules.

[
  %i[bold      Bold],
  %i[italic    Italic],
  %i[highlight Highlight],
  %i[monospace Monospace]
].freeze

Class Method Summary collapse

Class Method Details

.apply(transformer_class) ⇒ Object



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
98
99
100
101
102
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/coradoc/asciidoc/transformer/inline_rules.rb', line 19

def self.apply(transformer_class)
  transformer_class.class_eval do
    # Link
    rule(link: subtree(:link)) do
      Model::Inline::Link.new(
        path: link[:path].to_s,
        name: link[:text]&.to_s
      )
    end

    # Cross reference
    rule(href: simple(:href)) do
      Model::Inline::CrossReference.new(href: href.to_s)
    end

    rule(
      href: simple(:href),
      name: simple(:name)
    ) do
      Model::Inline::CrossReference.new(href: href.to_s, args: [name.to_s])
    end

    rule(cross_reference: sequence(:xref)) do
      args = xref.size > 1 ? xref[1..] : []
      Model::Inline::CrossReference.new(href: xref[0], args:)
    end

    # Inline image
    rule(inline_image: subtree(:inline_image)) do
      Model::Image::InlineImage.new(
        title: inline_image[:text],
        src: inline_image[:path],
        attributes: inline_image[:attribute_list]
      )
    end

    # Attribute reference
    rule(attribute_reference: simple(:name)) do
      Model::Inline::AttributeReference.new(name:)
    end

    # Term
    rule(
      term_type: simple(:term_type),
      term: simple(:term)
    ) do
      Coradoc::AsciiDoc::Model::Term.new(term:, type: term_type, lang: :en)
    end

    # Footnote
    rule(footnote: simple(:footnote)) do
      text_str = footnote.to_s
      Coradoc::AsciiDoc::Model::Inline::Footnote.new(text: text_str)
    end

    rule(footnote: simple(:footnote), id: simple(:id)) do
      text_str = footnote.to_s
      Coradoc::AsciiDoc::Model::Inline::Footnote.new(text: text_str, id: id.to_s)
    end

    # Footnote with empty content (reference to named footnote)
    rule(footnote: sequence(:footnote), id: simple(:id)) do
      text_str = footnote.map(&:to_s).join
      Coradoc::AsciiDoc::Model::Inline::Footnote.new(text: text_str, id: id.to_s)
    end

    # Footnote with empty content and no id
    rule(footnote: sequence(:footnote)) do
      text_str = footnote.map(&:to_s).join
      Coradoc::AsciiDoc::Model::Inline::Footnote.new(text: text_str)
    end

    # Href arg
    rule(href_arg: simple(:href_arg)) do
      href_arg.to_s
    end

    # Inline formatting rules generated from a single registry.
    # See InlineRules::FORMATTING_VARIANTS. `span` is special
    # because it carries `text:` + `attributes:` rather than
    # `content:`, so it stays inline below.
    InlineRules::FORMATTING_VARIANTS.each do |prefix, class_name|
      klass = Model::Inline.const_get(class_name)
      constrained_key = :"#{prefix}_constrained"
      unconstrained_key = :"#{prefix}_unconstrained"

      rule(constrained_key => subtree(:subtree)) do
        content = Transformer.extract_inline_content(subtree)
        klass.new(content: content, unconstrained: false)
      end
      rule(unconstrained_key => subtree(:subtree)) do
        content = Transformer.extract_inline_content(subtree)
        klass.new(content: content, unconstrained: true)
      end
    end

    # Span (constrained)
    rule(span_constrained: subtree(:span_constrained)) do
      Model::Inline::Span.new(
        text: span_constrained[:text],
        unconstrained: false,
        attributes: span_constrained[:attribute_list]
      )
    end

    # Span (unconstrained)
    rule(span_unconstrained: subtree(:span_unconstrained)) do
      Model::Inline::Span.new(
        text: span_unconstrained[:text],
        unconstrained: true,
        attributes: span_unconstrained[:attribute_list]
      )
    end

    # Superscript
    rule(superscript: subtree(:superscript)) do
      content = Transformer.extract_simple_inline_content(superscript)
      Model::Inline::Superscript.new(content:)
    end

    # Subscript
    rule(subscript: subtree(:subscript)) do
      content = Transformer.extract_simple_inline_content(subscript)
      Model::Inline::Subscript.new(content:)
    end

    # Highlight (simple)
    rule(highlight: simple(:text)) do
      Model::Highlight.new(content: text)
    end
    # Stem
    rule(stem: subtree(:stem)) do
      Coradoc::AsciiDoc::Model::Inline::Stem.new(
        type: stem[:stem_type],
        content: stem[:content]
      )
    end
  end
end