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

.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



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

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

.transform_annotation(annotation) ⇒ Object



218
219
220
221
222
223
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 218

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



273
274
275
276
277
278
279
280
281
282
283
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 273

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



285
286
287
288
289
290
291
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 285

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



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
158
159
160
161
162
163
164
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 67

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)

  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

.transform_comment_line(comment) ⇒ Object



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

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

.transform_definition_item(item, depth = 1) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 323

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



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

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



293
294
295
296
297
298
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 293

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



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

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

.transform_image(image) ⇒ Object



265
266
267
268
269
270
271
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 265

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 225

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



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 185

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



203
204
205
206
207
208
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 203

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: transform(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: transform(element.children)
    )
  else
    Coradoc::AsciiDoc::Model::Section.new(
      id: element.id,
      title: create_title(element.title, 1),
      contents: transform(element.children)
    )
  end
end

.transform_table(table) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 166

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



210
211
212
213
214
215
216
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 210

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



338
339
340
341
342
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 338

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

.transform_toc_entry(entry) ⇒ Object



344
345
346
347
348
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 344

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