From 8fb4e8fcca4cf967dc2a3eae8e8dac19e3a84b92 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 4 Dec 2012 20:45:20 -0600 Subject: [PATCH] Convert slashes in titles to dashes when migrating from WordPress When a post has a title that contains a slash, such as 'This is my cool blog post part 1/2', convert the slash to a dash so that the post filename is created correctly. Fixes issue #680 --- jekyll.gemspec | 3 +++ lib/jekyll/migrators/wordpress.rb | 1 + lib/jekyll/migrators/wordpressdotcom.rb | 6 +++++- test/test_wordpress_migrator.rb | 10 ++++++++++ test/test_wordpressdotcom_migrator.rb | 9 +++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/test_wordpress_migrator.rb create mode 100644 test/test_wordpressdotcom_migrator.rb diff --git a/jekyll.gemspec b/jekyll.gemspec index 55201ae8..22a9a401 100644 --- a/jekyll.gemspec +++ b/jekyll.gemspec @@ -38,6 +38,9 @@ Gem::Specification.new do |s| s.add_development_dependency('RedCloth', "~> 4.2") s.add_development_dependency('rdiscount', "~> 1.6") s.add_development_dependency('redcarpet', "~> 1.9") + s.add_development_dependency('sequel', "~> 3.42") + s.add_development_dependency('htmlentities', "~> 4.3") + s.add_development_dependency('hpricot', "~> 0.8") # = MANIFEST = s.files = %w[ diff --git a/lib/jekyll/migrators/wordpress.rb b/lib/jekyll/migrators/wordpress.rb index d2d19039..61e00ad1 100644 --- a/lib/jekyll/migrators/wordpress.rb +++ b/lib/jekyll/migrators/wordpress.rb @@ -276,6 +276,7 @@ module Jekyll text.gsub!(">", ">") text.gsub!(""", '"') text.gsub!("'", "'") + text.gsub!("/", "/") text end diff --git a/lib/jekyll/migrators/wordpressdotcom.rb b/lib/jekyll/migrators/wordpressdotcom.rb index 701c2af4..e3c594dd 100644 --- a/lib/jekyll/migrators/wordpressdotcom.rb +++ b/lib/jekyll/migrators/wordpressdotcom.rb @@ -19,7 +19,7 @@ module Jekyll permalink_title = item.at('wp:post_name').inner_text # Fallback to "prettified" title if post_name is empty (can happen) if permalink_title == "" - permalink_title = title.downcase.split.join('-') + permalink_title = sluggify(title) end date = Time.parse(item.at('wp:post_date').inner_text) @@ -66,5 +66,9 @@ module Jekyll puts "Imported #{value} #{key}s" end end + + def self.sluggify(title) + title.downcase.split.join('-').gsub('/','-') + end end end diff --git a/test/test_wordpress_migrator.rb b/test/test_wordpress_migrator.rb new file mode 100644 index 00000000..945fbc8b --- /dev/null +++ b/test/test_wordpress_migrator.rb @@ -0,0 +1,10 @@ +require 'helper' +require 'jekyll/migrators/wordpress' +require 'htmlentities' + +class TestWordpressMigrator < Test::Unit::TestCase + should "clean slashes from slugs" do + test_title = "blogs part 1/2" + assert_equal("blogs-part-1-2", Jekyll::WordPress.sluggify(test_title)) + end +end diff --git a/test/test_wordpressdotcom_migrator.rb b/test/test_wordpressdotcom_migrator.rb new file mode 100644 index 00000000..fb38737b --- /dev/null +++ b/test/test_wordpressdotcom_migrator.rb @@ -0,0 +1,9 @@ +require 'helper' +require 'jekyll/migrators/wordpressdotcom' + +class TestWordpressDotComMigrator < Test::Unit::TestCase + should "clean slashes from slugs" do + test_title = "blogs part 1/2" + assert_equal("blogs-part-1-2", Jekyll::WordpressDotCom.sluggify(test_title)) + end +end