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