LoginSignup
0
0

More than 3 years have passed since last update.

カクヨムの傍点記法を、小説家になろう向けの中黒ルビに変換するシェルスクリプト

Last updated at Posted at 2019-11-11

とりいそぎコードのみ。

kaku2narou.sh
for textfile in $(grep -lE '《《[^》]+》》' *); do #該当記述を含むファイルを検出
  for hit in $(grep -Eo 《《[^》]+》》 "${textfile}"); do #ファイルごとに該当記述を検出
    phrase=$(echo -n "${hit}" | sed -e 's/^《《//' -e 's/》》$//') #傍点部付きの文字列を検出
    cn=$(echo -n "${phrase}" | wc -cm | grep -Eo '\d+') #文字列の文字数をカウント
    dots=$(printf '%*s' "$cn" | sed -e 's/ /・/g') #中黒を並べる
    gsed -i -e "s/${hit}/|${phrase}${dots}》/" "${textfile}" #置換する
  done
done

上記のコードの場合、《《ぼうてんぶ》》|ぼうてんぶ《・・・・・》のような形に変換する。

ただし、小説家になろうでは10文字を超える連続ルビが使用できない。

そこで、原稿のメンテナンス性を無視して、|ぼ《・》|う《・》|て《・》|ん《・》|ぶ《・》のような形に変換するコードが以下。

kaku2narou.sh
for textfile in $(grep -lE '《《[^》]+》》' *); do
  for hit in $(grep -Eo 《《[^》]+》》 "${textfile}"); do
    phrase=$(echo -n "${hit}" | sed -e 's/^《《//' -e 's/》》$//')
    nphrase=$(echo -n "${phrase}" | gsed -E -e  's/(.)/|\1《・》/g')
    gsed -i -e "s/${hit}/${nphrase}/" "${textfile}"
  done
done

変数がきちゃない。
あと変数名をちゃんと{}でくくらないとバグってびびった。

0
0
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
0
0