]> git.lyx.org Git - lyx.git/blob - src/converter.C
3577adb9c401cf6f433bff354323879827d83df3
[lyx.git] / src / converter.C
1 /**
2  * \file converter.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "converter.h"
14 #include "graph.h"
15 #include "format.h"
16 #include "lyxrc.h"
17 #include "buffer.h"
18 #include "bufferview_funcs.h"
19 #include "LaTeX.h"
20 #include "lyx_cb.h" // ShowMessage()
21 #include "gettext.h"
22 #include "BufferView.h"
23 #include "debug.h"
24
25 #include "frontends/Alert.h"
26 #include "frontends/LyXView.h"
27
28 #include "support/filetools.h"
29 #include "support/lyxfunctional.h"
30 #include "support/path.h"
31 #include "support/tostr.h"
32 #include "support/systemcall.h"
33
34 #include <cctype>
35
36 #ifndef CXX_GLOBAL_CSTD
37 using std::isdigit;
38 #endif
39
40 using std::vector;
41 using std::endl;
42 using std::find_if;
43
44 namespace {
45
46 string const token_from("$$i");
47 string const token_base("$$b");
48 string const token_to("$$o");
49 string const token_path("$$p");
50
51
52
53 string const add_options(string const & command, string const & options)
54 {
55         string head;
56         string const tail = split(command, head, ' ');
57         return head + ' ' + options + ' ' + tail;
58 }
59
60 } // namespace anon
61
62
63 Converter::Converter(string const & f, string const & t, string const & c,
64           string const & l): from(f), to(t), command(c), flags(l),
65                              From(0), To(0), latex(false),
66                              original_dir(false), need_aux(false)
67 {}
68
69 void Converter::readFlags()
70 {
71         string flag_list(flags);
72         while (!flag_list.empty()) {
73                 string flag_name, flag_value;
74                 flag_list = split(flag_list, flag_value, ',');
75                 flag_value = split(flag_value, flag_name, '=');
76                 if (flag_name == "latex")
77                         latex = true;
78                 else if (flag_name == "originaldir")
79                         original_dir = true;
80                 else if (flag_name == "needaux")
81                         need_aux = true;
82                 else if (flag_name == "resultdir")
83                         result_dir = (flag_value.empty())
84                                 ? token_base : flag_value;
85                 else if (flag_name == "resultfile")
86                         result_file = flag_value;
87                 else if (flag_name == "parselog")
88                         parselog = flag_value;
89         }
90         if (!result_dir.empty() && result_file.empty())
91                 result_file = "index." + formats.extension(to);
92         //if (!contains(command, token_from))
93         //      latex = true;
94 }
95
96
97 bool operator<(Converter const & a, Converter const & b)
98 {
99         // use the compare_ascii_no_case instead of compare_no_case,
100         // because in turkish, 'i' is not the lowercase version of 'I',
101         // and thus turkish locale breaks parsing of tags.
102         int const i = compare_ascii_no_case(a.From->prettyname(),
103                                             b.From->prettyname());
104         if (i == 0)
105                 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
106                         < 0;
107         else
108                 return i < 0;
109 }
110
111
112
113 class compare_Converter {
114 public:
115         compare_Converter(string const & f, string const & t)
116                 : from(f), to(t) {}
117         bool operator()(Converter const & c) {
118                 return c.from == from && c.to == to;
119         }
120 private:
121         string const & from;
122         string const & to;
123 };
124
125
126
127 Converter const * Converters::getConverter(string const & from,
128                                             string const & to)
129 {
130         ConverterList::const_iterator cit =
131                 find_if(converterlist_.begin(), converterlist_.end(),
132                         compare_Converter(from, to));
133         if (cit != converterlist_.end())
134                 return &(*cit);
135         else
136                 return 0;
137 }
138
139
140 int Converters::getNumber(string const & from, string const & to)
141 {
142         ConverterList::const_iterator cit =
143                 find_if(converterlist_.begin(), converterlist_.end(),
144                         compare_Converter(from, to));
145         if (cit != converterlist_.end())
146                 return cit - converterlist_.begin();
147         else
148                 return -1;
149 }
150
151
152 void Converters::add(string const & from, string const & to,
153                      string const & command, string const & flags)
154 {
155         formats.add(from);
156         formats.add(to);
157         ConverterList::iterator it = find_if(converterlist_.begin(),
158                                              converterlist_.end(),
159                                              compare_Converter(from, to));
160
161         Converter converter(from, to, command, flags);
162         if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
163                 converter = *it;
164                 converter.command = command;
165                 converter.flags = flags;
166         }
167         converter.readFlags();
168
169         if (converter.latex && (latex_command_.empty() || to == "dvi"))
170                 latex_command_ = subst(command, token_from, "");
171         // If we have both latex & pdflatex, we set latex_command to latex.
172         // The latex_command is used to update the .aux file when running
173         // a converter that uses it.
174
175         if (it == converterlist_.end()) {
176                 converterlist_.push_back(converter);
177         } else {
178                 converter.From = it->From;
179                 converter.To = it->To;
180                 *it = converter;
181         }
182 }
183
184
185 void Converters::erase(string const & from, string const & to)
186 {
187         ConverterList::iterator it = find_if(converterlist_.begin(),
188                                              converterlist_.end(),
189                                              compare_Converter(from, to));
190         if (it != converterlist_.end())
191                 converterlist_.erase(it);
192 }
193
194
195 // This method updates the pointers From and To in all the converters.
196 // The code is not very efficient, but it doesn't matter as the number
197 // of formats and converters is small.
198 // Furthermore, this method is called only on startup, or after
199 // adding/deleting a format in FormPreferences (the latter calls can be
200 // eliminated if the formats in the Formats class are stored using a map or
201 // a list (instead of a vector), but this will cause other problems).
202 void Converters::update(Formats const & formats)
203 {
204         ConverterList::iterator it = converterlist_.begin();
205         ConverterList::iterator end = converterlist_.end();
206         for (; it != end; ++it) {
207                 it->From = formats.getFormat(it->from);
208                 it->To = formats.getFormat(it->to);
209         }
210 }
211
212
213 // This method updates the pointers From and To in the last converter.
214 // It is called when adding a new converter in FormPreferences
215 void Converters::updateLast(Formats const & formats)
216 {
217         if (converterlist_.begin() != converterlist_.end()) {
218                 ConverterList::iterator it = converterlist_.end() - 1;
219                 it->From = formats.getFormat(it->from);
220                 it->To = formats.getFormat(it->to);
221         }
222 }
223
224
225 void Converters::sort()
226 {
227         std::sort(converterlist_.begin(), converterlist_.end());
228 }
229
230
231 bool Converters::usePdflatex(Graph::EdgePath const & path)
232 {
233         for (Graph::EdgePath::const_iterator cit = path.begin();
234              cit != path.end(); ++cit) {
235                 Converter const & conv = converterlist_[*cit];
236                 if (conv.latex)
237                         return contains(conv.to, "pdf");
238         }
239         return false;
240 }
241
242
243 bool Converters::convert(Buffer const * buffer,
244                          string const & from_file, string const & to_file_base,
245                          string const & from_format, string const & to_format,
246                          string & to_file)
247 {
248         to_file = ChangeExtension(to_file_base,
249                                   formats.extension(to_format));
250
251         if (from_format == to_format)
252                 return move(from_file, to_file, false);
253
254         Graph::EdgePath edgepath = getPath(from_format, to_format);
255         if (edgepath.empty()) {
256                 return false;
257         }
258
259         string path = OnlyPath(from_file);
260         Path p(path);
261
262         bool run_latex = false;
263         string from_base = ChangeExtension(from_file, "");
264         string to_base = ChangeExtension(to_file, "");
265         string infile;
266         string outfile = from_file;
267         for (Graph::EdgePath::const_iterator cit = edgepath.begin();
268              cit != edgepath.end(); ++cit) {
269                 Converter const & conv = converterlist_[*cit];
270                 bool dummy = conv.To->dummy() && conv.to != "program";
271                 if (!dummy)
272                         lyxerr[Debug::FILES] << "Converting from  "
273                                << conv.from << " to " << conv.to << endl;
274                 infile = outfile;
275                 outfile = conv.result_dir.empty()
276                         ? ChangeExtension(from_file, conv.To->extension())
277                         : AddName(subst(conv.result_dir,
278                                         token_base, from_base),
279                                   subst(conv.result_file,
280                                         token_base, OnlyFilename(from_base)));
281
282                 // if input and output files are equal, we use a
283                 // temporary file as intermediary (JMarc)
284                 string real_outfile;
285                 if (outfile == infile) {
286                         real_outfile = infile;
287                         outfile = AddName(buffer->tmppath, "tmpfile.out");
288                 }
289
290                 if (conv.latex) {
291                         run_latex = true;
292                         string command = subst(conv.command, token_from, "");
293                         lyxerr[Debug::FILES] << "Running " << command << endl;
294                         if (!runLaTeX(buffer, command))
295                                 return false;
296                 } else {
297                         if (conv.need_aux && !run_latex
298                             && !latex_command_.empty()) {
299                                 lyxerr[Debug::FILES]
300                                         << "Running " << latex_command_
301                                         << " to update aux file"<<  endl;
302                                 runLaTeX(buffer, latex_command_);
303                         }
304
305                         string infile2 = (conv.original_dir)
306                                 ? infile : MakeRelPath(infile, path);
307                         string outfile2 = (conv.original_dir)
308                                 ? outfile : MakeRelPath(outfile, path);
309
310                         string command = conv.command;
311                         command = subst(command, token_from, QuoteName(infile2));
312                         command = subst(command, token_base, QuoteName(from_base));
313                         command = subst(command, token_to, QuoteName(outfile2));
314                         command = LibScriptSearch(command);
315
316                         if (!conv.parselog.empty())
317                                 command += " 2> " + QuoteName(infile2 + ".out");
318
319                         if (conv.from == "dvi" && conv.to == "ps")
320                                 command = add_options(command,
321                                                       dvips_options(buffer));
322                         else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
323                                 command = add_options(command,
324                                                       dvipdfm_options(buffer));
325
326                         lyxerr[Debug::FILES] << "Calling " << command << endl;
327                         if (buffer)
328                                 ShowMessage(buffer, _("Executing command:"), command);
329
330                         Systemcall::Starttype type = (dummy)
331                                 ? Systemcall::DontWait : Systemcall::Wait;
332                         Systemcall one;
333                         int res;
334                         if (conv.original_dir && buffer) {
335                                 Path p(buffer->filePath());
336                                 res = one.startscript(type, command);
337                         } else
338                                 res = one.startscript(type, command);
339
340                         if (!real_outfile.empty()) {
341                                 if (!lyx::rename(outfile, real_outfile))
342                                         res = -1;
343                                 else
344                                         lyxerr[Debug::FILES]
345                                                 << "renaming file " << outfile
346                                                 << " to " << real_outfile
347                                                 << endl;
348                         }
349
350                         if (!conv.parselog.empty()) {
351                                 string const logfile =  infile2 + ".log";
352                                 string const script = LibScriptSearch(conv.parselog);
353                                 string const command2 = script +
354                                         " < " + QuoteName(infile2 + ".out") +
355                                         " > " + QuoteName(logfile);
356                                 one.startscript(Systemcall::Wait, command2);
357                                 if (!scanLog(buffer, command, logfile))
358                                         return false;
359                         }
360
361                         if (res) {
362                                 if (conv.to == "program") {
363                                         Alert::error(_("Build errors"),
364                                                 _("There were errors during the build process."));
365                                 } else {
366 // FIXME: this should go out of here. For example, here we cannot say if
367 // it is a document (.lyx) or something else. Same goes for elsewhere.
368                                 Alert::error(_("Cannot convert file"),
369                                         bformat(_("An error occurred whilst running %1$s"),
370                                                 command.substr(0, 50)));
371                                 }
372                                 return false;
373                         }
374                 }
375         }
376
377         Converter const & conv = converterlist_[edgepath.back()];
378         if (conv.To->dummy())
379                 return true;
380
381
382         if (!conv.result_dir.empty()) {
383                 to_file = AddName(subst(conv.result_dir, token_base, to_base),
384                                   subst(conv.result_file,
385                                         token_base, OnlyFilename(to_base)));
386                 if (from_base != to_base) {
387                         string from = subst(conv.result_dir,
388                                             token_base, from_base);
389                         string to = subst(conv.result_dir,
390                                           token_base, to_base);
391                         if (!lyx::rename(from, to)) {
392                                 Alert::error(_("Cannot convert file"),
393                                         bformat(_("Could not move a temporary file from %1$s to %2$s."),
394                                                 from, to));
395                                 return false;
396                         }
397                 }
398                 return true;
399         } else
400                 return move(outfile, to_file, conv.latex);
401 }
402
403
404 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
405 // moves each /path/file*.ext file to /path2/file2*.ext2'
406 bool Converters::move(string const & from, string const & to, bool copy)
407 {
408         if (from == to)
409                 return true;
410
411         bool no_errors = true;
412         string const path = OnlyPath(from);
413         string const base = OnlyFilename(ChangeExtension(from, ""));
414         string const to_base = ChangeExtension(to, "");
415         string const to_extension = GetExtension(to);
416
417         vector<string> files = DirList(OnlyPath(from), GetExtension(from));
418         for (vector<string>::const_iterator it = files.begin();
419              it != files.end(); ++it)
420                 if (prefixIs(*it, base)) {
421                         string const from2 = path + *it;
422                         string to2 = to_base + it->substr(base.length());
423                         to2 = ChangeExtension(to2, to_extension);
424                         lyxerr[Debug::FILES] << "moving " << from2
425                                              << " to " << to2 << endl;
426                         bool const moved = (copy)
427                                 ? lyx::copy(from2, to2)
428                                 : lyx::rename(from2, to2);
429                         if (!moved && no_errors) {
430                                 Alert::error(_("Cannot convert file"),
431                                         bformat(_("Could not move a temporary file from %1$s to %2$s."),
432                                                 from2, to2));
433                                 no_errors = false;
434                         }
435                 }
436         return no_errors;
437 }
438
439
440 bool Converters::convert(Buffer const * buffer,
441                         string const & from_file, string const & to_file_base,
442                         string const & from_format, string const & to_format)
443 {
444         string to_file;
445         return convert(buffer, from_file, to_file_base, from_format, to_format,
446                        to_file);
447 }
448
449
450 bool Converters::formatIsUsed(string const & format)
451 {
452         ConverterList::const_iterator cit = converterlist_.begin();
453         ConverterList::const_iterator end = converterlist_.end();
454         for (; cit != end; ++cit) {
455                 if (cit->from == format || cit->to == format)
456                         return true;
457         }
458         return false;
459 }
460
461
462 bool Converters::scanLog(Buffer const * buffer, string const & command,
463                          string const & filename)
464 {
465         if (!buffer)
466                 return false;
467
468         BufferView * bv = buffer->getUser();
469         LaTeX latex("", filename, "");
470         TeXErrors terr;
471         int result = latex.scanLogFile(terr);
472         if (bv && (result & LaTeX::ERRORS))
473                 bv->showErrorList();
474
475         return true;
476 }
477
478
479 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
480 {
481         if (!buffer)
482                 return false;
483
484         BufferView * bv = buffer->getUser();
485
486         if (bv) {
487                 bv->owner()->busy(true);
488                 bv->owner()->message(_("Running LaTeX..."));
489                 // all the autoinsets have already been removed
490         }
491
492         // do the LaTeX run(s)
493         string name = buffer->getLatexName();
494         LaTeX latex(command, name, buffer->filePath());
495         TeXErrors terr;
496         int result = latex.run(terr,
497                                bv ? &bv->owner()->getLyXFunc() : 0);
498
499         if (bv && (result & LaTeX::ERRORS)) {
500                 //show errors
501                 bv->showErrorList();
502         }
503
504         // check return value from latex.run().
505         if ((result & LaTeX::NO_LOGFILE)) {
506                 string str = bformat(_("LaTeX did not run successfully. Additionally, LyX "
507                         "could not locate the LaTeX log %1$s."), name);
508                 Alert::error(_("LaTeX failed"), str);
509         } else if (result & LaTeX::NO_OUTPUT) {
510                 Alert::warning(_("Output is empty"),
511                         _("An empty output file was generated."));
512         }
513
514         if (bv)
515                 bv->owner()->busy(false);
516
517         int const ERROR_MASK =
518                         LaTeX::NO_LOGFILE |
519                         LaTeX::ERRORS |
520                         LaTeX::NO_OUTPUT;
521
522         return (result & ERROR_MASK) == 0;
523
524 }
525
526
527 string const Converters::dvips_options(Buffer const * buffer)
528 {
529         string result;
530         if (!buffer)
531                 return result;
532
533         if (buffer->params.use_geometry
534             && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
535             && !lyxrc.print_paper_dimension_flag.empty()
536             && !buffer->params.paperwidth.empty()
537             && !buffer->params.paperheight.empty()) {
538                 // using a custom papersize
539                 result = lyxrc.print_paper_dimension_flag;
540                 result += ' ' + buffer->params.paperwidth;
541                 result += ',' + buffer->params.paperheight;
542         } else {
543                 string const paper_option = papersize(buffer);
544                 if (paper_option != "letter" ||
545                     buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
546                         // dvips won't accept -t letter -t landscape.  In all other
547                         // cases, include the paper size explicitly.
548                         result = lyxrc.print_paper_flag;
549                         result += ' ' + paper_option;
550                 }
551         }
552         if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
553             buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
554                 result += ' ' + lyxrc.print_landscape_flag;
555         return result;
556 }
557
558
559 string const Converters::dvipdfm_options(Buffer const * buffer)
560 {
561         string result;
562         if (!buffer)
563                 return result;
564
565         if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
566                 string const paper_size = papersize(buffer);
567                 if (paper_size != "b5" && paper_size != "foolscap")
568                         result = "-p "+ paper_size;
569
570                 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
571                         result += " -l";
572         }
573
574         return result;
575 }
576
577 void Converters::buildGraph()
578 {
579         G_.init(formats.size());
580         ConverterList::iterator beg = converterlist_.begin();
581         ConverterList::iterator end = converterlist_.end();
582         for (ConverterList::iterator it = beg; it != end ; ++it) {
583                 int const s = formats.getNumber(it->from);
584                 int const t = formats.getNumber(it->to);
585                 G_.addEdge(s,t);
586         }
587 }
588
589 vector<Format const *> const
590 Converters::intToFormat(std::vector<int> const & input)
591 {
592         vector<Format const *> result(input.size());
593
594         vector<int>::const_iterator it = input.begin();
595         vector<int>::const_iterator end = input.end();
596         vector<Format const *>::iterator rit = result.begin();
597         for ( ; it != end; ++it, ++rit) {
598                 *rit = &formats.get(*it);
599         }
600         return result;
601 }
602
603 vector<Format const *> const
604 Converters::getReachableTo(string const & target, bool clear_visited)
605 {
606         vector<int> const & reachablesto =
607                 G_.getReachableTo(formats.getNumber(target), clear_visited);
608
609         return intToFormat(reachablesto);
610 }
611
612 vector<Format const *> const
613 Converters::getReachable(string const & from, bool only_viewable,
614              bool clear_visited)
615 {
616         vector<int> const & reachables =
617                 G_.getReachable(formats.getNumber(from),
618                                 only_viewable,
619                                 clear_visited);
620
621         return intToFormat(reachables);
622 }
623
624 bool Converters::isReachable(string const & from, string const & to)
625 {
626         return G_.isReachable(formats.getNumber(from),
627                               formats.getNumber(to));
628 }
629
630 Graph::EdgePath const
631 Converters::getPath(string const & from, string const & to)
632 {
633         return G_.getPath(formats.getNumber(from),
634                           formats.getNumber(to));
635 }
636
637 /// The global instance
638 Converters converters;
639
640 // The global copy after reading lyxrc.defaults
641 Converters system_converters;