Browse Source

switch to xmlstarlet xpath tool

main
lhess2021 4 years ago
parent
commit
8d32f670e8
  1. 40
      shell-scripts.d/xpath.sh

40
shell-scripts.d/xpath.sh

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
# An interactive xpath query commandline tool. # An interactive xpath query commandline tool.
# Stores your previous query. # Stores your previous query to reduce typing.
# File for previous query # File for previous query
touch .prev-xpath.tmp touch .prev-xpath.tmp
@ -16,26 +16,28 @@ NC='\033[0m' # no color
# Ask for canto file # Ask for canto file
clear clear
read -p "Choose a canto: [i,r,p][1-34] " CANTO read -p "Choose a canto: [i,r,p][1-34] " CANTO
CANTICLE="$(echo $CANTO | tr -d -c a-z)" canticle="$(echo $CANTO | tr -d -c a-z)"
CANTO_NUM="$(echo $CANTO | tr -d -c 0-9)" canto_num="$(echo $CANTO | tr -d -c 0-9)"
pad_num="$(printf "%03d\n" $canto_num)"
# Create complete filename # Create complete filename
if [[ "$CANTICLE" == 'i' ]]; then if [[ "$canticle" == 'i' ]]; then
CANTICLE='Inferno' canticle='Inferno'
file="inferno-${CANTO_NUM}.xml" file="$pad_num-inferno.xml"
elif [[ "$CANTICLE" == 'r' ]]; then echo $file
CANTICLE='Purgatorio' elif [[ "$canticle" == 'r' ]]; then
file="purgatorio-${CANTO_NUM}.xml" canticle='Purgatorio'
elif [[ "$CANTICLE" == 'p' ]]; then file="$pad_num-purgatorio.xml"
CANTICLE='Paradiso' elif [[ "$canticle" == 'p' ]]; then
file="paradiso-${CANTO_NUM}.xml" canticle='Paradiso'
file="$pad_num-paradiso.xml"
else else
echo "Not a valid canto file: $file" echo "Not a valid canto file: $file"
fi fi
# Start interactive query # Start interactive query
if [[ -e "$file" ]]; then if [[ -e "$file" ]]; then
clear && echo -e "You are searching ${CANTICLE^} ${CANTO_NUM}.\nAdd a trailing slash ${GREEN}\`/\`${NC} to your search before submitting it. The default first two search choices are ${GREEN}\`canto\`${NC} and then ${GREEN}\`terzine\`${NC}. Press ${GREEN}<enter>${NC} for those defaults and type something else to search that.\n" clear && echo -e "You are searching ${canticle^} ${CANTO_NUM}.\nAdd a trailing slash ${GREEN}\`/\`${NC} to your search before submitting it. The default first two search choices are ${GREEN}\`canto\`${NC} and then ${GREEN}\`terzine\`${NC}. Press ${GREEN}<enter>${NC} for those defaults and type something else to search that.\n"
read -p "Load previous: (Y)/n " PREV read -p "Load previous: (Y)/n " PREV
case "$PREV" in case "$PREV" in
N|n) N|n)
@ -52,13 +54,13 @@ if [[ -e "$file" ]]; then
if [[ "${nsearch: -1}" == / ]]; then # if last char trailing slash then search if [[ "${nsearch: -1}" == / ]]; then # if last char trailing slash then search
path="${nsearch::-1}" # remove trailing slash path="${nsearch::-1}" # remove trailing slash
echo "$path" > .prev-xpath.tmp # store query for next time echo "$path" > .prev-xpath.tmp # store query for next time
# perform the search (requires `xmllint` and `batcat` pager) # perform the search (requires `xmlstarlet` and `bat` pager)
xmllint --xpath "$path" $file | batcat -l xml xmlstarlet sel -t -v "$path" $file | batcat -l xml
exit exit
elif [[ "$i" == 0 ]] && [[ "$nsearch" == '' ]]; then # load '/canto' query on first loop elif [[ "$i" == 0 ]] && [[ "$nsearch" == '' ]]; then # load '/TEI/text' query on first loop
search="/canto" search="_:TEI/_:text/_:"
elif [[ "$nsearch" == '/canto' ]]; then # load '/canto/terzine' on second loop elif [[ "$nsearch" == '_:TEI/_:text/_:' ]]; then # load '/TEI/text/canto/tercets' on second loop
search="/canto/terzine" search="_:TEI/_:text/_:canto/_:tercets/_:"
else else
search="${nsearch}" # else keep search the same search="${nsearch}" # else keep search the same
fi fi

Loading…
Cancel
Save