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

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

Overview

Transforms CoreModel to AsciiDoc models

Class 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.



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

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



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

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.



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

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

.flatten_one(result) ⇒ Object



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

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

.register!Object



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

def register!
  return if @registered

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

.transform(model) ⇒ Object



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

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



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

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

.transform_annotation(annotation) ⇒ Object



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

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



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

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



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

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



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

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



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

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

.transform_definition_item(item, depth = 1) ⇒ Object



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

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



348
349
350
351
352
353
354
355
356
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 348

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



328
329
330
331
332
333
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 328

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



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

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

.transform_image(image) ⇒ Object



300
301
302
303
304
305
306
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 300

def transform_image(image)
  Coradoc::AsciiDoc::Model::Image::BlockImage.new(
    src: image.src,
    title: image.alt,
    attributes: build_image_attributes(image)
  )
end

.transform_inline(inline) ⇒ Object



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

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
    )
  else
    Coradoc::AsciiDoc::Model::TextElement.new(content: inline.content)
  end
end

.transform_list(list) ⇒ Object



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

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



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

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

.transform_structural_element(element) ⇒ Object



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

def transform_structural_element(element)
  case element
  when CoreModel::DocumentElement
    header = if element.title
               Coradoc::AsciiDoc::Model::Header.new(
                 title: Coradoc::AsciiDoc::Model::Title.new(
                   content: element.title,
                   level_int: 0
                 )
               )
             else
               Coradoc::AsciiDoc::Model::Header.new(title: '')
             end

    sections, frontmatter = extract_frontmatter(Array(element.children))

    Coradoc::AsciiDoc::Model::Document.new(
      id: element.id,
      header: header,
      sections: flatten_children(sections),
      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



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

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



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

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



373
374
375
376
377
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 373

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

.transform_toc_entry(entry) ⇒ Object



379
380
381
382
383
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 379

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

.verbatim_with_callouts?(semantic, block) ⇒ Boolean

Returns:

  • (Boolean)


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

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