]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalSupport.C
Wrap all #warning calls inside #ifdef WITH_WARNINGS blocks.
[lyx.git] / src / insets / ExternalSupport.C
1 /**
2  * \file ExternalSupport.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ExternalSupport.h"
15 #include "ExternalTemplate.h"
16 #include "ExternalTransforms.h"
17 #include "insetexternal.h"
18
19 #include "buffer.h"
20 #include "converter.h"
21 #include "debug.h"
22
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"
30
31 #include "support/std_ostream.h"
32
33 namespace support = lyx::support;
34
35 using std::endl;
36
37 using std::ostream;
38 using std::string;
39 using std::vector;
40
41
42 namespace lyx {
43 namespace external {
44
45 Template const * getTemplatePtr(InsetExternalParams const & params)
46 {
47         TemplateManager const & etm = TemplateManager::get();
48         return etm.getTemplateByName(params.templatename());
49 }
50
51
52 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
53 {
54         Template const * const et_ptr = getTemplatePtr(params);
55         if (!et_ptr)
56                 return;
57         Template const & et = *et_ptr;
58
59         if (et.editCommand.empty())
60                 return;
61
62         string const command = doSubstitution(params, buffer, et.editCommand);
63
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;
69         }
70         call.startscript(support::Forkedcall::DontWait, command);
71 }
72
73
74 namespace {
75
76 string const doSubstitution(InsetExternalParams const & params,
77                             Buffer const & buffer, string const & s,
78                             string const & filename)
79 {
80         string result;
81         string const basename = support::ChangeExtension(filename, string());
82         string const filepath = support::OnlyPath(filename);
83
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());
89
90         // Handle the $$Contents(filename) syntax
91         if (support::contains(result, "$$Contents(\"")) {
92
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));
96                 string contents;
97
98                 string const filepath = support::IsFileReadable(file) ?
99                         buffer.filePath() : buffer.getMasterBuffer()->temppath();
100                 support::Path p(filepath);
101
102                 if (support::IsFileReadable(file))
103                         contents = support::GetFileContents(file);
104
105                 result = support::subst(result,
106                                         ("$$Contents(\"" + file + "\")").c_str(),
107                                         contents);
108         }
109
110         return result;
111 }
112
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.
116 */
117 void updateExternal(InsetExternalParams const & params,
118                     string const & format,
119                     Buffer const & buffer,
120                     bool external_in_tmpdir)
121 {
122         Template const * const et_ptr = getTemplatePtr(params);
123         if (!et_ptr)
124                 return; // FAILURE
125         Template const & et = *et_ptr;
126
127         if (!et.automaticProduction)
128                 return; // NOT_NEEDED
129
130         Template::Formats::const_iterator cit = et.formats.find(format);
131         if (cit == et.formats.end())
132                 return; // FAILURE
133
134         Template::Format const & outputFormat = cit->second;
135         if (outputFormat.updateResult.empty())
136                 return; // NOT_NEEDED
137
138         string from_format = et.inputFormat;
139         if (from_format.empty())
140                 return; // NOT_NEEDED
141
142         string from_file = params.filename.absFilename();
143
144         if (from_format == "*") {
145                 if (from_file.empty())
146                         return; // NOT_NEEDED
147
148                 // Try and ascertain the file format from its contents.
149                 from_format = support::getExtFromContents(from_file);
150                 if (from_format.empty())
151                         return; // FAILURE
152         }
153
154         string const to_format = outputFormat.updateFormat;
155         if (to_format.empty())
156                 return; // NOT_NEEDED
157
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;
163                 return; // FAILURE
164         }
165
166         // The master buffer. This is useful when there are multiple levels
167         // of include files
168         Buffer const * m_buffer = buffer.getMasterBuffer();
169
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);
177
178                 if (from_checksum != temp_checksum) {
179                         if (!support::copy(from_file, temp_file))
180                                 return; // FAILURE
181                 }
182
183                 from_file = temp_file;
184         }
185
186         string const to_file = doSubstitution(params, buffer,
187                                               outputFormat.updateResult,
188                                               from_file);
189
190         string const abs_to_file =
191                 support::MakeAbsPath(to_file, external_in_tmpdir
192                         ? m_buffer->temppath()
193                         : buffer.filePath());
194
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)
198                 return; // SUCCESS
199
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);
205         // return success
206 }
207
208
209 string const substituteCommands(InsetExternalParams const & params,
210                                 string const & input, string const & format);
211
212 string const substituteOptions(InsetExternalParams const & params,
213                                string const & input, string const & format);
214
215 } // namespace anon
216
217
218 int writeExternal(InsetExternalParams const & params,
219                   string const & format,
220                   Buffer const & buffer, ostream & os,
221                   bool external_in_tmpdir)
222 {
223         Template const * const et_ptr = getTemplatePtr(params);
224         if (!et_ptr)
225                 return 0;
226         Template const & et = *et_ptr;
227
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;
234                 return 0;
235         }
236
237         updateExternal(params, format, buffer, external_in_tmpdir);
238
239         string from_file = params.filename.outputFilename(buffer.filePath());
240         if (external_in_tmpdir && !from_file.empty()) {
241                 // We are running stuff through LaTeX
242                 from_file =
243                         support::MakeAbsPath(params.filename.mangledFilename(),
244                                              buffer.getMasterBuffer()->temppath());
245         }
246
247         string str = doSubstitution(params, buffer, cit->second.product,
248                                     from_file);
249         str = substituteCommands(params, str, format);
250         str = substituteOptions(params, str, format);
251         os << str;
252         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
253 }
254
255
256 /// Substitute meta-variables in this string
257 string const doSubstitution(InsetExternalParams const & params,
258                             Buffer const & buffer, string const & s)
259 {
260         string const buffer_path = buffer.filePath();
261         string const filename = params.filename.outputFilename(buffer_path);
262         return doSubstitution(params, buffer, s, filename);
263 }
264
265 namespace {
266
267 // Empty template, specialised below.
268 template <typename TransformType>
269 string const substituteIt(string const &,
270                           TransformID,
271                           string const &,
272                           Template::Format const &,
273                           InsetExternalParams const &);
274
275
276 template <>
277 string const substituteIt<TransformCommand>(string const & input,
278                                             TransformID id,
279                                             string const & /* formatname */,
280                                             Template::Format const & format,
281                                             InsetExternalParams const & params)
282 {
283         typedef std::map<TransformID, TransformStore> Transformers;
284         Transformers::const_iterator it = format.command_transformers.find(id);
285         if (it == format.command_transformers.end())
286                 return input;
287
288         TransformStore const & store = it->second;
289
290         TransformCommand::ptr_type ptr;
291         if (id == Rotate)
292                 ptr = store.getCommandTransformer(params.rotationdata);
293         else if (id == Resize)
294                 ptr = store.getCommandTransformer(params.resizedata);
295
296         if (!ptr.get())
297                 return input;
298
299         string result =
300                 support::subst(input, ptr->front_placeholder(), ptr->front());
301         return support::subst(result, ptr->back_placeholder(),  ptr->back());
302 }
303
304
305 template <>
306 string const substituteIt<TransformOption>(string const & input,
307                                            TransformID id,
308                                            string const & fname,
309                                            Template::Format const & format,
310                                            InsetExternalParams const & params)
311 {
312         typedef std::map<TransformID, TransformStore> Transformers;
313         Transformers::const_iterator it = format.option_transformers.find(id);
314         if (it == format.option_transformers.end())
315                 return input;
316
317         TransformStore const & store = it->second;
318
319         TransformOption::ptr_type ptr;
320         switch (id) {
321         case Clip:
322                 ptr = store.getOptionTransformer(params.clipdata);
323                 break;
324         case Extra:
325                 ptr = store.getOptionTransformer(params.extradata.get(fname));
326                 break;
327         case Rotate:
328                 ptr = store.getOptionTransformer(params.rotationdata);
329                 break;
330         case Resize:
331                 ptr = store.getOptionTransformer(params.resizedata);
332                 break;
333         }
334
335         if (!ptr.get())
336                 return input;
337
338         return support::subst(input, ptr->placeholder(), ptr->option());
339 }
340
341
342 template <typename TransformerType>
343 string const transformIt(InsetExternalParams const & params,
344                          string const & s, string const & formatname)
345 {
346         Template const * const et = getTemplatePtr(params);
347         if (!et || et->transformIds.empty())
348                 return s;
349
350         Template::Formats::const_iterator fit = et->formats.find(formatname);
351         if (fit == et->formats.end())
352                 return s;
353
354         string result = s;
355         Template::Format const & format =  fit->second;
356
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,
362                                                        format, params);
363         }
364         return result;
365 }
366
367
368 string const substituteCommands(InsetExternalParams const & params,
369                                 string const & input, string const & format)
370 {
371         return transformIt<TransformCommand>(params, input, format);
372 }
373
374
375 string const substituteOption(InsetExternalParams const & params,
376                               string const & input, string const & format)
377 {
378         string opt = transformIt<TransformOption>(params, input, format);
379
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);
386         return opt;
387 }
388
389
390 string const substituteOptions(InsetExternalParams const & params,
391                                string const & input, string const & format)
392 {
393         string output = input;
394
395         Template const * const et = getTemplatePtr(params);
396         if (!et || et->transformIds.empty())
397                 return output;
398
399         Template::Formats::const_iterator fit = et->formats.find(format);
400         if (fit == et->formats.end() || fit->second.options.empty())
401                 return output;
402
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);
411         }
412
413         return output;
414  }
415
416 } // namespace anon
417
418 } // namespace external
419 } // namespace lyx