Skip to content

Instantly share code, notes, and snippets.

@yadomi
Created May 17, 2015 15:43
Show Gist options
  • Save yadomi/d3bf528daade95473642 to your computer and use it in GitHub Desktop.
Save yadomi/d3bf528daade95473642 to your computer and use it in GitHub Desktop.
Script to migrate articles from PluXML CMS to Jekyll's markdown
require 'nokogiri'
require 'reverse_markdown'
require 'date'
ARTICLE_PATH='/Users/yadomi/Desktop/data/articles/*.xml'
def title doc
doc.at('title').text
end
def content doc
chapo = ReverseMarkdown.convert doc.at('chapo').text
content = ReverseMarkdown.convert doc.at('content').text
"#{chapo} #{content}"
end
def tags doc
tag = doc.at('tags').text.split(',')[0]
if tag then tag.downcase end
end
def date doc
DateTime.parse(File.basename(doc).split('.')[3])
end
def createFile filename, header, content
puts filename
buffer = File.new(filename, "w")
buffer.puts(header)
buffer.puts(content)
buffer.close
end
def createHeader title, date, tags
date = date.strftime("%Y-%m-%d %H:%M:%S")
%{---
layout: post
title: "#{title}"
date: #{date}
categories: #{tags}
---}
end
Dir.glob(ARTICLE_PATH) do |file|
doc = Nokogiri::XML(File.open(file))
date = Date.parse(File.basename(file).split('.')[3][0...8])
title = title(doc).downcase.gsub(' ', '-').gsub(/[^a-zA-Z0-9\-]/,'').squeeze('-').chomp('-')
filename = "#{date}-#{title}.markdown"
puts filename
header = createHeader(title(doc), date(file), tags(doc))
createFile(filename, header, content(doc))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment