summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/braille/grade1.py
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
committerNorbert Preining <norbert@preining.info>2019-09-02 13:46:59 +0900
commite0c6872cf40896c7be36b11dcc744620f10adf1d (patch)
tree60335e10d2f4354b0674ec22d7b53f0f8abee672 /macros/latex/contrib/braille/grade1.py
Initial commit
Diffstat (limited to 'macros/latex/contrib/braille/grade1.py')
-rw-r--r--macros/latex/contrib/braille/grade1.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/macros/latex/contrib/braille/grade1.py b/macros/latex/contrib/braille/grade1.py
new file mode 100644
index 0000000000..925c2514e8
--- /dev/null
+++ b/macros/latex/contrib/braille/grade1.py
@@ -0,0 +1,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