Created
July 31, 2014 08:20
-
-
Save reflexing/3184e28a315ed0cc4a1c to your computer and use it in GitHub Desktop.
BASH script to copy only used images
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 script copies all images, needed by SOURCE_XML, to OUTPUT_PREFIX. | |
# Copyright © reflexing@reflexing.ru, MIT License | |
# Used in makefile this way: $(TOOLS_DIR)/copy-used-images.sh $(SOURCE) $(SOURCE)/$(PROJECT_NAME).xml $(realpath $(TOOLS_DIR)) $(OUTPUT_PREFIX) $(CALLOUTS_DIR) $(ADMON_DIR) $(NAVIG_DIR) $(CALLOUT_EXT) $(ADMON_EXT) | |
# Assign vars from arguments. Magic. | |
declare -i i=1 | |
for varname in SOURCE SOURCE_XML TOOLS_DIR OUTPUT_PREFIX CALLOUTS_DIR ADMON_DIR NAVIG_DIR CALLOUT_EXT ADMON_EXT; do | |
eval $varname=${!i} | |
let i++ | |
done | |
declare -r noise="\/ > \/ > Object is a number : " # noise to cut from xmllint's output | |
# Proper determine used images | |
files=$(echo -e 'setrootns\ncat //@fileref' | xmllint --xinclude --shell $SOURCE_XML \ | |
| sed -n 's/.*fileref="\([^"]*\)".*/\1/p' | sort | uniq) | |
mkdir --parents $OUTPUT_PREFIX/images | |
if [[ -n $files ]]; then | |
pushd . && cd $SOURCE && cp --parents --update $files ../$OUTPUT_PREFIX | |
popd | |
fi | |
# Determining quantity of <calloutlist>s proper way | |
declare -i num_calloutlists=$(echo -e 'setrootns\nxpath count(//defaultns:calloutlist)' \ | |
| xmllint --xinclude --shell $SOURCE_XML \ | |
| sed -e "s/$noise//g" \ | |
| cut -f9- -d " ") | |
if [[ $num_calloutlists -gt 0 ]]; then | |
# Getting maximum quantity of <callout>s in each <calloutlist> | |
declare -i max_callouts=0 | |
for i in $(eval echo {1..$num_calloutlists}); do | |
declare -i num_callouts=$(echo -e "setrootns\nxpath count((//defaultns:calloutlist)[$i]/defaultns:callout)" \ | |
| xmllint --xinclude --shell $SOURCE_XML \ | |
| sed -e "s/$noise//g" \ | |
| cut -f9- -d " ") | |
if [[ $num_callouts -gt $max_callouts ]]; then | |
max_callouts=$num_callouts | |
fi | |
done | |
if [[ $max_callouts -gt 0 ]]; then # copy only if there are callouts | |
mkdir --parents $OUTPUT_PREFIX/$CALLOUTS_DIR | |
eval cp --update $TOOLS_DIR/$CALLOUTS_DIR/{1..$max_callouts}.$CALLOUT_EXT \ | |
$OUTPUT_PREFIX/$CALLOUTS_DIR | |
fi | |
fi | |
# Checking if there are particular admonition and copying it | |
declare -i is_admon=0 | |
for admon in important tip note warning caution; do | |
declare -i num_admon=$(echo -e "setrootns\nxpath count(//defaultns:$admon)" \ | |
| xmllint --xinclude --shell $SOURCE_XML \ | |
| sed -e "s/$noise//g" \ | |
| cut -f9- -d " ") | |
if [[ $num_admon -gt 0 ]]; then | |
if (( ! is_admon )); then | |
is_admon=1 | |
mkdir --parents $OUTPUT_PREFIX/$ADMON_DIR | |
fi | |
cp --update $TOOLS_DIR/$ADMON_DIR/$admon.$ADMON_EXT $OUTPUT_PREFIX/$ADMON_DIR | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment