forked from anonfunc/talon-user
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatters.py
60 lines (52 loc) · 1.76 KB
/
formatters.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
from talon.voice import Word, Context, Str
from user.utility import surround, parse_word
formatters = {
# Smashed
"acronym": (True, lambda i, word, _: word[0:1].upper()),
"tree": (True, lambda i, word, _: word[0:3] if i == 0 else ""),
"quad": (True, lambda i, word, _: word[0:4] if i == 0 else ""),
"dunder": (
True,
lambda i, word, last: ("__%s" % word if i == 0 else word)
+ ("__" if last else ""),
),
"camel": (True, lambda i, word, _: word if i == 0 else word.capitalize()),
"snake": (True, lambda i, word, _: word if i == 0 else "_" + word),
"smash": (True, lambda i, word, _: word),
"spine": (True, lambda i, word, _: word if i == 0 else "-" + word),
# Spaced
"sentence": (False, lambda i, word, _: word.capitalize() if i == 0 else word),
"title": (False, lambda i, word, _: word.capitalize()),
"allcaps": (False, lambda i, word, _: word.upper()),
"string": (False, surround("'")),
"quoted": (False, surround('"')),
"padded": (False, surround(" ")),
}
def FormatText(m):
fmt = []
for w in m._words:
if isinstance(w, Word):
fmt.append(w.word)
words = [str(s).lower() for s in m.dgndictation[0]._words]
tmp = []
spaces = True
for i, word in enumerate(words):
word = parse_word(word)
for name in reversed(fmt):
smash, func = formatters[name]
word = func(i, word, i == len(words) - 1)
spaces = spaces and not smash
tmp.append(word)
words = tmp
sep = " "
if not spaces:
sep = ""
Str(sep.join(words))(None)
ctx = Context("formatters")
ctx.keymap(
{
# 'word <dgnwords>': word,
"(%s)+ <dgndictation>"
% (" | ".join(formatters)): FormatText
}
)