Class: Wpconv::Converter
- Inherits:
-
Object
- Object
- Wpconv::Converter
- Defined in:
- lib/wpconv/converter.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ output_dir: Dir.pwd, template: File.(File.join(File.dirname(__FILE__), '..', '..', 'template', 'markdown.erb')), filename_format: 'date-name', filter: 'markdown' }
- BUILT_IN_FILTERS =
['markdown', 'none']
Instance Method Summary collapse
- #default_filename_format ⇒ Object
- #default_filter ⇒ Object
- #default_template ⇒ Object
- #increase_convert_count ⇒ Object
- #item_filename ⇒ Object
- #item_output_dir ⇒ Object
- #print_convert_settings ⇒ Object
- #run(wp_xml_path, options = {}) ⇒ Object
- #setup_output_dirs ⇒ Object
Instance Method Details
#default_filename_format ⇒ Object
124 125 126 |
# File 'lib/wpconv/converter.rb', line 124 def default_filename_format 'date-name' end |
#default_filter ⇒ Object
128 129 130 |
# File 'lib/wpconv/converter.rb', line 128 def default_filter 'markdown' end |
#default_template ⇒ Object
120 121 122 |
# File 'lib/wpconv/converter.rb', line 120 def default_template File.(File.join(File.dirname(__FILE__), '..', '..', 'template', 'markdown.erb')) end |
#increase_convert_count ⇒ Object
79 80 81 82 83 84 85 |
# 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 ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# 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 ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# 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 ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/wpconv/converter.rb', line 112 def print_convert_settings print " soruce: #{@wp_xml_path}\n" print " template: #{@template}\n" print " output_dir: #{@output_base_dir}\n" print " filename_format: #{@filename_format}\n" print " filter: #{@filter}\n" end |
#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, = {}) @wp_xml_path = wp_xml_path @template = [:template] || DEFAULT_OPTIONS[:template] erb = File.open(@template) {|f| ERB.new(f.read)} @output_base_dir = [:output_dir] || DEFAULT_OPTIONS[:output_dir] setup_output_dirs @filename_format = [:filename_format] || DEFAULT_OPTIONS[:filename_format] @filter = [: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 ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# 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 |