# HG changeset patch # User František Kučera # Date 1637524406 -3600 # Node ID fd47709ed17c4ef4ac03da280d6626125f79b28d # Parent 917b8fbd29b9f8e7ee7dd18b0b6971326acaee52 examples: generic script for converting alternative formats (INI, MIME, YAML, JSON, ASN.1 etc.) to XML diff -r 917b8fbd29b9 -r fd47709ed17c examples/2xml.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/2xml.sh Sun Nov 21 20:53:26 2021 +0100 @@ -0,0 +1,69 @@ +#!/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 . + + +# 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