3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS
13 #include "converter.h"
18 #include "bufferparams.h"
19 #include "buffer_funcs.h"
20 #include "bufferview_funcs.h"
21 #include "errorlist.h"
26 #include "frontends/Alert.h"
27 #include "frontends/LyXView.h"
29 #include "support/filetools.h"
30 #include "support/lyxfunctional.h"
31 #include "support/path.h"
32 #include "support/tostr.h"
33 #include "support/systemcall.h"
35 #include <boost/signals/signal1.hpp>
36 #include <boost/signals/trackable.hpp>
40 using namespace lyx::support;
42 #ifndef CXX_GLOBAL_CSTD
52 string const token_from("$$i");
53 string const token_base("$$b");
54 string const token_to("$$o");
55 string const token_path("$$p");
59 string const add_options(string const & command, string const & options)
62 string const tail = split(command, head, ' ');
63 return head + ' ' + options + ' ' + tail;
67 string const dvipdfm_options(BufferParams const & bp)
71 if (bp.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
72 string const paper_size = bp.paperSizeName();
73 if (paper_size != "b5" && paper_size != "foolscap")
74 result = "-p "+ paper_size;
76 if (bp.orientation == BufferParams::ORIENTATION_LANDSCAPE)
86 Converter::Converter(string const & f, string const & t, string const & c,
87 string const & l): from(f), to(t), command(c), flags(l),
88 From(0), To(0), latex(false),
89 original_dir(false), need_aux(false)
93 void Converter::readFlags()
95 string flag_list(flags);
96 while (!flag_list.empty()) {
97 string flag_name, flag_value;
98 flag_list = split(flag_list, flag_value, ',');
99 flag_value = split(flag_value, flag_name, '=');
100 if (flag_name == "latex")
102 else if (flag_name == "originaldir")
104 else if (flag_name == "needaux")
106 else if (flag_name == "resultdir")
107 result_dir = (flag_value.empty())
108 ? token_base : flag_value;
109 else if (flag_name == "resultfile")
110 result_file = flag_value;
111 else if (flag_name == "parselog")
112 parselog = flag_value;
114 if (!result_dir.empty() && result_file.empty())
115 result_file = "index." + formats.extension(to);
116 //if (!contains(command, token_from))
121 bool operator<(Converter const & a, Converter const & b)
123 // use the compare_ascii_no_case instead of compare_no_case,
124 // because in turkish, 'i' is not the lowercase version of 'I',
125 // and thus turkish locale breaks parsing of tags.
126 int const i = compare_ascii_no_case(a.From->prettyname(),
127 b.From->prettyname());
129 return compare_ascii_no_case(a.To->prettyname(),
130 b.To->prettyname()) < 0;
136 class compare_Converter {
138 compare_Converter(string const & f, string const & t)
140 bool operator()(Converter const & c) {
141 return c.from == from && c.to == to;
150 Converter const * Converters::getConverter(string const & from,
153 ConverterList::const_iterator cit =
154 find_if(converterlist_.begin(), converterlist_.end(),
155 compare_Converter(from, to));
156 if (cit != converterlist_.end())
163 int Converters::getNumber(string const & from, string const & to)
165 ConverterList::const_iterator cit =
166 find_if(converterlist_.begin(), converterlist_.end(),
167 compare_Converter(from, to));
168 if (cit != converterlist_.end())
169 return cit - converterlist_.begin();
175 void Converters::add(string const & from, string const & to,
176 string const & command, string const & flags)
180 ConverterList::iterator it = find_if(converterlist_.begin(),
181 converterlist_.end(),
182 compare_Converter(from, to));
184 Converter converter(from, to, command, flags);
185 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
187 converter.command = command;
188 converter.flags = flags;
190 converter.readFlags();
192 if (converter.latex && (latex_command_.empty() || to == "dvi"))
193 latex_command_ = subst(command, token_from, "");
194 // If we have both latex & pdflatex, we set latex_command to latex.
195 // The latex_command is used to update the .aux file when running
196 // a converter that uses it.
198 if (it == converterlist_.end()) {
199 converterlist_.push_back(converter);
201 converter.From = it->From;
202 converter.To = it->To;
208 void Converters::erase(string const & from, string const & to)
210 ConverterList::iterator it = find_if(converterlist_.begin(),
211 converterlist_.end(),
212 compare_Converter(from, to));
213 if (it != converterlist_.end())
214 converterlist_.erase(it);
218 // This method updates the pointers From and To in all the converters.
219 // The code is not very efficient, but it doesn't matter as the number
220 // of formats and converters is small.
221 // Furthermore, this method is called only on startup, or after
222 // adding/deleting a format in FormPreferences (the latter calls can be
223 // eliminated if the formats in the Formats class are stored using a map or
224 // a list (instead of a vector), but this will cause other problems).
225 void Converters::update(Formats const & formats)
227 ConverterList::iterator it = converterlist_.begin();
228 ConverterList::iterator end = converterlist_.end();
229 for (; it != end; ++it) {
230 it->From = formats.getFormat(it->from);
231 it->To = formats.getFormat(it->to);
236 // This method updates the pointers From and To in the last converter.
237 // It is called when adding a new converter in FormPreferences
238 void Converters::updateLast(Formats const & formats)
240 if (converterlist_.begin() != converterlist_.end()) {
241 ConverterList::iterator it = converterlist_.end() - 1;
242 it->From = formats.getFormat(it->from);
243 it->To = formats.getFormat(it->to);
248 void Converters::sort()
250 std::sort(converterlist_.begin(), converterlist_.end());
254 bool Converters::usePdflatex(Graph::EdgePath const & path)
256 for (Graph::EdgePath::const_iterator cit = path.begin();
257 cit != path.end(); ++cit) {
258 Converter const & conv = converterlist_[*cit];
260 return contains(conv.to, "pdf");
266 bool Converters::convert(Buffer const * buffer,
267 string const & from_file, string const & to_file_base,
268 string const & from_format, string const & to_format,
271 to_file = ChangeExtension(to_file_base,
272 formats.extension(to_format));
274 if (from_format == to_format)
275 return move(from_file, to_file, false);
277 Graph::EdgePath edgepath = getPath(from_format, to_format);
278 if (edgepath.empty()) {
281 LatexRunParams runparams;
282 runparams.flavor = usePdflatex(edgepath) ?
283 LatexRunParams::PDFLATEX : LatexRunParams::LATEX;
285 string path = OnlyPath(from_file);
288 bool run_latex = false;
289 string from_base = ChangeExtension(from_file, "");
290 string to_base = ChangeExtension(to_file, "");
292 string outfile = from_file;
293 for (Graph::EdgePath::const_iterator cit = edgepath.begin();
294 cit != edgepath.end(); ++cit) {
295 Converter const & conv = converterlist_[*cit];
296 bool dummy = conv.To->dummy() && conv.to != "program";
298 lyxerr[Debug::FILES] << "Converting from "
299 << conv.from << " to " << conv.to << endl;
301 outfile = conv.result_dir.empty()
302 ? ChangeExtension(from_file, conv.To->extension())
303 : AddName(subst(conv.result_dir,
304 token_base, from_base),
305 subst(conv.result_file,
306 token_base, OnlyFilename(from_base)));
308 // if input and output files are equal, we use a
309 // temporary file as intermediary (JMarc)
311 if (outfile == infile) {
312 real_outfile = infile;
313 outfile = AddName(buffer->tmppath, "tmpfile.out");
318 string command = subst(conv.command, token_from, "");
319 lyxerr[Debug::FILES] << "Running " << command << endl;
320 if (!runLaTeX(buffer, command, runparams))
323 if (conv.need_aux && !run_latex
324 && !latex_command_.empty()) {
326 << "Running " << latex_command_
327 << " to update aux file"<< endl;
328 runLaTeX(buffer, latex_command_, runparams);
331 string infile2 = (conv.original_dir)
332 ? infile : MakeRelPath(infile, path);
333 string outfile2 = (conv.original_dir)
334 ? outfile : MakeRelPath(outfile, path);
336 string command = conv.command;
337 command = subst(command, token_from, QuoteName(infile2));
338 command = subst(command, token_base, QuoteName(from_base));
339 command = subst(command, token_to, QuoteName(outfile2));
340 command = LibScriptSearch(command);
342 if (!conv.parselog.empty())
343 command += " 2> " + QuoteName(infile2 + ".out");
345 if (conv.from == "dvi" && conv.to == "ps")
346 command = add_options(command,
347 buffer->params.dvips_options());
348 else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
349 command = add_options(command,
350 dvipdfm_options(buffer->params));
352 lyxerr[Debug::FILES] << "Calling " << command << endl;
354 buffer->message(_("Executing command: ") + command);
355 Systemcall::Starttype type = (dummy)
356 ? Systemcall::DontWait : Systemcall::Wait;
359 if (conv.original_dir && buffer) {
360 Path p(buffer->filePath());
361 res = one.startscript(type, command);
363 res = one.startscript(type, command);
365 if (!real_outfile.empty()) {
366 if (!rename(outfile, real_outfile))
370 << "renaming file " << outfile
371 << " to " << real_outfile
375 if (!conv.parselog.empty()) {
376 string const logfile = infile2 + ".log";
377 string const script = LibScriptSearch(conv.parselog);
378 string const command2 = script +
379 " < " + QuoteName(infile2 + ".out") +
380 " > " + QuoteName(logfile);
381 one.startscript(Systemcall::Wait, command2);
382 if (!scanLog(buffer, command, logfile))
387 if (conv.to == "program") {
388 Alert::error(_("Build errors"),
389 _("There were errors during the build process."));
391 // FIXME: this should go out of here. For example, here we cannot say if
392 // it is a document (.lyx) or something else. Same goes for elsewhere.
393 Alert::error(_("Cannot convert file"),
394 bformat(_("An error occurred whilst running %1$s"),
395 command.substr(0, 50)));
402 Converter const & conv = converterlist_[edgepath.back()];
403 if (conv.To->dummy())
407 if (!conv.result_dir.empty()) {
408 to_file = AddName(subst(conv.result_dir, token_base, to_base),
409 subst(conv.result_file,
410 token_base, OnlyFilename(to_base)));
411 if (from_base != to_base) {
412 string from = subst(conv.result_dir,
413 token_base, from_base);
414 string to = subst(conv.result_dir,
415 token_base, to_base);
416 if (!rename(from, to)) {
417 Alert::error(_("Cannot convert file"),
418 bformat(_("Could not move a temporary file from %1$s to %2$s."),
425 return move(outfile, to_file, conv.latex);
429 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
430 // moves each /path/file*.ext file to /path2/file2*.ext2'
431 bool Converters::move(string const & from, string const & to, bool copy)
436 bool no_errors = true;
437 string const path = OnlyPath(from);
438 string const base = OnlyFilename(ChangeExtension(from, ""));
439 string const to_base = ChangeExtension(to, "");
440 string const to_extension = GetExtension(to);
442 vector<string> files = DirList(OnlyPath(from), GetExtension(from));
443 for (vector<string>::const_iterator it = files.begin();
444 it != files.end(); ++it)
445 if (prefixIs(*it, base)) {
446 string const from2 = path + *it;
447 string to2 = to_base + it->substr(base.length());
448 to2 = ChangeExtension(to2, to_extension);
449 lyxerr[Debug::FILES] << "moving " << from2
450 << " to " << to2 << endl;
451 bool const moved = (copy)
452 ? lyx::support::copy(from2, to2)
453 : rename(from2, to2);
454 if (!moved && no_errors) {
455 Alert::error(_("Cannot convert file"),
456 bformat(_("Could not move a temporary file from %1$s to %2$s."),
465 bool Converters::convert(Buffer const * buffer,
466 string const & from_file, string const & to_file_base,
467 string const & from_format, string const & to_format)
470 return convert(buffer, from_file, to_file_base, from_format, to_format,
475 bool Converters::formatIsUsed(string const & format)
477 ConverterList::const_iterator cit = converterlist_.begin();
478 ConverterList::const_iterator end = converterlist_.end();
479 for (; cit != end; ++cit) {
480 if (cit->from == format || cit->to == format)
487 bool Converters::scanLog(Buffer const * buffer, string const & /*command*/,
488 string const & filename)
493 LatexRunParams runparams;
494 runparams.flavor = LatexRunParams::LATEX;
495 LaTeX latex("", runparams, filename, "");
497 int result = latex.scanLogFile(terr);
499 if (result & LaTeX::ERRORS)
500 bufferErrors(*buffer, terr);
507 class showMessage : public boost::signals::trackable {
509 showMessage(Buffer const * b) : buffer_(b) {};
510 void operator()(string m)
515 Buffer const * buffer_;
520 bool Converters::runLaTeX(Buffer const * buffer, string const & command,
521 LatexRunParams const & runparams)
523 // when is this needed?
528 buffer->message(_("Running LaTeX..."));
530 // do the LaTeX run(s)
531 string name = buffer->getLatexName();
532 LaTeX latex(command, runparams, name, buffer->filePath());
534 showMessage show(buffer);
535 latex.message.connect(show);
536 int result = latex.run(terr);
538 if (result & LaTeX::ERRORS)
539 bufferErrors(*buffer, terr);
541 // check return value from latex.run().
542 if ((result & LaTeX::NO_LOGFILE)) {
543 string str = bformat(_("LaTeX did not run successfully. "
544 "Additionally, LyX could not locate "
545 "the LaTeX log %1$s."), name);
546 Alert::error(_("LaTeX failed"), str);
547 } else if (result & LaTeX::NO_OUTPUT) {
548 Alert::warning(_("Output is empty"),
549 _("An empty output file was generated."));
555 int const ERROR_MASK =
560 return (result & ERROR_MASK) == 0;
566 void Converters::buildGraph()
568 G_.init(formats.size());
569 ConverterList::iterator beg = converterlist_.begin();
570 ConverterList::iterator end = converterlist_.end();
571 for (ConverterList::iterator it = beg; it != end ; ++it) {
572 int const s = formats.getNumber(it->from);
573 int const t = formats.getNumber(it->to);
579 std::vector<Format const *> const
580 Converters::intToFormat(std::vector<int> const & input)
582 vector<Format const *> result(input.size());
584 vector<int>::const_iterator it = input.begin();
585 vector<int>::const_iterator end = input.end();
586 vector<Format const *>::iterator rit = result.begin();
587 for ( ; it != end; ++it, ++rit) {
588 *rit = &formats.get(*it);
593 vector<Format const *> const
594 Converters::getReachableTo(string const & target, bool clear_visited)
596 vector<int> const & reachablesto =
597 G_.getReachableTo(formats.getNumber(target), clear_visited);
599 return intToFormat(reachablesto);
603 vector<Format const *> const
604 Converters::getReachable(string const & from, bool only_viewable,
607 vector<int> const & reachables =
608 G_.getReachable(formats.getNumber(from),
612 return intToFormat(reachables);
616 bool Converters::isReachable(string const & from, string const & to)
618 return G_.isReachable(formats.getNumber(from),
619 formats.getNumber(to));
623 Graph::EdgePath const
624 Converters::getPath(string const & from, string const & to)
626 return G_.getPath(formats.getNumber(from),
627 formats.getNumber(to));
631 /// The global instance
632 Converters converters;
634 // The global copy after reading lyxrc.defaults
635 Converters system_converters;