class Kramdown::Parser::Prismic

Public Instance Methods

parse() click to toggle source
# File lib/kramdown/parser/prismic.rb, line 6
def parse
  @root.options[:encoding] = 'UTF-8'
  @root.children = @source.reduce([]) do |memo, block|
    parse_element(block, memo)
  end
end

Private Instance Methods

find_ending_spans_for(block, index) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 125
def find_ending_spans_for(block, index)
  block[:content][:spans].find_all do |span|
    span[:end] == index
  end
end
find_starting_spans_for(block, index) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 117
def find_starting_spans_for(block, index)
  block[:content][:spans].find_all do |span|
    span[:start] == index
  end.sort_by do |span|
    -span[:end]
  end
end
parse_element(block, memo) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 15
def parse_element(block, memo)
  type = block[:type].gsub('-', '_')
  type = 'heading' if type.match(/heading/)
  if type == 'list_item'
    parse_list(:ul, block, memo)
    memo
  elsif type == 'o_list_item'
    parse_list(:ol, block, memo)
    memo
  else
    element = send("parse_#{type}", block)
    parse_spans(element, block)
    memo << element
  end
end
parse_embed(block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 70
def parse_embed(block)
  Element.new(:html_element, 'iframe', { src: block[:data][:embed_url] })
end
parse_heading(block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 31
def parse_heading(block)
  level = block[:type].match(/heading([1-6])/)[1].to_i
  Element.new(:header, nil, nil, { level: level, raw_text: '' })
end
parse_image(block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 40
def parse_image(block)
  p = Element.new(:p)
  img = Element.new(:img, nil, { 'src' => block[:data][:origin][:url], 'alt' => block[:data][:alt] })
  if block[:data][:linkTo]
    a = Element.new(:a, nil, { 'href' => block[:data][:linkTo][:url] })
    a.children << img
    p.children << a
  else
    p.children << img
  end
  p
end
parse_list(type, block, memo) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 57
def parse_list(type, block, memo)
  list = memo.last
  unless list && list.type == type
    list = Element.new(type)
    memo << list
  end
  li = Element.new(:li, nil, nil)
  list.children << li
  p = Element.new(:p, nil, nil, transparent: true)
  li.children << p
  parse_spans(p, block)
end
parse_paragraph(_block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 36
def parse_paragraph(_block)
  Element.new(:p)
end
parse_preformatted(_block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 53
def parse_preformatted(_block)
  Element.new(:blockquote)
end
parse_spans(element, block) click to toggle source
# File lib/kramdown/parser/prismic.rb, line 74
def parse_spans(element, block)
  stack = []

  (block[:content][:text].size + 1).times do |index|
    starting_spans = find_starting_spans_for(block, index)
    ending_spans   = find_ending_spans_for(block, index)

    ending_spans.each do |_ending_span|
      el = stack.pop
      if stack.empty?
        element.children << el
      else
        stack[-1].children << el
      end
    end
    starting_spans.each do |starting_span|
      stack << if starting_span[:type] == 'hyperlink'
                 Element.new(:a, nil, { 'href' => starting_span[:data][:url] })
               else
                 Element.new(starting_span[:type].to_sym)
               end
    end

    char = block[:content][:text][index]
    next if char.nil?

    current_text = if stack.empty?
                     element.children.last
                   else
                     stack[-1].children.last
                   end
    if current_text.nil? || current_text.type != :text
      current_text = Element.new(:text, '')
      if stack.empty?
        element.children << current_text
      else
        stack[-1].children << current_text
      end
    end
    current_text.value += char
  end
end