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_title, #treat, #treat_children

Instance Method Details

#convert(node) ⇒ Object



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

def convert(node)
  content     = treat_children(node)
  indentation = indentation_for(node)
  prefix      = prefix_for(node)
  "#{indentation}#{prefix}#{content.chomp}\n"
end

#indentation_for(node) ⇒ Object



20
21
22
23
# File 'lib/reverse_markdown/converters/li.rb', line 20

def indentation_for(node)
  length = node.ancestors('ol').size + node.ancestors('ul').size
  '  ' * [length - 1, 0].max
end

#prefix_for(node) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/reverse_markdown/converters/li.rb', line 11

def prefix_for(node)
  if node.parent.name == 'ol'
    index = node.parent.xpath('li').index(node)
    "#{index.to_i + 1}. "
  else
    '- '
  end
end