#!/bin/bash
# Relational pipes
# Copyright © 2021 František Kučera (Frantovo.cz, GlobalCode.info)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This script reads data in an alternative format (INI, MIME, YAML etc.)
# and converts it to XML using a relpipe-in-*table command.
#
# This script should not be called directly.
# Rather create a symlink like ini2xml for particular formats.
# The input format is determined by the symlink name.
#
# Optional features:
# - XML indentation: needs the xmllint command
# - XML syntax highlighting: needs the pygmentize command
set -o pipefail
indentXML() {
if type "xmllint" &> /dev/null; then
xmllint --format -
else
cat
fi
}
colorizeXML() {
if [ ! -t 1 ]; then
cat
elif type "pygmentize" &> /dev/null; then
pygmentize -l xml
else
# echo "Missing optional command „pygmentize“ needed for XML syntax highlighting." 1>&2
cat
fi
}
baseName=$(basename "$0");
if [[ "$baseName" =~ ^(.+)2xml$ ]]; then
inputFormat=${BASH_REMATCH[1]};
relpipeCommand="relpipe-in-${inputFormat}table";
if type "$relpipeCommand" &> /dev/null; then
"$relpipeCommand" \
"${@}" \
--relation "2xml" \
--records "/" \
--attribute "xml" string "." --mode raw-xml \
| relpipe-out-nullbyte | tr -d \\0 | indentXML | colorizeXML
else
echo "Unable to find „$relpipeCommand“ command needed for conversion of „$inputFormat“ format to XML.";
fi
else
echo "Usage: Do not use this script directly. Create a symling (e.g. ini2xml) to this file.";
fi