Class: Prismic::Fragments::StructuredText
- Defined in:
- lib/prismic/fragments/structured_text.rb
Defined Under Namespace
Classes: Block, BlockGroup, Span
Instance Attribute Summary collapse
-
#blocks ⇒ Object
Returns the value of attribute blocks.
Instance Method Summary collapse
-
#as_html(link_resolver, html_serializer = nil) ⇒ String
Serializes the current StructuredText fragment into a fully usable HTML code.
-
#as_text(separator = ' ') ⇒ String
Returns the StructuredText as plain text, with zero formatting.
-
#first_title ⇒ Object
Finds the first highest title in a structured text.
-
#initialize(blocks) ⇒ StructuredText
constructor
A new instance of StructuredText.
Constructor Details
#initialize(blocks) ⇒ StructuredText
Returns a new instance of StructuredText
22 23 24 |
# File 'lib/prismic/fragments/structured_text.rb', line 22 def initialize(blocks) @blocks = blocks end |
Instance Attribute Details
#blocks ⇒ Object
Returns the value of attribute blocks
20 21 22 |
# File 'lib/prismic/fragments/structured_text.rb', line 20 def blocks @blocks end |
Instance Method Details
#as_html(link_resolver, html_serializer = nil) ⇒ String
Serializes the current StructuredText fragment into a fully usable HTML code. You need to pass a proper link_resolver so that internal links are turned into the proper URL in your website. If you use a starter kit, one is provided, that you can still update later.
This method simply executes the as_html methods on blocks; it is not advised to override this method if you want to change the HTML output, you should override the as_html method at the block level (like Heading.as_html, or Preformatted.as_html, for instance).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/prismic/fragments/structured_text.rb', line 38 def as_html(link_resolver, html_serializer=nil) # Defining blocks that deserve grouping, assigning them "group kind" names block_group = ->(block){ case block when Block::ListItem block.ordered? ? "ol" : "ul" else nil end } # Initializing groups, which is an array of BlockGroup objects groups, last = [], nil blocks.each {|block| group = block_group.(block) groups << BlockGroup.new(group) if !last || group != last groups.last << block last = group } # HTML-serializing the groups object (delegating the serialization of Block objects), # without forgetting to frame the BlockGroup objects right if needed groups.map{|group| html = group.blocks.map { |b| b.as_html(link_resolver, html_serializer) }.join case group.kind when "ol" %(<ol>#{html}</ol>) when "ul" %(<ul>#{html}</ul>) else html end }.join("\n\n") end |
#as_text(separator = ' ') ⇒ String
Returns the StructuredText as plain text, with zero formatting. Non-textual blocks (like images and embeds) are simply ignored.
78 79 80 |
# File 'lib/prismic/fragments/structured_text.rb', line 78 def as_text(separator=' ') blocks.map{|block| block.as_text }.compact.join(separator) end |
#first_title ⇒ Object
Finds the first highest title in a structured text
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/prismic/fragments/structured_text.rb', line 83 def first_title max_level = 6 # any title with a higher level kicks the current one out title = false @blocks.each do |block| if block.is_a?(Prismic::Fragments::StructuredText::Block::Heading) if block.level < max_level title = block.text max_level = block.level # new maximum end end end title end |