class Prismic::Fragments::StructuredText::Block::Text

Attributes

label[RW]

@return [String] may be nil

spans[RW]

@return [Array<Span>]

text[RW]

@return [String]

Public Class Methods

new(text, spans, label = nil) click to toggle source
# File lib/prismic/fragments/structured_text.rb, line 169
def initialize(text, spans, label = nil)
  @text = text
  @spans = spans.select{|span| span.start < span.end}
  @label = label
end

Public Instance Methods

as_html(link_resolver=nil, html_serializer=nil) click to toggle source
# File lib/prismic/fragments/structured_text.rb, line 179
def as_html(link_resolver=nil, html_serializer=nil)
  html = ''
  # Getting Hashes of spanning tags to insert, sorted by starting position, and by ending position
  start_spans, end_spans = prepare_spans
  # Open tags
  stack = Array.new
  (text.length + 1).times do |pos| # Looping to length + 1 to catch closing tags
    end_spans[pos].each do |t|
      # Close a tag
      tag = stack.pop
      inner_html = serialize(tag[:span], tag[:html], link_resolver, html_serializer)
      if stack.empty?
        # The tag was top-level
        html += inner_html
      else
        # Add the content to the parent tag
        stack[-1][:html] += inner_html
      end
    end
    start_spans[pos].each do |tag|
      # Open a tag
      stack.push({
          :span => tag,
          :html => ''
      })
    end
    if pos < text.length
      if stack.empty?
        # Top level text
        html += cgi_escape_html(text[pos])
      else
        # Inner text of a span
        stack[-1][:html] += cgi_escape_html(text[pos])
      end
    end
  end
  html.gsub("\n", '<br>')
end
as_text() click to toggle source

Zero-formatted textual value of the block.

@return The textual value.

# File lib/prismic/fragments/structured_text.rb, line 250
def as_text
  @text
end
prepare_spans() click to toggle source

Building two span Hashes:

* start_spans, with the starting positions as keys, and spans as values
* end_spans, with the ending positions as keys, and spans as values
# File lib/prismic/fragments/structured_text.rb, line 232
def prepare_spans
  unless defined?(@prepared_spans)
    start_spans = Hash.new{|h,k| h[k] = [] }
    end_spans = Hash.new{|h,k| h[k] = [] }
    spans.each {|span|
      start_spans[span.start] << span
      end_spans[span.end] << span
    }
    # Make sure the spans are sorted bigger first to respect the hierarchy
    @start_spans = start_spans.each { |_, spans| spans.sort! { |a, b| b.end - b.start <=> a.end - a.start } }
    @end_spans = end_spans
  end
  [@start_spans, @end_spans]
end
serialize(elt, text, link_resolver, html_serializer) click to toggle source
# File lib/prismic/fragments/structured_text.rb, line 254
def serialize(elt, text, link_resolver, html_serializer)
  custom_html = html_serializer && html_serializer.serialize(elt, text)
  if custom_html.nil?
    elt.serialize(text, link_resolver)
  else
    custom_html
  end
end

Private Instance Methods

cgi_escape_html(string) click to toggle source
# File lib/prismic/fragments/structured_text.rb, line 218
def cgi_escape_html(string)
  # We don't use CGI::escapeHTML because the implementation changed from 1.9 to 2.0 and that break tests
  string.gsub(/['&\"<>]/, {
      "'" => '&#39;',
      '&' => '&amp;',
      '"' => '&quot;',
      '<' => '&lt;',
      '>' => '&gt;'
  })
end
class_code() click to toggle source
# File lib/prismic/fragments/structured_text.rb, line 175
def class_code
  (@label && %( class="#{label}")) || ''
end