123456789_123456789_123456789_123456789_123456789_

Module: ActionText::PlainTextConversion

Relationships & Source Files
Defined in: actiontext/lib/action_text/plain_text_conversion.rb

Instance Method Summary

Instance Method Details

#break_if_nested_list(node, text) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 117

def break_if_nested_list(node, text)
  if list_node_depth_for_node(node) > 0
    "\n#{text}"
  else
    text
  end
end

#bullet_for_li_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 94

def bullet_for_li_node(node, index)
  if list_node_name_for_li_node(node) == "ol"
    "#{index + 1}."
  else
    ""
  end
end

#indentation_for_li_node(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 106

def indentation_for_li_node(node)
  depth = list_node_depth_for_node(node)
  if depth > 1
    "  " * (depth - 1)
  end
end

#list_node_depth_for_node(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 113

def list_node_depth_for_node(node)
  node.ancestors.map(&:name).grep(/^[uo]l$/).count
end

#list_node_name_for_li_node(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 102

def list_node_name_for_li_node(node)
  node.ancestors.lazy.map(&:name).grep(/^[uo]l$/).first
end

#node_to_plain_text(node)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 9

def node_to_plain_text(node)
  remove_trailing_newlines(plain_text_for_node(node))
end

#plain_text_for_block(node, index = 0) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 40

def plain_text_for_block(node, index = 0)
  "#{remove_trailing_newlines(plain_text_for_node_children(node))}\n\n"
end

#plain_text_for_blockquote_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 72

def plain_text_for_blockquote_node(node, index)
  text = plain_text_for_block(node)
  return "“”" if text.blank?

  text = text.dup
  text.insert(text.rindex(/\S/) + 1, "")
  text.insert(text.index(/\S/), "")
  text
end

#plain_text_for_br_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 56

def plain_text_for_br_node(node, index)
  "\n"
end

#plain_text_for_div_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 64

def plain_text_for_div_node(node, index)
  "#{remove_trailing_newlines(plain_text_for_node_children(node))}\n"
end

#plain_text_for_figcaption_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 68

def plain_text_for_figcaption_node(node, index)
  "[#{remove_trailing_newlines(plain_text_for_node_children(node))}]"
end

#plain_text_for_li_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 82

def plain_text_for_li_node(node, index)
  bullet = bullet_for_li_node(node, index)
  text = remove_trailing_newlines(plain_text_for_node_children(node))
  indentation = indentation_for_li_node(node)

  "#{indentation}#{bullet} #{text}\n"
end

#plain_text_for_list(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 48

def plain_text_for_list(node, index)
  "#{break_if_nested_list(node, plain_text_for_block(node))}"
end

#plain_text_for_node(node, index = 0) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 14

def plain_text_for_node(node, index = 0)
  if respond_to?(plain_text_method_for_node(node), true)
    send(plain_text_method_for_node(node), node, index)
  else
    plain_text_for_node_children(node)
  end
end

#plain_text_for_node_children(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 22

def plain_text_for_node_children(node)
  texts = []
  node.children.each_with_index do |child, index|
    next if skippable?(child)

    texts << plain_text_for_node(child, index)
  end
  texts.join
end

#plain_text_for_text_node(node, index) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 60

def plain_text_for_text_node(node, index)
  remove_trailing_newlines(node.text)
end

#plain_text_method_for_node(node) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 36

def plain_text_method_for_node(node)
  :"plain_text_for_#{node.name}_node"
end

#remove_trailing_newlines(text) (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 90

def remove_trailing_newlines(text)
  text.chomp("")
end

#skippable?(node) ⇒ Boolean (private)

[ GitHub ]

  
# File 'actiontext/lib/action_text/plain_text_conversion.rb', line 32

def skippable?(node)
  node.name == "script" || node.name == "style"
end