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