1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
19 #include "ExternalTemplate.h"
23 #include "support/path.h"
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
31 extern string user_lyxdir;
34 // We have to have dummy default commands for security reasons!
36 ExternalTemplate::ExternalTemplate()
37 : viewCommand("true"), editCommand("true")
41 ExternalTemplate::FormatTemplate::FormatTemplate()
42 : updateCommand("true") {}
45 ExternalTemplateManager::ExternalTemplateManager()
47 // gimp gnuchess gnuplot ical netscape tetris xpaint
48 readTemplates(user_lyxdir);
49 if (lyxerr.debugging())
56 dumpTemplate(std::ostream & o)
58 void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
59 ExternalTemplate const & et = vt.second;
61 ost << "Template " << et.lyxName << "\n"
62 << "\tGuiName " << et.guiName << "\n"
66 << "\tFileFilter " << et.fileRegExp << "\n"
67 << "\tViewCommand " << et.viewCommand << "\n"
68 << "\tEditCommand " << et.editCommand << "\n"
69 << "\tAutomaticProduction " << et.automaticProduction << "\n";
71 ost << "TemplateEnd" << endl;
81 dumpFormat(ostream & o)
83 void operator()(ExternalTemplate::Formats::value_type const & vt) const{
84 ExternalTemplate::FormatTemplate const & ft = vt.second;
85 ost << "\tFormat " << vt.first << "\n"
86 << "\t\tProduct " << ft.product << "\n"
87 << "\t\tUpdateCommand " << ft.updateCommand << "\n"
88 << "\t\tRequirement " << ft.requirement << "\n"
91 << "\t\tPreambleEnd\n"
99 void ExternalTemplate::dumpFormats(ostream & os) const
101 for_each(formats.begin(), formats.end(), dumpFormat(os));
105 void ExternalTemplateManager::dumpTemplates() const
107 for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
111 ExternalTemplateManager & ExternalTemplateManager::get()
113 static ExternalTemplateManager externalTemplateManager;
114 return externalTemplateManager;
118 ExternalTemplateManager::Templates &
119 ExternalTemplateManager::getTemplates()
125 ExternalTemplateManager::Templates const &
126 ExternalTemplateManager::getTemplates() const
132 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
134 return templates[name];
138 void ExternalTemplateManager::readTemplates(string const & path)
147 keyword_item templatetags[] = {
148 { "template", TM_TEMPLATE },
149 { "templateend", TM_END }
152 string filename = LibFileSearch("", "external_templates");
153 if (filename.empty()) {
154 lyxerr << "ExternalTemplateManager::readTemplates: "
155 "No template file" << endl;
159 LyXLex lex(templatetags, TM_END);
160 if (!lex.setFile(filename)) {
161 lyxerr << "ExternalTemplateManager::readTemplates: "
162 "No template file" << endl;
170 string temp = lex.GetString();
171 ExternalTemplate & tmp = templates[temp];
173 tmp.readTemplate(lex);
178 lex.printError("Warning: End outside Template.");
185 void ExternalTemplate::readTemplate(LyXLex & lex)
187 enum TemplateOptionTags {
198 keyword_item templateoptiontags[] = {
199 { "automaticproduction", TO_AUTOMATIC },
200 { "editcommand", TO_EDITCMD },
201 { "filefilter", TO_FILTER },
202 { "format", TO_FORMAT },
203 { "guiname", TO_GUINAME },
204 { "helptext", TO_HELPTEXT },
205 { "templateend", TO_END },
206 { "viewcommand", TO_VIEWCMD }
209 pushpophelper pph(lex, templateoptiontags, TO_END);
215 guiName = lex.GetString();
219 helpText = lex.getLongString("HelpTextEnd");
224 fileRegExp = lex.GetString();
229 viewCommand = lex.GetString();
230 // For security reasons, a command may not be empty!
231 if (viewCommand.empty())
232 viewCommand = "true";
237 editCommand = lex.GetString();
238 // For security reasons, a command may not be empty!
239 if (editCommand.empty())
240 editCommand = "true";
245 automaticProduction = lex.GetBool();
250 formats[lex.GetString()].readFormat(lex);
257 lex.printError("ExternalTemplate::readTemplate: "
258 "Wrong tag: $$Token");
266 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
276 keyword_item formattags[] = {
277 { "formatend", FO_END },
278 { "preamble", FO_PREAMBLE },
279 { "product", FO_PRODUCT },
280 { "requirement", FO_REQUIREMENT },
281 { "updatecommand", FO_UPDATECMD }
284 pushpophelper pph(lex, formattags, FO_END);
290 product = lex.GetString();
295 updateCommand = lex.GetString();
296 // For security reasons, a command may not be empty!
297 if (updateCommand.empty())
298 updateCommand = "true";
303 requirement = lex.GetString();
307 preamble = lex.getLongString("preambleend");
311 if (lyxerr.debugging())
312 lex.printError("FormatEnd");