class Wpconv::Converter

Constants

BUILT_IN_FILTERS
DEFAULT_OPTIONS

Public Instance Methods

default_filename_format() click to toggle source
# File lib/wpconv/converter.rb, line 124
def default_filename_format
  'date-name'
end
default_filter() click to toggle source
# File lib/wpconv/converter.rb, line 128
def default_filter
  'markdown'
end
default_template() click to toggle source
# File lib/wpconv/converter.rb, line 120
def default_template
  File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'template', 'markdown.erb'))
end
increase_convert_count() click to toggle source
# File lib/wpconv/converter.rb, line 79
def increase_convert_count
  if @convert_counts.has_key? @item[:post_type].to_sym
    @convert_counts[@item[:post_type].to_sym] += 1
  else
    @convert_counts[:other] += 1
  end
end
item_filename() click to toggle source
# File lib/wpconv/converter.rb, line 87
def item_filename
  post_name = @item[:post_name] == '' ? @item[:post_id] : @item[:post_name]
  case @filename_format
  when 'date-name'
    "#{@item[:post_date].split(' ').first}-#{post_name}.md"
  when 'name'
    "#{post_name}.md"
  when 'id'
    "#{@item[:post_id]}.md"
  else
    "#{@item[:post_id]}.md"
  end
end
item_output_dir() click to toggle source
# File lib/wpconv/converter.rb, line 101
def item_output_dir
  case @item[:post_type]
  when 'post'
    @output_dirs[:post]
  when 'page'
    @output_dirs[:page]
  else
    @output_dirs[:other]
  end
end
print_convert_settings() click to toggle source
run(wp_xml_path, options = {}) click to toggle source
# 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
setup_output_dirs() click to toggle source
# File lib/wpconv/converter.rb, line 68
def setup_output_dirs
  @output_dirs = {
    page: File.join(@output_base_dir, 'pages'),
    post: File.join(@output_base_dir, 'posts'),
    other: File.join(@output_base_dir, 'others')
  }
  @output_dirs.each do |k, output_dir|
    FileUtils.mkdir_p(output_dir)
  end
end