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