Class: Coradoc::AsciiDoc::Transform::FromCoreModel
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Transform::FromCoreModel
- Includes:
- Transform::Base
- Defined in:
- lib/coradoc/asciidoc/transform/from_core_model.rb
Overview
Transforms CoreModel to AsciiDoc models
Class Method Summary collapse
-
.build_callout_paragraphs(callouts) ⇒ Object
Re-expands typed Callouts back into the AsciiDoc ‘<N> text` paragraph form so the round-trip is faithful.
- .build_verbatim_block(semantic, block, content_text) ⇒ Object
-
.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.
- .flatten_one(result) ⇒ Object
- .register! ⇒ Object
-
.strip_title_heading(children, document_title) ⇒ Object
The forward direction (DocumentTransformer#insert_title_heading_after_frontmatter) emits a level-0 HeaderElement after any FrontmatterBlock so consumers that walk the children see the title.
- .transform(model) ⇒ Object
- .transform_abbreviation(abbreviation) ⇒ Object
- .transform_annotation(annotation) ⇒ Object
- .transform_bibliography(bib) ⇒ Object
- .transform_bibliography_entry(entry) ⇒ Object
- .transform_block(block) ⇒ Object
- .transform_comment_line(comment) ⇒ Object
- .transform_definition_item(item, depth = 1) ⇒ Object
- .transform_definition_list(definition_list, depth = 1) ⇒ Object
- .transform_footnote(footnote) ⇒ Object
- .transform_footnote_reference(footnote_ref) ⇒ Object
- .transform_image(image) ⇒ Object
- .transform_include(include) ⇒ Object
- .transform_inline(inline) ⇒ Object
- .transform_list(list) ⇒ Object
- .transform_list_item(item) ⇒ Object
- .transform_structural_element(element) ⇒ Object
- .transform_table(table) ⇒ Object
- .transform_term(term) ⇒ Object
- .transform_toc(_toc) ⇒ Object
- .transform_toc_entry(entry) ⇒ Object
- .verbatim_with_callouts?(semantic, block) ⇒ Boolean
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.
220 221 222 223 224 225 226 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 220 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
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 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 130 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.
98 99 100 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 98 def flatten_children(children) Array(children).flat_map { |child| flatten_one(transform(child)) } end |
.flatten_one(result) ⇒ Object
102 103 104 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 102 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 |
.strip_title_heading(children, document_title) ⇒ Object
The forward direction (DocumentTransformer#insert_title_heading_after_frontmatter) emits a level-0 HeaderElement after any FrontmatterBlock so consumers that walk the children see the title. On the reverse path, that HeaderElement would round-trip back as a separate body element and the title would be emitted twice (once via the document header, once via the heading child). Drop it before serialization when it carries the same text as the document title.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 81 def strip_title_heading(children, document_title) return children unless document_title && !document_title.strip.empty? index = children.find_index do |child| child.is_a?(CoreModel::HeaderElement) && child.level.to_i.zero? && child.title.to_s == document_title.to_s end return children unless index children.reject.with_index { |_, i| i == index } 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
368 369 370 371 372 373 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 368 def transform_abbreviation(abbreviation) Coradoc::AsciiDoc::Model::TextElement.new( content: abbreviation.term.to_s + (abbreviation.definition ? " (#{abbreviation.definition})" : '') ) end |
.transform_annotation(annotation) ⇒ Object
280 281 282 283 284 285 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 280 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
335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 335 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
347 348 349 350 351 352 353 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 347 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 106 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
412 413 414 415 416 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 412 def transform_comment_line(comment) Coradoc::AsciiDoc::Model::CommentLine.new( text: comment.text.to_s ) end |
.transform_definition_item(item, depth = 1) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 385 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
375 376 377 378 379 380 381 382 383 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 375 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
355 356 357 358 359 360 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 355 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
362 363 364 365 366 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 362 def transform_footnote_reference(footnote_ref) Coradoc::AsciiDoc::Model::Inline::Footnote.new( id: footnote_ref.id ) end |
.transform_image(image) ⇒ Object
327 328 329 330 331 332 333 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 327 def transform_image(image) Coradoc::AsciiDoc::Model::Image::BlockImage.new( src: image.src, title: image.alt, attributes: build_image_attributes(image) ) end |
.transform_include(include) ⇒ Object
418 419 420 421 422 423 424 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 418 def transform_include(include) Coradoc::AsciiDoc::Model::Include.new( path: include.target.to_s, attributes: build_include_attributes(include), line_break: include.line_break.to_s ) end |
.transform_inline(inline) ⇒ Object
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 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 287 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
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 247 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
265 266 267 268 269 270 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 265 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 66 67 68 69 70 71 |
# 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 # Pull FrontmatterBlock out first so strip_title_heading sees # the body children in their natural order (frontmatter → # title heading → body). The reverse-direction strip then # matches on a level-0 HeaderElement wherever it sits among # the remaining children. without_frontmatter, frontmatter = extract_frontmatter(Array(element.children)) without_title = strip_title_heading(without_frontmatter, element.title) Coradoc::AsciiDoc::Model::Document.new( id: element.id, header: header, sections: flatten_children(without_title), 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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 228 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
272 273 274 275 276 277 278 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 272 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
400 401 402 403 404 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 400 def transform_toc(_toc) Coradoc::AsciiDoc::Model::TextElement.new( content: 'toc::[]' ) end |
.transform_toc_entry(entry) ⇒ Object
406 407 408 409 410 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 406 def transform_toc_entry(entry) Coradoc::AsciiDoc::Model::TextElement.new( content: entry.title.to_s ) end |
.verbatim_with_callouts?(semantic, block) ⇒ Boolean
211 212 213 214 215 216 |
# File 'lib/coradoc/asciidoc/transform/from_core_model.rb', line 211 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 |