Skip to content

Instantly share code, notes, and snippets.

@smeech
Last active July 19, 2025 13:50
Show Gist options
  • Save smeech/ad764bc78e0d7a34daa738dde1e0938d to your computer and use it in GitHub Desktop.
Save smeech/ad764bc78e0d7a34daa738dde1e0938d to your computer and use it in GitHub Desktop.
[Fractions] The Espanso package `espanso-fractions` I use routinely, produces the eighteen single character expansions of small fractions. I thought I'd play with a solution for longer fractions using super- and sub-script characters, and came up with the following, which can be triggered by e.g. `:1234/4567 `. #espanso #python #html
- regex: :(?P<before>\d+)/(?P<after>\d+)(?P<end>\s)
replace: "{{output}}{{end}}"
vars:
- name: output
type: script
params:
args:
- python
- -c
- |
sup = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
sub = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
print("{{before}}".translate(sup) + "/" + "{{after}}".translate(sub))
# For expansion into an HTML environment, like Thunderbird, or Google Docs,
# the following is simpler, avoids the need for a script, and produces a
# nicer output:
- regex: ;(?P<before>\d+)/(?P<after>\d+)(?P<end>\s)
html: |
<sup style="vertical-align: top; font-size: 0.6em;">{{before}}</sup>/
<sub style="vertical-align: bottom; font-size: 0.6em;">{{after}}</sub>
<span style="all: unset;">{{end}}</span>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment