Module: Coradoc::CoreModel::InlineContent
- Defined in:
- lib/coradoc/core_model/inline_content.rb
Overview
Operations on mixed arrays of inline content (String / InlineElement / CoreModel::Base). Single source of truth for text extraction and edge cleanup, replacing parallel implementations that previously lived in the HTML converters.
No method here mutates its inputs — InlineElements are duplicated via #with_content, Strings are replaced with new instances.
Class Method Summary collapse
-
.strip_edges(content) ⇒ Object
Return a new array with leading whitespace stripped from the first text-carrying item and trailing whitespace stripped from the last.
-
.text_of(content) ⇒ Object
Extract plain text from a mixed content value.
Class Method Details
.strip_edges(content) ⇒ Object
Return a new array with leading whitespace stripped from the first text-carrying item and trailing whitespace stripped from the last. Inputs are not mutated. Non-Array inputs return unchanged. If no item carries text, returns the input array unchanged.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/coradoc/core_model/inline_content.rb', line 33 def strip_edges(content) return content unless content.is_a?(Array) return content if content.empty? first_idx = content.index { |i| text_carrier?(i) } return content if first_idx.nil? last_idx = content.rindex { |i| text_carrier?(i) } content.map.with_index do |item, idx| next item unless text_carrier?(item) stripped = item_text(item) stripped = stripped.lstrip if idx == first_idx stripped = stripped.rstrip if idx == last_idx item.is_a?(String) ? stripped : item.with_content(stripped) end end |
.text_of(content) ⇒ Object
Extract plain text from a mixed content value.
nil → ” / String → itself / Array → text_of mapped + joined / InlineElement → #content.to_s / StructuralElement → recurse on #children / other Base → #content if String else #title.to_s / anything else → #to_s.
20 21 22 23 24 25 26 |
# File 'lib/coradoc/core_model/inline_content.rb', line 20 def text_of(content) return '' if content.nil? return content if content.is_a?(String) return text_of_one(content) unless content.is_a?(Array) content.map { |item| text_of_one(item) }.join end |