Skip to content

Instantly share code, notes, and snippets.

@PeteMichaud
Created September 7, 2013 17:23
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 PeteMichaud/6477449 to your computer and use it in GitHub Desktop.
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...
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