Skip to content

Instantly share code, notes, and snippets.

@jeremyf
Created January 12, 2012 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyf/1602073 to your computer and use it in GitHub Desktop.
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
#!/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>- ' ],
["&nbsp;", ' ' ],
[/\<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