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
- .list_marker_type(list) ⇒ 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, parent_id: nil) ⇒ 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
203 204 205 206 207 208 209 210 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 203 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 |
.list_marker_type(list) ⇒ Object
248 249 250 251 252 253 254 255 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 248 def list_marker_type(list) case list when Coradoc::AsciiDoc::Model::List::Ordered then 'ordered' when Coradoc::AsciiDoc::Model::List::Unordered then 'unordered' when Coradoc::AsciiDoc::Model::List::Definition then 'definition' else 'unordered' end 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.filter_map { |item| transform(item) } 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
325 326 327 328 329 330 331 332 333 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 325 def transform_admonition(admonition) children = transform_inline_content(admonition.content) block = Coradoc::CoreModel::AnnotationBlock.new( annotation_type: admonition.type, content: extract_text_content(admonition.content) ) block.children = children block end |
.transform_bibliography(bib) ⇒ Object
387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 387 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
400 401 402 403 404 405 406 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 400 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 |
# 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( 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
364 365 366 367 368 369 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 364 def transform_cross_reference(xref) Coradoc::CoreModel::CrossReferenceElement.new( target: xref.href, content: xref.args&.first || xref.href ) end |
.transform_document(doc) ⇒ Object
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 98 def transform_document(doc) title_text = extract_title_text(doc.header&.title) attributes = extract_document_attributes(doc) Coradoc::CoreModel::DocumentElement.new( id: doc.id, title: title_text, attributes: attributes, children: transform(doc.sections || doc.contents || []) ) end |
.transform_image(image) ⇒ Object
378 379 380 381 382 383 384 385 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 378 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
335 336 337 338 339 340 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 335 def transform_inline(inline, format_type) klass = Coradoc::CoreModel::InlineElement.format_type_class(format_type) klass.new( content: extract_text_content(inline.content) ) end |
.transform_inline_footnote(footnote) ⇒ Object
349 350 351 352 353 354 355 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 349 def transform_inline_footnote(footnote) parsed_content = parse_and_transform_inline(footnote.text.to_s) Coradoc::CoreModel::FootnoteElement.new( target: footnote.id, content: parsed_content ) end |
.transform_inline_text(inline, format_type) ⇒ Object
342 343 344 345 346 347 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 342 def transform_inline_text(inline, format_type) klass = Coradoc::CoreModel::InlineElement.format_type_class(format_type) klass.new( content: inline.text.to_s ) end |
.transform_link(link) ⇒ Object
357 358 359 360 361 362 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 357 def transform_link(link) Coradoc::CoreModel::LinkElement.new( target: link.path, content: link.name || link.path ) end |
.transform_list(list, marker_type) ⇒ Object
257 258 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 257 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 # Re-parse raw text through inline parser for structured content term_parts = term_content.is_a?(Array) ? term_content : [term_content] parsed_terms = term_parts.flat_map do |part| parse_inline_text(part) end parsed_defs = parse_inline_text(def_content) term_children = transform_inline_content(parsed_terms) def_children = transform_inline_content(parsed_defs) di = Coradoc::CoreModel::DefinitionItem.new( term: extract_text_content(term_children), definitions: [extract_text_content(def_children)], term_children: term_children, definition_children: def_children ) di.id = item.id if item.id di 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 if item.nested.is_a?(Coradoc::AsciiDoc::Model::List::Core) nested_core = transform_list(item.nested, list_marker_type(item.nested)) li.children << nested_core elsif item.nested.is_a?(Array) item.nested.each do |n| next unless n.is_a?(Coradoc::AsciiDoc::Model::List::Core) li.children << transform_list(n, list_marker_type(n)) end end 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
128 129 130 131 132 133 134 135 136 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 128 def transform_paragraph(para) children = transform_inline_content(para.content) Coradoc::CoreModel::ParagraphBlock.new( id: para.id, content: extract_text_content(para.content), children: children ) end |
.transform_section(section, parent_id: nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 109 def transform_section(section, parent_id: nil) title_text = extract_title_text(section.title) section_id = section.id || Coradoc::CoreModel::IdGenerator.generate_from_title( title_text, parent_id: parent_id ) content_children = transform(section.contents || []) nested_sections = (section.sections || []).map do |child| transform_section(child, parent_id: section_id) end Coradoc::CoreModel::SectionElement.new( id: section_id, level: section.level, title: title_text, children: content_children + nested_sections ) end |
.transform_source_block(block) ⇒ Object
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 138 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( id: block.id, title: extract_title_text(block.title), content: content_lines, language: language ) end |
.transform_stem(stem) ⇒ Object
371 372 373 374 375 376 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 371 def transform_stem(stem) Coradoc::CoreModel::StemElement.new( content: stem.content, stem_type: stem.type || 'stem' ) end |
.transform_table(table) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 212 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
234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 234 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
224 225 226 227 228 229 230 231 232 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 224 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
317 318 319 320 321 322 323 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 317 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
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 199 200 201 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 174 def transform_typed_block(block, klass, extra_attrs = {}) lines = Array(block.lines).reject do |line| line.is_a?(Coradoc::AsciiDoc::Model::LineBreak) || line.is_a?(Coradoc::AsciiDoc::Model::Break::PageBreak) end has_nested_blocks = lines.any?(Coradoc::AsciiDoc::Model::Block::Core) if has_nested_blocks children = lines.map { |line| transform(line) } klass.new( id: block.id, title: extract_title_text(block.title), children: children, language: extract_block_language(block), **extra_attrs ) else content_lines = lines.map { |line| extract_text_content(line) }.join("\n") klass.new( id: block.id, title: extract_title_text(block.title), content: content_lines, language: extract_block_language(block), **extra_attrs ) end 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 |
# 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::CommentBlock.new( 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 |