Last active
December 24, 2019 05:48
-
-
Save masa-ita/8fd6c9bd3bbaba25ade7b57daaff9cda to your computer and use it in GitHub Desktop.
Compare a translated notebook/md file with the English original in tensorflow/docs using nbdiff-web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This tool is to compare a translated notebook or markdown file to | |
# the original English document in tensorflow/docs. | |
# Check if the nbdiff is available | |
if ! [ -x "$(command -v nbdiff-web)" ]; then | |
echo 'Error: nbdiff-web is not installed. `pip install nbdime` please' >&2 | |
exit 1 | |
fi | |
# Check the number of arguments | |
if [ $# -ne 3 ]; then | |
echo "Error: Invalid arguments" | |
echo "Usage: transdiff <repository name> <branch name> <file to diff>" | |
exit 1 | |
fi | |
GITHUB_ORIGIN="https://github.com/tensorflow/docs" | |
GITHUB_REPOSITORY=${1} | |
GITHUB_REPOSITORY_URL="https://github.com/${GITHUB_REPOSITORY}" | |
BRANCH=${2} | |
FILE_TO_DIFF=${3} | |
TEMP_DIR="ghrepos" | |
# Remove temporary directory | |
rm -rf ${TEMP_DIR} | |
mkdir ${TEMP_DIR} | |
IFS='/' | |
read -ra ADDR <<< "$FILE_TO_DIFF" | |
IFS=' ' | |
arraylength=${#ADDR[@]} | |
origin_url="$GITHUB_ORIGIN/raw/master" | |
for (( i=1; i<${arraylength}+1; i++ )); do | |
if [[ i -eq 2 ]]; then | |
origin_url+="/en" | |
else | |
origin_url+="/${ADDR[$i-1]}" | |
fi | |
done | |
target_url="$GITHUB_REPOSITORY_URL/raw/$BRANCH/$FILE_TO_DIFF" | |
filename=$(basename "$target_url") | |
ext="${filename##*.}" | |
fname="${filename%.*}" | |
origin_file=en_$filename | |
target_file=${ADDR[1]}_$filename | |
echo $origin_file | |
echo $target_file | |
wget -q -O $TEMP_DIR/$origin_file $origin_url | |
wget -q -O $TEMP_DIR/$target_file $target_url | |
if [ $ext == "ipynb" ]; | |
then | |
nbdiff-web $TEMP_DIR/$origin_file $TEMP_DIR/$target_file | |
else | |
diff $TEMP_DIR/$origin_file $TEMP_DIR/$target_file | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment