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 "insetexternal.h"
18 #include "converter.h"
20 #include "ExternalTemplate.h"
22 #include "support/filetools.h"
23 #include "support/forkedcall.h"
24 #include "support/lstrings.h"
25 #include "support/lyxalgo.h"
26 #include "support/lyxlib.h"
27 #include "support/path.h"
28 #include "support/path_defines.h"
30 #include "support/std_ostream.h"
32 namespace support = lyx::support;
43 Template const * getTemplatePtr(InsetExternalParams const & params)
45 TemplateManager const & etm = TemplateManager::get();
46 return etm.getTemplateByName(params.templatename());
50 void editExternal(InsetExternalParams const & params, Buffer const & buffer)
52 Template const * const et_ptr = getTemplatePtr(params);
55 Template const & et = *et_ptr;
57 if (et.editCommand.empty())
60 string const command = doSubstitution(params, buffer, et.editCommand);
62 support::Path p(buffer.filePath());
63 support::Forkedcall call;
64 if (lyxerr.debugging(Debug::EXTERNAL)) {
65 lyxerr << "Executing '" << command << "' in '"
66 << buffer.filePath() << '\'' << endl;
68 call.startscript(support::Forkedcall::DontWait, command);
74 string const doSubstitution(InsetExternalParams const & params,
75 Buffer const & buffer, string const & s,
76 string const & filename)
79 string const basename = support::ChangeExtension(filename, string());
80 string const filepath = support::OnlyPath(filename);
82 result = support::subst(s, "$$FName", filename);
83 result = support::subst(result, "$$Basename", basename);
84 result = support::subst(result, "$$FPath", filepath);
85 result = support::subst(result, "$$Tempname", params.tempname());
86 result = support::subst(result, "$$Sysdir", support::system_lyxdir());
88 // Handle the $$Contents(filename) syntax
89 if (support::contains(result, "$$Contents(\"")) {
91 string::size_type const pos = result.find("$$Contents(\"");
92 string::size_type const end = result.find("\")", pos);
93 string const file = result.substr(pos + 12, end - (pos + 12));
96 string const filepath = support::IsFileReadable(file) ?
97 buffer.filePath() : buffer.temppath();
98 support::Path p(filepath);
100 if (support::IsFileReadable(file))
101 contents = support::GetFileContents(file);
103 result = support::subst(result,
104 ("$$Contents(\"" + file + "\")").c_str(),
111 /** update the file represented by the template.
112 If \param external_in_tmpdir == true, then the generated file is
113 place in the buffer's temporary directory.
115 void updateExternal(InsetExternalParams const & params,
116 string const & format,
117 Buffer const & buffer,
118 bool external_in_tmpdir)
120 Template const * const et_ptr = getTemplatePtr(params);
123 Template const & et = *et_ptr;
125 if (!et.automaticProduction)
126 return; // NOT_NEEDED
128 Template::Formats::const_iterator cit = et.formats.find(format);
129 if (cit == et.formats.end())
132 Template::Format const & outputFormat = cit->second;
133 if (outputFormat.updateResult.empty())
134 return; // NOT_NEEDED
136 string from_format = et.inputFormat;
137 if (from_format.empty())
138 return; // NOT_NEEDED
140 string from_file = params.filename.absFilename();
142 if (from_format == "*") {
143 if (from_file.empty())
144 return; // NOT_NEEDED
146 // Try and ascertain the file format from its contents.
147 from_format = support::getExtFromContents(from_file);
148 if (from_format.empty())
152 string const to_format = outputFormat.updateFormat;
153 if (to_format.empty())
154 return; // NOT_NEEDED
156 if (!converters.isReachable(from_format, to_format)) {
157 lyxerr[Debug::EXTERNAL]
158 << "external::updateExternal. "
159 << "Unable to convert from "
160 << from_format << " to " << to_format << endl;
164 if (external_in_tmpdir && !from_file.empty()) {
165 // We are running stuff through LaTeX
166 string const temp_file =
167 support::MakeAbsPath(params.filename.mangledFilename(),
169 unsigned long const from_checksum = support::sum(from_file);
170 unsigned long const temp_checksum = support::sum(temp_file);
172 if (from_checksum != temp_checksum) {
173 if (!support::copy(from_file, temp_file))
177 from_file = temp_file;
180 string const to_file = doSubstitution(params, buffer,
181 outputFormat.updateResult,
184 string const abs_to_file =
185 support::MakeAbsPath(to_file, buffer.filePath());
187 // Do we need to perform the conversion?
188 // Yes if to_file does not exist or if from_file is newer than to_file
189 if (support::compare_timestamps(from_file, abs_to_file) < 0)
192 string const to_file_base =
193 support::ChangeExtension(to_file, string());
194 /* bool const success = */
195 converters.convert(&buffer, from_file, to_file_base,
196 from_format, to_format);
203 int writeExternal(InsetExternalParams const & params,
204 string const & format,
205 Buffer const & buffer, ostream & os,
206 bool external_in_tmpdir)
208 Template const * const et_ptr = getTemplatePtr(params);
211 Template const & et = *et_ptr;
213 Template::Formats::const_iterator cit = et.formats.find(format);
214 if (cit == et.formats.end()) {
215 lyxerr[Debug::EXTERNAL]
216 << "External template format '" << format
217 << "' not specified in template "
218 << params.templatename() << endl;
222 updateExternal(params, format, buffer, external_in_tmpdir);
224 string from_file = params.filename.outputFilename(buffer.filePath());
225 if (external_in_tmpdir && !from_file.empty()) {
226 // We are running stuff through LaTeX
228 support::MakeAbsPath(params.filename.mangledFilename(),
232 string const str = doSubstitution(params, buffer, cit->second.product,
235 return int(lyx::count(str.begin(), str.end(),'\n') + 1);
239 /// Substitute meta-variables in this string
240 string const doSubstitution(InsetExternalParams const & params,
241 Buffer const & buffer, string const & s)
243 string const buffer_path = buffer.filePath();
244 string const filename = params.filename.outputFilename(buffer_path);
245 return doSubstitution(params, buffer, s, filename);
248 } // namespace external