Class: Prismic::Fragments::StructuredText::Block::Text
- Inherits:
-
Object
- Object
- Prismic::Fragments::StructuredText::Block::Text
- Defined in:
- lib/prismic/fragments/structured_text.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#label ⇒ String
May be nil.
- #spans ⇒ Array<Span>
- #text ⇒ String
Instance Method Summary collapse
- #as_html(link_resolver = nil, html_serializer = nil) ⇒ Object
-
#as_text ⇒ Object
Zero-formatted textual value of the block.
-
#initialize(text, spans, label = nil) ⇒ Text
constructor
A new instance of Text.
-
#prepare_spans ⇒ Object
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.
- #serialize(elt, text, link_resolver, html_serializer) ⇒ Object
Constructor Details
#initialize(text, spans, label = nil) ⇒ Text
Returns a new instance of Text
167 168 169 170 171 |
# File 'lib/prismic/fragments/structured_text.rb', line 167 def initialize(text, spans, label = nil) @text = text @spans = spans.select{|span| span.start < span.end} @label = label end |
Instance Attribute Details
#label ⇒ String
Returns may be nil
165 166 167 |
# File 'lib/prismic/fragments/structured_text.rb', line 165 def label @label end |
#spans ⇒ Array<Span>
163 164 165 |
# File 'lib/prismic/fragments/structured_text.rb', line 163 def spans @spans end |
#text ⇒ String
161 162 163 |
# File 'lib/prismic/fragments/structured_text.rb', line 161 def text @text end |
Instance Method Details
#as_html(link_resolver = nil, html_serializer = nil) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/prismic/fragments/structured_text.rb', line 177 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 ⇒ Object
Zero-formatted textual value of the block.
248 249 250 |
# File 'lib/prismic/fragments/structured_text.rb', line 248 def as_text @text end |
#prepare_spans ⇒ Object
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
230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/prismic/fragments/structured_text.rb', line 230 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) ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/prismic/fragments/structured_text.rb', line 252 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 |