Skip to content

Instantly share code, notes, and snippets.

@arika
Created March 28, 2012 13:40
Show Gist options
  • Save arika/2226259 to your computer and use it in GitHub Desktop.
Save arika/2226259 to your computer and use it in GitHub Desktop.
typoからoctopress/jekyllに記事を変換する
# Typoが動作する環境で、rails runnerにより実行する。Type 6.1.0で動作確認した。
# Articleだけ取り出しているが、Pageも似たやり方で取り出せると思う。
# どのように変換するかは運用に合わせて調整すること。
#
# メモ:
# 記事のタイトルからリンクを取り出して本文に置き直しているのは
# もともとtDiaryで運用していたのをTypoに移行したためで、
# うまくないのは分かっていながら放置していたのをここで修正しようとしているため。
# 最初からTypoだった場合や、記事タイトルにリンクを含まない場合は
# このような処理は必要ない。
destdir = '/tmp/_post'
Article.all.each do |a|
cat = a.categories.map(&:display_name).
map {|x| %Q|"#{x}"| }.uniq.join(', ')
tag = a.tags.map {|t| t.display_name.downcase }.
map {|x| %Q|"#{x}"| }.uniq.join(', ')
title = a.title.strip_html
title_links = a.title.scan(%r!<a href="[^"]*">.*?</a>!)
pt = a.published_at.localtime
fn = File.join(destdir, pt.strftime('%Y-%m-%d-') + a.permalink + '.none')
open(fn,"w") do |io|
io.print <<-E
---
layout: post
title: "#{title}"
date: #{pt.strftime('%Y-%m-%d %H:%M')}
categories: [#{cat}]
tags: [#{tag}]
published: #{a.state.published?}
typo_id: #{a.id}
---
E
io.print a.html(:body).chomp + "\n"
if a.extended?
io.print "<!-- more -->\n"
io.print a.html(:extended).chomp + "\n"
end
unless title_links.empty?
io.print "<hr /><p>link: #{title_links.join('<br />')}</p>"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment