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 "bufferview_funcs.h"
20 #include "lyx_cb.h" // ShowMessage()
22 #include "BufferView.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/systemcall.h"
33 #include "BoostFormat.h"
37 #ifndef CXX_GLOBAL_CSTD
47 string const token_from("$$i");
48 string const token_base("$$b");
49 string const token_to("$$o");
50 string const token_path("$$p");
54 string const add_options(string const & command, string const & options)
57 string const tail = split(command, head, ' ');
58 return head + ' ' + options + ' ' + tail;
64 Converter::Converter(string const & f, string const & t, string const & c,
65 string const & l): from(f), to(t), command(c), flags(l),
66 From(0), To(0), latex(false),
67 original_dir(false), need_aux(false)
70 void Converter::readFlags()
72 string flag_list(flags);
73 while (!flag_list.empty()) {
74 string flag_name, flag_value;
75 flag_list = split(flag_list, flag_value, ',');
76 flag_value = split(flag_value, flag_name, '=');
77 if (flag_name == "latex")
79 else if (flag_name == "originaldir")
81 else if (flag_name == "needaux")
83 else if (flag_name == "resultdir")
84 result_dir = (flag_value.empty())
85 ? token_base : flag_value;
86 else if (flag_name == "resultfile")
87 result_file = flag_value;
88 else if (flag_name == "parselog")
89 parselog = flag_value;
91 if (!result_dir.empty() && result_file.empty())
92 result_file = "index." + formats.extension(to);
93 //if (!contains(command, token_from))
98 bool operator<(Converter const & a, Converter const & b)
100 // use the compare_ascii_no_case instead of compare_no_case,
101 // because in turkish, 'i' is not the lowercase version of 'I',
102 // and thus turkish locale breaks parsing of tags.
103 int const i = compare_ascii_no_case(a.From->prettyname(),
104 b.From->prettyname());
106 return compare_ascii_no_case(a.To->prettyname(), b.To->prettyname())
114 class compare_Converter {
116 compare_Converter(string const & f, string const & t)
118 bool operator()(Converter const & c) {
119 return c.from == from && c.to == to;
128 Converter const * Converters::getConverter(string const & from,
131 ConverterList::const_iterator cit =
132 find_if(converterlist_.begin(), converterlist_.end(),
133 compare_Converter(from, to));
134 if (cit != converterlist_.end())
141 int Converters::getNumber(string const & from, string const & to)
143 ConverterList::const_iterator cit =
144 find_if(converterlist_.begin(), converterlist_.end(),
145 compare_Converter(from, to));
146 if (cit != converterlist_.end())
147 return cit - converterlist_.begin();
153 void Converters::add(string const & from, string const & to,
154 string const & command, string const & flags)
158 ConverterList::iterator it = find_if(converterlist_.begin(),
159 converterlist_.end(),
160 compare_Converter(from, to));
162 Converter converter(from, to, command, flags);
163 if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
165 converter.command = command;
166 converter.flags = flags;
168 converter.readFlags();
170 if (converter.latex && (latex_command_.empty() || to == "dvi"))
171 latex_command_ = subst(command, token_from, "");
172 // If we have both latex & pdflatex, we set latex_command to latex.
173 // The latex_command is used to update the .aux file when running
174 // a converter that uses it.
176 if (it == converterlist_.end()) {
177 converterlist_.push_back(converter);
179 converter.From = it->From;
180 converter.To = it->To;
186 void Converters::erase(string const & from, string const & to)
188 ConverterList::iterator it = find_if(converterlist_.begin(),
189 converterlist_.end(),
190 compare_Converter(from, to));
191 if (it != converterlist_.end())
192 converterlist_.erase(it);
196 // This method updates the pointers From and To in all the converters.
197 // The code is not very efficient, but it doesn't matter as the number
198 // of formats and converters is small.
199 // Furthermore, this method is called only on startup, or after
200 // adding/deleting a format in FormPreferences (the latter calls can be
201 // eliminated if the formats in the Formats class are stored using a map or
202 // a list (instead of a vector), but this will cause other problems).
203 void Converters::update(Formats const & formats)
205 ConverterList::iterator it = converterlist_.begin();
206 ConverterList::iterator end = converterlist_.end();
207 for (; it != end; ++it) {
208 it->From = formats.getFormat(it->from);
209 it->To = formats.getFormat(it->to);
214 // This method updates the pointers From and To in the last converter.
215 // It is called when adding a new converter in FormPreferences
216 void Converters::updateLast(Formats const & formats)
218 if (converterlist_.begin() != converterlist_.end()) {
219 ConverterList::iterator it = converterlist_.end() - 1;
220 it->From = formats.getFormat(it->from);
221 it->To = formats.getFormat(it->to);
226 void Converters::sort()
228 std::sort(converterlist_.begin(), converterlist_.end());
232 bool Converters::usePdflatex(Graph::EdgePath const & path)
234 for (Graph::EdgePath::const_iterator cit = path.begin();
235 cit != path.end(); ++cit) {
236 Converter const & conv = converterlist_[*cit];
238 return contains(conv.to, "pdf");
244 bool Converters::convert(Buffer const * buffer,
245 string const & from_file, string const & to_file_base,
246 string const & from_format, string const & to_format,
249 to_file = ChangeExtension(to_file_base,
250 formats.extension(to_format));
252 if (from_format == to_format)
253 return move(from_file, to_file, false);
255 Graph::EdgePath edgepath = getPath(from_format, to_format);
256 if (edgepath.empty()) {
260 string path = OnlyPath(from_file);
263 bool run_latex = false;
264 string from_base = ChangeExtension(from_file, "");
265 string to_base = ChangeExtension(to_file, "");
267 string outfile = from_file;
268 for (Graph::EdgePath::const_iterator cit = edgepath.begin();
269 cit != edgepath.end(); ++cit) {
270 Converter const & conv = converterlist_[*cit];
271 bool dummy = conv.To->dummy() && conv.to != "program";
273 lyxerr[Debug::FILES] << "Converting from "
274 << conv.from << " to " << conv.to << endl;
276 outfile = conv.result_dir.empty()
277 ? ChangeExtension(from_file, conv.To->extension())
278 : AddName(subst(conv.result_dir,
279 token_base, from_base),
280 subst(conv.result_file,
281 token_base, OnlyFilename(from_base)));
283 // if input and output files are equal, we use a
284 // temporary file as intermediary (JMarc)
286 if (outfile == infile) {
287 real_outfile = infile;
288 outfile = AddName(buffer->tmppath, "tmpfile.out");
293 string command = subst(conv.command, token_from, "");
294 lyxerr[Debug::FILES] << "Running " << command << endl;
295 if (!runLaTeX(buffer, command))
298 if (conv.need_aux && !run_latex
299 && !latex_command_.empty()) {
301 << "Running " << latex_command_
302 << " to update aux file"<< endl;
303 runLaTeX(buffer, latex_command_);
306 string infile2 = (conv.original_dir)
307 ? infile : MakeRelPath(infile, path);
308 string outfile2 = (conv.original_dir)
309 ? outfile : MakeRelPath(outfile, path);
311 string command = conv.command;
312 command = subst(command, token_from, QuoteName(infile2));
313 command = subst(command, token_base, QuoteName(from_base));
314 command = subst(command, token_to, QuoteName(outfile2));
315 command = LibScriptSearch(command);
317 if (!conv.parselog.empty())
318 command += " 2> " + QuoteName(infile2 + ".out");
320 if (conv.from == "dvi" && conv.to == "ps")
321 command = add_options(command,
322 dvips_options(buffer));
323 else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
324 command = add_options(command,
325 dvipdfm_options(buffer));
327 lyxerr[Debug::FILES] << "Calling " << command << endl;
329 ShowMessage(buffer, _("Executing command:"), command);
331 Systemcall::Starttype type = (dummy)
332 ? Systemcall::DontWait : Systemcall::Wait;
335 if (conv.original_dir && buffer) {
336 Path p(buffer->filePath());
337 res = one.startscript(type, command);
339 res = one.startscript(type, command);
341 if (!real_outfile.empty()) {
342 if (!lyx::rename(outfile, real_outfile))
346 << "renaming file " << outfile
347 << " to " << real_outfile
351 if (!conv.parselog.empty()) {
352 string const logfile = infile2 + ".log";
353 string const script = LibScriptSearch(conv.parselog);
354 string const command2 = script +
355 " < " + QuoteName(infile2 + ".out") +
356 " > " + QuoteName(logfile);
357 one.startscript(Systemcall::Wait, command2);
358 if (!scanLog(buffer, command, logfile))
363 if (conv.to == "program")
364 Alert::alert(_("There were errors during the Build process."),
365 _("You should try to fix them."));
367 Alert::alert(_("Cannot convert file"),
368 _("Error while executing"),
369 command.substr(0, 50));
375 Converter const & conv = converterlist_[edgepath.back()];
376 if (conv.To->dummy())
380 if (!conv.result_dir.empty()) {
381 to_file = AddName(subst(conv.result_dir, token_base, to_base),
382 subst(conv.result_file,
383 token_base, OnlyFilename(to_base)));
384 if (from_base != to_base) {
385 string from = subst(conv.result_dir,
386 token_base, from_base);
387 string to = subst(conv.result_dir,
388 token_base, to_base);
389 if (!lyx::rename(from, to)) {
391 Alert::alert(_("Error while trying to move directory:"),
392 from, boost::io::str(boost::format(_("to %1$s")) % to));
394 Alert::alert(_("Error while trying to move directory:"),
395 from, _("to ") + to);
402 return move(outfile, to_file, conv.latex);
406 // If from = /path/file.ext and to = /path2/file2.ext2 then this method
407 // moves each /path/file*.ext file to /path2/file2*.ext2'
408 bool Converters::move(string const & from, string const & to, bool copy)
413 bool no_errors = true;
414 string const path = OnlyPath(from);
415 string const base = OnlyFilename(ChangeExtension(from, ""));
416 string const to_base = ChangeExtension(to, "");
417 string const to_extension = GetExtension(to);
419 vector<string> files = DirList(OnlyPath(from), GetExtension(from));
420 for (vector<string>::const_iterator it = files.begin();
421 it != files.end(); ++it)
422 if (prefixIs(*it, base)) {
423 string const from2 = path + *it;
424 string to2 = to_base + it->substr(base.length());
425 to2 = ChangeExtension(to2, to_extension);
426 lyxerr[Debug::FILES] << "moving " << from2
427 << " to " << to2 << endl;
428 bool const moved = (copy)
429 ? lyx::copy(from2, to2)
430 : lyx::rename(from2, to2);
431 if (!moved && no_errors) {
433 Alert::alert(_("Error while trying to move file:"),
434 from2, boost::io::str(boost::format(_("to %1$s")) % to2));
436 Alert::alert(_("Error while trying to move file:"),
437 from2, _("to ") + to2);
446 bool Converters::convert(Buffer const * buffer,
447 string const & from_file, string const & to_file_base,
448 string const & from_format, string const & to_format)
451 return convert(buffer, from_file, to_file_base, from_format, to_format,
456 bool Converters::formatIsUsed(string const & format)
458 ConverterList::const_iterator cit = converterlist_.begin();
459 ConverterList::const_iterator end = converterlist_.end();
460 for (; cit != end; ++cit) {
461 if (cit->from == format || cit->to == format)
468 bool Converters::scanLog(Buffer const * buffer, string const & command,
469 string const & filename)
474 BufferView * bv = buffer->getUser();
476 bv->owner()->busy(true);
477 // all error insets should have been removed by now
480 LaTeX latex("", filename, "");
482 int result = latex.scanLogFile(terr);
484 if ((result & LaTeX::ERRORS)) {
485 // Insert all errors as errors boxes
486 bv->insertErrors(terr);
487 #warning repaint() or update() or nothing ?
491 bv->owner()->busy(false);
494 if ((result & LaTeX::ERRORS)) {
495 int num_errors = latex.getNumErrors();
498 if (num_errors == 1) {
499 s = _("One error detected");
500 t = _("You should try to fix it.");
502 s = tostr(num_errors);
503 s += _(" errors detected.");
504 t = _("You should try to fix them.");
507 split(command, head, ' ');
509 Alert::alert(boost::io::str(boost::format(_("There were errors during running of %1$s")) % head),
512 Alert::alert(_("There were errors during running of ") + head,
516 } else if (result & LaTeX::NO_OUTPUT) {
517 string const s = _("The operation resulted in");
518 string const t = _("an empty file.");
519 Alert::alert(_("Resulting file is empty"), s, t);
526 bool Converters::runLaTeX(Buffer const * buffer, string const & command)
531 BufferView * bv = buffer->getUser();
534 bv->owner()->busy(true);
535 bv->owner()->message(_("Running LaTeX..."));
536 // all the autoinsets have already been removed
539 // do the LaTeX run(s)
540 string name = buffer->getLatexName();
541 LaTeX latex(command, name, buffer->filePath());
543 int result = latex.run(terr,
544 bv ? &bv->owner()->getLyXFunc() : 0);
547 if ((result & LaTeX::ERRORS)) {
548 // Insert all errors as errors boxes
549 bv->insertErrors(terr);
550 #warning repaint() or update() or nothing ?
556 // check return value from latex.run().
557 if ((result & LaTeX::NO_LOGFILE)) {
558 Alert::alert(_("LaTeX did not work!"),
559 _("Missing log file:"), name);
560 } else if ((result & LaTeX::ERRORS)) {
561 int num_errors = latex.getNumErrors();
564 if (num_errors == 1) {
565 s = _("One error detected");
566 t = _("You should try to fix it.");
568 s = tostr(num_errors);
569 s += _(" errors detected.");
570 t = _("You should try to fix them.");
572 Alert::alert(_("There were errors during the LaTeX run."),
574 } else if (result & LaTeX::NO_OUTPUT) {
575 string const s = _("The operation resulted in");
576 string const t = _("an empty file.");
577 Alert::alert(_("Resulting file is empty"), s, t);
581 bv->owner()->busy(false);
583 int const ERROR_MASK =
588 return (result & ERROR_MASK) == 0;
593 string const Converters::dvips_options(Buffer const * buffer)
599 if (buffer->params.use_geometry
600 && buffer->params.papersize2 == BufferParams::VM_PAPER_CUSTOM
601 && !lyxrc.print_paper_dimension_flag.empty()
602 && !buffer->params.paperwidth.empty()
603 && !buffer->params.paperheight.empty()) {
604 // using a custom papersize
605 result = lyxrc.print_paper_dimension_flag;
606 result += ' ' + buffer->params.paperwidth;
607 result += ',' + buffer->params.paperheight;
609 string const paper_option = papersize(buffer);
610 if (paper_option != "letter" ||
611 buffer->params.orientation != BufferParams::ORIENTATION_LANDSCAPE) {
612 // dvips won't accept -t letter -t landscape. In all other
613 // cases, include the paper size explicitly.
614 result = lyxrc.print_paper_flag;
615 result += ' ' + paper_option;
618 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE &&
619 buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM)
620 result += ' ' + lyxrc.print_landscape_flag;
625 string const Converters::dvipdfm_options(Buffer const * buffer)
631 if (buffer->params.papersize2 != BufferParams::VM_PAPER_CUSTOM) {
632 string const paper_size = papersize(buffer);
633 if (paper_size != "b5" && paper_size != "foolscap")
634 result = "-p "+ paper_size;
636 if (buffer->params.orientation == BufferParams::ORIENTATION_LANDSCAPE)
643 void Converters::buildGraph()
645 G_.init(formats.size());
646 ConverterList::iterator beg = converterlist_.begin();
647 ConverterList::iterator end = converterlist_.end();
648 for (ConverterList::iterator it = beg; it != end ; ++it) {
649 int const s = formats.getNumber(it->from);
650 int const t = formats.getNumber(it->to);
655 vector<Format const *> const
656 Converters::intToFormat(std::vector<int> const & input)
658 vector<Format const *> result(input.size());
660 vector<int>::const_iterator it = input.begin();
661 vector<int>::const_iterator end = input.end();
662 vector<Format const *>::iterator rit = result.begin();
663 for ( ; it != end; ++it, ++rit) {
664 *rit = &formats.get(*it);
669 vector<Format const *> const
670 Converters::getReachableTo(string const & target, bool clear_visited)
672 vector<int> const & reachablesto =
673 G_.getReachableTo(formats.getNumber(target), clear_visited);
675 return intToFormat(reachablesto);
678 vector<Format const *> const
679 Converters::getReachable(string const & from, bool only_viewable,
682 vector<int> const & reachables =
683 G_.getReachable(formats.getNumber(from),
687 return intToFormat(reachables);
690 bool Converters::isReachable(string const & from, string const & to)
692 return G_.isReachable(formats.getNumber(from),
693 formats.getNumber(to));
696 Graph::EdgePath const
697 Converters::getPath(string const & from, string const & to)
699 return G_.getPath(formats.getNumber(from),
700 formats.getNumber(to));
703 /// The global instance
704 Converters converters;
706 // The global copy after reading lyxrc.defaults
707 Converters system_converters;