File: latex-access.ctb

package info (click to toggle)
brltty 6.8-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,776 kB
  • sloc: ansic: 150,447; java: 13,484; sh: 9,667; xml: 5,702; tcl: 2,634; makefile: 2,328; awk: 713; lisp: 366; python: 321; ml: 301
file content (216 lines) | stat: -rwxr-xr-x 6,231 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2025 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

# BRLTTY Contraction Table - latex-access (executable)

import sys, os

def putProgramMessage (message):
  stream = sys.stderr
  stream.write(os.path.basename(sys.argv[0]) + ": " + message + "\n")
  stream.flush()

def syntaxError (message):
  putProgramMessage(message)
  exit(2)

def semanticError (message):
  putProgramMessage(message)
  exit(3)

def systemError (message):
  putProgramMessage(message)
  exit(4)

def missingPackage (name):
  systemError("package not installed: " + name)

def putResponseProperty (keyword, value):
  value = unicode(value)
  if programArguments.verbose: putProgramMessage("rsp: " + keyword + "=" + value)

  stream = sys.stdout
  stream.write(keyword + "=" + value.encode("UTF-8") + "\n")
  stream.flush()

def getRequest ():
  request = {}

  while True:
    try:
      if programArguments.interactive:
        line = raw_input(packageName + "> ")
      else:
        line = raw_input()
    except EOFError:
      if len(request) == 0: return None
      semanticError("unexpected end-of-file on standard input")

    line = line.decode("UTF-8")
    components = line.split("=", 1)

    if len(components) > 1:
      (keyword, value) = components
      keyword = keyword.strip().lower()

      if programArguments.verbose: putProgramMessage("req: " + keyword + "=" + value)
      request[keyword] = value
      if keyword == "text": break

  return request

def processRequest ():
  request = getRequest()
  if not request: return False

  for attribute in ("displayLength", "cursorOffset", "expandCurrentWord", "consumedChars"):
    if hasattr(brailleTranslator, attribute):
      delattr(brailleTranslator, attribute)

  if request.has_key("maximum-length"):
    brailleTranslator.displayLength = int(request["maximum-length"])

  if request.has_key("cursor-position"):
    position = int(request["cursor-position"])

    if position > 0:
      brailleTranslator.cursorOffset = position - 1

      if request.has_key("expand-current-word"):
        brailleTranslator.expandCurrentWord = int(request["expand-current-word"]) != 0

  brailleTranslator.capitalisation = "6dot"
  if request.has_key("capitalization-mode"):
    if int(request["capitalization-mode"]) != 1:
      brailleTranslator.capitalisation = "8dot"

  text = request["text"]
  brf = brailleTranslator.translate(textPreprocessor.translate(text))

  if hasattr(brailleTranslator, "consumedChars"):
    consumedLength = brailleTranslator.consumedChars
    putResponseProperty("consumed-length", consumedLength)
  else:
    consumedLength = len(text)

  if hasattr(brailleTranslator, "src2trans"):
    offsets = brailleTranslator.src2trans
    if len(offsets) > consumedLength: offsets = offsets[:consumedLength]
    putResponseProperty("output-offsets", ",".join((str(offset) for offset in offsets)))

  putResponseProperty("brf", brf)
  return True

def parseProgramArguments ():
  import argparse
  parser = argparse.ArgumentParser(
    prog = os.path.basename(sys.argv[0]),
    usage = "%(prog)s [option ...]",
    description = """
      This is an executable contraction table for BRLTTY
      that uses the latex-access package
      to translate LaTeX mathematical notation into braille.
      BRLTTY can be found at [http://brltty.app/].
      latex-access can be found at [http://www.latex-access.sourceforge.net/].
    """
  )

  parser.add_argument(
    "-c", "--configuration",
    action = "store",
    nargs = 1,
    dest = "configuration",
    default = (
      os.path.join(os.path.expanduser("~"), "." + packageName),
      "/etc/" + packageName + ".conf"
    ),
    help = "the configuration file to use"
  )

  parser.add_argument(
    "-n", "--nemeth",
    action = "store_const",
    const = "nemeth",
    dest = "translator",
    help = "translate into Nemeth Code"
  )

  parser.add_argument(
    "-u", "--ueb",
    action = "store_const",
    const = "ueb",
    dest = "translator",
    help = "translate into Unified English Braille"
  )

  parser.add_argument(
    "-i", "--interactive",
    action = "store_true",
    dest = "interactive",
    help = "enable input editing and history"
  )

  parser.add_argument(
    "-v", "--verbose",
    action = "store_true",
    dest = "verbose",
    help = "show request and response properties on standard error"
  )

  return parser.parse_args()

if __name__ == "__main__":
  packageName = "latex-access"

  programArguments = parseProgramArguments()

  if programArguments.interactive:
    import readline, atexit
    historyFile=os.path.join(os.path.expanduser("~"), "." + packageName + ".history")
    atexit.register(readline.write_history_file, historyFile)
    readline.read_history_file(historyFile)

  try:
    from latex_access import nemeth, ueb, preprocessor, settings
  except ImportError:
    missingPackage(packageName)

  for configurationFile in programArguments.configuration:
    if os.path.exists(configurationFile):
      if programArguments.verbose:
        putProgramMessage("configuration file: " + configurationFile)

      settings.loadSettings(configurationFile)
      break

  if programArguments.translator:
    settings.settings["brailletable"] = programArguments.translator

  brailleTranslator = settings.brailleTableToUse()
  textPreprocessor = preprocessor.preprocessor()

  settings.activateSettings(
    {
      "braille": brailleTranslator,
      "preprocessor": textPreprocessor
    }
  )

  while processRequest(): pass