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
16 #include "ExternalTemplate.h"
20 #include "support/path.h"
21 #include "support/LAssert.h"
22 #include "support/filetools.h"
28 extern string user_lyxdir;
31 // We have to have dummy default commands for security reasons!
33 ExternalTemplate::ExternalTemplate()
37 ExternalTemplate::FormatTemplate::FormatTemplate()
41 ExternalTemplateManager::ExternalTemplateManager()
43 // gimp gnuchess gnuplot ical netscape tetris xpaint
44 readTemplates(user_lyxdir);
45 if (lyxerr.debugging())
52 dumpTemplate(ostream & o)
54 void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
55 ExternalTemplate const & et = vt.second;
57 ost << "Template " << et.lyxName << "\n"
58 << "\tGuiName " << et.guiName << "\n"
62 << "\tFileFilter " << et.fileRegExp << "\n"
63 << "\tViewCommand " << et.viewCommand << "\n"
64 << "\tEditCommand " << et.editCommand << "\n"
65 << "\tAutomaticProduction " << et.automaticProduction << "\n";
67 ost << "TemplateEnd" << endl;
77 dumpFormat(ostream & o)
79 void operator()(ExternalTemplate::Formats::value_type const & vt) const{
80 ExternalTemplate::FormatTemplate const & ft = vt.second;
81 ost << "\tFormat " << vt.first << "\n"
82 << "\t\tProduct " << ft.product << "\n"
83 << "\t\tUpdateCommand " << ft.updateCommand << "\n"
84 << "\t\tUpdateResult " << ft.updateResult << "\n"
85 << "\t\tRequirement " << ft.requirement << "\n"
88 << "\t\tPreambleEnd\n"
96 void ExternalTemplate::dumpFormats(ostream & os) const
98 for_each(formats.begin(), formats.end(), dumpFormat(os));
102 void ExternalTemplateManager::dumpTemplates() const
104 for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
108 ExternalTemplateManager & ExternalTemplateManager::get()
110 static ExternalTemplateManager externalTemplateManager;
111 return externalTemplateManager;
115 ExternalTemplateManager::Templates &
116 ExternalTemplateManager::getTemplates()
122 ExternalTemplateManager::Templates const &
123 ExternalTemplateManager::getTemplates() const
129 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
131 return templates[name];
135 void ExternalTemplateManager::readTemplates(string const & path)
144 keyword_item templatetags[] = {
145 { "template", TM_TEMPLATE },
146 { "templateend", TM_END }
149 string filename = LibFileSearch("", "external_templates");
150 if (filename.empty()) {
151 lyxerr << "ExternalTemplateManager::readTemplates: "
152 "No template file" << endl;
156 LyXLex lex(templatetags, TM_END);
157 if (!lex.setFile(filename)) {
158 lyxerr << "ExternalTemplateManager::readTemplates: "
159 "No template file" << endl;
167 string const temp = lex.getString();
168 ExternalTemplate & tmp = templates[temp];
170 tmp.readTemplate(lex);
175 lex.printError("Warning: End outside Template.");
182 void ExternalTemplate::readTemplate(LyXLex & lex)
184 enum TemplateOptionTags {
195 keyword_item templateoptiontags[] = {
196 { "automaticproduction", TO_AUTOMATIC },
197 { "editcommand", TO_EDITCMD },
198 { "filefilter", TO_FILTER },
199 { "format", TO_FORMAT },
200 { "guiname", TO_GUINAME },
201 { "helptext", TO_HELPTEXT },
202 { "templateend", TO_END },
203 { "viewcommand", TO_VIEWCMD }
206 pushpophelper pph(lex, templateoptiontags, TO_END);
212 guiName = lex.getString();
216 helpText = lex.getLongString("HelpTextEnd");
221 fileRegExp = lex.getString();
226 viewCommand = lex.getString();
231 editCommand = lex.getString();
236 automaticProduction = lex.getBool();
241 formats[lex.getString()].readFormat(lex);
248 lex.printError("ExternalTemplate::readTemplate: "
249 "Wrong tag: $$Token");
257 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
268 keyword_item formattags[] = {
269 { "formatend", FO_END },
270 { "preamble", FO_PREAMBLE },
271 { "product", FO_PRODUCT },
272 { "requirement", FO_REQUIREMENT },
273 { "updatecommand", FO_UPDATECMD },
274 { "updateresult", FO_UPDATERESULT }
277 pushpophelper pph(lex, formattags, FO_END);
283 product = lex.getString();
288 updateCommand = lex.getString();
291 case FO_UPDATERESULT:
293 updateResult = lex.getString();
298 requirement = lex.getString();
302 preamble = lex.getLongString("preambleend");
306 if (lyxerr.debugging())
307 lex.printError("FormatEnd");