examples/2xml.sh
branchv_0
changeset 16 fd47709ed17c
equal deleted inserted replaced
15:917b8fbd29b9 16:fd47709ed17c
       
     1 #!/bin/bash
       
     2 
       
     3 # Relational pipes
       
     4 # Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
       
     5 #
       
     6 # This program is free software: you can redistribute it and/or modify
       
     7 # it under the terms of the GNU General Public License as published by
       
     8 # the Free Software Foundation, version 3 of the License.
       
     9 #
       
    10 # This program is distributed in the hope that it will be useful,
       
    11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13 # GNU General Public License for more details.
       
    14 #
       
    15 # You should have received a copy of the GNU General Public License
       
    16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17 
       
    18 
       
    19 # This script reads data in an alternative format (INI, MIME, YAML etc.)
       
    20 # and converts it to XML using a relpipe-in-*table command.
       
    21 # 
       
    22 # This script should not be called directly.
       
    23 # Rather create a symlink like ini2xml for particular formats.
       
    24 # The input format is determined by the symlink name.
       
    25 # 
       
    26 # Optional features:
       
    27 #  - XML indentation: needs the xmllint command
       
    28 #  - XML syntax highlighting: needs the pygmentize command
       
    29 
       
    30 set -o pipefail
       
    31 
       
    32 indentXML() {
       
    33 	if type "xmllint" &> /dev/null; then
       
    34 		xmllint --format -
       
    35 	else
       
    36 		cat
       
    37 	fi
       
    38 }
       
    39 
       
    40 colorizeXML() {
       
    41 	if [ ! -t 1 ]; then
       
    42 		cat
       
    43 	elif type "pygmentize" &> /dev/null; then
       
    44 		pygmentize -l xml
       
    45 	else
       
    46 		# echo "Missing optional command „pygmentize“ needed for XML syntax highlighting." 1>&2
       
    47 		cat
       
    48 	fi
       
    49 }
       
    50 
       
    51 baseName=$(basename "$0");
       
    52 
       
    53 if [[ "$baseName" =~ ^(.+)2xml$ ]]; then
       
    54 	inputFormat=${BASH_REMATCH[1]};
       
    55 	relpipeCommand="relpipe-in-${inputFormat}table";
       
    56 	
       
    57 	if type "$relpipeCommand" &> /dev/null; then
       
    58 		"$relpipeCommand" \
       
    59 			"${@}" \
       
    60 			--relation "2xml" \
       
    61 				--records "/" \
       
    62 				--attribute "xml" string "." --mode raw-xml \
       
    63 			| relpipe-out-nullbyte | tr -d \\0 | indentXML | colorizeXML
       
    64 	else
       
    65 		echo "Unable to find „$relpipeCommand“ command needed for conversion of „$inputFormat“ format to XML.";
       
    66 	fi
       
    67 else
       
    68 	echo "Usage: Do not use this script directly. Create a symling (e.g. ini2xml) to this file.";
       
    69 fi