Module: HTML2Markdown::Converter

Included in:
HTMLPage
Defined in:
lib/html2markdown/converter.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

define custom node processor



72
73
74
75
76
# File 'lib/html2markdown/converter.rb', line 72

def method_missing(name,*args,&block)
  self.class.send :define_method,"parse_#{name}" do |node,contents|
    block.call node,contents
  end
end

Instance Method Details

#debugObject



78
79
80
81
82
# File 'lib/html2markdown/converter.rb', line 78

def debug
  puts '----------------------------------'
  puts yield
  puts '----------------------------------'
end

#parse_element(ele) ⇒ Object

a normal element maybe text maybe node



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/html2markdown/converter.rb', line 16

def parse_element(ele)
  if ele.is_a? Nokogiri::XML::Text
    return "#{ele.text}\n"
  else
    if (children = ele.children).count > 0 
      return wrap_node(ele,children.map {|ele| parse_element(ele)}.join )
    else
      return wrap_node(ele,ele.text)
    end
  end
end

#to_markdown(string_contents) ⇒ Object

Raises:



7
8
9
10
11
# File 'lib/html2markdown/converter.rb', line 7

def to_markdown string_contents
  raise NoContents unless string_contents!=nil and string_contents.is_a?(String)
  doc = Nokogiri::HTML(string_contents)
  doc.children.map { |ele| parse_element(ele) }.join
end

#wrap_node(node, contents = nil) ⇒ Object

wrap node with markdown



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/html2markdown/converter.rb', line 29

def wrap_node(node,contents=nil)
  result = ''
  contents.strip! unless contents==nil
  # check if there is a custom parse exist
  if respond_to? "parse_#{node.name}"
    return self.send("parse_#{node.name}",node,contents)
  end
  # skip hidden node
  return '' if node['style'] and node['style'] =~ /display:\s*none/
  # default parse
  case node.name.downcase
  when 'i'
  when 'script'
  when 'style'
  when 'li'
    result << "*#{contents}\n"
  when 'blockquote'
    contents.split('\n').each do |part|
      result << ">#{contents}\n"
    end
  when 'strong'
    result << "**#{contents}**\n"
  when 'h1'
    result << "##{contents}\n"
  when 'h2'
    result << "###{contents}\n"
  when 'h3'
    result << "####{contents}\n"
  when 'hr'
    result << "****\n"
  when 'br'
    result << "\n"
  when 'img'
    result << "![#{node['alt']}](#{node['src']})"
  when 'a'
    result << "[#{contents}](#{node['href']})"
  else
    result << contents unless contents == nil
  end
  result
end