| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
# This script autogenerates `IPython.core.latex_symbols.py`, which contains a |
| 4 |
# single dict , named `latex_symbols`. The keys in this dict are latex symbols, |
| 5 |
# such as `\\alpha` and the values in the dict are the unicode equivalents for |
| 6 |
# those. Most importantly, only unicode symbols that are valid identifiers in |
| 7 |
# Python 3 are included. |
| 8 |
|
| 9 |
# |
| 10 |
# The original mapping of latex symbols to unicode comes from the `latex_symbols.jl` files from Julia. |
| 11 |
|
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
# Import the Julia LaTeX symbols |
| 15 |
print("Importing latex_symbols.jl from Julia...") |
| 16 |
import requests |
| 17 |
|
| 18 |
url = "https://raw.githubusercontent.com/JuliaLang/julia/master/stdlib/REPL/src/latex_symbols.jl" |
| 19 |
r = requests.get(url) |
| 20 |
|
| 21 |
|
| 22 |
# Build a list of key, value pairs |
| 23 |
print("Building a list of (latex, unicode) key-value pairs...") |
| 24 |
lines = r.text.splitlines() |
| 25 |
|
| 26 |
prefixes_line = lines.index('# "font" prefixes') |
| 27 |
symbols_line = lines.index("# manual additions:") |
| 28 |
|
| 29 |
prefix_dict = {} |
| 30 |
for l in lines[prefixes_line + 1 : symbols_line]: |
| 31 |
p = l.split() |
| 32 |
if not p or p[1] == "latex_symbols": |
| 33 |
continue |
| 34 |
prefix_dict[p[1]] = p[3] |
| 35 |
|
| 36 |
idents = [] |
| 37 |
for l in lines[symbols_line:]: |
| 38 |
if "=>" not in l: |
| 39 |
continue # if it's not a def, skip |
| 40 |
if "#" in l: |
| 41 |
l = l[: l.index("#")] # get rid of eol comments |
| 42 |
x, y = l.strip().split("=>") |
| 43 |
if "*" in x: # if a prefix is present substitute it with its value |
| 44 |
p, x = x.split("*") |
| 45 |
x = prefix_dict[p][:-1] + x[1:] |
| 46 |
try: |
| 47 |
x = x.split('"')[1] |
| 48 |
y = y.split('"')[1] # get the values in quotes |
| 49 |
except IndexError as e: |
| 50 |
continue |
| 51 |
raise ValueError((x, y)) from e |
| 52 |
if not x.startswith("\\\\"): |
| 53 |
# reverse mapping |
| 54 |
continue |
| 55 |
idents.append((x, y)) |
| 56 |
|
| 57 |
# Filter out non-valid identifiers |
| 58 |
print("Filtering out characters that are not valid Python 3 identifiers") |
| 59 |
|
| 60 |
|
| 61 |
def test_ident(i): |
| 62 |
"""Is the unicode string valid in a Python 3 identifier.""" |
| 63 |
# Some characters are not valid at the start of a name, but we still want to |
| 64 |
# include them. So prefix with 'a', which is valid at the start. |
| 65 |
return ("a" + i).isidentifier() |
| 66 |
|
| 67 |
|
| 68 |
assert test_ident("α") |
| 69 |
assert not test_ident("‴") |
| 70 |
|
| 71 |
valid_idents = [line for line in idents if test_ident(line[1])] |
| 72 |
|
| 73 |
# Write the `latex_symbols.py` module in the cwd |
| 74 |
|
| 75 |
s = f"""# encoding: utf-8 |
| 76 |
|
| 77 |
# DO NOT EDIT THIS FILE BY HAND. |
| 78 |
|
| 79 |
# To update this file, run the script /tools/gen_latex_symbols.py using Python 3 |
| 80 |
|
| 81 |
# This file is autogenerated from the file: |
| 82 |
# {url} |
| 83 |
# This original list is filtered to remove any unicode characters that are not valid |
| 84 |
# Python identifiers. |
| 85 |
|
| 86 |
latex_symbols = {{ |
| 87 |
""" |
| 88 |
for line in valid_idents: |
| 89 |
s += ' "%s": "%s",\n' % (line[0], line[1]) |
| 90 |
s += "}\n" |
| 91 |
|
| 92 |
s += """ |
| 93 |
|
| 94 |
reverse_latex_symbol = {v: k for k, v in latex_symbols.items()} |
| 95 |
""" |
| 96 |
|
| 97 |
HERE = Path(__file__).parent |
| 98 |
|
| 99 |
fn = HERE / ".." / "IPython" / "core" / "latex_symbols.py" |
| 100 |
print("Writing the file: %s" % str(fn)) |
| 101 |
fn.write_text(s, encoding="utf-8") |