class ReverseMarkdown::Converters::Li

Public Instance Methods

convert(node, state = {}) click to toggle source
# File lib/reverse_markdown/converters/li.rb, line 4
def convert(node, state = {})
  contains_child_paragraph = node.children.first ? node.children.first.name == 'p' : false
  content_node             = contains_child_paragraph ? node.children.first : node
  content                  = treat_children(content_node, state)
  indentation              = indentation_from(state)
  prefix                   = prefix_for(node)

  "#{indentation}#{prefix}#{content.chomp}\n" +
    (contains_child_paragraph ? "\n" : '')
end
indentation_from(state) click to toggle source
# File lib/reverse_markdown/converters/li.rb, line 24
def indentation_from(state)
  length = state.fetch(:ol_count, 0)
  '  ' * [length - 1, 0].max
end
prefix_for(node) click to toggle source
# File lib/reverse_markdown/converters/li.rb, line 15
def prefix_for(node)
  if node.parent.name == 'ol'
    index = node.parent.xpath('li').index(node)
    "#{index.to_i + 1}. "
  else
    '- '
  end
end