class BerkeleyLibrary::Util::ODS::XML::Text::P

Constants

ESCAPABLE

Constant

Attributes

text[R]

Accessors

Public Class Methods

new(text, doc:) click to toggle source

Initializer

# File lib/berkeley_library/util/ods/xml/text/p.rb, line 23
def initialize(text, doc:)
  super(:text, 'p', doc: doc)

  @text = text
  add_default_children!
end

Private Instance Methods

add_default_children!() click to toggle source

Private methods

# File lib/berkeley_library/util/ods/xml/text/p.rb, line 35
def add_default_children!
  each_child_element_or_string { |c| children << c }
end
each_child_element_or_string(last_char = nil, text_remaining = text, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 39
def each_child_element_or_string(last_char = nil, text_remaining = text, &block)
  last_char, text_remaining = yield_while_escaped(last_char, text_remaining, &block)
  last_char, text_remaining = yield_while_unescaped(last_char, text_remaining, &block)
  each_child_element_or_string(last_char, text_remaining, &block) unless text_remaining.empty?
end
escape?(c, last_char) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 81
def escape?(c, last_char)
  ESCAPABLE.include?(c) && (last_char == ' ' || c != ' ')
end
escape_element_for(c) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 85
def escape_element_for(c)
  raise ArgumentError, "Not an escapable character: #{c}" unless ESCAPABLE.include?(c)

  return S.new(doc: doc) if c == ' '
  return Tab.new(doc: doc) if c == "\t"
  return LineBreak.new(doc: doc) if c == "\n"
end
take_while_unescaped(last_char, text_remaining) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 71
def take_while_unescaped(last_char, text_remaining)
  unescaped = text_remaining.each_char.with_object('') do |c, result|
    break result if escape?(c, last_char)

    result << c
    last_char = c
  end
  [unescaped, last_char]
end
yield_while_escaped(last_char, text_remaining, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 55
def yield_while_escaped(last_char, text_remaining, &block)
  escaped_char_count = 0
  text_remaining.each_char do |c|
    break unless escape?(c, last_char)

    # TODO: collapse contiguous spaces with attribute 'text:c'
    #   https://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#attribute-text_c
    block.call(escape_element_for(c))
    escaped_char_count += 1
    last_char = c
  end
  text_remaining = text_remaining[escaped_char_count..] unless escaped_char_count == 0

  [last_char, text_remaining]
end
yield_while_unescaped(last_char, text_remaining, &block) click to toggle source
# File lib/berkeley_library/util/ods/xml/text/p.rb, line 45
def yield_while_unescaped(last_char, text_remaining, &block)
  unescaped, last_char = take_while_unescaped(last_char, text_remaining)
  unless unescaped.empty?
    block.call(unescaped)
    text_remaining = text_remaining[unescaped.size..]
  end

  [last_char, text_remaining]
end