-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggtex.py
96 lines (69 loc) · 2.1 KB
/
ggtex.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Converts modified LaTeX math-mode code to Geogebra code.
See http://github.com/fnaufel/ggtex for syntax and more info.
Usage:
ggtex.py FILE
ggtex.py -h | --help
ggtex.py -v | --version
Arguments:
FILE File containing code to convert to Geogebra
Options:
-h, --help Show this screen.
-v, --version Show version.
"""
from docopt import docopt
import sys
import re
versionStr = 'ggtex v0.2\thttp://github.com/fnaufel/ggtex'
def ler(fonte):
try:
f = open(fonte)
conteudo = f.read()
f.close()
return conteudo
except IOError:
print('Error reading ' + fonte + '.')
print('Exiting.')
sys.exit(1)
def processar(conteudo):
# Delete \[ and \[, if present, along with whitespace around them
openmath = re.compile(r'^\s*\\\[\s*')
conteudo = re.sub(openmath, '', conteudo)
closemath = re.compile(r'\\\]\s*')
conteudo = re.sub(closemath, '', conteudo)
# Add call to FormulaText and Simplify at the beginning
conteudo = re.sub('^', r'FormulaText(Simplify(\n"', conteudo)
# Add ")) to the end
conteudo = conteudo.strip()
conteudo = re.sub(r'$', r'"\n))\n', conteudo)
# Add " + before and add + " after Geogebra expressions escaped with @
ggcode = re.compile(r'@([^@]+)@')
conteudo = re.sub(ggcode, r'" + \1 + "', conteudo)
# Delete possible "" + at the beginning
conteudo = re.sub(r'"" \+\s*', '', conteudo)
# Delete possible + "" at the end
conteudo = re.sub(r'\s*\+ ""', '', conteudo)
return conteudo
def escrever(traducao, fonte):
novo = fonte + '.ggtex'
try:
f = open(novo, 'w')
f.write(traducao)
f.close()
except IOError:
print('Error writing ' + fonte + '.')
print('Exiting.')
sys.exit(1)
def ggtex(fonte):
conteudo = ler(fonte)
traducao = processar(conteudo)
escrever(traducao, fonte)
if __name__ == '__main__':
arguments = docopt(
__doc__,
version = versionStr
)
fonte = arguments['FILE']
ggtex(fonte)