forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tex_to_unicode.py
63 lines (46 loc) · 1.69 KB
/
tex_to_unicode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
'''Convert TeX mathmode commands to unicode characters.
Synopsis: <trigger> <tex input>'''
import re
import unicodedata
from pylatexenc.latex2text import LatexNodes2Text
from albertv0 import *
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'TeX to unicode'
__version__ = '1.0'
__trigger__ = 'tex '
__author__ = 'Asger Hautop Drewsen'
__dependencies__ = ['python-pylatexenc']
COMBINING_LONG_SOLIDUS_OVERLAY = '\u0338'
def handleQuery(query):
if not query.isTriggered:
return
item = Item(completion=query.rawString)
stripped = query.string.strip()
success = False
if stripped:
if not stripped.startswith('\\'):
stripped = '\\' + stripped
# Remove double backslashes (newlines)
stripped = stripped.replace('\\\\', ' ')
# pylatexenc doesn't support \not
stripped = stripped.replace('\\not', '@NOT@')
# pylatexenc doesn't like backslashes at end of string
if not stripped.endswith('\\'):
n = LatexNodes2Text()
result = n.latex_to_text(stripped)
if result:
result = unicodedata.normalize('NFC', result)
result = re.sub(r'@NOT@\s*(\S)', '\\1' + COMBINING_LONG_SOLIDUS_OVERLAY, result)
result = result.replace('@NOT@', '')
result = unicodedata.normalize('NFC', result)
item.text = result
item.subtext = 'Result'
success = True
if not success:
item.text = stripped
item.subtext = 'Type some TeX math'
success = False
if success:
item.addAction(ClipAction('Copy result to clipboard', result))
return item