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