Skip to content

Instantly share code, notes, and snippets.

@ThatXliner
Created September 14, 2020 17:53
Show Gist options
  • Save ThatXliner/9b83ea58e34119c845fe163c72b67b8a to your computer and use it in GitHub Desktop.
Save ThatXliner/9b83ea58e34119c845fe163c72b67b8a to your computer and use it in GitHub Desktop.
An interactive transpiler that transpiles from a simpler version of LaTeX Math to LaTeX.
from blessings import Terminal
# import json # Because, in the future, this program's gonna be more advanced.
import re
t = Terminal()
print(
f"""
{'-' * t.width}
Welcome to LaTeXB.py! An easier version of LaTeX that {t.italic}converts{t.normal} to LaTeX!
Currently, {t.bold}it is very expiramental{t.normal}. Meaning, {t.bright_red}some converion results may be
wrong or not implmented{t.normal}.
{t.bold}Right now, LaTeXB.py supports:{t.normal}
- Multiplication
- Basic equality ({t.bright_green}"!="{t.normal} and {t.bright_green}"="{t.normal})
- Fractions
- Divison
- Additon and subtraction
Future features will be worked on. If you need a feature in LaTeX, you can
just write some pure \LaTeX .
{t.bright_red}WARNING: THE FOLLOWING FEATURES ARE NOT IMPLEMNTED YET. DO NOT TRY TO USE.{t.normal}
If you want to set the output setting (either color, typeset,
or extra pre-appending/suff-appending), prefix the line with a {t.bright_green}"@"{t.normal} (without
the quotes, of course) and type in the command
{t.yellow}set the_value_name value_you_want{t.normal}
For a full command reference, type {t.yellow}`@help`{t.normal}.
Happy LaTeXing!
{'-'*t.width}
"""
)
try:
set_values = {"color": "normal"}
output = None # Baisically allocate memory
while True:
the_input = input(f"{t.bright_green}{t.bold}(LaTeXB)> {t.normal}")
if not the_input.lstrip().startswith("@"):
output = the_input
# MULTIPLICATION
output = re.sub(r"\*", r"\\cdot", output)
# EQUALITY
output = re.sub(r"!=", r"\\neq", output)
#output = re.sub(r"=", r"\\eq", output) # Apprently, they are the same
# FRACTIONS
output = re.sub(
r"\\f\s*([\da-zA-Z]+.?[\da-zA-Z]*)\s*\/\s*([\da-zA-Z]+.?[\da-zA-Z]*)",
lambda re_group: r"\frac{"
+ re_group.group(1)
+ "}{"
+ re_group.group(2)
+ "}",
output,
)
# DIVISON
output = re.sub(
r"([\da-z-A-Z]+.?[\da-zA-Z]*)\s*\/\s*([\da-zA-Z]*.?[\da-zA-Z]*)",
lambda re_group: re_group.group(1) + r" \div " + re_group.group(2),
output,
)
else:
print(
f"{t.bold}{t.bright_red}THIS FEATURE ISN'T IMPLEMENTED YET.{t.normal}"
)
continue
# argv = re.split("\s+", the_input.lstrip())
# try:
# set_values[output[1]] = output[2]
# except KeyError:
# print(f"{t.bright_red}{t.bold}ERROR: Operation {'output[0]}' could not be completed")
print(output)
except (KeyboardInterrupt, EOFError):
print("\n Exiting...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment