Skip to content

Instantly share code, notes, and snippets.

@masa-ita
Last active December 24, 2019 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masa-ita/8fd6c9bd3bbaba25ade7b57daaff9cda to your computer and use it in GitHub Desktop.
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
#!/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