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