forked from nvaccess/nvda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyCommandsDoc.py
301 lines (248 loc) · 10.5 KB
/
keyCommandsDoc.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: UTF-8 -*-
# A part of NonVisual Desktop Access (NVDA)
# Copyright (C) 2023-2024 NV Access Limited, Mesar Hameed, Takuya Nishimoto
# This file is covered by the GNU General Public License.
# See the file COPYING for more details.
"""
TODO: move to site_scons/site_tools
Generates the Key Commands document from the User Guide.
Works as a Python Markdown Extension:
https://python-markdown.github.io/extensions/
TODO move docs to docs folder
Generation of the Key Commands document requires certain commands to be included in the user guide.
These commands must begin at the start of the line and take the form::
%kc:command: arg
The kc:title command must appear first and specifies the title of the Key Commands document.
For example:
%kc:title: NVDA Key Commands
The rest of these commands are used to include key commands into the document.
Appropriate headings from the User Guide will be included implicitly.
The kc:beginInclude command begins a block of text which should be included verbatim.
The block ends at the kc:endInclude command.
For example::
%kc:beginInclude
|| Name | Desktop command | Laptop command | Description |
...
%kc:endInclude
The kc:settingsSection command indicates the beginning of a section documenting individual settings.
It specifies the header row for a table summarising the settings indicated by the kc:setting command
(see below).
In order, it must consist of a name column, a column for each keyboard layout and a description column.
For example::
%kc:settingsSection: || Name | Desktop command | Laptop command | Description |
The kc:setting command indicates a section for an individual setting.
It must be followed by:
* A heading containing the name of the setting;
* A table row for each keyboard layout, or if the key is common to all layouts,
a single line of text specifying the key after a colon;
* A blank line; and
* A line describing the setting.
For example:
%kc:setting
==== Braille Tethered To ====
| Desktop command | NVDA+control+t |
| Laptop Command | NVDA+control+t |
This option allows you to choose whether the braille display will follow the system focus,
or whether it follows the navigator object / review cursor.
"""
from enum import auto, Enum, IntEnum, StrEnum
import re
from collections.abc import Iterator
from markdown import Extension, Markdown
from markdown.preprocessors import Preprocessor
LINE_END = "\r\n"
class Section(IntEnum):
"""Sections must be nested in this order."""
HEADER = auto()
BODY = auto()
class Command(StrEnum):
TITLE = "title"
BEGIN_INCLUDE = "beginInclude"
END_INCLUDE = "endInclude"
SETTING = "setting"
SETTINGS_SECTION = "settingsSection"
def t2tRegex(self) -> re.Pattern:
return re.compile(rf"%kc:({self.value}.*)")
class Regex(Enum):
COMMAND = re.compile(r"^<!-- KC:(?P<cmd>[^:\s]+)(?:: (?P<arg>.*))? -->$")
HEADING = re.compile(r"^(?P<id>#+)(?P<txt>.*)$")
SETTING_SINGLE_KEY = re.compile(r"^[^|]+?[::]\s*(.+?)\s*$")
TABLE_ROW = re.compile(r"^(\|.*\|)$")
class KeyCommandsError(Exception):
"""Raised due to an error encountered in the User Guide related to generation of the Key Commands document.
"""
class KeyCommandsExtension(Extension):
# Magic number, priorities are not well documented.
# It's unclear what the range of priorities are to compare to, but 25 seems to work, 1 doesn't.
# See https://python-markdown.github.io/extensions/api/#registries
PRIORITY = 25
def extendMarkdown(self, md: Markdown):
md.preprocessors.register(KeyCommandsPreprocessor(md), 'key_commands', self.PRIORITY)
class KeyCommandsPreprocessor(Preprocessor):
def __init__(self, md: Markdown | None):
super().__init__(md)
self.initialize()
def initialize(self):
self._ugLines: Iterator[str] = iter(())
self._kcLines: list[str] = []
#: The current section of the key commands file.
self._kcSect: Section = Section.HEADER
#: The current stack of headings.
self._headings: list[re.Match] = []
#: The 0 based level of the last heading in L{_headings} written to the key commands file.
self._kcLastHeadingLevel: int = -1
#: Whether lines which aren't commands should be written to the key commands file as is.
self._kcInclude: bool = False
#: The header row for settings sections.
self._settingsHeaderRow: str | None = None
#: The number of layouts for settings in a settings section.
self._settingsNumLayouts: int = 0
#: The current line number being processed, used to present location of syntax errors
self._lineNum: int = 0
# We want to skip the title line to replace it with the KC:TITLE command argument.
self._skippedTitle = False
def run(self, lines: list[str]) -> list[str]:
# Turn this into an iterator so we can use next() to seek through lines.
self._ugLines = iter(lines)
for line in self._ugLines:
line = line.strip()
self._lineNum += 1
# We want to skip the title line to replace it with the KC:TITLE command argument.
if line.startswith("# ") and not self._skippedTitle:
self._skippedTitle = True
continue
m = Regex.COMMAND.value.match(line)
if m:
self._command(**m.groupdict())
continue
m = Regex.HEADING.value.match(line)
if m:
self._heading(m)
continue
if self._kcInclude:
self._kcLines.append(line)
return self._kcLines.copy()
def _command(self, cmd: Command | None = None, arg: str | None = None):
# Handle header commands.
if cmd == Command.TITLE.value:
if self._kcSect > Section.HEADER:
raise KeyCommandsError(f"{self._lineNum}, title command is not valid here")
# Write the title and two blank lines to complete the txt2tags header section.
self._kcLines.append("# " + arg + LINE_END * 2)
self._kcSect = Section.BODY
return
elif self._kcSect == Section.HEADER:
raise KeyCommandsError(f"{self._lineNum}, title must be the first command")
if cmd == Command.BEGIN_INCLUDE.value:
self._writeHeadings()
self._kcInclude = True
elif cmd == Command.END_INCLUDE.value:
self._kcInclude = False
self._kcLines.append("")
elif cmd == Command.SETTINGS_SECTION.value:
# The argument is the table header row for the settings section.
# Replace t2t header syntax with markdown syntax.
self._settingsHeaderRow = arg.replace("||", "|")
# There are name and description columns.
# Each of the remaining columns provides keystrokes for one layout.
# There's one less delimiter than there are columns, hence subtracting 1 instead of 2.
self._settingsNumLayouts = arg.strip("|").count("|") - 1
if self._settingsNumLayouts < 1:
raise KeyCommandsError(
f"{self._lineNum}, settingsSection command must specify the header row for a table"
" summarising the settings"
)
elif cmd == Command.SETTING.value:
self._handleSetting()
else:
raise KeyCommandsError(f"{self._lineNum}, Invalid command {cmd}")
def _seekNonEmptyLine(self) -> str:
"""Seeks to the next non-empty line in the user guide.
"""
line = next(self._ugLines).strip()
self._lineNum += 1
while not line:
try:
line = next(self._ugLines).strip()
except StopIteration:
return line
self._lineNum += 1
return line
def _areHeadingsPending(self) -> bool:
return self._kcLastHeadingLevel < len(self._headings) - 1
def _writeHeadings(self):
level = self._kcLastHeadingLevel + 1
# Only write headings we haven't yet written.
for level, heading in enumerate(self._headings[level:], level):
self._kcLines.append(heading.group(0))
self._kcLastHeadingLevel = level
def _heading(self, m: re.Match):
# We work with 0 based heading levels.
level = len(m.group("id")) - 1
try:
del self._headings[level:]
except IndexError:
pass
self._headings.append(m)
self._kcLastHeadingLevel = min(self._kcLastHeadingLevel, level - 1)
def _handleSetting(self):
if not self._settingsHeaderRow:
raise KeyCommandsError("%d, setting command cannot be used before settingsSection command" % self._lineNum)
if self._areHeadingsPending():
# There are new headings to write.
# If there was a previous settings table, it ends here, so write a blank line.
self._kcLines.append("")
self._writeHeadings()
# New headings were written, so we need to output the header row.
self._kcLines.append(self._settingsHeaderRow)
numCols = self._settingsNumLayouts + 2 # name + description + layouts
self._kcLines.append("|" + "---|" * numCols)
# The next line should be a heading which is the name of the setting.
line = self._seekNonEmptyLine()
m = Regex.HEADING.value.match(line)
if not m:
raise KeyCommandsError(f"{self._lineNum}, setting command must be followed by heading")
name = m.group("txt")
# The next few lines should be table rows for each layout.
# Alternatively, if the key is common to all layouts, there will be a single line of text specifying the key after a colon.
keys: list[str] = []
for _layout in range(self._settingsNumLayouts):
line = self._seekNonEmptyLine()
m = Regex.SETTING_SINGLE_KEY.value.match(line)
if m:
keys.append(m.group(1))
break
elif not Regex.TABLE_ROW.value.match(line):
raise KeyCommandsError(
f"{self._lineNum}, setting command: "
"There must be one table row for each keyboard layout"
)
# This is a table row.
# The key will be the second column.
try:
key = line.strip("|").split("|")[1].strip()
except IndexError:
raise KeyCommandsError(f"{self._lineNum}, setting command: Key entry not found in table row.")
else:
keys.append(key)
if 1 == len(keys) < self._settingsNumLayouts:
# The key has only been specified once, so it is the same in all layouts.
key = keys[0]
keys[1:] = (key for _layout in range(self._settingsNumLayouts - 1))
# There should now be a blank line.
line = next(self._ugLines).strip()
self._lineNum += 1
if line:
raise KeyCommandsError(
f"{self._lineNum}, setting command: The keyboard shortcuts must be followed by a blank line. "
"Multiple keys must be included in a table. "
f"Erroneous key: {key}"
)
# Finally, the next line should be the description.
desc = self._seekNonEmptyLine()
self._kcLines.append(f"| {name} | {' | '.join(keys)} | {desc} |")
if not self._kcLines[-2].startswith("|"):
# The previous line was not a table, so this is a new table.
# Write the header row.
numCols = len(keys) + 2 # name + description + layouts
self._kcLines.append("|" + "---|" * numCols)