forked from brando4526/programming-language-dragonfly-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_html_grammar.py
188 lines (169 loc) · 8.16 KB
/
_html_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Author:Brandon Lovrien
# This script includes commands used for HTML coding
from dragonfly import (Grammar, CompoundRule, Dictation, Text, Key, AppContext, MappingRule, Choice)
class HTMLEnabler(CompoundRule):
spec = "Enable HTML" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
htmlBootstrap.disable()
htmlGrammar.enable()
print "HTML grammar enabled"
class HTMLDisabler(CompoundRule):
spec = "switch language" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
htmlGrammar.disable()
htmlBootstrap.enable()
print "HTML grammar disabled"
class HTMLTestRule(CompoundRule):
spec = "test HTML" # Spoken form of command.
def _process_recognition(self, node, extras): # Callback when command is spoken.
print "HTML grammar tested"
class HTMLTags(MappingRule):
mapping = {
"doc type": Text("<!DOCTYPE HTML>"),
"comment": Text( "<!---->" ) + Key( "left" ) + Key( "left" ) + Key( "left" ),
"tags": Text("<>") + Text("</>"),
"single tag": Text("</>"),
"line break": Text( "<br />" ),
"image": Text( "<img />" ),
"equals": Text( "=" ),
"<tagname> tags": Text("<%(tagname)s>") ,#+ Text("</%(tagname)s>"),
# used to specify tag attributes
"attribute": Text( ' attributeName=""' ) + Key( "left" ),
"attribute <attribute>": Text( ' %(attribute)s=""' ) + Key( "left" ),
}
extras = [
Choice("attribute", {
"ID": "id",
"class": "class",
"style": "style",
"title": "title",
"SRC": "src",
"HREF": "href",
}
),
Choice("tagname", {
"anchor": "a",
"abbreviation": "abbr",
"address": "address",
"area": "area",
"article": "article",
"aside": "aside",
"audio": "audio",
"bold": "b",
"base": "base",
"BDI": "bdi",
"BDO": "bdo",
"block quote": "blockquote",
"body": "body",
"button": "button",
"Canvas": "canvas",
# means the same thing and represents a table caption
"caption": "caption", "table caption": "caption",
"cite": "cite",
"code": "code",
"table column": "col",
"table column group": "colgroup",
"command": "command",
"data list": "datalist",
"definition description": "dd",
"del": "del",
"details": "details",
"dfn": "dfn",
"div": "div",
"dl": "dl",
"dt": "dt",
"em": "em",
"embed": "embed",
"field set": "fieldset",
"figure caption": "figcaption",
"figure": "figure",
"footer": "footer",
"form": "form",
"H1": "h1",
"H2": "h2",
"H3": "h3",
"H4": "h4",
"H5": "h5",
"H6": "h6",
"head": "head",
"H group": "hgroup",
"HR": "hr",
"HTML": "html",
"I": "i",
"I frame": "iframe",
"input": "input",
"INS": "ins",
"key gen": "keygen",
"KBD": "kbd",
"label": "label",
"legend": "legend",
"list item": "li",
"Link": "link",
"Mark": "mark",
"menu": "menu",
"meta": "meta",
"meter": "meter",
"nav": "nav",
"no script": "noscript",
"object": "object",
"ordered list": "ol",
"option group": "optgroup",
"option": "option",
"output": "output",
"p": "p",
"parameter": "param",
"pre": "pre",
"progress": "progress",
"g": "g",
"RP": "rp",
"RT": "rt",
"Ruby": "ruby",
"s": "s",
"sample": "samp",
"script": "script",
"section": "section",
"select": "select",
"small": "small",
"source": "source",
"span": "span",
"strong": "strong",
"style": "style",
"sub": "sub",
"summary": "summary",
"superscript": "sup",
"table": "table",
"T body": "tbody",
# means the same thing and represents a table cell
"TD": "td", "table cell": "td",
"textarea": "textarea",
"T foot": "tfoot",
# means the same thing and represents a table header
"TH": "th", "table header": "th",
"T head": "thead",
"time": "time",
"title": "title",
# means the same thing and represents a table row
"table row": "tr", "TR": "tr",
"track": "track",
"unordered list": "ul",
"variable": "var",
"video": "video",
"label": "label",
}
)
]
# Code for initial setup of the HTML grammar
htmlBootstrap = Grammar("html bootstrap") # Create a grammar to contain the command rule.
htmlBootstrap.add_rule(HTMLEnabler())
htmlBootstrap.load()
htmlGrammar = Grammar("html grammar")
htmlGrammar.add_rule(HTMLTestRule())
htmlGrammar.add_rule(HTMLDisabler())
htmlGrammar.add_rule(HTMLTags())
htmlGrammar.load()
htmlGrammar.disable()
# Unload function which will be called by natlink at unload time.
def unload():
global htmlGrammar
if htmlGrammar: htmlGrammar.unload()
htmlGrammar = None