Created
January 12, 2012 17:56
-
-
Save jeremyf/1602073 to your computer and use it in GitHub Desktop.
A simple script to do some of the heavy lifting for moving Wordpress reviews to BoardGameGeek
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'highline/import' | |
require 'open-uri' | |
conversions = [ | |
["<blockquote>", '[q]' ], | |
["</blockquote>", '[/q]' ], | |
["<strong>", '[b]' ], | |
["</strong>", '[/b]' ], | |
["<em>", '[i]' ], | |
["</em>", '[/i]' ], | |
["<h2>", '[size=18]' ], | |
["</h2>", '[/size]' ], | |
["<h3>", '[size=14]' ], | |
["</h3>", '[/size]' ], | |
["<li>", '<li>- ' ], | |
[" ", ' ' ], | |
[/\<a href="([^"]*)"\>([^\<]*)\<\/a\>/, '\2 (see [url]\1[/url])'], | |
] | |
prompt_response = ask("WordPress Blog Post URL:") | |
prompt_location = ask("Output Filename:") {|q| q.default = "review.txt" } | |
uri = URI.parse(prompt_response) | |
body = open(uri.to_s) | |
doc = Nokogiri::HTML(body) | |
node = doc.css('.entry-content').first | |
node.css('#ilikeposts').each(&:unlink) | |
node.css('.wpadvert').each(&:unlink) | |
node.css('.sharedaddy').each(&:unlink) | |
converted_node = Nokogiri::HTML(conversions.inject(node.inner_html) {|mem,(from,to)| mem.gsub(from,to) }) | |
text = converted_node.text | |
text.gsub!(/^-\s*\n\[/,'-[') | |
text.gsub!(/^([^-])/, "\n" << '\1') | |
File.open(prompt_location, 'w+') {|file| | |
file.puts "Originally posted at [url]#{uri}[/url]\n\n" << text | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment