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 void ExternalTemplateManager::readTemplates(string const & path)
141 keyword_item templatetags[] = {
142 { "template", TM_TEMPLATE },
143 { "templateend", TM_END }
146 string filename = LibFileSearch("", "external_templates");
147 if (filename.empty()) {
148 lyxerr << "ExternalTemplateManager::readTemplates: "
149 "No template file" << endl;
153 LyXLex lex(templatetags, TM_END);
154 if (!lex.setFile(filename)) {
155 lyxerr << "ExternalTemplateManager::readTemplates: "
156 "No template file" << endl;
164 string temp = lex.GetString();
165 ExternalTemplate & tmp = templates[temp];
167 tmp.readTemplate(lex);
172 lex.printError("Warning: End outside Template.");
179 void ExternalTemplate::readTemplate(LyXLex & lex)
181 enum TemplateOptionTags {
192 keyword_item templateoptiontags[] = {
193 { "automaticproduction", TO_AUTOMATIC },
194 { "editcommand", TO_EDITCMD },
195 { "filefilter", TO_FILTER },
196 { "format", TO_FORMAT },
197 { "guiname", TO_GUINAME },
198 { "helptext", TO_HELPTEXT },
199 { "templateend", TO_END },
200 { "viewcommand", TO_VIEWCMD }
203 pushpophelper pph(lex, templateoptiontags, TO_END);
209 guiName = lex.GetString();
213 helpText = lex.getLongString("HelpTextEnd");
218 fileRegExp = lex.GetString();
223 viewCommand = lex.GetString();
224 // For security reasons, a command may not be empty!
225 if (viewCommand.empty())
226 viewCommand = "true";
231 editCommand = lex.GetString();
232 // For security reasons, a command may not be empty!
233 if (editCommand.empty())
234 editCommand = "true";
239 automaticProduction = lex.GetBool();
244 formats[lex.GetString()].readFormat(lex);
251 lex.printError("ExternalTemplate::readTemplate: "
252 "Wrong tag: $$Token");
260 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
270 keyword_item formattags[] = {
271 { "formatend", FO_END },
272 { "preamble", FO_PREAMBLE },
273 { "product", FO_PRODUCT },
274 { "requirement", FO_REQUIREMENT },
275 { "updatecommand", FO_UPDATECMD }
278 pushpophelper pph(lex, formattags, FO_END);
284 product = lex.GetString();
289 updateCommand = lex.GetString();
290 // For security reasons, a command may not be empty!
291 if (updateCommand.empty())
292 updateCommand = "true";
297 requirement = lex.GetString();
301 preamble = lex.getLongString("preambleend");
305 if (lyxerr.debugging())
306 lex.printError("FormatEnd");