Class: ReverseMarkdown::Converters::Li

Inherits:
Base
  • Object
show all
Defined in:
lib/reverse_markdown/converters/li.rb

Instance Method Summary collapse

Methods inherited from Base

#escape_keychars, #extract_src, #extract_title, #treat, #treat_children

Instance Method Details

#convert(node, state = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/reverse_markdown/converters/li.rb', line 4

def convert(node, state = {})
  contains_child_paragraph = node.first_element_child ? node.first_element_child.name == 'p' : false
  content_node             = contains_child_paragraph ? node.first_element_child : 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) ⇒ Object



24
25
26
27
# 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) ⇒ Object



15
16
17
18
19
20
21
22
# 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