-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathintegrate_v2.py
226 lines (190 loc) · 6.3 KB
/
integrate_v2.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
# !/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Borja Menendez Moreno
Copyright (C) 2013 Ángel Torrado Carvajal
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@author: Borja Menéndez Moreno
@organization: Universidad Rey Juan Carlos
@copyright: Borja Menéndez Moreno (Madrid, Spain)
@license: GNU GPL version 3 or any later version
@contact: [email protected]
@author: Ángel Torrado Carvajal
@organization: Universidad Rey Juan Carlos
@copyright: Ángel Torrado Carvajal (Madrid, Spain)
@license: GNU GPL version 3 or any later version
@contact: [email protected]
Script for the integration of the GNU GPLv3 translated to Spanish
into tex or html in order to save time.
"""
import sys, os, re
# reload(sys)
# sys.setdefaultencoding('utf-8')
def usage():
print 'Method of use:'
print sys.argv[0] + ' <directory> <format>'
print 'Example of use of integration of "dir" directory to a ".tex" format:'
print sys.argv[0] + ' dir tex'
def convertTex(text):
encoded = text
encoded = encoded.replace('Á', "\\'A")
encoded = encoded.replace('É', "\\'E")
encoded = encoded.replace('Í', "\\'I")
encoded = encoded.replace('Ó', "\\'O")
encoded = encoded.replace('Ú', "\\'U")
encoded = encoded.replace('Ñ', "\\~N")
encoded = encoded.replace('á', "\\'a")
encoded = encoded.replace('é', "\\'e")
encoded = encoded.replace('í', "\\'i")
encoded = encoded.replace('ó', "\\'o")
encoded = encoded.replace('ú', "\\'u")
encoded = encoded.replace('ñ', "\\~n")
return encoded
def convertHtml(text):
encoded = text
encoded = encoded.replace('Á', "Á")
encoded = encoded.replace('É', "É")
encoded = encoded.replace('Í', "Í")
encoded = encoded.replace('Ó', "Ó")
encoded = encoded.replace('Ú', "Ú")
encoded = encoded.replace('Ñ', "Ñ")
encoded = encoded.replace('á', "á")
encoded = encoded.replace('é', "é")
encoded = encoded.replace('í', "í")
encoded = encoded.replace('ó', "ó")
encoded = encoded.replace('ú', "ú")
encoded = encoded.replace('ñ', "ñ")
return encoded
def convertRtf(text):
encoded = text
encoded = encoded.replace('Á', r"\'c1")
encoded = encoded.replace('É', r"\'c9")
encoded = encoded.replace('Í', r"\'cd")
encoded = encoded.replace('Ó', r"\'c3")
encoded = encoded.replace('Ú', r"\'ca")
encoded = encoded.replace('Ñ', r"\'d1")
encoded = encoded.replace('á', r"\'e1")
encoded = encoded.replace('é', r"\'e9")
encoded = encoded.replace('í', r"\'ed")
encoded = encoded.replace('ó', r"\'f3")
encoded = encoded.replace('ú', r"\'fa")
encoded = encoded.replace('ñ', r"\'f1")
return encoded
def changeToTex(route, files):
out = open('integrated.tex', 'w')
for f in files:
op = open(route + '/' + f, 'r')
# Convert written accents into tex words
read = convertTex(op.read())
# Convert the titles of each section into items
lines = read.split('\n')
for line in lines:
if re.match('.*[0-9]+\..*[a-zA-Z]\.', line):
li = '\item' + line.split('.')[1]
read = read.replace(line, li)
# Convert the double quotes when required
conv = True
final = ''
for word in read:
if word == '"':
if conv:
word = '``'
conv = not conv
final += word
out.write(final)
out.write('\r\n')
op.close()
out.close()
def changeToHtml(route, files):
out = open('integrated.html', 'w')
for f in files:
op = open(route + '/' + f, 'r')
# Convert written accents into html words
read = convertHtml(op.read())
# Convert the titles of each section into:
# <h4><a name="section<number>"></a><section_name></h4>
# as in the original GNU GPLv3 html document
lines = read.split('\n')
section = 0
for line in lines:
if re.match('.*[0-9]+\..*[a-zA-Z]\.', line):
li = '<h4><a "name=section' + str(section) + '"></a>' + line + '</h4>'
read = read.replace(line, li)
section += 1
# Convert each paragraph into <p><paragraph></p>
paragraphs = read.split('\n\n')
for paragraph in paragraphs:
par = '<p>' + paragraph + '</p>'
read = read.replace(paragraph, par)
# Convert the double quotes when required
left = True
final = ''
for word in read:
if word == '"':
if left:
word = '“'
else:
word = '”'
left = not left
final += word
out.write(final)
op.close()
out.close()
def changeToRtf(route, files):
out = open('integrated.rtf', 'w')
out.write(r'{\rtf1\ansi{\fonttbl\f0\fswiss\qj Arial;}\f0\pard' + '\n');
for f in files:
op = open(route + '/' + f, 'r')
# Convert written accents into tex words
read = convertRtf(op.read())
# Convert the titles of each section into:
# \par{\b Title}
lines = read.split('\n')
for line in lines:
if re.match('.*[0-9]+\..*[a-zA-Z]\.', line):
li = r'\fs38\qj{\b' + line + '}'
out.write(li)
else:
# Convert each paragraph into:
# \par\qj{ paragraph }
if line == '':
li = '\n' + r'\par' + '\n' + r'\par\fs24'
out.write(li)
# Include spaces between lines
else:
li = line + ' '
out.write(li)
op.close()
out.write('}')
out.close()
def changeTo(route, files, format):
if format == 'tex':
changeToTex(route, files)
elif format == 'html':
changeToHtml(route, files)
elif format == 'rtf':
changeToRtf(route, files)
else:
print 'The format ' + format + ' is not supported in this version'
print 'Formats supported: tex, html and rtf'
if len(sys.argv) < 3:
print 'Please, introduce at least a directory and a format'
usage()
sys.exit(1)
fi = sys.argv[1]
directorio = os.path.isdir(fi)
formats = sys.argv[2:]
if directorio:
myFiles = os.listdir(fi)
myFiles = sorted(myFiles)
for format in formats:
changeTo(fi, myFiles, format)