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"
22 #include "support/path.h"
23 #include "support/LAssert.h"
29 extern string user_lyxdir;
32 // We have to have dummy default commands for security reasons!
34 ExternalTemplate::ExternalTemplate()
35 : viewCommand("true"), editCommand("true")
39 ExternalTemplate::FormatTemplate::FormatTemplate()
40 : updateCommand("true") {}
43 ExternalTemplateManager::ExternalTemplateManager()
45 // gimp gnuchess gnuplot ical netscape tetris xpaint
46 readTemplates(user_lyxdir);
53 dumpTemplate(std::ostream & o)
55 void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
56 ExternalTemplate const & et = vt.second;
58 ost << "Template " << et.lyxName << "\n"
59 << "\tGuiName " << et.guiName << "\n"
63 << "\tFileFilter " << et.fileRegExp << "\n"
64 << "\tViewCommand " << et.viewCommand << "\n"
65 << "\tEditCommand " << et.editCommand << "\n"
66 << "\tAutomaticProduction " << et.automaticProduction << "\n";
68 ost << "TemplateEnd" << endl;
78 dumpFormat(ostream & o)
80 void operator()(ExternalTemplate::Formats::value_type const & vt) const{
81 ExternalTemplate::FormatTemplate const & ft = vt.second;
82 ost << "\tFormat " << vt.first << "\n"
83 << "\t\tProduct " << ft.product << "\n"
84 << "\t\tUpdateCommand " << ft.updateCommand << "\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 void ExternalTemplateManager::readTemplates(string const & path)
138 keyword_item templatetags[] = {
139 { "template", TM_TEMPLATE },
140 { "templateend", TM_END }
143 string filename = LibFileSearch("", "external_templates");
144 if (filename.empty()) {
145 lyxerr << "No template file" << endl;
149 LyXLex lex(templatetags, TM_END);
150 if (!lex.setFile(filename)) {
151 lyxerr << "No template file" << endl;
159 string temp = lex.GetString();
160 ExternalTemplate & tmp = templates[temp];
162 tmp.readTemplate(lex);
167 lyxerr << "TemplateEnd: " << lex.GetString() << endl;
168 lyxerr << "Warning: End outside Template." << endl;
175 void ExternalTemplate::readTemplate(LyXLex & lex)
177 enum TemplateOptionTags {
188 keyword_item templateoptiontags[] = {
189 { "automaticproduction", TO_AUTOMATIC },
190 { "editcommand", TO_EDITCMD },
191 { "filefilter", TO_FILTER },
192 { "format", TO_FORMAT },
193 { "guiname", TO_GUINAME },
194 { "helptext", TO_HELPTEXT },
195 { "templateend", TO_END },
196 { "viewcommand", TO_VIEWCMD }
199 pushpophelper pph(lex, templateoptiontags, TO_END);
205 guiName = lex.GetString();
209 helpText = lex.getLongString("HelpTextEnd");
214 fileRegExp = lex.GetString();
219 viewCommand = lex.GetString();
220 // For security reasons, a command may not be empty!
221 if (viewCommand.empty())
222 viewCommand = "true";
227 editCommand = lex.GetString();
228 // For security reasons, a command may not be empty!
229 if (editCommand.empty())
230 editCommand = "true";
235 automaticProduction = lex.GetBool();
240 formats[lex.GetString()].readFormat(lex);
247 lyxerr << "Default: " << lex.GetString() << endl;
255 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
265 keyword_item formattags[] = {
266 { "formatend", FO_END },
267 { "preamble", FO_PREAMBLE },
268 { "product", FO_PRODUCT },
269 { "requirement", FO_REQUIREMENT },
270 { "updatecommand", FO_UPDATECMD }
273 pushpophelper pph(lex, formattags, FO_END);
279 product = lex.GetString();
284 updateCommand = lex.GetString();
285 // For security reasons, a command may not be empty!
286 if (updateCommand.empty())
287 updateCommand = "true";
292 requirement = lex.GetString();
296 preamble = lex.getLongString("preambleend");
300 lyxerr << "FormatEnd: " << lex.GetString() << endl;