#!/usr/bin/env python3

# Copyright © 2018-2022 Jakub Wilk <jwilk@jwilk.net>
# SPDX-License-Identifier: MIT

import argparse
import re

class Script(object):

    def __init__(self, s):
        d = self._dict = {}
        plus, minus, *digits = s
        d[ord('+')] = plus
        d[ord('-')] = minus
        for u, digit in enumerate(digits, start=ord('0')):
            d[u] = digit

    def translate(self, s):
        return s.translate(self._dict)

sup = Script('⁺⁻⁰¹²³⁴⁵⁶⁷⁸⁹')
sub = Script('₊₋₀₁₂₃₄₅₆₇₈₉')
misc = {
    '->': '→',
    '=': '⇌',
    '.': '·',
    '*': '·',
}

_regexp = re.compile(r'''
(?<=[])A-Za-z])(?P<sub>[0-9]+) |
(?<=[])A-Za-z0-9])(?P<sup1>\s+[0-9]+[+-]) |
(?<=[])A-Za-z0-9])(?P<sup2>[+-]) |
(?P<misc>->|[=.*])
''', re.VERBOSE)

def translate(formula):
    def subst(match):
        k = match.lastgroup
        s = match.group()
        if k == 'sub':
            return sub.translate(s)
        elif k[:3] == 'sup':
            return sup.translate(s.strip())
        else:
            return misc[s]
    return _regexp.sub(subst, formula)

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('formula', metavar='FORMULA', nargs='+')
    options = ap.parse_args()
    formula = str.join(' ', options.formula)
    print(translate(formula))

if __name__ == '__main__':
    main()

# vim:ts=4 sts=4 sw=4 et
