LibreOffice Writerの文章を保存時にtextlintして校正する

LibreOfficeというオープンソースのオフィススイートがあります.オフィススイートを使いたいときにはこれを使うことが多いです.
今回LibreOffice Writerでtextlintを使って日本語文章を校正したいなと思って少し試してみました.

まずLibreOffice Writerからtextlintを呼び出せないか調べましたが方法が見つかりません.
仕方がないのでリアルタイムプレビューは諦めてLibreOffice Writerの文章をプレーンテキストに変換してtextlintにかけることにしました.

LibreOffice Writerのファイルをプレーンテキストに変換

はじめLibreOffice同梱の soffice コマンドを試しましたがSTDOUTに出力する方法がわかりませんでした.

$ soffice --headless --convert-to "txt:Text (encoded):UTF8" ./test.odt
$ cat test.txt
Github,youtubeとか
食べれますか?

次にPandocを試しました.STDOUTに出力してくれました.こちらのほうがリソースも少なくて済むでしょうしこちらのほうが良さそうです.

$ pandoc test.odt -t plain
Github,youtubeとか

食べれますか?

標準入力からtextlint

Pandocからの出力を直にtextlintに渡してみます.いい感じそうです.


$ pandoc test.odt -t plain | textlint --color --stdin --stdin-filename text.txt

text.txt
  1:1   error  Github, => GitHub,                              prh
  1:7   error  全角のピリオドとカンマは使用しません。            jtf-style/1.2.2.ピリオド(.)とカンマ(,)
  1:8   error  youtube => YouTube                                prh
  1:16  error    文末が"。"で終わっていません。                    ja-technical-writing/ja-no-mixed-period
  3:3   error    ら抜き言葉を使用しています。                      no-dropping-the-ra
  3:7   error    Disallow to use "?"                               ja-technical-writing/no-exclamation-question-mark
  3:7   error  疑問符(?)を使用する場合は「全角」で表記します。  jtf-style/4.2.2.疑問符(?)

✖ 7 problems (7 errors, 0 warnings)
✓ 4 fixable problems.
Try to run: $ textlint --fix [file]

保存したときに実行する

inotifywaitでLibreOfficeのファイルを監視して保存時に自動的にtextlintが走るようにします.
LibreOfficeの上で編集を行い, Ctrl+s で保存するとinotifywaitが反応して後ろの処理が走りプレーンテキストに変換,textlintでチェックまで動くようになりました.


$ inotifywait test.odt && pandoc test.odt -t plain | textlint --color --stdin --stdin-filename text.txt | aha | xsel -b
Setting up watches.
Watches established.
test.odt OPEN

text.txt
  1:1   error  Github, => GitHub,                              prh
  1:7   error  全角のピリオドとカンマは使用しません。            jtf-style/1.2.2.ピリオド(.)とカンマ(,)
  1:8   error  youtube => YouTube                                prh
  1:16  error    文末が"。"で終わっていません。                    ja-technical-writing/ja-no-mixed-period
  3:3   error    ら抜き言葉を使用しています。                      no-dropping-the-ra
  3:7   error    Disallow to use "?"                               ja-technical-writing/no-exclamation-question-mark
  3:7   error  疑問符(?)を使用する場合は「全角」で表記します。  jtf-style/4.2.2.疑問符(?)

✖ 7 problems (7 errors, 0 warnings)
✓ 4 fixable problems.
Try to run: $ textlint --fix [file]

無限ループ

無限ループの中に入れて保存のたびに走るようにしました.

$ while true; do inotifywait test.odt && pandoc test.odt -t plain | textlint --color --stdin --stdin-filename text.txt; done

LibO textlint

shell script化

一応shell scriptにしておきます.

#!/bin/sh

file="$1"
if [ ! -r "$file" ]; then
  echo "file open error."
  exit 1
fi
textfile=$(basename "${file}" .odt)
dir=$(dirname "${file}")
textfile=${dir}/${textfile}.txt

while true
  do
    inotifywait "${file}" && pandoc "${file}" -t plain | textlint --color --stdin --stdin-filename "${textfile}"
done
環境
$ dpkg-query -W libreoffice-writer pandoc
libreoffice-writer      4:24.8.2-2
pandoc  3.1.11.1+ds-2
$ textlint -v
v14.2.1
$ lsb_release -dr
Description:    Debian GNU/Linux trixie/sid
Release:        n/a
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)