chriswarbo-net: 6646eb0325450f827b87804d5229d5c24aa9dabb

     1: #!/usr/bin/env bash
     2: set -eu
     3: 
     4: # Rendering defaults
     5: NEG=bar
     6: CONTEXT=inline
     7: SEM=1
     8: 
     9: # Allow defaults to be overridden by arguments
    10: for ARG in "$@"
    11: do
    12:     case "$ARG" in
    13:          minus)     NEG=minus  ;;
    14:            bar)     NEG=bar    ;;
    15:          block) CONTEXT=block  ;;
    16:         inline) CONTEXT=inline ;;
    17:          nosem)     SEM=0      ;;
    18:            sem)     SEM=1      ;;
    19:     esac
    20: done
    21: 
    22: # Capture the markup we've been given on stdin
    23: GIVEN=$(cat)
    24: 
    25: # All MathML must occur in a math element with this namespace
    26: wrap() {
    27:     echo '<math xmlns="http://www.w3.org/1998/Math/MathML">'
    28:     cat
    29:     echo '</math>'
    30: }
    31: 
    32: # Format negatives: defaults to using overbars, but may be overridden by giving
    33: # this script the argument 'minus'
    34: negs() {
    35:     if [[ "$NEG" = 'bar' ]]
    36:     then
    37:         # Use custom stylesheet to transform unary_minus into overbar
    38:         xsltproc "$BARS" -
    39:     else
    40:         # Leave unary_minus as-is, so CTOP stylesheet will render it "normally"
    41:         cat
    42:     fi
    43: }
    44: 
    45: # Convert Content MathML to Presentation MathML
    46: format() {
    47:     # The $OURS stylesheet lets us tweak the presentation unconditionally. We
    48:     # apply it twice, so it can preprocess the Content MathML and postprocess
    49:     # Presentation MathML. The $CTOP stylesheet comes from the W3C examples, and
    50:     # converts C(ontent MathML) to P(resentation MathML).
    51:     wrap | xsltproc "$OURS" - | negs | xsltproc "$CTOP" - | xsltproc "$OURS" -
    52: }
    53: 
    54: # Makes Presentation MathML more browser-compatible
    55: fixup() {
    56:     sed -e 's@<?xml[^>]*?>@@g' \
    57:         -e 's@<m:@<@g' \
    58:         -e 's@</m:@</@g' \
    59:         -e 's@xmlns:m=.http://www.w3.org/1998/Math/MathML.@@g' | tr -d '\n'
    60: }
    61: 
    62: # Adds the original (untransformed) markup as a semantic annotation
    63: semantics() {
    64:     if [[ "$SEM" -eq 0 ]]
    65:     then
    66:         # Don't add semantics, just stick with the presentation markup
    67:         cat
    68:     else
    69:         # Insert an opening <semantics> tag after the opening <math> tag, and
    70:         # (temporarily) remove the closing </math> tag
    71:         sed -e 's@\(<math[^>]*>\)@\1<semantics>@g' \
    72:             -e 's@</math>@@g'
    73: 
    74:         # Add the given markup (assumed to be Content MathML) as an annotation
    75:         printf '<annotation-xml encoding="MathML-Content">%s</annotation-xml>' \
    76:                "$GIVEN"
    77: 
    78:         # Close everything off
    79:         printf '</semantics></math>'
    80:     fi
    81: }
    82: 
    83: echo "BEGIN math.sh" 1>&2
    84: 
    85: MATHS=$(echo "$GIVEN" | format | fixup | semantics)
    86: echo "MATHS='$MATHS'" 1>&2
    87: 
    88: # Convert to Pandoc JSON. We can't just pipe it into Pandoc, since that will try
    89: # to convert the MathML into bits of LaTeX which aren't adequate. Instead, we
    90: # give Pandoc some raw inline HTML (a </span> tag), then swap that in the output
    91: # MathML.
    92: if [[ "$CONTEXT" = 'block' ]]
    93: then
    94:     JQ='.blocks[0].c[1] |= $maths'
    95:     DUMMY='</div>'
    96: else
    97:     JQ='.blocks[0].c[0].c[1] |= $maths'
    98:     DUMMY='</span>'
    99: fi
   100: 
   101: echo "$DUMMY" | pandoc -f html+raw_html -t json |
   102:     jq --arg maths "$MATHS" "$JQ" | tee >(cat 1>&2)
   103: echo "END math.sh" 1>&2

Generated by git2html.