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

.transform(model) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 15

def transform(model)
  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)
  return transformer.call(model) if transformer

  transform_with_case(model)
end

.transform_abbreviation(abbreviation) ⇒ Object



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

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



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

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



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

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



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 103

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,
      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")
    )
  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.chars.first
    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_definition_item(item) ⇒ Object



354
355
356
357
358
359
360
361
362
363
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 354

def transform_definition_item(item)
  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
  Coradoc::AsciiDoc::Model::List::DefinitionItem.new(
    terms: [term],
    contents: contents
  )
end

.transform_definition_list(definition_list) ⇒ Object



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

def transform_definition_list(definition_list)
  items = Array(definition_list.items).map do |item|
    transform_definition_item(item)
  end
  Coradoc::AsciiDoc::Model::List::Definition.new(items: items)
end

.transform_footnote(footnote) ⇒ Object



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

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



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

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

.transform_image(image) ⇒ Object



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

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



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

def transform_inline(inline)
  case inline.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



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



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

def transform_structural_element(element)
  case element.element_type
  when 'document'
    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

    Coradoc::AsciiDoc::Model::Document.new(
      id: element.id,
      header: header,
      sections: transform(element.children)
    )
  when 'section'
    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



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



365
366
367
368
369
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 365

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

.transform_toc_entry(entry) ⇒ Object



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

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

.transform_with_case(model) ⇒ Object



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

def transform_with_case(model)
  case model
  when Coradoc::CoreModel::StructuralElement
    transform_structural_element(model)
  when Coradoc::CoreModel::AnnotationBlock
    transform_annotation(model)
  when Coradoc::CoreModel::Block
    transform_block(model)
  when Coradoc::CoreModel::Table
    transform_table(model)
  when Coradoc::CoreModel::ListBlock
    transform_list(model)
  when Coradoc::CoreModel::ListItem
    transform_list_item(model)
  when Coradoc::CoreModel::Term
    transform_term(model)
  when Coradoc::CoreModel::InlineElement
    transform_inline(model)
  when Coradoc::CoreModel::Image
    transform_image(model)
  when Coradoc::CoreModel::Footnote
    transform_footnote(model)
  when Coradoc::CoreModel::FootnoteReference
    transform_footnote_reference(model)
  when Coradoc::CoreModel::Abbreviation
    transform_abbreviation(model)
  when Coradoc::CoreModel::DefinitionList
    transform_definition_list(model)
  when Coradoc::CoreModel::DefinitionItem
    transform_definition_item(model)
  when Coradoc::CoreModel::Toc
    transform_toc(model)
  when Coradoc::CoreModel::TocEntry
    transform_toc_entry(model)
  when Coradoc::CoreModel::Bibliography
    transform_bibliography(model)
  when Coradoc::CoreModel::BibliographyEntry
    transform_bibliography_entry(model)
  else
    model
  end
end

Instance Method Details

#transform(model) ⇒ Object



10
11
12
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 10

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