Class: Coradoc::AsciiDoc::Transform::FromCoreModel

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/asciidoc/transform/from_core_model.rb

Overview

Transforms CoreModel to AsciiDoc models

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_callout_paragraphs(callouts) ⇒ Object

Re-expands typed Callouts back into the AsciiDoc <N> text paragraph form so the round-trip is faithful.



192
193
194
195
196
197
198
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 192

def build_callout_paragraphs(callouts)
  callouts.sort_by { |c| c.index || Float::INFINITY }.map do |callout|
    Coradoc::AsciiDoc::Model::Paragraph.new(
      content: create_text_elements("<#{callout.index}> #{callout.content}")
    )
  end
end

.build_verbatim_block(semantic, block, content_text) ⇒ Object



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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 102

def build_verbatim_block(semantic, block, content_text)
  case semantic
  when :source_code
    Coradoc::AsciiDoc::Model::Block::SourceCode.new(
      id: block.id,
      title: block.title,
      lang: block.language,
      lines: content_text.split("\n"),
      attributes: build_attributes(block)
    )
  when :quote
    Coradoc::AsciiDoc::Model::Block::Quote.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :example
    Coradoc::AsciiDoc::Model::Block::Example.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :sidebar
    Coradoc::AsciiDoc::Model::Block::Side.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :literal
    Coradoc::AsciiDoc::Model::Block::Literal.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :open
    Coradoc::AsciiDoc::Model::Block::Open.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :pass
    Coradoc::AsciiDoc::Model::Block::Pass.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :listing
    Coradoc::AsciiDoc::Model::Block::Listing.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  when :verse
    Coradoc::AsciiDoc::Model::Block::Quote.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n"),
      delimiter: '[verse]'
    )
  when :reviewer
    Coradoc::AsciiDoc::Model::Block::ReviewerComment.new(
      id: block.id,
      title: block.title,
      lines: content_text.split("\n")
    )
  else
    delim = block.delimiter_type.to_s
    delim_char = delim[0]
    delim_len = delim.length

    Coradoc::AsciiDoc::Model::Block::Core.new(
      id: block.id,
      title: block.title,
      delimiter: delim,
      delimiter_char: delim_char,
      delimiter_len: delim_len,
      lines: content_text.split("\n")
    )
  end
end

.flatten_children(children) ⇒ Object

Transforms each CoreModel child and flattens one level so a transform that returns multiple siblings (e.g. a source block followed by its re-expanded callout paragraphs) stays in document order.



70
71
72
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 70

def flatten_children(children)
  Array(children).flat_map { |child| flatten_one(transform(child)) }
end

.flatten_one(result) ⇒ Object



74
75
76
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 74

def flatten_one(result)
  result.is_a?(Array) ? result : [result]
end

.image_attributes(image) ⇒ Object



312
313
314
315
316
317
318
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 312

def image_attributes(image)
  {
    src: image.src, alt: image.alt, title: image.title,
    caption: image.caption, width: image.width, height: image.height,
    link: image.link, role: image.role
  }
end

.register!Object



11
12
13
14
15
16
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 11

def register!
  return if @registered

  Transform::FromCoreModelRegistrations.register_all!
  @registered = true
end

.transform(model) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 18

def transform(model)
  register!
  return model.map { |item| transform(item) } if model.is_a?(Array)
  return model unless model.is_a?(Coradoc::CoreModel::Base)

  transformer = Registry.lookup(model.class)
  transformer ? transformer.call(model) : model
end

.transform_abbreviation(abbreviation) ⇒ Object



353
354
355
356
357
358
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 353

def transform_abbreviation(abbreviation)
  Coradoc::AsciiDoc::Model::TextElement.new(
    content: abbreviation.term.to_s +
             (abbreviation.definition ? " (#{abbreviation.definition})" : '')
  )
end

.transform_annotation(annotation) ⇒ Object



252
253
254
255
256
257
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 252

def transform_annotation(annotation)
  Coradoc::AsciiDoc::Model::Admonition.new(
    type: annotation.annotation_type.to_s.upcase,
    content: create_text_elements(annotation.renderable_content)
  )
end

.transform_bibliography(bib) ⇒ Object



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 320

def transform_bibliography(bib)
  entries = Array(bib.entries).map do |entry|
    transform_bibliography_entry(entry)
  end

  Coradoc::AsciiDoc::Model::Bibliography.new(
    id: bib.id,
    title: bib.title,
    entries: entries
  )
end

.transform_bibliography_entry(entry) ⇒ Object



332
333
334
335
336
337
338
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 332

def transform_bibliography_entry(entry)
  Coradoc::AsciiDoc::Model::BibliographyEntry.new(
    anchor_name: entry.anchor_name,
    document_id: entry.document_id,
    ref_text: entry.ref_text
  )
end

.transform_block(block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 78

def transform_block(block)
  content = block.renderable_content

  semantic = resolve_semantic_type(block)

  case semantic
  when :paragraph
    return Coradoc::AsciiDoc::Model::Paragraph.new(
      id: block.id,
      content: create_text_elements(content)
    )
  when :comment
    return Coradoc::AsciiDoc::Model::CommentBlock.new(
      text: safe_content_to_string(content)
    )
  end

  content_text = safe_content_to_string(content)
  result = build_verbatim_block(semantic, block, content_text)
  return result unless verbatim_with_callouts?(semantic, block)

  [result, *build_callout_paragraphs(block.callouts)]
end

.transform_comment_line(comment) ⇒ Object



397
398
399
400
401
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 397

def transform_comment_line(comment)
  Coradoc::AsciiDoc::Model::CommentLine.new(
    text: comment.text.to_s
  )
end

.transform_definition_item(item, depth = 1) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 370

def transform_definition_item(item, depth = 1)
  delimiter = ':' * (depth + 1)
  term = Coradoc::AsciiDoc::Model::Term.new(term: item.term.to_s)
  contents = Array(item.definitions).map do |defn|
    Coradoc::AsciiDoc::Model::TextElement.new(content: defn.to_s)
  end
  di = Coradoc::AsciiDoc::Model::List::DefinitionItem.new(
    terms: [term],
    contents: contents,
    delimiter: delimiter
  )
  di.nested << transform_definition_list(item.nested, depth + 1) if item.nested&.items&.any?
  di
end

.transform_definition_list(definition_list, depth = 1) ⇒ Object



360
361
362
363
364
365
366
367
368
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 360

def transform_definition_list(definition_list, depth = 1)
  delimiter = ':' * (depth + 1)
  items = Array(definition_list.items).map do |item|
    transform_definition_item(item, depth)
  end
  list = Coradoc::AsciiDoc::Model::List::Definition.new(items: items)
  list.delimiter = delimiter
  list
end

.transform_footnote(footnote) ⇒ Object



340
341
342
343
344
345
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 340

def transform_footnote(footnote)
  Coradoc::AsciiDoc::Model::Inline::Footnote.new(
    id: footnote.id,
    text: footnote.content.to_s
  )
end

.transform_footnote_reference(footnote_ref) ⇒ Object



347
348
349
350
351
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 347

def transform_footnote_reference(footnote_ref)
  Coradoc::AsciiDoc::Model::Inline::Footnote.new(
    id: footnote_ref.id
  )
end

.transform_image(image) ⇒ Object



303
304
305
306
307
308
309
310
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 303

def transform_image(image)
  target_class = if image.inline
                   Coradoc::AsciiDoc::Model::Image::InlineImage
                 else
                   Coradoc::AsciiDoc::Model::Image::BlockImage
                 end
  target_class.new(**image_attributes(image))
end

.transform_include(include) ⇒ Object



403
404
405
406
407
408
409
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 403

def transform_include(include)
  Coradoc::AsciiDoc::Model::Include.new(
    path: include.target.to_s,
    attributes: build_include_attributes(include),
    line_break: include.line_break.to_s
  )
end

.transform_inline(inline) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 259

def transform_inline(inline)
  case inline.resolve_format_type
  when 'bold'
    Coradoc::AsciiDoc::Model::Inline::Bold.new(content: inline.content)
  when 'italic'
    Coradoc::AsciiDoc::Model::Inline::Italic.new(content: inline.content)
  when 'monospace'
    Coradoc::AsciiDoc::Model::Inline::Monospace.new(content: inline.content)
  when 'highlight'
    Coradoc::AsciiDoc::Model::Inline::Highlight.new(content: inline.content)
  when 'strikethrough'
    Coradoc::AsciiDoc::Model::Inline::Strikethrough.new(content: inline.content)
  when 'subscript'
    Coradoc::AsciiDoc::Model::Inline::Subscript.new(content: inline.content)
  when 'superscript'
    Coradoc::AsciiDoc::Model::Inline::Superscript.new(content: inline.content)
  when 'underline'
    Coradoc::AsciiDoc::Model::Inline::Underline.new(text: inline.content)
  when 'link'
    Coradoc::AsciiDoc::Model::Inline::Link.new(
      path: inline.target,
      name: inline.content
    )
  when 'xref'
    Coradoc::AsciiDoc::Model::Inline::CrossReference.new(href: inline.target)
  when 'footnote'
    Coradoc::AsciiDoc::Model::Inline::Footnote.new(
      id: inline.target,
      text: inline.content
    )
  when 'stem'
    Coradoc::AsciiDoc::Model::Inline::Stem.new(
      type: inline.stem_type || 'latexmath',
      content: inline.content
    )
  when 'raw_inline'
    Coradoc::AsciiDoc::Model::Inline::Passthrough.new(content: inline.content)
  when 'hard_line_break'
    Coradoc::AsciiDoc::Model::Inline::HardLineBreak.new
  else
    Coradoc::AsciiDoc::Model::TextElement.new(content: inline.content)
  end
end

.transform_list(list) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 219

def transform_list(list)
  items = Array(list.items).map do |item|
    Coradoc::AsciiDoc::Model::List::Item.new(
      content: item.flat_text,
      marker: item.marker || default_marker(list.marker_type)
    )
  end

  case list.marker_type
  when 'ordered'
    Coradoc::AsciiDoc::Model::List::Ordered.new(items: items)
  when 'definition'
    Coradoc::AsciiDoc::Model::List::Definition.new(items: items)
  else
    Coradoc::AsciiDoc::Model::List::Unordered.new(items: items)
  end
end

.transform_list_item(item) ⇒ Object



237
238
239
240
241
242
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 237

def transform_list_item(item)
  Coradoc::AsciiDoc::Model::List::Item.new(
    content: item.flat_text,
    marker: item.marker
  )
end

.transform_structural_element(element) ⇒ Object



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
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 27

def transform_structural_element(element)
  case element
  when CoreModel::DocumentElement
    without_frontmatter, frontmatter = extract_frontmatter(Array(element.children))
    without_title_heading, title_text = extract_title_heading(without_frontmatter, element.title)

    header = if title_text
                Coradoc::AsciiDoc::Model::Header.new(
                  title: Coradoc::AsciiDoc::Model::Title.new(
                    content: title_text,
                    level_int: 0
                  )
                )
              else
                Coradoc::AsciiDoc::Model::Header.new(title: '')
              end

    Coradoc::AsciiDoc::Model::Document.new(
      id: element.id,
      header: header,
      sections: flatten_children(without_title_heading),
      frontmatter: frontmatter
    )
  when CoreModel::SectionElement
    Coradoc::AsciiDoc::Model::Section.new(
      id: element.id,
      level: element.level,
      title: create_title(element.title, element.level),
      contents: flatten_children(element.children)
    )
  else
    Coradoc::AsciiDoc::Model::Section.new(
      id: element.id,
      title: create_title(element.title, 1),
      contents: flatten_children(element.children)
    )
  end
end

.transform_table(table) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 200

def transform_table(table)
  rows = Array(table.rows).map do |row|
    columns = Array(row.cells).map do |cell|
      Coradoc::AsciiDoc::Model::TableCell.new(
        content: cell.flat_text
      )
    end
    Coradoc::AsciiDoc::Model::TableRow.new(
      columns: columns
    )
  end

  Coradoc::AsciiDoc::Model::Table.new(
    id: table.id,
    title: table.title,
    rows: rows
  )
end

.transform_term(term) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 244

def transform_term(term)
  Coradoc::AsciiDoc::Model::Term.new(
    term: term.text,
    type: term.type&.to_s || 'preferred',
    lang: term.lang || 'en'
  )
end

.transform_toc(_toc) ⇒ Object



385
386
387
388
389
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 385

def transform_toc(_toc)
  Coradoc::AsciiDoc::Model::TextElement.new(
    content: 'toc::[]'
  )
end

.transform_toc_entry(entry) ⇒ Object



391
392
393
394
395
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 391

def transform_toc_entry(entry)
  Coradoc::AsciiDoc::Model::TextElement.new(
    content: entry.title.to_s
  )
end

.verbatim_with_callouts?(semantic, block) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 183

def verbatim_with_callouts?(semantic, block)
  return false unless %i[source_code listing].include?(semantic)
  return false if block.callouts.nil? || block.callouts.empty?

  true
end

Instance Method Details

#transform(model) ⇒ Object



573
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 573

def transform(model) = self.class.transform(model)