]> git.lyx.org Git - lyx.git/blob - src/Converter.cpp
Always require a mask to get a temporary filename
[lyx.git] / src / Converter.cpp
1 /**
2  * \file Converter.cpp
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
15 #include "Buffer.h"
16 #include "buffer_funcs.h"
17 #include "BufferParams.h"
18 #include "ConverterCache.h"
19 #include "Encoding.h"
20 #include "ErrorList.h"
21 #include "Format.h"
22 #include "Language.h"
23 #include "LaTeX.h"
24 #include "Mover.h"
25
26 #include "frontends/alert.h"
27
28 #include "support/debug.h"
29 #include "support/FileNameList.h"
30 #include "support/filetools.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33 #include "support/os.h"
34 #include "support/Package.h"
35 #include "support/Path.h"
36 #include "support/Systemcall.h"
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43 namespace Alert = lyx::frontend::Alert;
44
45
46 namespace {
47
48 string const token_from("$$i");
49 string const token_base("$$b");
50 string const token_to("$$o");
51 string const token_path("$$p");
52 string const token_orig_path("$$r");
53 string const token_orig_from("$$f");
54 string const token_encoding("$$e");
55 string const token_latex_encoding("$$E");
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
66 string const dvipdfm_options(BufferParams const & bp)
67 {
68         string result;
69
70         if (bp.papersize != PAPER_CUSTOM) {
71                 string const paper_size = bp.paperSizeName(BufferParams::DVIPDFM);
72                 if (!paper_size.empty())
73                         result = "-p "+ paper_size;
74
75                 if (bp.orientation == ORIENTATION_LANDSCAPE)
76                         result += " -l";
77         }
78
79         return result;
80 }
81
82
83 class ConverterEqual {
84 public:
85         ConverterEqual(string const & from, string const & to)
86                 : from_(from), to_(to) {}
87         bool operator()(Converter const & c) const {
88                 return c.from == from_ && c.to == to_;
89         }
90 private:
91         string const from_;
92         string const to_;
93 };
94
95 } // namespace anon
96
97
98 Converter::Converter(string const & f, string const & t,
99                      string const & c, string const & l)
100         : from(f), to(t), command(c), flags(l),
101           From(0), To(0), latex(false), xml(false),
102           need_aux(false)
103 {}
104
105
106 void Converter::readFlags()
107 {
108         string flag_list(flags);
109         while (!flag_list.empty()) {
110                 string flag_name, flag_value;
111                 flag_list = split(flag_list, flag_value, ',');
112                 flag_value = split(flag_value, flag_name, '=');
113                 if (flag_name == "latex") {
114                         latex = true;
115                         latex_flavor = flag_value.empty() ?
116                                 "latex" : flag_value;
117                 } else if (flag_name == "xml")
118                         xml = true;
119                 else if (flag_name == "needaux")
120                         need_aux = true;
121                 else if (flag_name == "resultdir")
122                         result_dir = (flag_value.empty())
123                                 ? token_base : flag_value;
124                 else if (flag_name == "resultfile")
125                         result_file = flag_value;
126                 else if (flag_name == "parselog")
127                         parselog = flag_value;
128         }
129         if (!result_dir.empty() && result_file.empty())
130                 result_file = "index." + formats.extension(to);
131         //if (!contains(command, token_from))
132         //      latex = true;
133 }
134
135
136 Converter const * Converters::getConverter(string const & from,
137                                             string const & to) const
138 {
139         ConverterList::const_iterator const cit =
140                 find_if(converterlist_.begin(), converterlist_.end(),
141                         ConverterEqual(from, to));
142         if (cit != converterlist_.end())
143                 return &(*cit);
144         else
145                 return 0;
146 }
147
148
149 int Converters::getNumber(string const & from, string const & to) const
150 {
151         ConverterList::const_iterator const cit =
152                 find_if(converterlist_.begin(), converterlist_.end(),
153                         ConverterEqual(from, to));
154         if (cit != converterlist_.end())
155                 return distance(converterlist_.begin(), cit);
156         else
157                 return -1;
158 }
159
160
161 void Converters::add(string const & from, string const & to,
162                      string const & command, string const & flags)
163 {
164         formats.add(from);
165         formats.add(to);
166         ConverterList::iterator it = find_if(converterlist_.begin(),
167                                              converterlist_.end(),
168                                              ConverterEqual(from , to));
169
170         Converter converter(from, to, command, flags);
171         if (it != converterlist_.end() && !flags.empty() && flags[0] == '*') {
172                 converter = *it;
173                 converter.command = command;
174                 converter.flags = flags;
175         }
176         converter.readFlags();
177
178         // The latex_command is used to update the .aux file when running
179         // a converter that uses it.
180         if (converter.latex) {
181                 if (latex_command_.empty() ||
182                     converter.latex_flavor == "latex")
183                         latex_command_ = subst(command, token_from, "");
184                 if (dvilualatex_command_.empty() ||
185                     converter.latex_flavor == "dvilualatex")
186                         dvilualatex_command_ = subst(command, token_from, "");
187                 if (lualatex_command_.empty() ||
188                     converter.latex_flavor == "lualatex")
189                         lualatex_command_ = subst(command, token_from, "");
190                 if (pdflatex_command_.empty() ||
191                     converter.latex_flavor == "pdflatex")
192                         pdflatex_command_ = subst(command, token_from, "");
193                 if (xelatex_command_.empty() ||
194                     converter.latex_flavor == "xelatex")
195                         xelatex_command_ = subst(command, token_from, "");
196         }
197
198         if (it == converterlist_.end()) {
199                 converterlist_.push_back(converter);
200         } else {
201                 converter.From = it->From;
202                 converter.To = it->To;
203                 *it = converter;
204         }
205 }
206
207
208 void Converters::erase(string const & from, string const & to)
209 {
210         ConverterList::iterator const it =
211                 find_if(converterlist_.begin(),
212                         converterlist_.end(),
213                         ConverterEqual(from, to));
214         if (it != converterlist_.end())
215                 converterlist_.erase(it);
216 }
217
218
219 // This method updates the pointers From and To in all the converters.
220 // The code is not very efficient, but it doesn't matter as the number
221 // of formats and converters is small.
222 // Furthermore, this method is called only on startup, or after
223 // adding/deleting a format in FormPreferences (the latter calls can be
224 // eliminated if the formats in the Formats class are stored using a map or
225 // a list (instead of a vector), but this will cause other problems).
226 void Converters::update(Formats const & formats)
227 {
228         ConverterList::iterator it = converterlist_.begin();
229         ConverterList::iterator end = converterlist_.end();
230         for (; it != end; ++it) {
231                 it->From = formats.getFormat(it->from);
232                 it->To = formats.getFormat(it->to);
233         }
234 }
235
236
237 // This method updates the pointers From and To in the last converter.
238 // It is called when adding a new converter in FormPreferences
239 void Converters::updateLast(Formats const & formats)
240 {
241         if (converterlist_.begin() != converterlist_.end()) {
242                 ConverterList::iterator it = converterlist_.end() - 1;
243                 it->From = formats.getFormat(it->from);
244                 it->To = formats.getFormat(it->to);
245         }
246 }
247
248
249 OutputParams::FLAVOR Converters::getFlavor(Graph::EdgePath const & path,
250                                            Buffer const * buffer)
251 {
252         for (Graph::EdgePath::const_iterator cit = path.begin();
253              cit != path.end(); ++cit) {
254                 Converter const & conv = converterlist_[*cit];
255                 if (conv.latex) {
256                         if (conv.latex_flavor == "latex")
257                                 return OutputParams::LATEX;
258                         if (conv.latex_flavor == "xelatex")
259                                 return OutputParams::XETEX;
260                         if (conv.latex_flavor == "lualatex")
261                                 return OutputParams::LUATEX;
262                         if (conv.latex_flavor == "dvilualatex")
263                                 return OutputParams::DVILUATEX;
264                         if (conv.latex_flavor == "pdflatex")
265                                 return OutputParams::PDFLATEX;
266                 }
267                 if (conv.xml)
268                         return OutputParams::XML;
269         }
270         return buffer ? buffer->params().getOutputFlavor()
271                       : OutputParams::LATEX;
272 }
273
274
275 bool Converters::convert(Buffer const * buffer,
276                          FileName const & from_file, FileName const & to_file,
277                          FileName const & orig_from,
278                          string const & from_format, string const & to_format,
279                          ErrorList & errorList, int conversionflags)
280 {
281         if (from_format == to_format)
282                 return move(from_format, from_file, to_file, false);
283
284         if ((conversionflags & try_cache) &&
285             ConverterCache::get().inCache(orig_from, to_format))
286                 return ConverterCache::get().copy(orig_from, to_format, to_file);
287
288         Graph::EdgePath edgepath = getPath(from_format, to_format);
289         if (edgepath.empty()) {
290                 if (conversionflags & try_default) {
291                         // if no special converter defined, then we take the
292                         // default one from ImageMagic.
293                         string const from_ext = from_format.empty() ?
294                                 getExtension(from_file.absFileName()) :
295                                 formats.extension(from_format);
296                         string const to_ext = formats.extension(to_format);
297                         string const command =
298                                 os::python() + ' ' +
299                                 quoteName(libFileSearch("scripts", "convertDefault.py").toFilesystemEncoding()) +
300                                 ' ' +
301                                 quoteName(from_ext + ':' + from_file.toFilesystemEncoding()) +
302                                 ' ' +
303                                 quoteName(to_ext + ':' + to_file.toFilesystemEncoding());
304                         LYXERR(Debug::FILES, "No converter defined! "
305                                    "I use convertDefault.py:\n\t" << command);
306                         Systemcall one;
307                         one.startscript(Systemcall::Wait, command, buffer ?
308                                         buffer->filePath() : string());
309                         if (to_file.isReadableFile()) {
310                                 if (conversionflags & try_cache)
311                                         ConverterCache::get().add(orig_from,
312                                                         to_format, to_file);
313                                 return true;
314                         }
315                 }
316
317                 // only warn once per session and per file type
318                 static std::map<string, string> warned;
319                 if (warned.find(from_format) != warned.end() && warned.find(from_format)->second == to_format) {
320                         return false;
321                 }
322                 warned.insert(make_pair(from_format, to_format));
323
324                 Alert::error(_("Cannot convert file"),
325                              bformat(_("No information for converting %1$s "
326                                                     "format files to %2$s.\n"
327                                                     "Define a converter in the preferences."),
328                                                         from_ascii(from_format), from_ascii(to_format)));
329                 return false;
330         }
331
332         // buffer is only invalid for importing, and then runparams is not
333         // used anyway.
334         OutputParams runparams(buffer ? &buffer->params().encoding() : 0);
335         runparams.flavor = getFlavor(edgepath, buffer);
336
337         if (buffer) {
338                 runparams.use_japanese = buffer->params().bufferFormat() == "platex";
339                 runparams.use_indices = buffer->params().use_indices;
340                 runparams.bibtex_command = (buffer->params().bibtex_command == "default") ?
341                         string() : buffer->params().bibtex_command;
342                 runparams.index_command = (buffer->params().index_command == "default") ?
343                         string() : buffer->params().index_command;
344         }
345
346         // Some converters (e.g. lilypond) can only output files to the
347         // current directory, so we need to change the current directory.
348         // This has the added benefit that all other files that may be
349         // generated by the converter are deleted when LyX closes and do not
350         // clutter the real working directory.
351         // FIXME: This does not work if path is an UNC path on windows
352         //        (bug 6127).
353         string const path(onlyPath(from_file.absFileName()));
354         // Prevent the compiler from optimizing away p
355         FileName pp(path);
356         PathChanger p(pp);
357
358         // empty the error list before any new conversion takes place.
359         errorList.clear();
360
361         bool run_latex = false;
362         string from_base = changeExtension(from_file.absFileName(), "");
363         string to_base = changeExtension(to_file.absFileName(), "");
364         FileName infile;
365         FileName outfile = from_file;
366         for (Graph::EdgePath::const_iterator cit = edgepath.begin();
367              cit != edgepath.end(); ++cit) {
368                 Converter const & conv = converterlist_[*cit];
369                 bool dummy = conv.To->dummy() && conv.to != "program";
370                 if (!dummy) {
371                         LYXERR(Debug::FILES, "Converting from  "
372                                << conv.from << " to " << conv.to);
373                 }
374                 infile = outfile;
375                 outfile = FileName(conv.result_file.empty()
376                         ? changeExtension(from_file.absFileName(), conv.To->extension())
377                         : addName(subst(conv.result_dir,
378                                         token_base, from_base),
379                                   subst(conv.result_file,
380                                         token_base, onlyFileName(from_base))));
381
382                 // if input and output files are equal, we use a
383                 // temporary file as intermediary (JMarc)
384                 FileName real_outfile;
385                 if (!conv.result_file.empty())
386                         real_outfile = FileName(changeExtension(from_file.absFileName(),
387                                 conv.To->extension()));
388                 if (outfile == infile) {
389                         real_outfile = infile;
390                         // when importing, a buffer does not necessarily exist
391                         if (buffer)
392                                 outfile = FileName(addName(buffer->temppath(), "tmpfile.out"));
393                         else
394                                 outfile = FileName(addName(package().temp_dir().absFileName(),
395                                                    "tmpfile.out"));
396                 }
397
398                 if (conv.latex) {
399                         run_latex = true;
400                         string command = conv.command;
401                         command = subst(command, token_from, "");
402                         command = subst(command, token_latex_encoding, buffer ?
403                                 buffer->params().encoding().latexName() : string());
404                         LYXERR(Debug::FILES, "Running " << command);
405                         if (!runLaTeX(*buffer, command, runparams, errorList))
406                                 return false;
407                 } else {
408                         if (conv.need_aux && !run_latex) {
409                                 string command;
410                                 switch (runparams.flavor) {
411                                 case OutputParams::DVILUATEX:
412                                         command = dvilualatex_command_;
413                                         break;
414                                 case OutputParams::LUATEX:
415                                         command = lualatex_command_;
416                                         break;
417                                 case OutputParams::PDFLATEX:
418                                         command = pdflatex_command_;
419                                         break;
420                                 case OutputParams::XETEX:
421                                         command = xelatex_command_;
422                                         break;
423                                 default:
424                                         command = latex_command_;
425                                         break;
426                                 }
427                                 if (!command.empty()) {
428                                         LYXERR(Debug::FILES, "Running "
429                                                 << command
430                                                 << " to update aux file");
431                                         if (!runLaTeX(*buffer, command,
432                                                       runparams, errorList))
433                                                 return false;
434                                 }
435                         }
436
437                         // FIXME UNICODE
438                         string const infile2 =
439                                 to_utf8(makeRelPath(from_utf8(infile.absFileName()), from_utf8(path)));
440                         string const outfile2 =
441                                 to_utf8(makeRelPath(from_utf8(outfile.absFileName()), from_utf8(path)));
442
443                         string command = conv.command;
444                         command = subst(command, token_from, quoteName(infile2));
445                         command = subst(command, token_base, quoteName(from_base));
446                         command = subst(command, token_to, quoteName(outfile2));
447                         command = subst(command, token_path, quoteName(onlyPath(infile.absFileName())));
448                         command = subst(command, token_orig_path, quoteName(onlyPath(orig_from.absFileName())));
449                         command = subst(command, token_orig_from, quoteName(onlyFileName(orig_from.absFileName())));
450                         command = subst(command, token_encoding, buffer ? buffer->params().encoding().iconvName() : string());
451                         command = libScriptSearch(command);
452
453                         if (!conv.parselog.empty())
454                                 command += " 2> " + quoteName(infile2 + ".out");
455
456                         if (conv.from == "dvi" && conv.to == "ps")
457                                 command = add_options(command,
458                                                       buffer->params().dvips_options());
459                         else if (conv.from == "dvi" && prefixIs(conv.to, "pdf"))
460                                 command = add_options(command,
461                                                       dvipdfm_options(buffer->params()));
462
463                         LYXERR(Debug::FILES, "Calling " << command);
464                         if (buffer)
465                                 buffer->message(_("Executing command: ")
466                                 + from_utf8(command));
467
468                         Systemcall one;
469                         int res;
470                         if (dummy) {
471                                 res = one.startscript(Systemcall::DontWait,
472                                         to_filesystem8bit(from_utf8(command)),
473                                         buffer ? buffer->filePath() : string());
474                                 // We're not waiting for the result, so we can't do anything
475                                 // else here.
476                         } else {
477                                 res = one.startscript(Systemcall::Wait,
478                                                 to_filesystem8bit(from_utf8(command)),
479                                                 buffer ? buffer->filePath()
480                                                        : string());
481                                 if (!real_outfile.empty()) {
482                                         Mover const & mover = getMover(conv.to);
483                                         if (!mover.rename(outfile, real_outfile))
484                                                 res = -1;
485                                         else
486                                                 LYXERR(Debug::FILES, "renaming file " << outfile
487                                                         << " to " << real_outfile);
488                                         // Finally, don't forget to tell any future
489                                         // converters to use the renamed file...
490                                         outfile = real_outfile;
491                                 }
492   
493                                 if (!conv.parselog.empty()) {
494                                         string const logfile =  infile2 + ".log";
495                                         string const script = libScriptSearch(conv.parselog);
496                                         string const command2 = script +
497                                                 " < " + quoteName(infile2 + ".out") +
498                                                 " > " + quoteName(logfile);
499                                         one.startscript(Systemcall::Wait,
500                                                 to_filesystem8bit(from_utf8(command2)),
501                                                 buffer->filePath());
502                                         if (!scanLog(*buffer, command, makeAbsPath(logfile, path), errorList))
503                                                 return false;
504                                 }
505                         }
506
507                         if (res) {
508                                 if (conv.to == "program") {
509                                         Alert::error(_("Build errors"),
510                                                 _("There were errors during the build process."));
511                                 } else {
512 // FIXME: this should go out of here. For example, here we cannot say if
513 // it is a document (.lyx) or something else. Same goes for elsewhere.
514                                         Alert::error(_("Cannot convert file"),
515                                                 bformat(_("An error occurred while running:\n%1$s"),
516                                                 wrapParas(from_utf8(command))));
517                                 }
518                                 return false;
519                         }
520                 }
521         }
522
523         Converter const & conv = converterlist_[edgepath.back()];
524         if (conv.To->dummy())
525                 return true;
526
527         if (!conv.result_dir.empty()) {
528                 // The converter has put the file(s) in a directory.
529                 // In this case we ignore the given to_file.
530                 if (from_base != to_base) {
531                         string const from = subst(conv.result_dir,
532                                             token_base, from_base);
533                         string const to = subst(conv.result_dir,
534                                           token_base, to_base);
535                         Mover const & mover = getMover(conv.from);
536                         if (!mover.rename(FileName(from), FileName(to))) {
537                                 Alert::error(_("Cannot convert file"),
538                                         bformat(_("Could not move a temporary directory from %1$s to %2$s."),
539                                                 from_utf8(from), from_utf8(to)));
540                                 return false;
541                         }
542                 }
543                 return true;
544         } else {
545                 if (conversionflags & try_cache)
546                         ConverterCache::get().add(orig_from, to_format, outfile);
547                 return move(conv.to, outfile, to_file, conv.latex);
548         }
549 }
550
551
552 bool Converters::move(string const & fmt,
553                       FileName const & from, FileName const & to, bool copy)
554 {
555         if (from == to)
556                 return true;
557
558         bool no_errors = true;
559         string const path = onlyPath(from.absFileName());
560         string const base = onlyFileName(removeExtension(from.absFileName()));
561         string const to_base = removeExtension(to.absFileName());
562         string const to_extension = getExtension(to.absFileName());
563
564         support::FileNameList const files = FileName(path).dirList(getExtension(from.absFileName()));
565         for (support::FileNameList::const_iterator it = files.begin();
566              it != files.end(); ++it) {
567                 string const from2 = it->absFileName();
568                 string const file2 = onlyFileName(from2);
569                 if (prefixIs(file2, base)) {
570                         string const to2 = changeExtension(
571                                 to_base + file2.substr(base.length()),
572                                 to_extension);
573                         LYXERR(Debug::FILES, "moving " << from2 << " to " << to2);
574
575                         Mover const & mover = getMover(fmt);
576                         bool const moved = copy
577                                 ? mover.copy(*it, FileName(to2))
578                                 : mover.rename(*it, FileName(to2));
579                         if (!moved && no_errors) {
580                                 Alert::error(_("Cannot convert file"),
581                                         bformat(copy ?
582                                                 _("Could not copy a temporary file from %1$s to %2$s.") :
583                                                 _("Could not move a temporary file from %1$s to %2$s."),
584                                                 from_utf8(from2), from_utf8(to2)));
585                                 no_errors = false;
586                         }
587                 }
588         }
589         return no_errors;
590 }
591
592
593 bool Converters::formatIsUsed(string const & format)
594 {
595         ConverterList::const_iterator cit = converterlist_.begin();
596         ConverterList::const_iterator end = converterlist_.end();
597         for (; cit != end; ++cit) {
598                 if (cit->from == format || cit->to == format)
599                         return true;
600         }
601         return false;
602 }
603
604
605 bool Converters::scanLog(Buffer const & buffer, string const & /*command*/,
606                          FileName const & filename, ErrorList & errorList)
607 {
608         OutputParams runparams(0);
609         runparams.flavor = OutputParams::LATEX;
610         LaTeX latex("", runparams, filename);
611         TeXErrors terr;
612         int const result = latex.scanLogFile(terr);
613
614         if (result & LaTeX::ERRORS)
615                 buffer.bufferErrors(terr, errorList);
616
617         return true;
618 }
619
620
621 namespace {
622
623 class ShowMessage
624         : public boost::signals::trackable {
625 public:
626         ShowMessage(Buffer const & b) : buffer_(b) {}
627         void operator()(docstring const & msg) const { buffer_.message(msg); }
628 private:
629         Buffer const & buffer_;
630 };
631
632 }
633
634
635 bool Converters::runLaTeX(Buffer const & buffer, string const & command,
636                           OutputParams const & runparams, ErrorList & errorList)
637 {
638         buffer.setBusy(true);
639         buffer.message(_("Running LaTeX..."));
640
641         runparams.document_language = buffer.params().language->babel();
642
643         // do the LaTeX run(s)
644         string const name = buffer.latexName();
645         LaTeX latex(command, runparams, FileName(makeAbsPath(name)),
646                     buffer.filePath());
647         TeXErrors terr;
648         ShowMessage show(buffer);
649         latex.message.connect(show);
650         int const result = latex.run(terr);
651
652         if (result & LaTeX::ERRORS)
653                 buffer.bufferErrors(terr, errorList);
654
655         // check return value from latex.run().
656         if ((result & LaTeX::NO_LOGFILE) && !buffer.isClone()) {
657                 docstring const str =
658                         bformat(_("LaTeX did not run successfully. "
659                                                "Additionally, LyX could not locate "
660                                                "the LaTeX log %1$s."), from_utf8(name));
661                 Alert::error(_("LaTeX failed"), str);
662         } else if ((result & LaTeX::NO_OUTPUT) && !buffer.isClone()) {
663                 Alert::warning(_("Output is empty"),
664                                _("An empty output file was generated."));
665         }
666
667
668         buffer.setBusy(false);
669
670         int const ERROR_MASK =
671                         LaTeX::NO_LOGFILE |
672                         LaTeX::ERRORS |
673                         LaTeX::NO_OUTPUT;
674
675         return (result & ERROR_MASK) == 0;
676
677 }
678
679
680
681 void Converters::buildGraph()
682 {
683         // clear graph's data structures
684         G_.init(formats.size());
685         // each of the converters knows how to convert one format to another
686         // so, for each of them, we create an arrow on the graph, going from 
687         // the one to the other
688         ConverterList::iterator it = converterlist_.begin();
689         ConverterList::iterator const end = converterlist_.end();
690         for (; it != end ; ++it) {
691                 int const from = formats.getNumber(it->from);
692                 int const to   = formats.getNumber(it->to);
693                 G_.addEdge(from, to);
694         }
695 }
696
697
698 vector<Format const *> const
699 Converters::intToFormat(vector<int> const & input)
700 {
701         vector<Format const *> result(input.size());
702
703         vector<int>::const_iterator it = input.begin();
704         vector<int>::const_iterator const end = input.end();
705         vector<Format const *>::iterator rit = result.begin();
706         for ( ; it != end; ++it, ++rit) {
707                 *rit = &formats.get(*it);
708         }
709         return result;
710 }
711
712
713 vector<Format const *> const
714 Converters::getReachableTo(string const & target, bool const clear_visited)
715 {
716         vector<int> const & reachablesto =
717                 G_.getReachableTo(formats.getNumber(target), clear_visited);
718
719         return intToFormat(reachablesto);
720 }
721
722
723 vector<Format const *> const
724 Converters::getReachable(string const & from, bool const only_viewable,
725                          bool const clear_visited, set<string> const & excludes)
726 {
727         set<int> excluded_numbers;;
728
729         set<string>::const_iterator sit = excludes.begin();
730         set<string>::const_iterator const end = excludes.end();
731         for (; sit != end; ++sit)
732                 excluded_numbers.insert(formats.getNumber(*sit));
733
734         vector<int> const & reachables =
735                 G_.getReachable(formats.getNumber(from),
736                                 only_viewable,
737                                 clear_visited,
738                                 excluded_numbers);
739
740         return intToFormat(reachables);
741 }
742
743
744 bool Converters::isReachable(string const & from, string const & to)
745 {
746         return G_.isReachable(formats.getNumber(from),
747                               formats.getNumber(to));
748 }
749
750
751 Graph::EdgePath Converters::getPath(string const & from, string const & to)
752 {
753         return G_.getPath(formats.getNumber(from),
754                           formats.getNumber(to));
755 }
756
757
758 vector<Format const *> Converters::importableFormats()
759 {
760         vector<string> l = loaders();
761         vector<Format const *> result = getReachableTo(l[0], true);
762         vector<string>::const_iterator it = l.begin() + 1;
763         vector<string>::const_iterator en = l.end();
764         for (; it != en; ++it) {
765                 vector<Format const *> r = getReachableTo(*it, false);
766                 result.insert(result.end(), r.begin(), r.end());
767         }
768         return result;
769 }
770
771
772 vector<Format const *> Converters::exportableFormats(bool only_viewable)
773 {
774         vector<string> s = savers();
775         vector<Format const *> result = getReachable(s[0], only_viewable, true);
776         vector<string>::const_iterator it = s.begin() + 1;
777         vector<string>::const_iterator en = s.end();
778         for (; it != en; ++it) {
779                 vector<Format const *> r =
780                         getReachable(*it, only_viewable, false);
781                 result.insert(result.end(), r.begin(), r.end());
782         }
783         return result;
784 }
785
786
787 vector<string> Converters::loaders() const
788 {
789         vector<string> v;
790         v.push_back("lyx");
791         v.push_back("text");
792         v.push_back("textparagraph");
793         return v;
794 }
795
796
797 vector<string> Converters::savers() const
798 {
799         vector<string> v;
800         v.push_back("docbook");
801         v.push_back("latex");
802         v.push_back("literate");
803         v.push_back("luatex");
804         v.push_back("dviluatex");
805         v.push_back("lyx");
806         v.push_back("xhtml");
807         v.push_back("pdflatex");
808         v.push_back("platex");
809         v.push_back("text");
810         v.push_back("xetex");
811         return v;
812 }
813
814
815 } // namespace lyx