-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
65 lines (45 loc) · 2.11 KB
/
main.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
64
65
import re
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
from pylatexenc.latex2text import LatexNodes2Text
import unicodedata
def tex_to_unicode(data):
stripped = data.strip()
if not stripped:
return
if not stripped.startswith("\\"):
stripped = "\\" + stripped
# Remove double backslashes (newlines)
stripped = stripped.replace("\\\\", " ")
# pylatexenc doesn't support \not
stripped = stripped.replace("\\not", "@NOT@")
n = LatexNodes2Text()
return n.latex_to_text(stripped)
class TexToUnicodeExtension(Extension):
def __init__(self):
super().__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
items = []
data = event.get_query()
result = tex_to_unicode(data)
if result:
items.append(ExtensionResultItem(icon='images/icon.png',
name=result,
description='Enter to copy to clipboard',
on_enter=CopyToClipboardAction(result)))
else:
items.append(ExtensionResultItem(icon='images/icon.png',
name="No result",
description="Type some TeX math",
on_enter=DoNothingAction()))
return RenderResultListAction(items)
if __name__ == '__main__':
TexToUnicodeExtension().run()