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"
23 #include "support/filetools.h"
24 #include "support/forkedcall.h"
25 #include "support/lstrings.h"
26 #include "support/lyxalgo.h"
27 #include "support/lyxlib.h"
28 #include "support/path.h"
29 #include "support/path_defines.h"
31 #include "support/std_ostream.h"
33 namespace support = lyx::support;
45 Template const * getTemplatePtr(InsetExternalParams const & params)
47 TemplateManager const & etm = TemplateManager::get();
48 return etm.getTemplateByName(params.templatename());
52 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
54 Template const * const et_ptr = getTemplatePtr(params);
57 Template const & et = *et_ptr;
59 if (et.editCommand.empty())
62 string const command = doSubstitution(params, buffer, et.editCommand);
64 support::Path p(buffer.filePath());
65 support::Forkedcall call;
66 if (lyxerr.debugging(Debug::EXTERNAL)) {
67 lyxerr << "Executing '" << command << "' in '"
68 << buffer.filePath() << '\'' << endl;
70 call.startscript(support::Forkedcall::DontWait, command);
76 /** Substitute meta-variables in the string \p s.
77 \p filename has to be the filename as read from the .lyx file (this
78 can be an absolute path or a path relative to the parent document).
79 Otherwise, the $$AbsOrRelPath* variables would not work.
80 If we are using a temporary directory, \p filename is the mangled name.
82 string const doSubstitution(InsetExternalParams const & params,
83 Buffer const & buffer, string const & s,
84 string const & filename)
87 string const basename = support::ChangeExtension(
88 support::OnlyFilename(filename), string());
89 string const absname = support::MakeAbsPath(filename, buffer.filePath());
90 string const filepath = support::OnlyPath(filename);
91 string const abspath = support::OnlyPath(absname);
92 Buffer const * m_buffer = buffer.getMasterBuffer();
93 string relToMasterPath = support::OnlyPath(
94 support::MakeRelPath(absname, m_buffer->filePath()));
95 if (relToMasterPath == "./")
96 relToMasterPath.clear();
97 string relToParentPath = support::OnlyPath(
98 support::MakeRelPath(absname, buffer.filePath()));
99 if (relToParentPath == "./")
100 relToParentPath.clear();
102 result = support::subst(s, "$$FName", filename);
103 result = support::subst(result, "$$Basename", basename);
104 result = support::subst(result, "$$Extension",
105 '.' + support::GetExtension(filename));
106 result = support::subst(result, "$$FPath", filepath);
107 result = support::subst(result, "$$AbsPath", abspath);
108 result = support::subst(result, "$$RelPathMaster", relToMasterPath);
109 result = support::subst(result, "$$RelPathParent", relToParentPath);
110 if (support::AbsolutePath(filename)) {
111 result = support::subst(result, "$$AbsOrRelPathMaster",
113 result = support::subst(result, "$$AbsOrRelPathParent",
116 result = support::subst(result, "$$AbsOrRelPathMaster",
118 result = support::subst(result, "$$AbsOrRelPathParent",
121 result = support::subst(result, "$$Tempname", params.tempname());
122 result = support::subst(result, "$$Sysdir", support::system_lyxdir());
124 // Handle the $$Contents(filename) syntax
125 if (support::contains(result, "$$Contents(\"")) {
127 string::size_type const pos = result.find("$$Contents(\"");
128 string::size_type const end = result.find("\")", pos);
129 string const file = result.substr(pos + 12, end - (pos + 12));
132 string const filepath = support::IsFileReadable(file) ?
133 buffer.filePath() : m_buffer->temppath();
134 support::Path p(filepath);
136 if (support::IsFileReadable(file))
137 contents = support::GetFileContents(file);
139 result = support::subst(result,
140 ("$$Contents(\"" + file + "\")").c_str(),
147 /** update the file represented by the template.
148 If \param external_in_tmpdir == true, then the generated file is
149 place in the buffer's temporary directory.
151 void updateExternal(InsetExternalParams const & params,
152 string const & format,
153 Buffer const & buffer,
154 bool external_in_tmpdir)
156 Template const * const et_ptr = getTemplatePtr(params);
159 Template const & et = *et_ptr;
161 if (!et.automaticProduction)
162 return; // NOT_NEEDED
164 Template::Formats::const_iterator cit = et.formats.find(format);
165 if (cit == et.formats.end())
168 Template::Format const & outputFormat = cit->second;
169 if (outputFormat.updateResult.empty())
170 return; // NOT_NEEDED
172 string from_format = et.inputFormat;
173 if (from_format.empty())
174 return; // NOT_NEEDED
176 string abs_from_file = params.filename.absFilename();
178 if (from_format == "*") {
179 if (abs_from_file.empty())
180 return; // NOT_NEEDED
182 // Try and ascertain the file format from its contents.
183 from_format = support::getExtFromContents(abs_from_file);
184 if (from_format.empty())
189 string const to_format = outputFormat.updateFormat;
190 if (to_format.empty())
191 return; // NOT_NEEDED
193 if (!converters.isReachable(from_format, to_format)) {
194 lyxerr[Debug::EXTERNAL]
195 << "external::updateExternal. "
196 << "Unable to convert from "
197 << from_format << " to " << to_format << endl;
201 string from_file = params.filename.outputFilename(buffer.filePath());
203 // The master buffer. This is useful when there are multiple levels
205 Buffer const * m_buffer = buffer.getMasterBuffer();
207 if (external_in_tmpdir && !abs_from_file.empty()) {
208 // We are running stuff through LaTeX
209 string const temp_file =
210 support::MakeAbsPath(params.filename.mangledFilename(),
211 m_buffer->temppath());
212 unsigned long const from_checksum = support::sum(abs_from_file);
213 unsigned long const temp_checksum = support::sum(temp_file);
215 if (from_checksum != temp_checksum) {
216 if (!support::copy(abs_from_file, temp_file)) {
217 lyxerr[Debug::EXTERNAL]
218 << "external::updateExternal. "
220 << abs_from_file << " to " << temp_file << endl;
225 from_file = temp_file;
226 abs_from_file = temp_file;
229 string const to_file = doSubstitution(params, buffer,
230 outputFormat.updateResult,
233 string const abs_to_file =
234 support::MakeAbsPath(to_file, external_in_tmpdir
235 ? m_buffer->temppath()
236 : buffer.filePath());
238 // Do we need to perform the conversion?
239 // Yes if to_file does not exist or if from_file is newer than to_file
240 if (support::compare_timestamps(abs_from_file, abs_to_file) < 0)
243 string const to_file_base =
244 support::ChangeExtension(to_file, string());
245 /* bool const success = */
246 converters.convert(&buffer, abs_from_file, to_file_base,
247 from_format, to_format);
252 string const substituteCommands(InsetExternalParams const & params,
253 string const & input, string const & format);
255 string const substituteOptions(InsetExternalParams const & params,
256 string const & input, string const & format);
261 int writeExternal(InsetExternalParams const & params,
262 string const & format,
263 Buffer const & buffer, ostream & os,
264 bool external_in_tmpdir)
266 Template const * const et_ptr = getTemplatePtr(params);
269 Template const & et = *et_ptr;
271 Template::Formats::const_iterator cit = et.formats.find(format);
272 if (cit == et.formats.end()) {
273 lyxerr[Debug::EXTERNAL]
274 << "External template format '" << format
275 << "' not specified in template "
276 << params.templatename() << endl;
280 updateExternal(params, format, buffer, external_in_tmpdir);
282 string from_file = params.filename.outputFilename(buffer.filePath());
283 if (external_in_tmpdir && !from_file.empty()) {
284 // We are running stuff through LaTeX
286 support::MakeAbsPath(params.filename.mangledFilename(),
287 buffer.getMasterBuffer()->temppath());
290 string str = doSubstitution(params, buffer, cit->second.product,
292 str = substituteCommands(params, str, format);
293 str = substituteOptions(params, str, format);
295 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
299 /// Substitute meta-variables in this string
300 string const doSubstitution(InsetExternalParams const & params,
301 Buffer const & buffer, string const & s)
303 string const buffer_path = buffer.filePath();
304 string const filename = params.filename.outputFilename(buffer_path);
305 return doSubstitution(params, buffer, s, filename);
310 // Empty template, specialised below.
311 template <typename TransformType>
312 string const substituteIt(string const &,
315 Template::Format const &,
316 InsetExternalParams const &);
320 string const substituteIt<TransformCommand>(string const & input,
322 string const & /* formatname */,
323 Template::Format const & format,
324 InsetExternalParams const & params)
326 typedef std::map<TransformID, TransformStore> Transformers;
327 Transformers::const_iterator it = format.command_transformers.find(id);
328 if (it == format.command_transformers.end())
331 TransformStore const & store = it->second;
333 TransformCommand::ptr_type ptr;
335 ptr = store.getCommandTransformer(params.rotationdata);
336 else if (id == Resize)
337 ptr = store.getCommandTransformer(params.resizedata);
343 support::subst(input, ptr->front_placeholder(), ptr->front());
344 return support::subst(result, ptr->back_placeholder(), ptr->back());
349 string const substituteIt<TransformOption>(string const & input,
351 string const & fname,
352 Template::Format const & format,
353 InsetExternalParams const & params)
355 typedef std::map<TransformID, TransformStore> Transformers;
356 Transformers::const_iterator it = format.option_transformers.find(id);
357 if (it == format.option_transformers.end())
360 TransformStore const & store = it->second;
362 TransformOption::ptr_type ptr;
365 ptr = store.getOptionTransformer(params.clipdata);
368 ptr = store.getOptionTransformer(params.extradata.get(fname));
371 ptr = store.getOptionTransformer(params.rotationdata);
374 ptr = store.getOptionTransformer(params.resizedata);
381 return support::subst(input, ptr->placeholder(), ptr->option());
385 template <typename TransformerType>
386 string const transformIt(InsetExternalParams const & params,
387 string const & s, string const & formatname)
389 Template const * const et = getTemplatePtr(params);
390 if (!et || et->transformIds.empty())
393 Template::Formats::const_iterator fit = et->formats.find(formatname);
394 if (fit == et->formats.end())
398 Template::Format const & format = fit->second;
400 typedef vector<TransformID> TransformsIDs;
401 TransformsIDs::const_iterator it = et->transformIds.begin();
402 TransformsIDs::const_iterator end = et->transformIds.end();
403 for (; it != end; ++it) {
404 result = substituteIt<TransformerType>(result, *it, formatname,
411 string const substituteCommands(InsetExternalParams const & params,
412 string const & input, string const & format)
414 return transformIt<TransformCommand>(params, input, format);
418 string const substituteOption(InsetExternalParams const & params,
419 string const & input, string const & format)
421 string opt = transformIt<TransformOption>(params, input, format);
423 if (format == "LaTeX" || format == "PDFLaTeX")
424 return sanitizeLatexOption(opt);
425 if (format == "DocBook")
426 return sanitizeDocBookOption(opt);
427 if (format == "LinuxDoc")
428 return sanitizeLinuxDocOption(opt);
433 string const substituteOptions(InsetExternalParams const & params,
434 string const & input, string const & format)
436 string output = input;
438 Template const * const et = getTemplatePtr(params);
439 if (!et || et->transformIds.empty())
442 Template::Formats::const_iterator fit = et->formats.find(format);
443 if (fit == et->formats.end() || fit->second.options.empty())
446 typedef vector<Template::Option> Options;
447 Options const & options = fit->second.options;
448 Options::const_iterator it = options.begin();
449 Options::const_iterator end = options.end();
450 for (; it != end; ++it) {
451 string const opt = substituteOption(params, it->option, format);
452 string const placeholder = "$$" + it->name;
453 output = support::subst(output, placeholder, opt);
461 } // namespace external