1
2

More than 3 years have passed since last update.

wordpressからhugoへの移行(超絶シンプル-簡単編-)

Last updated at Posted at 2021-03-18

きっかけ

CMSの維持管理が大変。Tumblrなんかで独自ドメイン運用も考えたけど、今更htmlでサイト作りたくなかったのパスした。
最終的にgitlab-pagesでhugoを運用することにした。コストは全然掛からない。が!wordpressから記事を移行しなくてはいけない。

方法

wordpressの移行ツールや、xmlのエクスポートは直接触らない。画像などのデータのみエクスポート機能を使った。
おもむろに動いているwordpressのRSSにすべての記事、本文全文を配信するように設定した後、以下のrubyスクリプトを実行する。

#!/bin/ruby

require 'xmlsimple'
require 'open-uri'
require 'reverse_markdown'
require 'date'
require "fileutils"

domain = "XXXX.YYYYY.ZZZ"
rss = "https://" + domain + "/feed"

hash = XmlSimple.xml_in(open(rss))
arr = hash["channel"][0]["item"]

arr.each do |item|

  title = item["title"][0].empty? ? "無題" : item["title"][0]
  date = Time.parse(item["pubDate"][0]).iso8601()
  contents = ReverseMarkdown.convert item["encoded"][0]
  discription = ReverseMarkdown.convert item["description"][0]
  discription = discription.split("...")[0] + "..."

  dir =  item["link"][0].split("/")[3] +"/"+ item["link"][0].split("/")[4]
  name = "/index.md"
  FileUtils.mkdir_p(dir)
  mdfile = "---\n"
  mdfile = mdfile + 'title: "' + title + '"'
  mdfile = mdfile + "\ndate: " + date + "\n"
  mdfile = mdfile + 'description: "' + discription  + '"'
  mdfile = mdfile + "\ndraft: false"
  mdfile = mdfile + "\n---"
  mdfile = mdfile + "\n\n"
  mdfile = mdfile + contents.to_s

  File.open(dir + name, mode = "w"){|f|
    f.write(mdfile)
  }
end

これでカレントディレクトリにURL階層を守ったマークダウンが生成されるはず。
車輪の再発明をするなとか怒られそうだけど、生成中に移行したメッセージ等の追記なんかもできる。

シンプルな機能は大げさな機能は必要なくて、自分が使える言語で書かれたスクリプトが必要なのだ。

必要な人はタグやカテゴリーの記載もできると思います。

また、僕はwordpressでやり取りしたコメントの記載も本文記事に書いてしまって移行しました。

最後に

ブログのURLを維持することは実はめっちゃ簡単なのにそれだけのために金を巻き上げる有料サービスは一体何なの?
インターネットは無料で、自由だ!スキルが有るなら。

大げさなこと言ったけど、gitlab最高。有能すぎる。

移行した最後の記事,シン・エヴァンゲリオン見てきました。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2