Created
September 7, 2013 17:23
-
-
Save PeteMichaud/6477449 to your computer and use it in GitHub Desktop.
After importing from Wordpress to Jekyll, you get your posts and pages in html format, without any hard line wraps. I wanted to get the files into markdown format and wrap the lines at 120 characters wide so I could edit it nicely in my IDE. I also have like 500 pages to convert, so doing it by hand was a nonstarter...
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
require 'html2markdown' #gem install html2markdown | |
dirs = %w(_pages _posts) | |
class String | |
def word_wrap(line_width = 120) | |
self.split("\n").collect do |line| | |
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line | |
end * "\n" | |
end | |
end | |
def md(file_name) | |
file_name.chomp(File.extname(file_name)) + '.md' | |
end | |
dirs.each do |dir| | |
Dir.open(dir) do |d_handle| | |
d_handle.reject{ |f| f == '.' || f == '..' }.each do |file_name| | |
content = HTMLPage.new(:contents => File.read("#{dir}/#{file_name}")) | |
File.write("#{dir}/#{md(file_name)}", content.markdown.word_wrap) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment