diff --git a/Scripts/Bash/Functions/Svg/svg.sh b/Scripts/Bash/Functions/Svg/svg.sh index 24a6acb..c7978cf 100755 --- a/Scripts/Bash/Functions/Svg/svg.sh +++ b/Scripts/Bash/Functions/Svg/svg.sh @@ -31,12 +31,18 @@ function svg { # Define list of scalable vector graphic files to process using # option value as reference. if [[ -d $OPTIONVAL ]];then - FILES=$(find $OPTIONVAL -regextype posix-egrep -type f -regex '.*/*.svg$') - elif [[ -f $OPTIONVAL ]];then - FILES=$OPTIONVAL + FILES=$(find $OPTIONVAL -regextype posix-egrep -type f -regex "^${REGEX}\.svg$") fi - + + # Check list of files to process. If list of files is empty there + # is nothing to do except to print a message and end script + # execution. + if [[ $FILES == '' ]];then + cli_printMessage "`gettext "There is no file to process."`" + cli_printMessage "$(caller)" 'AsToKnowMoreLine' + fi + # Define command line interface for svg-based actions. - svg_getActions + svg_getActions } diff --git a/Scripts/Bash/Functions/Svg/svg_updateMetadata.sh b/Scripts/Bash/Functions/Svg/svg_updateMetadata.sh index 35b8084..2ce5fd5 100755 --- a/Scripts/Bash/Functions/Svg/svg_updateMetadata.sh +++ b/Scripts/Bash/Functions/Svg/svg_updateMetadata.sh @@ -1,18 +1,10 @@ #!/bin/bash # -# svg_updateMetadata.sh -- This function replaces metadata section -# inside scalable vector graphic (SVG) files with one of pre-defined -# metadata templates available. Use this function to maintain metadata -# information inside repository. -# -# Usage: -# centos-art svg --update-metadata=path/to/dir --filter=filename -# -# In the above usage example `path/to/dir' represents the parent -# directory where scalable vector graphics you want to update metadata -# information are. The `--filter=filename' is optional and if provided -# just the file specificed is affected. Otherwise all files ending in -# `.svg' are massively modified. +# svg_updateMetadata.sh -- This function updates metadata values +# inside scalable vector graphic (SVG) files. First, we ask user to +# provide the information. If user doesn't provide the information, +# centos-art.sh script uses autogenerated values as default ---when +# possible--- taking as reference SVG file path. # # Copyright (C) 2009-2010 Alain Reguera Delgado # @@ -37,88 +29,186 @@ function svg_updateMetadata { - local TITLE='' - local DATE='' - local CREATOR='' - local RIGHTS='' - local PUBLISHER='' - local COVERAGE='' local TEMPLATES='' - local KEYWORDS='' local INSTANCE='' + local COUNT=0 + local NAM='' + local URL='' + local KEYS='' + local -a TITLE + local -a VALUE + local -a PATTERN - # Define absolute path to metadata templates parent directory. - # This is the place where we store metadata template files. - TEMPLATES=~/artwork/trunk/Scripts/Bash/Functions/Svg/Tpl - - # Define metadata template file we want to apply. More than one - # metadata template file may exist, so let choosing which one to - # use. - cli_printMessage "`gettext "Select the metadata template you want to apply:"`" - select TEMPLATE in $(ls $TEMPLATES);do - TEMPLATE=$TEMPLATES/$TEMPLATE - break - done + # Define metadata template file name. + TEMPLATE="/home/centos/artwork/trunk/Scripts/Bash/Functions/Svg/Config/tpl_forMetadata.sed" # Check metadata template file existence. cli_checkFiles $TEMPLATE 'f' - # Define metadata template instance. + # Define file name to metadata template instance. INSTANCE=$(cli_getTemporalFile $TEMPLATE) - # Define svg document date. - DATE=$(date +%Y-%m-%d) + # Define metadata titles using Inkscape 0.46 metadata definition + # as reference. + TITLE[0]="`gettext "Title"`" + TITLE[1]="`gettext "Date"`" + TITLE[2]="`gettext "Creator"`" + TITLE[3]="`gettext "Rights"`" + TITLE[4]="`gettext "Publisher"`" + TITLE[5]="`gettext "Identifier"`" + TITLE[6]="`gettext "Source"`" + TITLE[7]="`gettext "Relation"`" + TITLE[8]="`gettext "Language"`" + TITLE[9]="`gettext "Keywords"`" + TITLE[10]="`gettext "Coverage"`" + TITLE[11]="`gettext "Description"`" + TITLE[12]="`gettext "Contributor"`" + + # Define metadata pattern. These values are used as regular + # expression patterns for user's input further verification. + PATTERN[0]='^([[:alnum:] _-.]+)?$' + PATTERN[1]='^([0-9]{4}-(0[1-9]|1[0-2])-([0-2][1-9]|3[0-1]))?$' + PATTERN[2]=${PATTERN[0]} + PATTERN[3]=${PATTERN[0]} + PATTERN[4]=${PATTERN[0]} + PATTERN[5]='^(https://projects.centos.org/svn/artwork/[[:alnum:]/._-]+)?$' + PATTERN[6]=${PATTERN[5]} + PATTERN[7]=${PATTERN[5]} + PATTERN[8]='^([a-z]{2}(_[A-Z]{2})?)?$' + PATTERN[9]=${PATTERN[0]} + PATTERN[10]=${PATTERN[0]} + PATTERN[11]=${PATTERN[0]} + PATTERN[12]=${PATTERN[0]} + + # Define metadata pattern message. These values are used as output + # message when user's input doesn't match the related pattern. + PATTERN_MSG[0]="`gettext "Try using alphanumeric characters."`" + PATTERN_MSG[1]="`gettext "Try using 'YYYY-MM-DD' date format."`" + PATTERN_MSG[2]=${PATTERN_MSG[0]} + PATTERN_MSG[3]=${PATTERN_MSG[0]} + PATTERN_MSG[4]=${PATTERN_MSG[0]} + PATTERN_MSG[5]="`gettext "Only locations under https://projects.centos.ort/svn/artwork are supported."`" + PATTERN_MSG[6]=${PATTERN_MSG[0]} + PATTERN_MSG[7]=${PATTERN_MSG[0]} + PATTERN_MSG[8]="`gettext "Try using 'LL' or 'LL_CC' locale format."`" + PATTERN_MSG[9]=${PATTERN_MSG[0]} + PATTERN_MSG[10]=${PATTERN_MSG[0]} + PATTERN_MSG[11]=${PATTERN_MSG[0]} + PATTERN_MSG[12]=${PATTERN_MSG[0]} + + # Define metadata common default values. + DEFAULT[1]=$(date +%Y-%m-%d) + DEFAULT[2]="The CentOS Project" + DEFAULT[3]=${DEFAULT[2]} + DEFAULT[4]=${DEFAULT[2]} + DEFAULT[8]=$(cli_getCurrentLocale) + DEFAULT[10]=${DEFAULT[2]} + + # Initialize metadata values using user's input. + cli_printMessage "`gettext "Enter metadata you want to apply:"`" + while [[ $COUNT -ne ${#TITLE[*]} ]];do + + # Request metadata value. + cli_printMessage "${TITLE[$COUNT]}" 'AsRequestLine' + read VALUE[$COUNT] + + # Sanitate metadata values to exclude characters that could + # introduce possible markup malformations to final SVG files. + until [[ ${VALUE[$COUNT]} =~ ${PATTERN[$COUNT]} ]];do + cli_printMessage "${PATTERN_MSG[$COUNT]}" + cli_printMessage "${TITLE[$COUNT]}" 'AsRequestLine' + read VALUE[$COUNT] + done + + # Set default value to metadata empty values. + if [[ ${VALUE[$COUNT]} == '' ]];then + VALUE[$COUNT]=${DEFAULT[$COUNT]} + fi + + # Increase counter. + COUNT=$(($COUNT + 1)) - # Define svg document creator. - CREATOR='CentOS Artwork SIG' - - # Define svg document rights. - RIGHTS=$CREATOR + done - # Define svg document publisher. - PUBLISHER='The CentOS Project' + for FILE in $FILES;do - # Define svg document coverage. - COVERAGE=$PUBLISHER + # Output action message. + cli_printMessage $FILE 'AsUpdatingLine' - for FILE in $FILES;do + # Build title from file path. + NAM=$(basename $FILE) - # Define svg document title. - TITLE=$(basename $FILE) + # Build url from file path. + URL=$(echo $FILE | sed 's!/home/centos!https://projects.centos.org/svn!') - # Define svg document keywords. - KEYWORDS=$(echo $FILE | cut -d/ -f6- | tr '/' '\n') + # Build keywords from file path. Do not include filename, it + # is already on title. + KEYS=$(dirname $FILE | cut -d/ -f6- | tr '/' '\n') - # Redifine keywords using SVG standard format. Note that this + # Build keywords using SVG standard format. Note that this # information is inserted inside metadata template file. The # metadata template file is a replacement set of sed commands # so we need to escape the new line of each line using one # backslash (\). As we are doing this inside bash, it is # required to escape the backslash with another backslash so - # it passes literally. - KEYWORDS=$(\ - for KEY in $KEYWORDS;do - echo " $KEY\\" - done) + # one of them passes literally to metadata template file. + KEYS=$(\ + for KEY in $KEYS;do + echo " $KEY\\" + done) # Create metadata template instance. - sed -r \ - -e "s!=TITLE=!$TITLE!" \ - -e "s!=DATE=!$DATE!" \ - -e "s!=CREATOR=!$CREATOR!" \ - -e "s!=RIGHTS=!$RIGHTS!" \ - -e "s!=PUBLISHER=!$PUBLISHER!" \ - -e "s!=COVERAGE=!$COVERAGE!" \ - -e "/=KEYWORDS=/c\\${KEYWORDS}" \ - $TEMPLATE > $INSTANCE + cp $TEMPLATE $INSTANCE + + # Check metadata template instance. We cannot continue if + # template instance couldn't be created. + cli_checkFiles $INSTANCE 'f' + + # Reset metadata counter. + COUNT=0 + + while [[ $COUNT -ne ${#TITLE[*]} ]];do + + # Redefine file-specific metadata values. + if [[ $COUNT -eq 0 ]];then + VALUE[$COUNT]=$NAM + elif [[ $COUNT -eq 5 ]];then + VALUE[$COUNT]=$URL + elif [[ $COUNT -eq 6 ]];then + VALUE[$COUNT]=$URL + elif [[ $COUNT -eq 7 ]];then + VALUE[$COUNT]=$URL + elif [[ $COUNT -eq 9 ]];then + VALUE[$COUNT]=$KEYS + fi + + # Define translation marker pattern. + PATTERN[$COUNT]=$(echo ${TITLE[$COUNT]} | tr '[[:lower:]]' '[[:upper:]]') + + # Apply translation marker replacement. + if [[ $COUNT -eq 9 ]];then + sed -i -r "/=${PATTERN[$COUNT]}=/c\\${VALUE[$COUNT]}" $INSTANCE + else + sed -i -r "s!=${PATTERN[$COUNT]}=!${VALUE[$COUNT]}!g" $INSTANCE + fi + + # Increase metadata counter. + COUNT=$(($COUNT + 1)) + + done + + # Sanitate metadata template instance. sed -i -r -e 's/>$/>\\/g' $INSTANCE # Apply metadata template instance to scalable vector graphic # file. sed -i -f $INSTANCE $FILE + # Sanitate scalable vector graphic. + sed -i -r '/^[[:space:]]*$/d' $FILE + # Remove metadata template instance. - cli_checkFiles $INSTANCE 'f' + cli_checkFiles "${INSTANCE}" 'f' rm $INSTANCE done \