Class: Coradoc::AsciiDoc::Transform::ToCoreModel
- Inherits:
-
Object
- Object
- Coradoc::AsciiDoc::Transform::ToCoreModel
- Includes:
- Transform::Base
- 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_text_content(content) ⇒ Object
- .extract_title_text(title) ⇒ Object
- .parse_and_transform_inline(text) ⇒ Object
- .parse_inline_text(raw_text) ⇒ Object
- .transform(model) ⇒ Object
- .transform_inline_content(content) ⇒ Object
Class Method Details
.asciidoc_delimiter_to_semantic(delimiter) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 74 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
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 50 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
20 21 22 23 24 25 26 27 28 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 20 def extract_block_lines(block) non_break_lines = Array(block.lines).reject do |line| line.is_a?(Coradoc::AsciiDoc::Model::LineBreak) || line.is_a?(Coradoc::AsciiDoc::Model::Break::PageBreak) end non_break_lines.map do |line| extract_text_content(line) end.join("\n") end |
.extract_document_attributes(doc) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 64 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_text_content(content) ⇒ Object
46 47 48 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 46 def extract_text_content(content) TextExtractVisitor.new.extract(content) end |
.extract_title_text(title) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 30 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
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 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 81 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 |
.transform(model) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 12 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) transformer ? transformer.call(model) : model end |
.transform_inline_content(content) ⇒ Object
105 106 107 |
# File 'lib/coradoc/asciidoc/transform/to_core_model.rb', line 105 def transform_inline_content(content) InlineTransformVisitor.new(self).transform(content) end |