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 string const doSubstitution(InsetExternalParams const & params,
77 Buffer const & buffer, string const & s,
78 string const & filename)
81 string const basename = support::ChangeExtension(filename, string());
82 string const filepath = support::OnlyPath(filename);
84 result = support::subst(s, "$$FName", filename);
85 result = support::subst(result, "$$Basename", basename);
86 result = support::subst(result, "$$FPath", filepath);
87 result = support::subst(result, "$$Tempname", params.tempname());
88 result = support::subst(result, "$$Sysdir", support::system_lyxdir());
90 // Handle the $$Contents(filename) syntax
91 if (support::contains(result, "$$Contents(\"")) {
93 string::size_type const pos = result.find("$$Contents(\"");
94 string::size_type const end = result.find("\")", pos);
95 string const file = result.substr(pos + 12, end - (pos + 12));
98 string const filepath = support::IsFileReadable(file) ?
99 buffer.filePath() : buffer.getMasterBuffer()->temppath();
100 support::Path p(filepath);
102 if (support::IsFileReadable(file))
103 contents = support::GetFileContents(file);
105 result = support::subst(result,
106 ("$$Contents(\"" + file + "\")").c_str(),
113 /** update the file represented by the template.
114 If \param external_in_tmpdir == true, then the generated file is
115 place in the buffer's temporary directory.
117 void updateExternal(InsetExternalParams const & params,
118 string const & format,
119 Buffer const & buffer,
120 bool external_in_tmpdir)
122 Template const * const et_ptr = getTemplatePtr(params);
125 Template const & et = *et_ptr;
127 if (!et.automaticProduction)
128 return; // NOT_NEEDED
130 Template::Formats::const_iterator cit = et.formats.find(format);
131 if (cit == et.formats.end())
134 Template::Format const & outputFormat = cit->second;
135 if (outputFormat.updateResult.empty())
136 return; // NOT_NEEDED
138 string from_format = et.inputFormat;
139 if (from_format.empty())
140 return; // NOT_NEEDED
142 string from_file = params.filename.absFilename();
144 if (from_format == "*") {
145 if (from_file.empty())
146 return; // NOT_NEEDED
148 // Try and ascertain the file format from its contents.
149 from_format = support::getExtFromContents(from_file);
150 if (from_format.empty())
154 string const to_format = outputFormat.updateFormat;
155 if (to_format.empty())
156 return; // NOT_NEEDED
158 if (!converters.isReachable(from_format, to_format)) {
159 lyxerr[Debug::EXTERNAL]
160 << "external::updateExternal. "
161 << "Unable to convert from "
162 << from_format << " to " << to_format << endl;
166 // The master buffer. This is useful when there are multiple levels
168 Buffer const * m_buffer = buffer.getMasterBuffer();
170 if (external_in_tmpdir && !from_file.empty()) {
171 // We are running stuff through LaTeX
172 string const temp_file =
173 support::MakeAbsPath(params.filename.mangledFilename(),
174 m_buffer->temppath());
175 unsigned long const from_checksum = support::sum(from_file);
176 unsigned long const temp_checksum = support::sum(temp_file);
178 if (from_checksum != temp_checksum) {
179 if (!support::copy(from_file, temp_file))
183 from_file = temp_file;
186 string const to_file = doSubstitution(params, buffer,
187 outputFormat.updateResult,
190 string const abs_to_file =
191 support::MakeAbsPath(to_file, external_in_tmpdir
192 ? m_buffer->temppath()
193 : buffer.filePath());
195 // Do we need to perform the conversion?
196 // Yes if to_file does not exist or if from_file is newer than to_file
197 if (support::compare_timestamps(from_file, abs_to_file) < 0)
200 string const to_file_base =
201 support::ChangeExtension(to_file, string());
202 /* bool const success = */
203 converters.convert(&buffer, from_file, to_file_base,
204 from_format, to_format);
209 string const substituteCommands(InsetExternalParams const & params,
210 string const & input, string const & format);
212 string const substituteOptions(InsetExternalParams const & params,
213 string const & input, string const & format);
218 int writeExternal(InsetExternalParams const & params,
219 string const & format,
220 Buffer const & buffer, ostream & os,
221 bool external_in_tmpdir)
223 Template const * const et_ptr = getTemplatePtr(params);
226 Template const & et = *et_ptr;
228 Template::Formats::const_iterator cit = et.formats.find(format);
229 if (cit == et.formats.end()) {
230 lyxerr[Debug::EXTERNAL]
231 << "External template format '" << format
232 << "' not specified in template "
233 << params.templatename() << endl;
237 updateExternal(params, format, buffer, external_in_tmpdir);
239 string from_file = params.filename.outputFilename(buffer.filePath());
240 if (external_in_tmpdir && !from_file.empty()) {
241 // We are running stuff through LaTeX
243 support::MakeAbsPath(params.filename.mangledFilename(),
244 buffer.getMasterBuffer()->temppath());
247 string str = doSubstitution(params, buffer, cit->second.product,
249 str = substituteCommands(params, str, format);
250 str = substituteOptions(params, str, format);
252 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
256 /// Substitute meta-variables in this string
257 string const doSubstitution(InsetExternalParams const & params,
258 Buffer const & buffer, string const & s)
260 string const buffer_path = buffer.filePath();
261 string const filename = params.filename.outputFilename(buffer_path);
262 return doSubstitution(params, buffer, s, filename);
267 // Empty template, specialised below.
268 template <typename TransformType>
269 string const substituteIt(string const &,
272 Template::Format const &,
273 InsetExternalParams const &);
277 string const substituteIt<TransformCommand>(string const & input,
279 string const & /* formatname */,
280 Template::Format const & format,
281 InsetExternalParams const & params)
283 typedef std::map<TransformID, TransformStore> Transformers;
284 Transformers::const_iterator it = format.command_transformers.find(id);
285 if (it == format.command_transformers.end())
288 TransformStore const & store = it->second;
290 TransformCommand::ptr_type ptr;
292 ptr = store.getCommandTransformer(params.rotationdata);
293 else if (id == Resize)
294 ptr = store.getCommandTransformer(params.resizedata);
300 support::subst(input, ptr->front_placeholder(), ptr->front());
301 return support::subst(result, ptr->back_placeholder(), ptr->back());
306 string const substituteIt<TransformOption>(string const & input,
308 string const & fname,
309 Template::Format const & format,
310 InsetExternalParams const & params)
312 typedef std::map<TransformID, TransformStore> Transformers;
313 Transformers::const_iterator it = format.option_transformers.find(id);
314 if (it == format.option_transformers.end())
317 TransformStore const & store = it->second;
319 TransformOption::ptr_type ptr;
322 ptr = store.getOptionTransformer(params.clipdata);
325 ptr = store.getOptionTransformer(params.extradata.get(fname));
328 ptr = store.getOptionTransformer(params.rotationdata);
331 ptr = store.getOptionTransformer(params.resizedata);
338 return support::subst(input, ptr->placeholder(), ptr->option());
342 template <typename TransformerType>
343 string const transformIt(InsetExternalParams const & params,
344 string const & s, string const & formatname)
346 Template const * const et = getTemplatePtr(params);
347 if (!et || et->transformIds.empty())
350 Template::Formats::const_iterator fit = et->formats.find(formatname);
351 if (fit == et->formats.end())
355 Template::Format const & format = fit->second;
357 typedef vector<TransformID> TransformsIDs;
358 TransformsIDs::const_iterator it = et->transformIds.begin();
359 TransformsIDs::const_iterator end = et->transformIds.end();
360 for (; it != end; ++it) {
361 result = substituteIt<TransformerType>(result, *it, formatname,
368 string const substituteCommands(InsetExternalParams const & params,
369 string const & input, string const & format)
371 return transformIt<TransformCommand>(params, input, format);
375 string const substituteOption(InsetExternalParams const & params,
376 string const & input, string const & format)
378 string opt = transformIt<TransformOption>(params, input, format);
380 if (format == "LaTeX" || format == "PDFLaTeX")
381 return sanitizeLatexOption(opt);
382 if (format == "DocBook")
383 return sanitizeDocBookOption(opt);
384 if (format == "LinuxDoc")
385 return sanitizeLinuxDocOption(opt);
390 string const substituteOptions(InsetExternalParams const & params,
391 string const & input, string const & format)
393 string output = input;
395 Template const * const et = getTemplatePtr(params);
396 if (!et || et->transformIds.empty())
399 Template::Formats::const_iterator fit = et->formats.find(format);
400 if (fit == et->formats.end() || fit->second.options.empty())
403 typedef vector<Template::Option> Options;
404 Options const & options = fit->second.options;
405 Options::const_iterator it = options.begin();
406 Options::const_iterator end = options.end();
407 for (; it != end; ++it) {
408 string const opt = substituteOption(params, it->option, format);
409 string const placeholder = "$$" + it->name;
410 output = support::subst(output, placeholder, opt);
418 } // namespace external