--- /dev/null
+# Copyright © 2021 Nick Bowler
+# License GPLv3+: GNU General Public License version 3 or any later version.
+# This is free software: you are free to change and redistribute it.
+# There is NO WARRANTY, to the extent permitted by law.
+
+EAPI=7
+
+inherit sgml-catalog-r1
+
+DESCRIPTION="DTD and entity files for the Mathematical Markup Language 2.0"
+HOMEPAGE="http://www.w3.org/TR/MathML2/"
+SRC_URI="http://www.w3.org/Math/DTD/$PN.tgz -> $P.tar.gz"
+
+LICENSE="W3C"
+SLOT="0"
+KEYWORDS="~amd64 ~x86"
+
+BDEPEND="dev-libs/libxml2
+ virtual/awk"
+RDEPEND="!app-text/mathml-xml-dtd"
+DEPEND="$RDEPEND
+ app-text/sgml-common"
+
+S=$WORKDIR/$PN
+
+src_prepare () {
+ rm -f xhtml*.dtd
+ default
+}
+
+src_compile () {
+ cat >"$WORKDIR/gen-catalog.awk" <<-'EOF'
+ BEGIN { print "OVERRIDE YES" }
+ /^[[:space:]]*PUBLIC.*-\/\/W3C\/\// {
+ pubid=$0
+ sub(/^[[:space:]]*/, "", pubid);
+
+ if (getline > 0) {
+ file=substr($1, 2, length($1)-2)
+
+ if (length(file) && getline < file > 0) {
+ print pubid, "\"" file "\""
+ print "SYSTEM", "\"http://www.w3.org/TR/MathML2/dtd/" file "\"", "\"" file "\""
+ print "SYSTEM", "\"http://www.w3.org/Math/DTD/mathml2/" file "\"", "\"" file "\""
+ close(file)
+ }
+ }
+ }
+ EOF
+
+ ( set -x; awk -f "$WORKDIR/gen-catalog.awk" mathml2.dtd >catalog ) || die
+}
+
+src_install () {
+ declare -g pubmap=()
+
+ local insdir=/usr/share/sgml/$PN
+ local a b
+
+ insinto $insdir
+ doins -r catalog math* iso*
+
+ insinto /etc/sgml
+ printf 'CATALOG "%s"\n' "${EPREFIX%/}$insdir/catalog" | newins - $PN.cat
+
+ # Extract public identifiers from SGML catalog
+ exec 3<catalog
+ while read a b <&3; do
+ case $a in
+ PUBLIC)
+ save_IFS=$IFS
+ IFS=\"; set x $b; shift
+ IFS=$save_IFS
+
+ pubmap+=("$2" "${EPREFIX%/}$4")
+ esac
+ done
+ exec 3<&-
+}
+
+update_catalog () {
+ local insdir="file://${EPREFIX%/}/usr/share/sgml/$PN"
+ local xmlcatalog="${BROOT%/}/usr/bin/xmlcatalog"
+ local systemuri=("http://www.w3.org/TR/MathML2/dtd" "http://www.w3.org/Math/DTD/mathml2")
+ local x mode=$1 args=()
+
+ command -v "$xmlcatalog" >/dev/null || die
+
+ set x "${pubmap[@]}"; shift
+ while test $# -gt 0; do
+ case $mode in
+ --add) args+=( --add public "$1" "$insdir/$2" ) ;;
+ --del) args+=( --del "$1" ) ;;
+ esac
+ shift 2
+ done
+
+ for x in "${systemuri[@]}"; do
+ case $mode in
+ --add) args+=( --add rewriteSystem "$x" "$insdir"
+ --add rewriteURI "$x" "$insdir" ) ;;
+ --del) args+=( --del "$x" ) ;;
+ esac
+ done
+
+ xmlcatalog --noout "${args[@]}" "${EROOT%/}/etc/xml/catalog"
+}
+
+pkg_postinst () {
+ sgml-catalog-r1_pkg_postinst
+
+ einfo "Adding $P to global XML catalog"
+ update_catalog --add
+}
+
+pkg_postrm () {
+ sgml-catalog-r1_pkg_postrm
+
+ einfo "Removing $P from global XML catalog"
+ update_catalog --del
+}