Skip to content

Commit

Permalink
🎶 [Shell] 見出しレベルをオプションで指定する
Browse files Browse the repository at this point in the history
  • Loading branch information
noranuko13 committed May 9, 2023
1 parent 7c5c264 commit 2caf3f3
Showing 1 changed file with 1 addition and 32 deletions.
33 changes: 1 addition & 32 deletions toc.sh
Expand Up @@ -4,49 +4,18 @@
set -u
#set -x

# 見出しレベル
min=2
max=3

## 見出しレベル入力チェック
if [ $min -le 0 ] || [ $min -gt 6 ]; then
echo "1 <= min <= 6 (min: ${min})"
exit
fi
if [ $max -le 0 ] || [ $max -gt 6 ]; then
echo "1 <= max <= 6 (max: ${max})"
exit
fi

# 置換パターン
patterns=(
"/#はじめに/d"
"/#目次/d"
)

## 見出しレベルによる置換パターンの追加
pattern () {
indent="\s\s"
var=$(for ((i=0; i < $1; i++)); do echo -n $indent; done)
echo "/^${var}\*\s/d"
}
for ((i=0; i < min-1; i++)); do
# shellcheck disable=SC2207
# https://github.com/koalaman/shellcheck/issues/1476
patterns+=($(pattern "$i"))
done
for ((i=max; i < 7; i++)); do
# shellcheck disable=SC2207
# https://github.com/koalaman/shellcheck/issues/1476
patterns+=($(pattern "$i"))
done

# * [アスタリスク]() から - [ハイフン]() への変換
patterns+=("s/\*\s/- /")

# コンソール出力
tmp=$(mktemp)
./gh-md-toc --indent=2 /curriculum-vitae/README.md > "$tmp"
./gh-md-toc --indent=2 --start-depth=1 --depth=3 /curriculum-vitae/README.md > "$tmp"
(for f in "${patterns[@]}" ; do sed -ie "$f" "$tmp"; done)
cat "$tmp"
rm "$tmp"

0 comments on commit 2caf3f3

Please sign in to comment.