2 * \file ExternalSupport.C
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"
24 #include "support/filetools.h"
25 #include "support/forkedcall.h"
26 #include "support/lstrings.h"
27 #include "support/lyxalgo.h"
28 #include "support/lyxlib.h"
29 #include "support/path.h"
30 #include "support/path_defines.h"
32 #include "support/std_ostream.h"
34 namespace support = lyx::support;
46 Template const * getTemplatePtr(InsetExternalParams const & params)
48 TemplateManager const & etm = TemplateManager::get();
49 return etm.getTemplateByName(params.templatename());
53 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
55 string const file_with_path = params.filename.absFilename();
56 formats.edit(buffer, file_with_path,
57 support::getExtFromContents(file_with_path));
63 /** Substitute meta-variables in the string \p s.
64 \p filename has to be the filename as read from the .lyx file (this
65 can be an absolute path or a path relative to the parent document).
66 Otherwise, the $$AbsOrRelPath* variables would not work.
67 If we are using a temporary directory, \p filename is the mangled name.
69 string const doSubstitution(InsetExternalParams const & params,
70 Buffer const & buffer, string const & s,
71 string const & filename)
74 string const basename = support::ChangeExtension(
75 support::OnlyFilename(filename), string());
76 string const absname = support::MakeAbsPath(filename, buffer.filePath());
77 string const filepath = support::OnlyPath(filename);
78 string const abspath = support::OnlyPath(absname);
79 Buffer const * m_buffer = buffer.getMasterBuffer();
80 string relToMasterPath = support::OnlyPath(
81 support::MakeRelPath(absname, m_buffer->filePath()));
82 if (relToMasterPath == "./")
83 relToMasterPath.clear();
84 string relToParentPath = support::OnlyPath(
85 support::MakeRelPath(absname, buffer.filePath()));
86 if (relToParentPath == "./")
87 relToParentPath.clear();
89 result = support::subst(s, "$$FName", filename);
90 result = support::subst(result, "$$Basename", basename);
91 result = support::subst(result, "$$Extension",
92 '.' + support::GetExtension(filename));
93 result = support::subst(result, "$$FPath", filepath);
94 result = support::subst(result, "$$AbsPath", abspath);
95 result = support::subst(result, "$$RelPathMaster", relToMasterPath);
96 result = support::subst(result, "$$RelPathParent", relToParentPath);
97 if (support::AbsolutePath(filename)) {
98 result = support::subst(result, "$$AbsOrRelPathMaster",
100 result = support::subst(result, "$$AbsOrRelPathParent",
103 result = support::subst(result, "$$AbsOrRelPathMaster",
105 result = support::subst(result, "$$AbsOrRelPathParent",
108 result = support::subst(result, "$$Tempname", params.tempname());
109 result = support::subst(result, "$$Sysdir", support::system_lyxdir());
111 // Handle the $$Contents(filename) syntax
112 if (support::contains(result, "$$Contents(\"")) {
114 string::size_type const pos = result.find("$$Contents(\"");
115 string::size_type const end = result.find("\")", pos);
116 string const file = result.substr(pos + 12, end - (pos + 12));
119 string const filepath = support::IsFileReadable(file) ?
120 buffer.filePath() : m_buffer->temppath();
121 support::Path p(filepath);
123 if (support::IsFileReadable(file))
124 contents = support::GetFileContents(file);
126 result = support::subst(result,
127 ("$$Contents(\"" + file + "\")").c_str(),
134 /** update the file represented by the template.
135 If \param external_in_tmpdir == true, then the generated file is
136 place in the buffer's temporary directory.
138 void updateExternal(InsetExternalParams const & params,
139 string const & format,
140 Buffer const & buffer,
141 bool external_in_tmpdir)
143 Template const * const et_ptr = getTemplatePtr(params);
146 Template const & et = *et_ptr;
148 if (!et.automaticProduction)
149 return; // NOT_NEEDED
151 Template::Formats::const_iterator cit = et.formats.find(format);
152 if (cit == et.formats.end())
155 Template::Format const & outputFormat = cit->second;
156 if (outputFormat.updateResult.empty())
157 return; // NOT_NEEDED
159 string from_format = et.inputFormat;
160 if (from_format.empty())
161 return; // NOT_NEEDED
163 string abs_from_file = params.filename.absFilename();
165 if (from_format == "*") {
166 if (abs_from_file.empty())
167 return; // NOT_NEEDED
169 // Try and ascertain the file format from its contents.
170 from_format = support::getExtFromContents(abs_from_file);
171 if (from_format.empty())
176 string const to_format = outputFormat.updateFormat;
177 if (to_format.empty())
178 return; // NOT_NEEDED
180 if (!converters.isReachable(from_format, to_format)) {
181 lyxerr[Debug::EXTERNAL]
182 << "external::updateExternal. "
183 << "Unable to convert from "
184 << from_format << " to " << to_format << endl;
188 string from_file = params.filename.outputFilename(buffer.filePath());
190 // The master buffer. This is useful when there are multiple levels
192 Buffer const * m_buffer = buffer.getMasterBuffer();
194 if (external_in_tmpdir && !abs_from_file.empty()) {
195 // We are running stuff through LaTeX
196 string const temp_file =
197 support::MakeAbsPath(params.filename.mangledFilename(),
198 m_buffer->temppath());
199 unsigned long const from_checksum = support::sum(abs_from_file);
200 unsigned long const temp_checksum = support::sum(temp_file);
202 if (from_checksum != temp_checksum) {
203 if (!support::copy(abs_from_file, temp_file)) {
204 lyxerr[Debug::EXTERNAL]
205 << "external::updateExternal. "
207 << abs_from_file << " to " << temp_file << endl;
212 from_file = temp_file;
213 abs_from_file = temp_file;
216 string const to_file = doSubstitution(params, buffer,
217 outputFormat.updateResult,
220 string const abs_to_file =
221 support::MakeAbsPath(to_file, external_in_tmpdir
222 ? m_buffer->temppath()
223 : buffer.filePath());
225 // Do we need to perform the conversion?
226 // Yes if to_file does not exist or if from_file is newer than to_file
227 if (support::compare_timestamps(abs_from_file, abs_to_file) < 0)
230 string const to_file_base =
231 support::ChangeExtension(to_file, string());
232 /* bool const success = */
233 converters.convert(&buffer, abs_from_file, to_file_base,
234 from_format, to_format);
239 string const substituteCommands(InsetExternalParams const & params,
240 string const & input, string const & format);
242 string const substituteOptions(InsetExternalParams const & params,
243 string const & input, string const & format);
248 int writeExternal(InsetExternalParams const & params,
249 string const & format,
250 Buffer const & buffer, ostream & os,
251 bool external_in_tmpdir)
253 Template const * const et_ptr = getTemplatePtr(params);
256 Template const & et = *et_ptr;
258 Template::Formats::const_iterator cit = et.formats.find(format);
259 if (cit == et.formats.end()) {
260 lyxerr[Debug::EXTERNAL]
261 << "External template format '" << format
262 << "' not specified in template "
263 << params.templatename() << endl;
267 updateExternal(params, format, buffer, external_in_tmpdir);
269 string from_file = params.filename.outputFilename(buffer.filePath());
270 if (external_in_tmpdir && !from_file.empty()) {
271 // We are running stuff through LaTeX
273 support::MakeAbsPath(params.filename.mangledFilename(),
274 buffer.getMasterBuffer()->temppath());
277 string str = doSubstitution(params, buffer, cit->second.product,
279 str = substituteCommands(params, str, format);
280 str = substituteOptions(params, str, format);
282 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
286 /// Substitute meta-variables in this string
287 string const doSubstitution(InsetExternalParams const & params,
288 Buffer const & buffer, string const & s)
290 string const buffer_path = buffer.filePath();
291 string const filename = params.filename.outputFilename(buffer_path);
292 return doSubstitution(params, buffer, s, filename);
297 // Empty template, specialised below.
298 template <typename TransformType>
299 string const substituteIt(string const &,
302 Template::Format const &,
303 InsetExternalParams const &);
307 string const substituteIt<TransformCommand>(string const & input,
309 string const & /* formatname */,
310 Template::Format const & format,
311 InsetExternalParams const & params)
313 typedef std::map<TransformID, TransformStore> Transformers;
314 Transformers::const_iterator it = format.command_transformers.find(id);
315 if (it == format.command_transformers.end())
318 TransformStore const & store = it->second;
320 TransformCommand::ptr_type ptr;
322 ptr = store.getCommandTransformer(params.rotationdata);
323 else if (id == Resize)
324 ptr = store.getCommandTransformer(params.resizedata);
330 support::subst(input, ptr->front_placeholder(), ptr->front());
331 return support::subst(result, ptr->back_placeholder(), ptr->back());
336 string const substituteIt<TransformOption>(string const & input,
338 string const & fname,
339 Template::Format const & format,
340 InsetExternalParams const & params)
342 typedef std::map<TransformID, TransformStore> Transformers;
343 Transformers::const_iterator it = format.option_transformers.find(id);
344 if (it == format.option_transformers.end())
347 TransformStore const & store = it->second;
349 TransformOption::ptr_type ptr;
352 ptr = store.getOptionTransformer(params.clipdata);
355 ptr = store.getOptionTransformer(params.extradata.get(fname));
358 ptr = store.getOptionTransformer(params.rotationdata);
361 ptr = store.getOptionTransformer(params.resizedata);
368 return support::subst(input, ptr->placeholder(), ptr->option());
372 template <typename TransformerType>
373 string const transformIt(InsetExternalParams const & params,
374 string const & s, string const & formatname)
376 Template const * const et = getTemplatePtr(params);
377 if (!et || et->transformIds.empty())
380 Template::Formats::const_iterator fit = et->formats.find(formatname);
381 if (fit == et->formats.end())
385 Template::Format const & format = fit->second;
387 typedef vector<TransformID> TransformsIDs;
388 TransformsIDs::const_iterator it = et->transformIds.begin();
389 TransformsIDs::const_iterator end = et->transformIds.end();
390 for (; it != end; ++it) {
391 result = substituteIt<TransformerType>(result, *it, formatname,
398 string const substituteCommands(InsetExternalParams const & params,
399 string const & input, string const & format)
401 return transformIt<TransformCommand>(params, input, format);
405 string const substituteOption(InsetExternalParams const & params,
406 string const & input, string const & format)
408 string opt = transformIt<TransformOption>(params, input, format);
410 if (format == "LaTeX" || format == "PDFLaTeX")
411 return sanitizeLatexOption(opt);
412 if (format == "DocBook")
413 return sanitizeDocBookOption(opt);
414 if (format == "LinuxDoc")
415 return sanitizeLinuxDocOption(opt);
420 string const substituteOptions(InsetExternalParams const & params,
421 string const & input, string const & format)
423 string output = input;
425 Template const * const et = getTemplatePtr(params);
426 if (!et || et->transformIds.empty())
429 Template::Formats::const_iterator fit = et->formats.find(format);
430 if (fit == et->formats.end() || fit->second.options.empty())
433 typedef vector<Template::Option> Options;
434 Options const & options = fit->second.options;
435 Options::const_iterator it = options.begin();
436 Options::const_iterator end = options.end();
437 for (; it != end; ++it) {
438 string const opt = substituteOption(params, it->option, format);
439 string const placeholder = "$$" + it->name;
440 output = support::subst(output, placeholder, opt);
448 } // namespace external