-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-deck-tsv.sh
executable file
·74 lines (54 loc) · 2.22 KB
/
generate-deck-tsv.sh
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/zsh
# Set the locale to UTF-8 to handle Unicode filenames correctly.
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
# Define the data directory and output file names
data_dir="./data"
tsv_file="$data_dir/xiehanzi_offline_data.tsv"
json_dir="$data_dir/json_files"
html_file="$data_dir/front.html"
# URL of the GitHub repository zip.
repo_url="https://github.com/chanind/hanzi-writer-data/archive/refs/heads/master.zip"
# Temporary directory for storing the downloaded zip.
temp_dir="./hanzi_writer_data_temp"
# Download the repository zip file.
echo "Downloading repository..."
curl -L "$repo_url" --output "$temp_dir.zip"
# Extract the downloaded file into the temporary directory using ditto.
echo "Extracting files..."
mkdir -p "$temp_dir" # Ensure the extraction directory exists.
ditto -x -k "$temp_dir.zip" "$temp_dir"
echo "Extraction complete."
# Ensure the data directory, JSON directory, and output directories exist.
mkdir -p "$json_dir"
[[ ! -f "$tsv_file" ]] && touch "$tsv_file"
# Initialize the HTML file
echo "" > "$html_file"
# Find all matching .json files, excluding 'all.json'.
files=($(find "$temp_dir/hanzi-writer-data-master/data" -type f -name "*.json" ! -name "all.json"))
# Count of total files to process.
total_files=${#files}
current_file=0
# Iterate over matched *.json files.
for file in "${files[@]}"; do
# Update file count.
((current_file++))
# Extract filename without path and extension.
filename="${file:t:r}"
# Prepare the line to be added to the TSV.
line="_$filename\t[_$filename.json]\txiehanzi-offline-utility-note"
# Append the line to the output TSV file.
echo -e "$line" >> "$tsv_file"
# Copy the file to the JSON directory with the prefix '_'.
cp "$file" "$json_dir/_$filename.json"
# Add an <img> tag to the HTML file.
echo "<img src=\"_$filename.json\">" >> "$html_file"
# Print progress bar.
percent=$(( 100 * $current_file / $total_files ))
printf "\rProgress: [%-50s] %d%%" $(printf '#%.0s' {1..$((percent / 2))}) $percent
done
# Finalize the HTML file
echo "" >> "$html_file"
echo "\nTSV and HTML files have been created, and JSON files have been copied."
# Clean up the temporary directory.
rm -rf "$temp_dir" "$temp_dir.zip"