forked from brando4526/programming-language-dragonfly-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_css_grammar.py
84 lines (65 loc) · 2.85 KB
/
_css_grammar.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Author:Brandon Lovrien
# This script includes commands used for CSS coding
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule, Choice, Integer, Function)
# Voice command rule for "middle-slash" naming convention.
def middle_slash_format(command): # Callback when command is spoken.
textToPrint = command
someString = str(textToPrint)
printer = Text(someString.replace(' ', '-'))
printer.execute()
class CSSEnabler(CompoundRule):
spec = "Enable CSS" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
cssBootstrap.disable()
cssGrammar.enable()
print "CSS grammar enabled"
class CSSDisabler(CompoundRule):
spec = "switch language" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
cssGrammar.disable()
cssBootstrap.enable()
print "CSS grammar disabled"
class CSSTestRule(CompoundRule):
spec = "test CSS" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "CSS grammar tested"
class CSSSelectors(MappingRule):
mapping = {
"ID selector": Text("#idName")+ Key("enter") + Text("{\n\n}"),
"class selector": Text(".className")+ Key("enter") + Text("{\n\n}"),
"tag selector": Text("tagName")+ Key("enter") + Text("{\n\n}"),
# I know there are more types of selectors in CSS but these are the most common
}
class CSSValues(MappingRule):
mapping = {
"<value> (pixels | pixel)": Text("%(value)dpx"),
"<value> (points | point)": Text("%(value)dpt"),
}
extras = [
Integer("value", 0, 800),
]
class CSSTags(MappingRule):
mapping = {
"property <command>": Function(middle_slash_format) + Text(":;") + Key("left"),
"comment": Text("/* */"),
}
extras = [
Dictation("command"),
]
# Code for initial setup of the HTML grammar
cssBootstrap = Grammar("css bootstrap") # Create a grammar to contain the command rule.
cssBootstrap.add_rule(CSSEnabler())
cssBootstrap.load()
cssGrammar = Grammar("css grammar")
cssGrammar.add_rule(CSSTestRule())
cssGrammar.add_rule(CSSDisabler())
cssGrammar.add_rule(CSSValues())
cssGrammar.add_rule(CSSSelectors())
cssGrammar.add_rule(CSSTags())
cssGrammar.load()
cssGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global cssGrammar
if cssGrammar: cssGrammar.unload()
cssGrammar = None