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