2 * \file ExternalTemplate.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
8 * Full author contact details are available in file CREDITS.
13 #include "ExternalTemplate.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/path.h"
21 #include "support/path_defines.h"
23 #include <boost/assert.hpp>
27 namespace support = lyx::support;
36 // We have to have dummy default commands for security reasons!
38 ExternalTemplate::ExternalTemplate()
43 ExternalTemplate::FormatTemplate::FormatTemplate()
47 ExternalTemplateManager::ExternalTemplateManager()
49 readTemplates(support::user_lyxdir());
50 if (lyxerr.debugging(Debug::EXTERNAL)) {
51 dumpPreambleDefs(lyxerr);
53 dumpTemplates(lyxerr);
58 class dumpPreambleDef {
60 typedef ExternalTemplateManager::PreambleDefs::value_type value_type;
62 dumpPreambleDef(ostream & o) : ost(o) {}
64 void operator()(value_type const & vt) {
65 ost << "PreambleDef " << vt.first << '\n'
67 << "PreambleDefEnd" << endl;
77 typedef ExternalTemplateManager::Templates::value_type value_type;
79 dumpTemplate(ostream & o) : ost(o) {}
81 void operator()(value_type const & vt) {
82 ExternalTemplate const & et = vt.second;
84 ost << "Template " << et.lyxName << '\n'
85 << "\tGuiName " << et.guiName << '\n'
89 << "\tInputFormat " << et.inputFormat << '\n'
90 << "\tFileFilter " << et.fileRegExp << '\n'
91 << "\tEditCommand " << et.editCommand << '\n'
92 << "\tAutomaticProduction " << et.automaticProduction << '\n';
94 ost << "TemplateEnd" << endl;
104 typedef ExternalTemplate::Formats::value_type value_type;
106 dumpFormat(ostream & o) : ost(o) {}
108 void operator()(value_type const & vt) const{
109 ExternalTemplate::FormatTemplate const & ft = vt.second;
110 ost << "\tFormat " << vt.first << '\n'
111 << "\t\tProduct " << ft.product << '\n'
112 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
113 << "\t\tUpdateResult " << ft.updateResult << '\n'
114 << "\t\tRequirement " << ft.requirement << '\n';
116 vector<string>::const_iterator it = ft.preambleNames.begin();
117 vector<string>::const_iterator end = ft.preambleNames.end();
118 for (; it != end; ++it) {
119 ost << "\t\tPreamble " << *it << '\n';
122 ost << "\tFormatEnd\n";
129 void ExternalTemplate::dumpFormats(ostream & os) const
131 for_each(formats.begin(), formats.end(), dumpFormat(os));
135 void ExternalTemplateManager::dumpPreambleDefs(ostream & os) const
137 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
141 void ExternalTemplateManager::dumpTemplates(ostream & os) const
143 for_each(templates.begin(), templates.end(), dumpTemplate(os));
147 ExternalTemplateManager & ExternalTemplateManager::get()
149 static ExternalTemplateManager externalTemplateManager;
150 return externalTemplateManager;
154 ExternalTemplateManager::Templates &
155 ExternalTemplateManager::getTemplates()
161 ExternalTemplateManager::Templates const &
162 ExternalTemplateManager::getTemplates() const
168 ExternalTemplate const *
169 ExternalTemplateManager::getTemplateByName(string const & name) const
171 Templates::const_iterator it = templates.find(name);
172 return (it == templates.end()) ? 0 : &it->second;
177 ExternalTemplateManager::getPreambleDefByName(string const & name) const
179 string const trimmed_name = support::trim(name);
180 if (trimmed_name.empty())
183 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
184 if (it == preambledefs.end())
191 void ExternalTemplateManager::readTemplates(string const & path)
193 support::Path p(path);
202 keyword_item templatetags[] = {
203 { "preambledef", TM_PREAMBLEDEF },
204 { "preambledefend", TM_PREAMBLEDEF_END },
205 { "template", TM_TEMPLATE },
206 { "templateend", TM_TEMPLATE_END }
209 LyXLex lex(templatetags, TM_TEMPLATE_END);
211 string filename = support::LibFileSearch("", "external_templates");
212 if (filename.empty() || !lex.setFile(filename)) {
213 lex.printError("ExternalTemplateManager::readTemplates: "
218 char const * const preamble_end_tag =
219 templatetags[TM_PREAMBLEDEF_END-1].tag;
223 case TM_PREAMBLEDEF: {
225 string const name = lex.getString();
226 preambledefs[name] = lex.getLongString(preamble_end_tag);
232 string const name = lex.getString();
233 ExternalTemplate & tmp = templates[name];
235 tmp.readTemplate(lex);
239 case TM_TEMPLATE_END:
240 lex.printError("Warning: End outside Template.");
243 case TM_PREAMBLEDEF_END:
244 lex.printError("Warning: End outside PreambleDef.");
251 void ExternalTemplate::readTemplate(LyXLex & lex)
253 enum TemplateOptionTags {
264 keyword_item templateoptiontags[] = {
265 { "automaticproduction", TO_AUTOMATIC },
266 { "editcommand", TO_EDITCMD },
267 { "filefilter", TO_FILTER },
268 { "format", TO_FORMAT },
269 { "guiname", TO_GUINAME },
270 { "helptext", TO_HELPTEXT },
271 { "inputformat", TO_INPUTFORMAT },
272 { "templateend", TO_END }
275 pushpophelper pph(lex, templateoptiontags, TO_END);
281 guiName = lex.getString();
285 helpText = lex.getLongString("HelpTextEnd");
290 inputFormat = lex.getString();
295 fileRegExp = lex.getString();
300 editCommand = lex.getString();
305 automaticProduction = lex.getBool();
310 formats[lex.getString()].readFormat(lex);
317 lex.printError("ExternalTemplate::readTemplate: "
318 "Wrong tag: $$Token");
326 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
337 keyword_item formattags[] = {
338 { "formatend", FO_END },
339 { "preamble", FO_PREAMBLE },
340 { "product", FO_PRODUCT },
341 { "requirement", FO_REQUIREMENT },
342 { "updateformat", FO_UPDATEFORMAT },
343 { "updateresult", FO_UPDATERESULT }
346 pushpophelper pph(lex, formattags, FO_END);
352 product = lex.getString();
355 case FO_UPDATEFORMAT:
357 updateFormat = lex.getString();
360 case FO_UPDATERESULT:
362 updateResult = lex.getString();
367 requirement = lex.getString();
372 preambleNames.push_back(lex.getString());