LoginSignup
2
3

More than 5 years have passed since last update.

Windows ユーザのための Scrivener 原稿を git diff する方法

Posted at

動機

  • Scrivenerで書いている原稿をGitで管理したい
  • diffも見たい
  • Mac は持っていない

Windows で日本語原稿書きを Scrivener でするとか、無茶するなよという点は置いておく。だってまともなアウトラインエディタがないんだもの。

環境

  • Windows 7 Professional / Windows 10 Home
  • Git for Windows 2.13.2.windows.1
  • Python 2.7

参考

こちらの記事がほぼ答えそのもの。唯一の問題は Windows には textutil はないということだ。

RTF の実体

中身を覗いてみると 7-bit ASCII の構造化文書で、マルチバイト文字はエスケープされている。
RTF 1.2 の仕様書を見る限り、 Scrivener は\uで始まる Unicode コードポイントを使っているようだ。
この程度なら読み込んで Unicode コードポイントにデコードするプログラムを書くのも難しくはなさそうだ。

{\rtf1\ansi\ansicpg1252\uc1\deff0
{\fonttbl{\f0\fmodern\fcharset0\fprq2 CourierNewPSMT;}}
{\colortbl;\red0\green0\blue0;\red255\green255\blue255;}
\paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\fet2\ftnbj\aenddoc
\pgnrestart\pgnstarts0
\pard\plain \fi720\ltrch\loch {\f0\fs24\b0\i0 Scrivener\loch\af0\hich\af0\dbch\af0\uc1\u12398\'3F\u12501\'3F\u12449\'3F\u12452\'3F\u12523\'3F\u24418\'3F\u24335\'3F\u12399\'3F\u12354\'3F\u12426\'3F\u12364\'3F\u12383\'3F\u12356\'3F\u12371\'3F\u12392\'3F\u12395\'3F(\u36066\'3F\u26126\'3F\u12394\'3F\u12371\'3F\u12392\'3F\u12395\'3F)\u12486\'3F\u12461\'3F\u12473\'3F\u12488\'3F\u12505\'3F\u12540\'3F\u12473\'3F\u12398\'3F\u12501\'3F\u12449\'3F\u12452\'3F\u12523\'3F\u38598\'3F\u21512\'3F\u12394\'3F\u12398\'3F\u12391\'3F\u12289\'3FGit\u12391\'3F\u12486\'3F\u12461\'3F\u12473\'3F\u12488\'3F\u30340\'3F\u12395\'3F\u12496\'3F\u12540\'3F\u12472\'3F\u12519\'3F\u12531\'3F\u31649\'3F\u29702\'3F\u12377\'3F\u12427\'3F\u12398\'3F\u12395\'3F\u21521\'3F\u12356\'3F\u12390\'3F\u12356\'3F\u12427\'3F\u12290\'3F}
\par\plain {\f0\fs24\b0\i0 \loch\af0\hich\af0\dbch\af0\uc1\u12375\'3F\u12363\'3F\u12375\'3F\u12394\'3F\u12364\'3F\u12425\'3F\u12289\'3F\u26085\'3F\u26412\'3F\u35486\'3F\u12398\'3F\u12489\'3F\u12461\'3F\u12517\'3F\u12513\'3F\u12531\'3F\u12488\'3F\u12434\'3F\u26360\'3F\u12371\'3F\u12358\'3F\u12392\'3F\u24605\'3F\u12387\'3F\u12383\'3F\u12425\'3F\u12289\'3F\u19968\'3F\u12388\'3F\u12384\'3F\u12369\'3F\u33268\'3F\u21629\'3F\u30340\'3F\u12394\'3F\u27424\'3F\u38501\'3F\u12364\'3F\u12354\'3F\u12427\'3F\u12290\'3F}
\par\plain {\f0\fs24\b0\i0 \loch\af0\hich\af0\dbch\af0\uc1\u20491\'3F\u12293\'3F\u12398\'3F\u25991\'3F\u26360\'3F\u12434\'3F\u34920\'3F\u29694\'3F\u12375\'3F\u12390\'3F\u12356\'3F\u12427\'3F Rich Text Format \u12364\'3F\u21476\'3F\u12377\'3F\u12366\'3F\u12390\'3F\u12289\'3Funicode\u25991\'3F\u23383\'3F\u12434\'3F\u12456\'3F\u12473\'3F\u12465\'3F\u12540\'3F\u12503\'3F\u12471\'3F\u12540\'3F\u12465\'3F\u12531\'3F\u12473\'3F\u12391\'3F\u34920\'3F\u12375\'3F\u12390\'3F\u12356\'3F\u12427\'3F\u12383\'3F\u12417\'3F\u12395\'3F\u12289\'3F\u24046\'3F\u20998\'3F\u12364\'3F\u26085\'3F\u26412\'3F\u35486\'3F\u12391\'3F\u35211\'3F\u12428\'3F\u12394\'3F\u12356\'3F\u12398\'3F\u12384\'3F\u12290\'3F}}

extract_rtf.py の組み込み

と思ったらすでにやっている人がいた(extract_rtf.py)
これをリポジトリに入れて、あとは参考に示した記事を真似て、 .gitattributes を作ってから .git/Config に次のように書き加えればよい。

[diff "richtext"]
  binary = true
  textconv = python extract_rtf.py

あるいはコマンドラインで

echo "*.rtf diff=richtext" >> .gitattributes
git config diff.richtext.binary true
git config diff.richtext.textconv "python extract_rtf.py"

と打っても良い。

extract_rtf.py を Python 2 へダウングレード

上記のスクリプト extract_rtf.py は Python 3 用で、それは結構なことなのだけども、たまたま Python 2.7 を使っていたのでそれ用に書き直した。
Unicode 周りは Python 2 と 3 で大きく変わっているので、両方で同じスクリプトが動くようにするのは断念した。それがこちらのGist

あとは git diff で差分がとれるし、 Git Extensions を使っていればコミットダイアログやログにも差分が出る。
image.png

2
3
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
2
3