LoginSignup
24
22

More than 5 years have passed since last update.

Backlog記法で書かれたWikiをMarkdownのプロジェクトのWikiに全面自動移行するスクリプト

Last updated at Posted at 2014-02-28

経緯

前々からBacklogを使っていてBacklog記法で課題やWikiを編集していましたが、ちょっと前からMarkdownのプロジェクトを新設しそちらでの運用を始めていました。普段の課題はMarkdownで書いてますが、Wikiに関しては過去の資料が大量にあることと2重管理が怖かったので古いBacklog記法のプロジェクトを使い続けていました。

ただそれだとWikiを書くときだけBacklog記法で書く必要性がありめんどうだと思っていて、検索しても、

過去のプロジェクトを含めて新wikiルール(markdown)に移行したい

このページを見てm9(^Д^)プギャーってなるだけだったので、ついカッとなって自動でWikiを移行するスクリプトを書きました。

ソース

各モジュールは以下を担当してます。

  • mechanize
    • Backlog記法のWikiのURL一覧を取得
    • 一覧のURLごとに開いてタイトルとHTMLを取得
    • Markdownを新設プロジェクトのWikiに自動投稿
  • pandoc
    • HTMLをMarkdownへ変換

gistはこちら → https://gist.github.com/kazuph/9263163

backlog2markdown.rb
#!/usr/bin/env ruby
# coding : utf-8

require 'open-uri'
require "mechanize"
require 'pandoc-ruby'
require "awesome_print"
require "pry"
require "date"

class Backlog2Markdown

  def initialize(user_id, password, subdomain, from, to)
    @user_id   = user_id
    @password  = password
    @subdomain = subdomain
    @from      = from
    @to        = to

    @agent                  = Mechanize.new
    @agent.user_agent_alias = "Mac Safari"
    @agent.max_history      = 1

  end

  def login
    page = @agent.get("https://#{@subdomain}.backlog.jp/LoginDisplay.action")
    form = page.forms[0]
    form.field_with(id: "userId").value    = @user_id
    form.field_with(id: "password").value  = @password
    @agent.submit(form, form.buttons.first)
  end

  def wiki_url_list()
    page = @agent.get("https://#{@subdomain}.backlog.jp/wiki/#{@from}/Home")
    wikipagelist = page.at('#wikipagelist')
    @links = wikipagelist.search('a').map{|a|
      a.attributes['href'].value
    }
  end

  def wiki_html(link)
    page  = @agent.get("https://#{@subdomain}.backlog.jp" + link)
    title = page.at('#mainTitle').text.gsub(/(\n|\t)/, '')
    article = page.at('#loom').to_s
    {title: title, article: article}
  end

  def to_markdown(html)
    @converter = PandocRuby.new(html, :from => :html, :to => :markdown)
    md = @converter.convert
  end

  def post_wiki (title, article)
    page = @agent.get("https://#{@subdomain}.backlog.jp/NewWiki.action?projectKey=#{@to}")
    form = page.forms[3]
    form.field_with(id: "page.name").value    = fronted_tag title
    form.field_with(id: "page.content").value = work_around article
    form.checkbox_with(name: 'mailNotify').uncheck
    # binding.pry
    @agent.submit(form, form.buttons.first)
  end

  def fronted_tag title
    title.gsub!(/(.*?)(\[.*\])/, '\2\1')
    title
  end

  def work_around text
    text.gsub!(/^\\$\n/,  '')
    text.gsub!(/!\[\]/,  '![some_image]')
    text
  end

end

if __FILE__ == $0
  i = Backlog2Markdown.new( ENV["USER_ID"], ENV["PASS"], ENV["SUBDOMAIN"], ENV["FROM"], ENV["TO"])
  i.login

  links = i.wiki_url_list

  links.each do |l|
    wiki = i.wiki_html l
    # binding.pry
    md   = i.to_markdown wiki[:article]
    puts wiki[:title]
    i.post_wiki wiki[:title], md
    sleep 3
  end
end
Gemfile
source "https://rubygems.org"

gem "mechanize"
gem 'pandoc-ruby'
gem "awesome_print"
gem "pry"

実行方法

pandocは予めインストールしておいてください。

必要な変数の説明

  • USER_ID: ユーザーID
  • PASS: パスワード
  • SUBDOMAIN: XXXXX.backlog.jpのXXXXXの部分
  • FROM: Backlog記法のプロジェクトのID
  • TO: Markdown記法のプロジェクトのID

環境変数使うの面倒臭かったらハードコーディングしてください。

実行

bundle install
USER_ID=user_id PASS=password SUBDOMAIN=your_backlog_subdomain  FROM=backlog_prj  TO=markdown_prj bundle exec ruby backlog2markdown.rb

いきなりやるのは怖いと思うのでbinding.pryを挟んで徐々に値確認しつつ実行するといいと思います。

あとBacklogがViewを変えるとすぐに動かなくなると思いますがその辺はご了承ください。

TODO

  • テーブル記法が崩れる
  • ソースにコメントがない(読めばわかると思いますが)
24
22
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
24
22