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 "buffer_funcs.h"
19 #include "bufferview_funcs.h"
20 #include "errorlist.h"
25 #include "frontends/Alert.h"
26 #include "frontends/LyXView.h"
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"
34 #include <boost/signals/signal1.hpp>
35 #include <boost/signals/trackable.hpp>
39 using namespace lyx::support;
41 #ifndef CXX_GLOBAL_CSTD
51 string const token_from("$$i");
52 string const token_base("$$b");
53 string const token_to("$$o");
54 string const token_path("$$p");
58 string const add_options(string const & command, string const & options)
61 string const tail = split(command, head, ' ');
62 return head + ' ' + options + ' ' + tail;
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)
75 void Converter::readFlags()
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")
84 else if (flag_name == "originaldir")
86 else if (flag_name == "needaux")
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;
96 if (!result_dir.empty() && result_file.empty())
97 result_file = "index." + formats.extension(to);
98 //if (!contains(command, token_from))
103 bool operator<(Converter const & a, Converter const & b)
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());
111 return compare_ascii_no_case(a.To->prettyname(),
112 b.To->prettyname()) < 0;
119 class compare_Converter {
121 compare_Converter(string const & f, string const & t)
123 bool operator()(Converter const & c) {
124 return c.from == from && c.to == to;
133 Converter const * Converters::getConverter(string const & from,
136 ConverterList::const_iterator cit =
137 find_if(converterlist_.begin(), converterlist_.end(),
138 compare_Converter(from, to));
139 if (cit != converterlist_.end())
146 int Converters::getNumber(string const & from, string const & to)
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();
158 void Converters::add(string const & from, string const & to,
159 string const & command, string const & flags)
163 ConverterList::iterator it = find_if(converterlist_.begin(),
164 converterlist_.end(),
165 compare_Converter(from, to));
167 Converter converter(from, to, command, flags);
168 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
170 converter.command = command;
171 converter.flags = flags;
173 converter.readFlags();
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.
181 if (it == converterlist_.end()) {
182 converterlist_.push_back(converter);
184 converter.From = it->From;
185 converter.To = it->To;
191 void Converters::erase(string const & from, string const & to)
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);
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)
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);
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)
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);
231 void Converters::sort()
233 std::sort(converterlist_.begin(), converterlist_.end());
237 bool Converters::usePdflatex(Graph::EdgePath const & path)
239 for (Graph::EdgePath::const_iterator cit = path.begin();
240 cit != path.end(); ++cit) {
241 Converter const & conv = converterlist_[*cit];
243 return contains(conv.to, "pdf");
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,
254 to_file = ChangeExtension(to_file_base,
255 formats.extension(to_format));
257 if (from_format == to_format)
258 return move(from_file, to_file, false);
260 Graph::EdgePath edgepath = getPath(from_format, to_format);
261 if (edgepath.empty()) {
264 LatexRunParams runparams;
265 runparams.flavor = usePdflatex(edgepath) ?
266 LatexRunParams::PDFLATEX : LatexRunParams::LATEX;
268 string path = OnlyPath(from_file);
271 bool run_latex = false;
272 string from_base = ChangeExtension(from_file, "");
273 string to_base = ChangeExtension(to_file, "");
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";
281 lyxerr[Debug::FILES] << "Converting from "
282 << conv.from << " to " << conv.to << endl;
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)));
291 // if input and output files are equal, we use a
292 // temporary file as intermediary (JMarc)
294 if (outfile == infile) {
295 real_outfile = infile;
296 outfile = AddName(buffer->tmppath, "tmpfile.out");
301 string command = subst(conv.command, token_from, "");
302 lyxerr[Debug::FILES] << "Running " << command << endl;
303 if (!runLaTeX(buffer, command, runparams))
306 if (conv.need_aux && !run_latex
307 && !latex_command_.empty()) {
309 << "Running " << latex_command_
310 << " to update aux file"<< endl;
311 runLaTeX(buffer, latex_command_, runparams);
314 string infile2 = (conv.original_dir)
315 ? infile : MakeRelPath(infile, path);
316 string outfile2 = (conv.original_dir)
317 ? outfile : MakeRelPath(outfile, path);
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);
325 if (!conv.parselog.empty())
326 command += " 2> " + QuoteName(infile2 + ".out");
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));
335 lyxerr[Debug::FILES] << "Calling " << command << endl;
337 buffer->message(_("Executing command: ") + command);
338 Systemcall::Starttype type = (dummy)
339 ? Systemcall::DontWait : Systemcall::Wait;
342 if (conv.original_dir && buffer) {
343 Path p(buffer->filePath());
344 res = one.startscript(type, command);
346 res = one.startscript(type, command);
348 if (!real_outfile.empty()) {
349 if (!rename(outfile, real_outfile))
353 << "renaming file " << outfile
354 << " to " << real_outfile
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))
370 if (conv.to == "program") {
371 Alert::error(_("Build errors"),
372 _("There were errors during the build process."));
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)));
385 Converter const & conv = converterlist_[edgepath.back()];
386 if (conv.To->dummy())
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."),
408 return move(outfile, to_file, conv.latex);
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)
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);
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."),
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)
453 return convert(buffer, from_file, to_file_base, from_format, to_format,
458 bool Converters::formatIsUsed(string const & format)
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)
470 bool Converters::scanLog(Buffer const * buffer, string const & /*command*/,
471 string const & filename)
476 LatexRunParams runparams;
477 runparams.flavor = LatexRunParams::LATEX;
478 LaTeX latex("", runparams, filename, "");
480 int result = latex.scanLogFile(terr);
482 if (result & LaTeX::ERRORS)
483 bufferErrors(*buffer, terr);
490 class showMessage : public boost::signals::trackable {
492 showMessage(Buffer const * b) : buffer_(b) {};
493 void operator()(string m)
498 Buffer const * buffer_;
503 bool Converters::runLaTeX(Buffer const * buffer, string const & command,
504 LatexRunParams const & runparams)
506 // when is this needed?
511 buffer->message(_("Running LaTeX..."));
513 // do the LaTeX run(s)
514 string name = buffer->getLatexName();
515 LaTeX latex(command, runparams, name, buffer->filePath());
517 showMessage show(buffer);
518 latex.message.connect(show);
519 int result = latex.run(terr);
521 if (result & LaTeX::ERRORS)
522 bufferErrors(*buffer, terr);
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."));
538 int const ERROR_MASK =
543 return (result & ERROR_MASK) == 0;
548 string const Converters::dvips_options(Buffer const * buffer)
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;
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;
573 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
574 buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
575 result += ' ' + lyxrc.print_landscape_flag;
580 string const Converters::dvipdfm_options(Buffer const * buffer)
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;
591 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
599 void Converters::buildGraph()
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);
612 std::vector<Format const *> const
613 Converters::intToFormat(std::vector<int> const & input)
615 vector<Format const *> result(input.size());
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);
626 vector<Format const *> const
627 Converters::getReachableTo(string const & target, bool clear_visited)
629 vector<int> const & reachablesto =
630 G_.getReachableTo(formats.getNumber(target), clear_visited);
632 return intToFormat(reachablesto);
636 vector<Format const *> const
637 Converters::getReachable(string const & from, bool only_viewable,
640 vector<int> const & reachables =
641 G_.getReachable(formats.getNumber(from),
645 return intToFormat(reachables);
649 bool Converters::isReachable(string const & from, string const & to)
651 return G_.isReachable(formats.getNumber(from),
652 formats.getNumber(to));
656 Graph::EdgePath const
657 Converters::getPath(string const & from, string const & to)
659 return G_.getPath(formats.getNumber(from),
660 formats.getNumber(to));
664 /// The global instance
665 Converters converters;
667 // The global copy after reading lyxrc.defaults
668 Converters system_converters;