2 * \file ExternalSupport.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup Nielsen
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "ExternalSupport.h"
15 #include "ExternalTemplate.h"
16 #include "ExternalTransforms.h"
17 #include "InsetExternal.h"
20 #include "Converter.h"
22 #include "ErrorList.h"
27 #include "support/filetools.h"
28 #include "support/Forkedcall.h"
29 #include "support/lstrings.h"
30 #include "support/lyxalgo.h"
31 #include "support/lyxlib.h"
32 #include "support/os.h"
33 #include "support/Package.h"
35 #include <boost/filesystem/operations.hpp>
41 using boost::filesystem::is_directory;
46 using support::FileName;
50 Template const * getTemplatePtr(InsetExternalParams const & params)
52 TemplateManager const & etm = TemplateManager::get();
53 return etm.getTemplateByName(params.templatename());
57 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
59 formats.edit(buffer, params.filename,
60 formats.getFormatFromFile(params.filename));
66 string const subst_path(string const & input,
67 string const & placeholder,
70 support::latex_path_extension ext = support::PROTECT_EXTENSION,
71 support::latex_path_dots dots = support::LEAVE_DOTS)
73 if (input.find(placeholder) == string::npos)
75 // Don't use external_path here when use_latex_path is false, as the
76 // path will be compared with another one in internal style later
77 // in Converters::move.
78 string const path2 = use_latex_path ?
79 support::latex_path(path, ext, dots) : path;
80 return support::subst(input, placeholder, path2);
86 string const doSubstitution(InsetExternalParams const & params,
87 Buffer const & buffer, string const & s,
89 bool external_in_tmpdir,
92 Buffer const * m_buffer = buffer.getMasterBuffer();
93 string const parentpath = external_in_tmpdir ?
94 m_buffer->temppath() :
96 string const filename = external_in_tmpdir ?
97 params.filename.mangledFilename() :
98 params.filename.outputFilename(parentpath);
99 string const basename = support::changeExtension(
100 support::onlyFilename(filename), string());
101 string const absname = support::makeAbsPath(filename, parentpath).absFilename();
104 if (what != ALL_BUT_PATHS) {
105 string const filepath = support::onlyPath(filename);
106 string const abspath = support::onlyPath(absname);
107 string const masterpath = external_in_tmpdir ?
108 m_buffer->temppath() :
109 m_buffer->filePath();
111 string relToMasterPath = support::onlyPath(
112 to_utf8(support::makeRelPath(from_utf8(absname),
113 from_utf8(masterpath))));
114 if (relToMasterPath == "./")
115 relToMasterPath.clear();
117 string relToParentPath = support::onlyPath(
118 to_utf8(support::makeRelPath(from_utf8(absname),
119 from_utf8(parentpath))));
120 if (relToParentPath == "./")
121 relToParentPath.clear();
123 result = subst_path(result, "$$FPath", filepath,
125 support::PROTECT_EXTENSION,
126 support::ESCAPE_DOTS);
127 result = subst_path(result, "$$AbsPath", abspath,
129 support::PROTECT_EXTENSION,
130 support::ESCAPE_DOTS);
131 result = subst_path(result, "$$RelPathMaster",
132 relToMasterPath, use_latex_path,
133 support::PROTECT_EXTENSION,
134 support::ESCAPE_DOTS);
135 result = subst_path(result, "$$RelPathParent",
136 relToParentPath, use_latex_path,
137 support::PROTECT_EXTENSION,
138 support::ESCAPE_DOTS);
139 if (support::absolutePath(filename)) {
140 result = subst_path(result, "$$AbsOrRelPathMaster",
141 abspath, use_latex_path,
142 support::PROTECT_EXTENSION,
143 support::ESCAPE_DOTS);
144 result = subst_path(result, "$$AbsOrRelPathParent",
145 abspath, use_latex_path,
146 support::PROTECT_EXTENSION,
147 support::ESCAPE_DOTS);
149 result = subst_path(result, "$$AbsOrRelPathMaster",
150 relToMasterPath, use_latex_path,
151 support::PROTECT_EXTENSION,
152 support::ESCAPE_DOTS);
153 result = subst_path(result, "$$AbsOrRelPathParent",
154 relToParentPath, use_latex_path,
155 support::PROTECT_EXTENSION,
156 support::ESCAPE_DOTS);
163 result = subst_path(result, "$$FName", filename, use_latex_path,
164 support::EXCLUDE_EXTENSION);
165 result = subst_path(result, "$$Basename", basename, use_latex_path,
166 support::PROTECT_EXTENSION, support::ESCAPE_DOTS);
167 result = subst_path(result, "$$Extension",
168 '.' + support::getExtension(filename), use_latex_path);
169 result = subst_path(result, "$$Tempname", params.tempname().absFilename(), use_latex_path);
170 result = subst_path(result, "$$Sysdir",
171 support::package().system_support().absFilename(), use_latex_path);
173 // Handle the $$Contents(filename) syntax
174 if (support::contains(result, "$$Contents(\"")) {
175 // Since use_latex_path may be true we must extract the file
176 // name from s instead of result and do the substitutions
177 // again, this time with use_latex_path false.
178 string::size_type const spos = s.find("$$Contents(\"");
179 string::size_type const send = s.find("\")", spos);
180 string const file_template = s.substr(spos + 12, send - (spos + 12));
181 string const file = doSubstitution(params, buffer,
182 file_template, false,
183 external_in_tmpdir, what);
186 FileName const absfile(
187 support::makeAbsPath(file, m_buffer->temppath()));
188 if (support::isFileReadable(absfile))
189 contents = support::getFileContents(absfile);
191 string::size_type const pos = result.find("$$Contents(\"");
192 string::size_type const end = result.find("\")", pos);
193 result.replace(pos, end + 2, contents);
202 /** update the file represented by the template.
203 If \p external_in_tmpdir == true, then the generated file is
204 placed in the buffer's temporary directory.
206 void updateExternal(InsetExternalParams const & params,
207 string const & format,
208 Buffer const & buffer,
209 ExportData & exportdata,
210 bool external_in_tmpdir,
213 Template const * const et_ptr = getTemplatePtr(params);
216 Template const & et = *et_ptr;
218 if (!et.automaticProduction)
219 return; // NOT_NEEDED
221 Template::Formats::const_iterator cit = et.formats.find(format);
222 if (cit == et.formats.end())
225 Template::Format const & outputFormat = cit->second;
226 if (outputFormat.updateResult.empty())
227 return; // NOT_NEEDED
229 string from_format = et.inputFormat;
230 if (from_format.empty())
231 return; // NOT_NEEDED
233 if (from_format == "*") {
234 if (params.filename.empty())
235 return; // NOT_NEEDED
237 // Try and ascertain the file format from its contents.
238 from_format = formats.getFormatFromFile(params.filename);
239 if (from_format.empty())
244 string const to_format = outputFormat.updateFormat;
245 if (to_format.empty())
246 return; // NOT_NEEDED
248 // The master buffer. This is useful when there are multiple levels
250 Buffer const * m_buffer = buffer.getMasterBuffer();
252 // We copy the source file to the temp dir and do the conversion
253 // there if necessary
254 FileName const temp_file(
255 support::makeAbsPath(params.filename.mangledFilename(),
256 m_buffer->temppath()));
257 if (!params.filename.empty() && !is_directory(params.filename.toFilesystemEncoding())) {
258 unsigned long const from_checksum = support::sum(params.filename);
259 unsigned long const temp_checksum = support::sum(temp_file);
261 if (from_checksum != temp_checksum) {
262 Mover const & mover = getMover(from_format);
263 if (!mover.copy(params.filename, temp_file)) {
264 LYXERR(Debug::EXTERNAL)
265 << "external::updateExternal. "
267 << params.filename << " to " << temp_file << endl;
273 // the generated file (always in the temp dir)
274 string const to_file = doSubstitution(params, buffer,
275 outputFormat.updateResult,
277 FileName const abs_to_file(
278 support::makeAbsPath(to_file, m_buffer->temppath()));
281 // Record the referenced files for the exporter.
282 // The exporter will copy them to the export dir.
283 typedef Template::Format::FileMap FileMap;
284 FileMap::const_iterator rit = outputFormat.referencedFiles.begin();
285 FileMap::const_iterator rend = outputFormat.referencedFiles.end();
286 for (; rit != rend; ++rit) {
287 vector<string>::const_iterator fit = rit->second.begin();
288 vector<string>::const_iterator fend = rit->second.end();
289 for (; fit != fend; ++fit) {
290 FileName const source(support::makeAbsPath(
291 doSubstitution(params, buffer, *fit,
293 m_buffer->temppath()));
294 // The path of the referenced file is never the
295 // temp path, but the filename may be the mangled
296 // or the real name. Therefore we substitute the
297 // paths and names separately.
298 string file = support::subst(*fit, "$$FName",
299 "$$FPath$$Basename$$Extension");
300 file = doSubstitution(params, buffer, file, false, false,
302 file = doSubstitution(params, buffer, file,
303 false, external_in_tmpdir,
305 // if file is a relative name, it is interpreted
306 // relative to the master document.
307 exportdata.addExternalFile(rit->first, source, file);
312 // Do we need to perform the conversion?
313 // Yes if to_file does not exist or if from_file is newer than to_file
314 if (support::compare_timestamps(temp_file, abs_to_file) < 0)
317 // FIXME (Abdel 12/08/06): Is there a need to show these errors?
320 theConverters().convert(&buffer, temp_file, abs_to_file,
321 params.filename, from_format, to_format, el,
322 Converters::try_default | Converters::try_cache);
325 LYXERR(Debug::EXTERNAL)
326 << "external::updateExternal. "
327 << "Unable to convert from "
328 << from_format << " to " << to_format << endl;
334 string const substituteCommands(InsetExternalParams const & params,
335 string const & input, string const & format);
337 string const substituteOptions(InsetExternalParams const & params,
338 string const & input, string const & format);
343 int writeExternal(InsetExternalParams const & params,
344 string const & format,
345 Buffer const & buffer, odocstream & os,
346 ExportData & exportdata,
347 bool external_in_tmpdir,
350 Template const * const et_ptr = getTemplatePtr(params);
353 Template const & et = *et_ptr;
355 Template::Formats::const_iterator cit = et.formats.find(format);
356 if (cit == et.formats.end()) {
357 LYXERR(Debug::EXTERNAL)
358 << "External template format '" << format
359 << "' not specified in template "
360 << params.templatename() << endl;
364 if (!dryrun || support::contains(cit->second.product, "$$Contents"))
365 updateExternal(params, format, buffer, exportdata,
366 external_in_tmpdir, dryrun);
368 bool const use_latex_path = format == "LaTeX";
369 string str = doSubstitution(params, buffer, cit->second.product,
370 use_latex_path, external_in_tmpdir);
371 str = substituteCommands(params, str, format);
372 str = substituteOptions(params, str, format);
374 os << from_utf8(str);
375 return int(lyx::count(str.begin(), str.end(),'\n'));
380 // Empty template, specialised below.
381 template <typename TransformType>
382 string const substituteIt(string const &,
385 Template::Format const &,
386 InsetExternalParams const &);
390 string const substituteIt<TransformCommand>(string const & input,
392 string const & /* formatname */,
393 Template::Format const & format,
394 InsetExternalParams const & params)
396 typedef std::map<TransformID, TransformStore> Transformers;
397 Transformers::const_iterator it = format.command_transformers.find(id);
398 if (it == format.command_transformers.end())
401 TransformStore const & store = it->second;
403 TransformCommand::ptr_type ptr;
405 ptr = store.getCommandTransformer(params.rotationdata);
406 else if (id == Resize)
407 ptr = store.getCommandTransformer(params.resizedata);
413 support::subst(input, ptr->front_placeholder(), ptr->front());
414 return support::subst(result, ptr->back_placeholder(), ptr->back());
419 string const substituteIt<TransformOption>(string const & input,
421 string const & fname,
422 Template::Format const & format,
423 InsetExternalParams const & params)
425 typedef std::map<TransformID, TransformStore> Transformers;
426 Transformers::const_iterator it = format.option_transformers.find(id);
427 if (it == format.option_transformers.end())
430 TransformStore const & store = it->second;
432 TransformOption::ptr_type ptr;
435 ptr = store.getOptionTransformer(params.clipdata);
438 ptr = store.getOptionTransformer(params.extradata.get(fname));
441 ptr = store.getOptionTransformer(params.rotationdata);
444 ptr = store.getOptionTransformer(params.resizedata);
451 return support::subst(input, ptr->placeholder(), ptr->option());
455 template <typename TransformerType>
456 string const transformIt(InsetExternalParams const & params,
457 string const & s, string const & formatname)
459 Template const * const et = getTemplatePtr(params);
460 if (!et || et->transformIds.empty())
463 Template::Formats::const_iterator fit = et->formats.find(formatname);
464 if (fit == et->formats.end())
468 Template::Format const & format = fit->second;
470 typedef vector<TransformID> TransformsIDs;
471 TransformsIDs::const_iterator it = et->transformIds.begin();
472 TransformsIDs::const_iterator end = et->transformIds.end();
473 for (; it != end; ++it) {
474 result = substituteIt<TransformerType>(result, *it, formatname,
481 string const substituteCommands(InsetExternalParams const & params,
482 string const & input, string const & format)
484 return transformIt<TransformCommand>(params, input, format);
488 string const substituteOption(InsetExternalParams const & params,
489 string const & input, string const & format)
491 string opt = transformIt<TransformOption>(params, input, format);
493 if (format == "LaTeX" || format == "PDFLaTeX")
494 return sanitizeLatexOption(opt);
495 if (format == "DocBook")
496 return sanitizeDocBookOption(opt);
501 string const substituteOptions(InsetExternalParams const & params,
502 string const & input, string const & format)
504 string output = input;
506 Template const * const et = getTemplatePtr(params);
507 if (!et || et->transformIds.empty())
510 Template::Formats::const_iterator fit = et->formats.find(format);
511 if (fit == et->formats.end() || fit->second.options.empty())
514 typedef vector<Template::Option> Options;
515 Options const & options = fit->second.options;
516 Options::const_iterator it = options.begin();
517 Options::const_iterator end = options.end();
518 for (; it != end; ++it) {
519 string const opt = substituteOption(params, it->option, format);
520 string const placeholder = "$$" + it->name;
521 output = support::subst(output, placeholder, opt);
529 } // namespace external