Method: Wpconv::Converter#run

Defined in:
lib/wpconv/converter.rb

#run(wp_xml_path, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
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
# File 'lib/wpconv/converter.rb', line 21

def run(wp_xml_path, options = {})
  @wp_xml_path = wp_xml_path

  @template = options[:template] || DEFAULT_OPTIONS[:template]
  erb = File.open(@template) {|f| ERB.new(f.read)}

  @output_base_dir = options[:output_dir] || DEFAULT_OPTIONS[:output_dir]
  setup_output_dirs

  @filename_format = options[:filename_format] || DEFAULT_OPTIONS[:filename_format]

  @filter = options[:filter] || DEFAULT_OPTIONS[:filter]

  print "converting...\n"
  print_convert_settings

  doc = ::Nokogiri::XML(File.open(@wp_xml_path).read)
  @channel = WpXML::Channel.parse(doc.at('channel'))

  @convert_counts = {page: 0, post: 0, other: 0}

  doc.search('item').each do |doc_item|
    @item = WpXML::Item.parse(doc_item)

    # filter
    if not BUILT_IN_FILTERS.include?(@filter)
      @filter = "./#{@filter}" if not @filter =~ /\//
      require @filter
    end
    filter_class_name = File.basename(@filter).sub(/.rb$/, '').camelize
    @item[:content] = eval("Filter::#{filter_class_name}.apply(@item[:content])")

    # output
    File.open(File.join(item_output_dir, item_filename), "w") do |f|
      converted =  erb.result(binding)
      f.write(converted)
    end

    increase_convert_count

    print "."
  end

  print "done.\n"
  print "#{@convert_counts[:page]} pages, #{@convert_counts[:post]} posts and #{@convert_counts[:other]} something items are converted.\n"
end