1 
2 
3 %{
4 
5 
6 
7 #include <cstdlib>
8 #include <iostream>
9 #include <map>
10 #include <string>
11 #include <vector>
12 #include <apertium/latex_accentsmap.h>
13 
14 extern "C" {
15 #if !defined(__STDC__)
16 # define __STDC__ 1
17 #endif
18 #include <regex.h>
19 }
20 
21 #include <string>
22 #include <lttoolbox/lt_locale.h>
23 #include <lttoolbox/ltstr.h>
24 #ifndef GENFORMAT
25 #include "apertium_config.h"
26 #endif
27 #include <apertium/unlocked_cstdio.h>
28 #ifdef _WIN32
29 #include <io.h>
30 #include <fcntl.h>
31 #endif
32 
33 using namespace std;
34 
35 AccentsMap accentsMap(true);
36 wstring closesym = L"";
37 string memconv = "";
38 
convertir(string const & multibyte,int const length)39 wstring convertir(string const &multibyte, int const length)
40 {
41   memconv.append(multibyte.c_str(), length);
42   int tam = memconv.size();
43   wchar_t *retval = new wchar_t[tam+1];
44   size_t l = mbstowcs(retval, memconv.c_str(), tam);
45 
46   if(l == ((size_t) -1))
47   {
48     delete[] retval;
49     if(memconv.size() >= 4)
50     {
51       wcerr << L"Warning: wrong encoding" << endl;
52     }
53     return L"";
54   }
55   else
56   {
57     memconv = "";
58     retval[l] = 0;
59     wstring ret = retval;
60     delete[] retval;
61     return ret;
62   }
63 }
64 
65 
66 
67 
68 %}
69 
70 
71 %option nounput
72 %option noyywrap
73 %option stack
74 
75 %x mathenv
76 %x readbrackets
77 
78 %%
79 
80 
81 
82 &quot;	{
83 	fputws(L"\"",yyout);
84 }
85 &apos;	{
86 	fputws(L"\'",yyout);
87 }
88 &lt;	{
89 	fputws(L"<",yyout);
90 }
91 &gt;	{
92 	fputws(L">",yyout);
93 }
94 &amp;	{
95 	fputws(L"\\&",yyout);
96 }
97 \<AMP\/\>	{
98 	fputws(L"&",yyout);
99 }
100 
101 \<LEFTESCAPEDBRACE\/\>	{
102         fputws(L"\\{", yyout);
103 }
104 
105 \<RIGHTESCAPEDBRACE\/\>	{
106         fputws(L"\\}", yyout);
107 }
108 
109 \<ESCAPEDPERCENT\/\>	{
110         fputws(L"\\%", yyout);
111 }
112 
113 ¿	{
114 	fputws(L"?`",yyout);
115 }
116 
117 ¡	{
118 	fputws(L"!`",yyout);
119 }
120 
121 
122 
123 \<MATH_DOLLARS\>	{
124 	BEGIN(mathenv);
125 	fputws(L"$$",yyout);
126 }
127 
128 <mathenv>\<\/MATH_DOLLARS\>	{
129 	fputws(L"$$",yyout);
130 	BEGIN(0);
131 }
132 
133 
134 \<MATH_DOLLAR\>	{
135 	BEGIN(mathenv);
136 	fputws(L"$",yyout);
137 }
138 
139 <mathenv>\<\/MATH_DOLLAR\>	{
140 	fputws(L"$",yyout);
141 	BEGIN(0);
142 }
143 
144 \<MATH_PAR\>	{
145 	fputws(L"\\(",yyout);
146 }
147 
148 \<\/MATH_PAR\>	{
149 	fputws(L"\\)",yyout);
150 }
151 
152 \<MATH_BRA\>	{
153 	fputws(L"\\[",yyout);
154 }
155 
156 \<\/MATH_BRA\>	{
157 	fputws(L"\\]",yyout);
158 }
159 
160 
161 \<CONTENTS\>	{
162 	fputws(L"{",yyout);
163 }
164 
165 \<\/CONTENTS\>	{
166 	fputws(L"}",yyout);
167 }
168 
169 &NBSP;	{
170 	fputws(L"~",yyout);
171 }
172 
173 
174 
175 \<BR\/\>	{
176 	fputws(L"\\\\",yyout);
177 }
178 
179 \<COMMENT\>[^\<]*	{
180 	fputws((wstring(L"\%")+convertir(yytext+9,yyleng-9)).c_str(),yyout);
181 }
182 
183 \<\/COMMENT\>	{
184 }
185 
186 
187 \<PARAM\>[^\<]*	{
188 	fputws((wstring(L"[")+convertir(yytext+7,yyleng-7)).c_str(),yyout);
189 }
190 \<\/PARAM\>	{
191 	fputws(L"]", yyout);
192 }
193 
194 \<VERB\>	{
195         fputws(L"\\verb", yyout);
196 }
197 
198 \<\/VERB\>	{
199         ;
200 }
201 
202 
203 
204 ł	{
205 	fputws(L"\\l", yyout);
206 }
207 
208 
209 œ	{
210 	fputws(L"{\\oe}",yyout);
211 }
212 
213 Œ	{
214 	fputws(L"{\\OE}",yyout);
215 }
216 
217 æ	{
218 	fputws(L"{\\ae}",yyout);
219 }
220 
221 Æ	{
222 	fputws(L"{\\AE}",yyout);
223 }
224 
225 å	{
226 	fputws(L"{\\aa}",yyout);
227 }
228 
229 Å	{
230 	fputws(L"{\\AA}",yyout);
231 }
232 
233 ø	{
234 	fputws(L"{\\o}",yyout);
235 }
236 
237 Ø	{
238 	fputws(L"{\\O}",yyout);
239 }
240 
241 ß	{
242 	fputws(L"{\\ss}",yyout);
243 }
244 
245 \<[a-zA-Z0-9]+\>	{
246 	fputws((wstring(L"\\begin{")+convertir(yytext+1,yyleng-2)+wstring(L"}")).c_str(),yyout);
247 }
248 
249 \<HASH_[0-9]+\/\>	{
250 	fputws((wstring(L"\\#")+convertir(yytext+6,yyleng-8)).c_str(),yyout);
251 }
252 
253 \<HASH\/\>		{
254         fputws(L"\\#", yyout);
255 }
256 
257 \<[a-zA-Z0-9]+_STAR\>	{
258 	fputws((wstring(L"\\begin{")+convertir(yytext+1,yyleng-7)+wstring(L"*}")).c_str(),yyout);
259 }
260 
261 \<\/[a-zA-Z0-9]+\>	{
262 	fputws((wstring(L"\\end{")+convertir(yytext+2,yyleng-3)+wstring(L"}")).c_str(),yyout);
263 }
264 
265 \<\/[a-zA-Z0-9]+_STAR\>	{
266 	fputws((wstring(L"\\end{")+convertir(yytext+2,yyleng-8)+wstring(L"*}")).c_str(),yyout);
267 }
268 
269 \<[a-zA-Z0-9]+\/\>	{
270 	fputws((wstring(L"\\")+convertir(yytext+1,yyleng-3)).c_str(),yyout);
271 }
272 
273 \<[a-zA-Z0-9]+_STAR\/\>	{
274 	fputws((wstring(L"\\")+convertir(yytext+1,yyleng-8)+wstring(L"*")).c_str(),yyout);
275 }
276  /*NO ENTIENDO ESTA REGLA
277  \#	{
278         fputws(L"\\#", yyout);
279  }*/
280 
281 
282 [^A-Za-z\n]	{
283 	wstring wt = convertir(yytext,yyleng);
284 	wstring wa = accentsMap.get(wt);
285 	if( wa == L"" )
286 		fputws(wt.c_str(),yyout);
287  	else
288 		fputws(wstring(L"\\"+wa.substr(0,1)+L"{"+wa.substr(1)+L"}").c_str(),yyout);
289 }
290 
291 
292 (.|\n)	{
293 	fputws(convertir(yytext,yyleng).c_str(),yyout);
294 }
295 
296 <mathenv>(.|\n)	{
297 	fputws(convertir(yytext,yyleng).c_str(),yyout);
298 }
299 
300 
301 <<EOF>>	{
302 	return 0;
303 }
304 %%
305 
306 
307 
308 void usage(string const &progname)
309 {
310 
311   cerr << "USAGE: " << progname << " [input_file [output_file]" << ']' << endl;
312 
313   cerr << "LaTeX format postprocessor " << endl;
314   exit(EXIT_SUCCESS);
315 }
316 
main(int argc,char * argv[])317 int main(int argc, char *argv[])
318 {
319   LtLocale::tryToSetLocale();
320   size_t base = 0;
321 
322   if(argc >= 2 && !strcmp(argv[1],"-i"))
323   {
324     base++;
325   }
326 
327  if((argc-base) > 4)
328   {
329     usage(argv[0]);
330   }
331 
332   switch(argc-base)
333   {
334     case 3:
335       yyout = fopen(argv[2+base], "w");
336       if(!yyout)
337       {
338         usage(argv[0]);
339       }
340     case 2:
341       yyin = fopen(argv[1+base], "r");
342       if(!yyin)
343       {
344         usage(argv[0]);
345       }
346       break;
347     default:
348       break;
349   }
350 
351 #ifdef _MSC_VER
352   _setmode(_fileno(yyin), _O_U8TEXT);
353   _setmode(_fileno(yyout), _O_U8TEXT);
354 #endif
355   // prevent warning message
356   yy_push_state(1);
357   yy_top_state();
358   yy_pop_state();
359 
360   yylex();
361 
362   fclose(yyin);
363   fclose(yyout);
364 }
365