summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/braille/grade1.py
blob: 925c2514e8d4a201341937bed04ea4704fc82157 (plain)
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
"""
Converts ASCII text to Grade 1 Braille as described in
    braille.sty, summary.tex
Essentially, it marks up 
    {Letter}, {Number}, 
    {``}...{''} for double quotation which must be typed as ``...'',
    {.`}...{'.} for single quotation which must be typed as .`...'., 
    {percent}
so that the text is acceptable as input to \braille{}.  The output is
striped and returned all in one line.

Usage:
>>> grade1.convert("I like computer")
"I like computer"
>>> grade1.convert("April 1999")
"April {Number}1999"
>>> grade1.convert("I said ``hello'', and she said ``goodbye''.")
"I said {``}hello{''}, and she said {``}goodbye{''}."

Usage:
    python grade1.py < text > tags
"""
import string

Number = '0123456789'

def convert(line):
    line = string.replace(line, "``", "{``}")	# ``...''
    line = string.replace(line, "''", "{''}")
    line = string.replace(line, ".`", "{.`}")	# .`...'.
    line = string.replace(line, "'.", "{'.}")
    line = string.replace(line, '%', '{percent}')	# %
    s, oldi, oldii, skipchars = '', ' ', ' ', 0
    for i in line:
	if i == '{':
	    skipchars = 1
	elif i == '}':
	    skipchars = 0
	elif skipchars:		# skip anything inside {...}
	    pass
	elif oldi in Number and i in 'abcdefghij':
	    s = s + '{Letter}'
	elif i in Number:
	    if (oldii in Number and oldi in '.-') or (oldi in Number):
		pass
	    else:
		s = s + '{Number}'
	s, oldi, oldii = s + i, i, oldi
    return string.join(string.split(s))		# return all in one line


if __name__ == '__main__':
    import sys
    line = sys.stdin.read()	# swallow the whole thing 
    print convert(line)		# may produce extra \n