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;
35 // We have to have dummy default commands for security reasons!
37 ExternalTemplate::ExternalTemplate()
42 ExternalTemplate::FormatTemplate::FormatTemplate()
46 ExternalTemplateManager::ExternalTemplateManager()
48 // gimp gnuchess gnuplot ical netscape tetris xpaint
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'
115 << "\t\tPreamble " << ft.preambleName << '\n'
123 void ExternalTemplate::dumpFormats(ostream & os) const
125 for_each(formats.begin(), formats.end(), dumpFormat(os));
129 void ExternalTemplateManager::dumpPreambleDefs(ostream & os) const
131 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
135 void ExternalTemplateManager::dumpTemplates(ostream & os) const
137 for_each(templates.begin(), templates.end(), dumpTemplate(os));
141 ExternalTemplateManager & ExternalTemplateManager::get()
143 static ExternalTemplateManager externalTemplateManager;
144 return externalTemplateManager;
148 ExternalTemplateManager::Templates &
149 ExternalTemplateManager::getTemplates()
155 ExternalTemplateManager::Templates const &
156 ExternalTemplateManager::getTemplates() const
162 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
164 return templates[name];
169 ExternalTemplateManager::getPreambleDefByName(string const & name) const
171 string const trimmed_name = support::trim(name);
172 if (trimmed_name.empty())
175 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
176 if (it == preambledefs.end())
183 void ExternalTemplateManager::readTemplates(string const & path)
185 support::Path p(path);
194 keyword_item templatetags[] = {
195 { "preambledef", TM_PREAMBLEDEF },
196 { "preambledefend", TM_PREAMBLEDEF_END },
197 { "template", TM_TEMPLATE },
198 { "templateend", TM_TEMPLATE_END }
201 LyXLex lex(templatetags, TM_TEMPLATE_END);
203 string filename = support::LibFileSearch("", "external_templates");
204 if (filename.empty() || !lex.setFile(filename)) {
205 lex.printError("ExternalTemplateManager::readTemplates: "
210 char const * const preamble_end_tag =
211 templatetags[TM_PREAMBLEDEF_END-1].tag;
215 case TM_PREAMBLEDEF: {
217 string const name = lex.getString();
218 preambledefs[name] = lex.getLongString(preamble_end_tag);
224 string const name = lex.getString();
225 ExternalTemplate & tmp = templates[name];
227 tmp.readTemplate(lex);
231 case TM_TEMPLATE_END:
232 lex.printError("Warning: End outside Template.");
235 case TM_PREAMBLEDEF_END:
236 lex.printError("Warning: End outside PreambleDef.");
243 void ExternalTemplate::readTemplate(LyXLex & lex)
245 enum TemplateOptionTags {
256 keyword_item templateoptiontags[] = {
257 { "automaticproduction", TO_AUTOMATIC },
258 { "editcommand", TO_EDITCMD },
259 { "filefilter", TO_FILTER },
260 { "format", TO_FORMAT },
261 { "guiname", TO_GUINAME },
262 { "helptext", TO_HELPTEXT },
263 { "inputformat", TO_INPUTFORMAT },
264 { "templateend", TO_END }
267 pushpophelper pph(lex, templateoptiontags, TO_END);
273 guiName = lex.getString();
277 helpText = lex.getLongString("HelpTextEnd");
282 inputFormat = lex.getString();
287 fileRegExp = lex.getString();
292 editCommand = lex.getString();
297 automaticProduction = lex.getBool();
302 formats[lex.getString()].readFormat(lex);
309 lex.printError("ExternalTemplate::readTemplate: "
310 "Wrong tag: $$Token");
318 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
329 keyword_item formattags[] = {
330 { "formatend", FO_END },
331 { "preamble", FO_PREAMBLE },
332 { "product", FO_PRODUCT },
333 { "requirement", FO_REQUIREMENT },
334 { "updateformat", FO_UPDATEFORMAT },
335 { "updateresult", FO_UPDATERESULT }
338 pushpophelper pph(lex, formattags, FO_END);
344 product = lex.getString();
347 case FO_UPDATEFORMAT:
349 updateFormat = lex.getString();
352 case FO_UPDATERESULT:
354 updateResult = lex.getString();
359 requirement = lex.getString();
364 preambleName = lex.getString();