File size: 1,280 Bytes
d29f16e
 
95dd954
347431d
d29f16e
 
 
 
 
95dd954
 
 
 
 
 
 
 
d3cce16
 
 
 
 
 
 
95dd954
 
 
 
 
 
 
 
 
d29f16e
 
 
d3cce16
d29f16e
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash

# 対象拡張子
extensions="js|mjs|jsx|json|txt|html|htm|json5|md|css"

# 一時ファイル
TMP_RESULT=$(mktemp)

find . -type f -regextype posix-extended -regex ".*\.($extensions)$" | while read -r file; do
    encoding=$(file -bi "$file" | sed -n 's/.*charset=\(.*\)$/\1/p')

    if ! iconv -f "$encoding" -t UTF-8 "$file" > /dev/null 2>&1; then
        continue
    fi

    content=$(iconv -f "$encoding" -t UTF-8 "$file")

    matches=$(echo "$content" | perl -CS -ne '
        while (/([\p{Hiragana}\p{Katakana}\p{Han}a-zA-Z0-9ー]+(?:[\p{Hiragana}\p{Katakana}\p{Han}a-zA-Z0-9ー]+)*)/g) {
            my $word = $1;
            # 日本語を含むものだけ出力
            print "$word\n" if $word =~ /[\p{Hiragana}\p{Katakana}\p{Han}]/;
        }
    ' | sort | uniq)

    if [[ -n "$matches" ]]; then
        echo "\"$file\": {" >> "$TMP_RESULT"
        echo "$matches" | while read -r line; do
            escaped=$(echo "$line" | sed 's/"/\\"/g')
            echo "  \"$escaped\": []," >> "$TMP_RESULT"
        done
        sed -i '$ s/,$//' "$TMP_RESULT"
        echo "}," >> "$TMP_RESULT"
    fi
done

# JSON出力
if [[ -s "$TMP_RESULT" ]]; then
    echo "{"
    sed '$ s/,$//' "$TMP_RESULT"
    echo "}"
else
    echo "{}"
fi

rm "$TMP_RESULT"