Class: Coradoc::AsciiDoc::Transform::ToCoreModel
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Transform::ToCoreModel
- Defined in:
- lib/coradoc/asciidoc/transform/to_core_model.rb
Class Method Summary collapse
- .asciidoc_delimiter_to_semantic(delimiter) ⇒ Object
- .extract_block_language(block) ⇒ Object
- .extract_block_lines(block) ⇒ Object
- .extract_document_attributes(doc) ⇒ Object
-
.extract_source_line(content) ⇒ Object
Extracts the source_line of the first AsciiDoc::Model::Base in
content. -
.extract_source_lines(content) ⇒ Object
Returns the source-line view of an AsciiDoc content array as an Array
. - .extract_text_content(content) ⇒ Object
- .extract_title_text(title) ⇒ Object
- .parse_and_transform_inline(text) ⇒ Object
- .parse_inline_text(raw_text) ⇒ Object
- .register! ⇒ Object
- .transform(model) ⇒ Object
- .transform_inline_content(content) ⇒ Object
Instance Method Summary collapse
Class Method Details
.asciidoc_delimiter_to_semantic(delimiter) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 104 def asciidoc_delimiter_to_semantic(delimiter) return :open if delimiter && delimiter.length < 4 char = delimiter&.[](0) DelimiterMapping::CHAR_TO_SEMANTIC[char] || :open end |
.extract_block_language(block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 80 def extract_block_language(block) lang = block.lang return lang if lang.is_a?(String) && !lang.empty? attrs = block.attributes return nil unless attrs.is_a?(Coradoc::AsciiDoc::Model::AttributeList) named_lang = attrs['language'] return named_lang.to_s if named_lang positional = attrs.positional positional[1]&.value&.to_s if positional.length > 1 end |
.extract_block_lines(block) ⇒ Object
41 42 43 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 41 def extract_block_lines(block) extract_source_lines(block.lines).join("\n") end |
.extract_document_attributes(doc) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 94 def extract_document_attributes(doc) return nil unless doc.document_attributes = Coradoc::CoreModel::Metadata.new doc.document_attributes.to_hash.each do |key, value| [key.to_s] = value.to_s end end |
.extract_source_line(content) ⇒ Object
Extracts the source_line of the first AsciiDoc::Model::Base in
content. Single source of truth for source-line propagation
from AsciiDoc::Model trees into CoreModel objects built from
grouped content (e.g., paragraphs inside typed blocks).
Returns nil when no model carries a source_line.
50 51 52 53 54 55 56 57 58 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 50 def extract_source_line(content) Array(content).each do |item| next unless item.is_a?(Coradoc::AsciiDoc::Model::Base) line = item.source_line return line if line end nil end |
.extract_source_lines(content) ⇒ Object
Returns the source-line view of an AsciiDoc content array as
an Array
32 33 34 35 36 37 38 39 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 32 def extract_source_lines(content) Array(content).filter_map do |item| next if item.is_a?(Coradoc::AsciiDoc::Model::LineBreak) || item.is_a?(Coradoc::AsciiDoc::Model::Break::PageBreak) extract_text_content(item).to_s end end |
.extract_text_content(content) ⇒ Object
76 77 78 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 76 def extract_text_content(content) TextExtractVisitor.new.extract(content) end |
.extract_title_text(title) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 60 def extract_title_text(title) return nil if title.nil? return title.to_s unless title.is_a?(Coradoc::AsciiDoc::Model::Title) content = title.content return '' if content.nil? if content.is_a?(String) content elsif content.is_a?(Array) content.map { |c| extract_text_content(c) }.join else extract_text_content(content) end end |
.parse_and_transform_inline(text) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 139 def parse_and_transform_inline(text) return text if text.nil? || text.to_s.strip.empty? parsed_elements = Coradoc::AsciiDoc::Transformer.parse_inline_content(text) content_array = parsed_elements.flat_map do |element| element.is_a?(Coradoc::AsciiDoc::Model::TextElement) ? element.content : element end transformed = transform_inline_content(content_array) if transformed.all?(Coradoc::CoreModel::TextContent) transformed.map(&:text).join else transformed end rescue Parslet::ParseFailed text end |
.parse_inline_text(raw_text) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 111 def parse_inline_text(raw_text) return [] if raw_text.nil? || raw_text.to_s.strip.empty? text = raw_text.to_s parser = Coradoc::AsciiDoc::Parser::Base.new transformer = Coradoc::AsciiDoc::Transformer.new parsed = parser.text_any.parse(text) result = transformer.apply({ text: parsed }) case result when Coradoc::AsciiDoc::Model::TextElement result.content.is_a?(Array) ? result.content : [result.content] when Array result when Coradoc::AsciiDoc::Model::Base [result] else [text] end rescue Parslet::ParseFailed [text] end |
.register! ⇒ Object
10 11 12 13 14 15 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 10 def register! return if @registered Transform::ToCoreModelRegistrations.register_all! @registered = true end |
.transform(model) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 17 def transform(model) register! 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) transformer ? transformer.call(model) : model end |
.transform_inline_content(content) ⇒ Object
135 136 137 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 135 def transform_inline_content(content) InlineTransformVisitor.new(self).transform(content) end |
Instance Method Details
#transform(model) ⇒ Object
159 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 159 def transform(model) = self.class.transform(model) |