Skip to content

Instantly share code, notes, and snippets.

@codebrane
Last active December 18, 2015 05:39
Show Gist options
  • Save codebrane/5734266 to your computer and use it in GitHub Desktop.
Save codebrane/5734266 to your computer and use it in GitHub Desktop.
Change wordpress post links with octocpress post links
require 'time'
SLUG_TITLE = 0
LINK = 1
DATE = 2
FIND_WP_POST_LINKS = 'grep "http://codebrane.com/blog/?p=" /Users/alistair/dev/codebrane/codebrane.com/codebrane.com.octopress/source/_posts/*'
FIND_WP_POST_IN_EXPORT = 'grep -C 1 "<link>http://codebrane.com/blog/?p=__POST_ID__</link>" ../codebrane.com.wp.export/codebraneblog.wordpress.2013-06-23.xml'
FAR = "find /Users/alistair/dev/codebrane/codebrane.com/codebrane.com.octopress/source/_posts/ -type f | xargs perl -pi -e 's/http:\\/\\/codebrane.com\\/blog\\/\\?p=__WP_POST_ID__\"/__OCTOPRESS_LINK__\"/g'"
# Find all Wordpress style post links in the Octopress posts
wp_post_links_output = IO.popen(FIND_WP_POST_LINKS)
while (line = wp_post_links_output.gets)
# pull the post id from the link:
# (http://codebrane.com/blog/?p=1499) or (http://codebrane.com/blog/?p=1499 )
# line.strip.scan(/(http:\/\/codebrane.com\/blog\/\?p=[0-9]+ ?\))/) {|match|
line.strip.scan(/(http:\/\/codebrane.com\/blog\/\?p=[0-9]+ ?)/) {|match|
# post_id = match[0].gsub(/.*p=([0-9]+) ?\).*/, '\1')
post_id = match[0].gsub(/.*p=([0-9]+) ?.*/, '\1')
# find the Wordpress post link in the original Wordpress export and
# take the line on either side of it:
# <title>Coffee break Shibboleth IdP install with custom certificate, login page and LDAP</title>
# <link>http://codebrane.com/blog/?p=2899</link> <-- match this line
# <pubDate>Thu, 07 Feb 2013 09:05:08 +0000</pubDate>
command = FIND_WP_POST_IN_EXPORT.gsub(/__POST_ID__/, post_id)
output = IO.popen(command)
lines = Array.new
match_count = 0
while (wp_line = output.gets)
# remove the xml elements
lines[lines.length] = wp_line.strip.gsub(/<.*>(.*)<\/.*>/, '\1')
end
# convert the post title to a slug
lines[SLUG_TITLE] = lines[SLUG_TITLE].downcase.strip.gsub(/:/, '').gsub(/ +/, '-').gsub(/[^\w-]/, '')
date = Time.parse(lines[DATE])
slug_date = date.strftime("%Y/%m/%d")
octopress_post_link = "http://codebrane.com/blog/#{slug_date}/#{lines[SLUG_TITLE]}"
# replace the Wordpress post link in the Octopress posts with the Octoporess post link
puts "replacing #{post_id} with #{octopress_post_link}"
system(FAR.gsub(/__WP_POST_ID__/, post_id).gsub(/__OCTOPRESS_LINK__/, octopress_post_link.gsub(/\//, '\\/')))
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment