Class: Coradoc::AsciiDoc::Transform::ToCoreModel
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Transform::ToCoreModel
- Defined in:
- lib/coradoc/asciidoc/transform/to_core_model.rb
Overview
Transforms AsciiDoc models to CoreModel equivalents
Class Method Summary collapse
- .extract_block_lines(block) ⇒ Object
- .transform(model) ⇒ Object
- .transform_admonition(admonition) ⇒ Object
- .transform_bibliography(bib) ⇒ Object
- .transform_bibliography_entry(entry) ⇒ Object
- .transform_block(block, semantic_type_or_delimiter) ⇒ Object
- .transform_cross_reference(xref) ⇒ Object
- .transform_document(doc) ⇒ Object
- .transform_image(image) ⇒ Object
- .transform_inline(inline, format_type) ⇒ Object
- .transform_inline_footnote(footnote) ⇒ Object
- .transform_inline_text(inline, format_type) ⇒ Object
- .transform_link(link) ⇒ Object
- .transform_list(list, marker_type) ⇒ Object
- .transform_paragraph(para) ⇒ Object
- .transform_section(section) ⇒ Object
- .transform_source_block(block) ⇒ Object
- .transform_stem(stem) ⇒ Object
- .transform_table(table) ⇒ Object
- .transform_table_cell(cell) ⇒ Object
- .transform_table_row(row) ⇒ Object
- .transform_term(term) ⇒ Object
- .transform_typed_block(block, klass, extra_attrs = {}) ⇒ Object
- .transform_with_case(model) ⇒ Object
Instance Method Summary collapse
Class Method Details
.extract_block_lines(block) ⇒ Object
188 189 190 191 192 193 194 195 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 188 def extract_block_lines(block) Array(block.lines).reject do |line| line.is_a?(Coradoc::AsciiDoc::Model::LineBreak) || line.is_a?(Coradoc::AsciiDoc::Model::Break::PageBreak) end.map do |line| extract_text_content(line) end.join("\n") end |
.transform(model) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 15 def transform(model) return model.map { |item| transform(item) }.compact if model.is_a?(Array) return model unless model.is_a?(Coradoc::AsciiDoc::Model::Base) transformer = Registry.lookup(model.class) return transformer.call(model) if transformer transform_with_case(model) end |
.transform_admonition(admonition) ⇒ Object
274 275 276 277 278 279 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 274 def transform_admonition(admonition) Coradoc::CoreModel::AnnotationBlock.new( annotation_type: admonition.type, content: extract_text_content(admonition.content) ) end |
.transform_bibliography(bib) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 337 def transform_bibliography(bib) entries = Array(bib.entries).map do |entry| transform_bibliography_entry(entry) end Coradoc::CoreModel::Bibliography.new( id: bib.id, title: bib.title.to_s, level: nil, entries: entries ) end |
.transform_bibliography_entry(entry) ⇒ Object
350 351 352 353 354 355 356 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 350 def transform_bibliography_entry(entry) Coradoc::CoreModel::BibliographyEntry.new( anchor_name: entry.anchor_name, document_id: entry.document_id, ref_text: entry.ref_text.to_s ) end |
.transform_block(block, semantic_type_or_delimiter) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 156 def transform_block(block, semantic_type_or_delimiter) content_lines = extract_block_lines(block) semantic_type = if semantic_type_or_delimiter.is_a?(Symbol) semantic_type_or_delimiter else asciidoc_delimiter_to_semantic(semantic_type_or_delimiter) end Coradoc::CoreModel::Block.new( element_type: 'block', block_semantic_type: semantic_type, delimiter_type: semantic_type_or_delimiter.is_a?(String) ? semantic_type_or_delimiter : nil, id: block.id, title: extract_title_text(block.title), content: content_lines, language: extract_block_language(block) ) end |
.transform_cross_reference(xref) ⇒ Object
312 313 314 315 316 317 318 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 312 def transform_cross_reference(xref) Coradoc::CoreModel::InlineElement.new( format_type: 'xref', target: xref.href, content: xref.args&.first || xref.href ) end |
.transform_document(doc) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 99 def transform_document(doc) title_text = extract_title_text(doc.header&.title) attributes = extract_document_attributes(doc) Coradoc::CoreModel::StructuralElement.new( element_type: 'document', id: doc.id, title: title_text, attributes: attributes, children: transform(doc.sections || doc.contents || []) ) end |
.transform_image(image) ⇒ Object
328 329 330 331 332 333 334 335 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 328 def transform_image(image) Coradoc::CoreModel::Image.new( src: image.src, alt: image.title&.to_s, width: image.attributes&.[]('width'), height: image.attributes&.[]('height') ) end |
.transform_inline(inline, format_type) ⇒ Object
281 282 283 284 285 286 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 281 def transform_inline(inline, format_type) Coradoc::CoreModel::InlineElement.new( format_type: format_type, content: extract_text_content(inline.content) ) end |
.transform_inline_footnote(footnote) ⇒ Object
295 296 297 298 299 300 301 302 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 295 def transform_inline_footnote(footnote) parsed_content = parse_and_transform_inline(footnote.text.to_s) Coradoc::CoreModel::InlineElement.new( format_type: 'footnote', target: footnote.id, content: parsed_content ) end |
.transform_inline_text(inline, format_type) ⇒ Object
288 289 290 291 292 293 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 288 def transform_inline_text(inline, format_type) Coradoc::CoreModel::InlineElement.new( format_type: format_type, content: inline.text.to_s ) end |
.transform_link(link) ⇒ Object
304 305 306 307 308 309 310 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 304 def transform_link(link) Coradoc::CoreModel::InlineElement.new( format_type: 'link', target: link.path, content: link.name || link.path ) end |
.transform_list(list, marker_type) ⇒ Object
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 264 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 233 def transform_list(list, marker_type) items = Array(list.items).map do |item| if item.is_a?(Coradoc::AsciiDoc::Model::List::DefinitionItem) term_content = item.terms def_content = item.contents Coradoc::CoreModel::DefinitionItem.new( term: extract_text_content(term_content), definitions: [extract_text_content(def_content)] ) else content_val = item.content children = transform_inline_content(content_val) li = Coradoc::CoreModel::ListItem.new( content: extract_text_content(content_val), marker: item.marker ) li.children = children li end end if marker_type == 'definition' Coradoc::CoreModel::DefinitionList.new(items: items) else Coradoc::CoreModel::ListBlock.new( marker_type: marker_type, items: items ) end end |
.transform_paragraph(para) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 125 def transform_paragraph(para) children = transform_inline_content(para.content) Coradoc::CoreModel::Block.new( element_type: 'paragraph', block_semantic_type: :paragraph, id: para.id, content: extract_text_content(para.content), children: children ) end |
.transform_section(section) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 111 def transform_section(section) title_text = extract_title_text(section.title) content_children = transform(section.contents || []) nested_sections = transform(section.sections || []) Coradoc::CoreModel::StructuralElement.new( element_type: 'section', id: section.id, level: section.level, title: title_text, children: content_children + nested_sections ) end |
.transform_source_block(block) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 137 def transform_source_block(block) content_lines = Array(block.lines).reject do |line| line.is_a?(Coradoc::AsciiDoc::Model::LineBreak) || line.is_a?(Coradoc::AsciiDoc::Model::Break::PageBreak) end.map do |line| extract_text_content(line) end.join("\n") language = extract_block_language(block) Coradoc::CoreModel::SourceBlock.new( element_type: 'block', id: block.id, title: extract_title_text(block.title), content: content_lines, language: language ) end |
.transform_stem(stem) ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 320 def transform_stem(stem) Coradoc::CoreModel::InlineElement.new( format_type: 'stem', content: stem.content, stem_type: stem.type || 'stem' ) end |
.transform_table(table) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 197 def transform_table(table) rows = Array(table.rows).map do |row| transform_table_row(row) end Coradoc::CoreModel::Table.new( id: table.id, title: table.title&.to_s, rows: rows ) end |
.transform_table_cell(cell) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 219 def transform_table_cell(cell) children = transform_inline_content(cell.content) Coradoc::CoreModel::TableCell.new( content: extract_text_content(cell.content), alignment: cell.horizontal_alignment, vertical_alignment: cell.vertical_alignment, colspan: cell.colspan, rowspan: cell.rowspan, style: cell.style_name, children: children ) end |
.transform_table_row(row) ⇒ Object
209 210 211 212 213 214 215 216 217 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 209 def transform_table_row(row) cells = Array(row.columns).map do |cell| transform_table_cell(cell) end Coradoc::CoreModel::TableRow.new( cells: cells, header: row.header ) end |
.transform_term(term) ⇒ Object
266 267 268 269 270 271 272 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 266 def transform_term(term) Coradoc::CoreModel::Term.new( text: term.term.to_s, type: term.type&.to_s || 'preferred', lang: term.lang&.to_s || 'en' ) end |
.transform_typed_block(block, klass, extra_attrs = {}) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 175 def transform_typed_block(block, klass, extra_attrs = {}) content_lines = extract_block_lines(block) klass.new( element_type: 'block', id: block.id, title: extract_title_text(block.title), content: content_lines, language: extract_block_language(block), **extra_attrs ) 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 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 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 25 def transform_with_case(model) case model when Coradoc::AsciiDoc::Model::Document transform_document(model) when Coradoc::AsciiDoc::Model::Section transform_section(model) when Coradoc::AsciiDoc::Model::Paragraph transform_paragraph(model) when Coradoc::AsciiDoc::Model::Block::SourceCode transform_source_block(model) when Coradoc::AsciiDoc::Model::Block::Quote transform_typed_block(model, Coradoc::CoreModel::QuoteBlock) when Coradoc::AsciiDoc::Model::Block::Example transform_typed_block(model, Coradoc::CoreModel::ExampleBlock) when Coradoc::AsciiDoc::Model::Block::Side transform_typed_block(model, Coradoc::CoreModel::SidebarBlock) when Coradoc::AsciiDoc::Model::Block::Literal transform_typed_block(model, Coradoc::CoreModel::LiteralBlock) when Coradoc::AsciiDoc::Model::Block::Open transform_typed_block(model, Coradoc::CoreModel::OpenBlock) when Coradoc::AsciiDoc::Model::Block::Pass transform_typed_block(model, Coradoc::CoreModel::PassBlock) when Coradoc::AsciiDoc::Model::Block::Listing transform_typed_block(model, Coradoc::CoreModel::ListingBlock) when Coradoc::AsciiDoc::Model::Block::Core transform_block(model, model.delimiter.to_s) when Coradoc::AsciiDoc::Model::Table transform_table(model) when Coradoc::AsciiDoc::Model::TableRow transform_table_row(model) when Coradoc::AsciiDoc::Model::TableCell transform_table_cell(model) when Coradoc::AsciiDoc::Model::List::Unordered transform_list(model, 'unordered') when Coradoc::AsciiDoc::Model::List::Ordered transform_list(model, 'ordered') when Coradoc::AsciiDoc::Model::List::Definition transform_list(model, 'definition') when Coradoc::AsciiDoc::Model::Term transform_term(model) when Coradoc::AsciiDoc::Model::Admonition transform_admonition(model) when Coradoc::AsciiDoc::Model::Inline::Bold transform_inline(model, 'bold') when Coradoc::AsciiDoc::Model::Inline::Italic transform_inline(model, 'italic') when Coradoc::AsciiDoc::Model::Inline::Monospace transform_inline(model, 'monospace') when Coradoc::AsciiDoc::Model::Inline::Highlight transform_inline(model, 'highlight') when Coradoc::AsciiDoc::Model::Inline::Link transform_link(model) when Coradoc::AsciiDoc::Model::Inline::CrossReference transform_cross_reference(model) when Coradoc::AsciiDoc::Model::Inline::Stem transform_stem(model) when Coradoc::AsciiDoc::Model::CommentBlock Coradoc::CoreModel::Block.new( element_type: 'comment', content: model.text.to_s ) when Coradoc::AsciiDoc::Model::Bibliography transform_bibliography(model) when Coradoc::AsciiDoc::Model::BibliographyEntry transform_bibliography_entry(model) when Coradoc::AsciiDoc::Model::Image::BlockImage transform_image(model) when Coradoc::AsciiDoc::Model::TextElement extract_text_content(model) else model end end |
Instance Method Details
#transform(model) ⇒ Object
10 11 12 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 10 def transform(model) self.class.transform(model) end |