]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalSupport.C
Georg's add an editor to Formats / safe external_templates patch.
[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 #include "format.h"
23
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"
31
32 #include "support/std_ostream.h"
33
34 namespace support = lyx::support;
35
36 using std::endl;
37
38 using std::ostream;
39 using std::string;
40 using std::vector;
41
42
43 namespace lyx {
44 namespace external {
45
46 Template const * getTemplatePtr(InsetExternalParams const & params)
47 {
48         TemplateManager const & etm = TemplateManager::get();
49         return etm.getTemplateByName(params.templatename());
50 }
51
52
53 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
54 {
55         string const file_with_path = params.filename.absFilename();
56         formats.edit(buffer, file_with_path,
57                      support::getExtFromContents(file_with_path));
58 }
59
60
61 namespace {
62
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.
68 */
69 string const doSubstitution(InsetExternalParams const & params,
70                             Buffer const & buffer, string const & s,
71                             string const & filename)
72 {
73         string result;
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();
88
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",
99                                         abspath);
100                 result = support::subst(result, "$$AbsOrRelPathParent",
101                                         abspath);
102         } else {
103                 result = support::subst(result, "$$AbsOrRelPathMaster",
104                                         relToMasterPath);
105                 result = support::subst(result, "$$AbsOrRelPathParent",
106                                         relToParentPath);
107         }
108         result = support::subst(result, "$$Tempname", params.tempname());
109         result = support::subst(result, "$$Sysdir", support::system_lyxdir());
110
111         // Handle the $$Contents(filename) syntax
112         if (support::contains(result, "$$Contents(\"")) {
113
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));
117                 string contents;
118
119                 string const filepath = support::IsFileReadable(file) ?
120                         buffer.filePath() : m_buffer->temppath();
121                 support::Path p(filepath);
122
123                 if (support::IsFileReadable(file))
124                         contents = support::GetFileContents(file);
125
126                 result = support::subst(result,
127                                         ("$$Contents(\"" + file + "\")").c_str(),
128                                         contents);
129         }
130
131         return result;
132 }
133
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.
137 */
138 void updateExternal(InsetExternalParams const & params,
139                     string const & format,
140                     Buffer const & buffer,
141                     bool external_in_tmpdir)
142 {
143         Template const * const et_ptr = getTemplatePtr(params);
144         if (!et_ptr)
145                 return; // FAILURE
146         Template const & et = *et_ptr;
147
148         if (!et.automaticProduction)
149                 return; // NOT_NEEDED
150
151         Template::Formats::const_iterator cit = et.formats.find(format);
152         if (cit == et.formats.end())
153                 return; // FAILURE
154
155         Template::Format const & outputFormat = cit->second;
156         if (outputFormat.updateResult.empty())
157                 return; // NOT_NEEDED
158
159         string from_format = et.inputFormat;
160         if (from_format.empty())
161                 return; // NOT_NEEDED
162
163         string abs_from_file = params.filename.absFilename();
164
165         if (from_format == "*") {
166                 if (abs_from_file.empty())
167                         return; // NOT_NEEDED
168
169                 // Try and ascertain the file format from its contents.
170                 from_format = support::getExtFromContents(abs_from_file);
171                 if (from_format.empty())
172                         return; // FAILURE
173
174         }
175
176         string const to_format = outputFormat.updateFormat;
177         if (to_format.empty())
178                 return; // NOT_NEEDED
179
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;
185                 return; // FAILURE
186         }
187
188         string from_file = params.filename.outputFilename(buffer.filePath());
189
190         // The master buffer. This is useful when there are multiple levels
191         // of include files
192         Buffer const * m_buffer = buffer.getMasterBuffer();
193
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);
201
202                 if (from_checksum != temp_checksum) {
203                         if (!support::copy(abs_from_file, temp_file)) {
204                                 lyxerr[Debug::EXTERNAL]
205                                         << "external::updateExternal. "
206                                         << "Unable to copy "
207                                         << abs_from_file << " to " << temp_file << endl;
208                                 return; // FAILURE
209                         }
210                 }
211
212                 from_file = temp_file;
213                 abs_from_file = temp_file;
214         }
215
216         string const to_file = doSubstitution(params, buffer,
217                                               outputFormat.updateResult,
218                                               from_file);
219
220         string const abs_to_file =
221                 support::MakeAbsPath(to_file, external_in_tmpdir
222                         ? m_buffer->temppath()
223                         : buffer.filePath());
224
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)
228                 return; // SUCCESS
229
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);
235         // return success
236 }
237
238
239 string const substituteCommands(InsetExternalParams const & params,
240                                 string const & input, string const & format);
241
242 string const substituteOptions(InsetExternalParams const & params,
243                                string const & input, string const & format);
244
245 } // namespace anon
246
247
248 int writeExternal(InsetExternalParams const & params,
249                   string const & format,
250                   Buffer const & buffer, ostream & os,
251                   bool external_in_tmpdir)
252 {
253         Template const * const et_ptr = getTemplatePtr(params);
254         if (!et_ptr)
255                 return 0;
256         Template const & et = *et_ptr;
257
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;
264                 return 0;
265         }
266
267         updateExternal(params, format, buffer, external_in_tmpdir);
268
269         string from_file = params.filename.outputFilename(buffer.filePath());
270         if (external_in_tmpdir && !from_file.empty()) {
271                 // We are running stuff through LaTeX
272                 from_file =
273                         support::MakeAbsPath(params.filename.mangledFilename(),
274                                              buffer.getMasterBuffer()->temppath());
275         }
276
277         string str = doSubstitution(params, buffer, cit->second.product,
278                                     from_file);
279         str = substituteCommands(params, str, format);
280         str = substituteOptions(params, str, format);
281         os << str;
282         return int(lyx::count(str.begin(), str.end(),'\n') + 1);
283 }
284
285
286 /// Substitute meta-variables in this string
287 string const doSubstitution(InsetExternalParams const & params,
288                             Buffer const & buffer, string const & s)
289 {
290         string const buffer_path = buffer.filePath();
291         string const filename = params.filename.outputFilename(buffer_path);
292         return doSubstitution(params, buffer, s, filename);
293 }
294
295 namespace {
296
297 // Empty template, specialised below.
298 template <typename TransformType>
299 string const substituteIt(string const &,
300                           TransformID,
301                           string const &,
302                           Template::Format const &,
303                           InsetExternalParams const &);
304
305
306 template <>
307 string const substituteIt<TransformCommand>(string const & input,
308                                             TransformID id,
309                                             string const & /* formatname */,
310                                             Template::Format const & format,
311                                             InsetExternalParams const & params)
312 {
313         typedef std::map<TransformID, TransformStore> Transformers;
314         Transformers::const_iterator it = format.command_transformers.find(id);
315         if (it == format.command_transformers.end())
316                 return input;
317
318         TransformStore const & store = it->second;
319
320         TransformCommand::ptr_type ptr;
321         if (id == Rotate)
322                 ptr = store.getCommandTransformer(params.rotationdata);
323         else if (id == Resize)
324                 ptr = store.getCommandTransformer(params.resizedata);
325
326         if (!ptr.get())
327                 return input;
328
329         string result =
330                 support::subst(input, ptr->front_placeholder(), ptr->front());
331         return support::subst(result, ptr->back_placeholder(),  ptr->back());
332 }
333
334
335 template <>
336 string const substituteIt<TransformOption>(string const & input,
337                                            TransformID id,
338                                            string const & fname,
339                                            Template::Format const & format,
340                                            InsetExternalParams const & params)
341 {
342         typedef std::map<TransformID, TransformStore> Transformers;
343         Transformers::const_iterator it = format.option_transformers.find(id);
344         if (it == format.option_transformers.end())
345                 return input;
346
347         TransformStore const & store = it->second;
348
349         TransformOption::ptr_type ptr;
350         switch (id) {
351         case Clip:
352                 ptr = store.getOptionTransformer(params.clipdata);
353                 break;
354         case Extra:
355                 ptr = store.getOptionTransformer(params.extradata.get(fname));
356                 break;
357         case Rotate:
358                 ptr = store.getOptionTransformer(params.rotationdata);
359                 break;
360         case Resize:
361                 ptr = store.getOptionTransformer(params.resizedata);
362                 break;
363         }
364
365         if (!ptr.get())
366                 return input;
367
368         return support::subst(input, ptr->placeholder(), ptr->option());
369 }
370
371
372 template <typename TransformerType>
373 string const transformIt(InsetExternalParams const & params,
374                          string const & s, string const & formatname)
375 {
376         Template const * const et = getTemplatePtr(params);
377         if (!et || et->transformIds.empty())
378                 return s;
379
380         Template::Formats::const_iterator fit = et->formats.find(formatname);
381         if (fit == et->formats.end())
382                 return s;
383
384         string result = s;
385         Template::Format const & format =  fit->second;
386
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,
392                                                        format, params);
393         }
394         return result;
395 }
396
397
398 string const substituteCommands(InsetExternalParams const & params,
399                                 string const & input, string const & format)
400 {
401         return transformIt<TransformCommand>(params, input, format);
402 }
403
404
405 string const substituteOption(InsetExternalParams const & params,
406                               string const & input, string const & format)
407 {
408         string opt = transformIt<TransformOption>(params, input, format);
409
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);
416         return opt;
417 }
418
419
420 string const substituteOptions(InsetExternalParams const & params,
421                                string const & input, string const & format)
422 {
423         string output = input;
424
425         Template const * const et = getTemplatePtr(params);
426         if (!et || et->transformIds.empty())
427                 return output;
428
429         Template::Formats::const_iterator fit = et->formats.find(format);
430         if (fit == et->formats.end() || fit->second.options.empty())
431                 return output;
432
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);
441         }
442
443         return output;
444  }
445
446 } // namespace anon
447
448 } // namespace external
449 } // namespace lyx