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);
55 dumpTemplate(std::ostream & o)
57 void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
58 ExternalTemplate const & et = vt.second;
60 ost << "Template " << et.lyxName << "\n"
61 << "\tGuiName " << et.guiName << "\n"
65 << "\tFileFilter " << et.fileRegExp << "\n"
66 << "\tViewCommand " << et.viewCommand << "\n"
67 << "\tEditCommand " << et.editCommand << "\n"
68 << "\tAutomaticProduction " << et.automaticProduction << "\n";
70 ost << "TemplateEnd" << endl;
80 dumpFormat(ostream & o)
82 void operator()(ExternalTemplate::Formats::value_type const & vt) const{
83 ExternalTemplate::FormatTemplate const & ft = vt.second;
84 ost << "\tFormat " << vt.first << "\n"
85 << "\t\tProduct " << ft.product << "\n"
86 << "\t\tUpdateCommand " << ft.updateCommand << "\n"
87 << "\t\tRequirement " << ft.requirement << "\n"
90 << "\t\tPreambleEnd\n"
98 void ExternalTemplate::dumpFormats(ostream & os) const
100 for_each(formats.begin(), formats.end(), dumpFormat(os));
104 void ExternalTemplateManager::dumpTemplates() const
106 for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
110 ExternalTemplateManager & ExternalTemplateManager::get()
112 static ExternalTemplateManager externalTemplateManager;
113 return externalTemplateManager;
117 ExternalTemplateManager::Templates &
118 ExternalTemplateManager::getTemplates()
124 ExternalTemplateManager::Templates const &
125 ExternalTemplateManager::getTemplates() const
131 void ExternalTemplateManager::readTemplates(string const & path)
140 keyword_item templatetags[] = {
141 { "template", TM_TEMPLATE },
142 { "templateend", TM_END }
145 string filename = LibFileSearch("", "external_templates");
146 if (filename.empty()) {
147 lyxerr << "No template file" << endl;
151 LyXLex lex(templatetags, TM_END);
152 if (!lex.setFile(filename)) {
153 lyxerr << "No template file" << endl;
161 string temp = lex.GetString();
162 ExternalTemplate & tmp = templates[temp];
164 tmp.readTemplate(lex);
169 lyxerr << "TemplateEnd: " << lex.GetString() << endl;
170 lyxerr << "Warning: End outside Template." << endl;
177 void ExternalTemplate::readTemplate(LyXLex & lex)
179 enum TemplateOptionTags {
190 keyword_item templateoptiontags[] = {
191 { "automaticproduction", TO_AUTOMATIC },
192 { "editcommand", TO_EDITCMD },
193 { "filefilter", TO_FILTER },
194 { "format", TO_FORMAT },
195 { "guiname", TO_GUINAME },
196 { "helptext", TO_HELPTEXT },
197 { "templateend", TO_END },
198 { "viewcommand", TO_VIEWCMD }
201 pushpophelper pph(lex, templateoptiontags, TO_END);
207 guiName = lex.GetString();
211 helpText = lex.getLongString("HelpTextEnd");
216 fileRegExp = lex.GetString();
221 viewCommand = lex.GetString();
222 // For security reasons, a command may not be empty!
223 if (viewCommand.empty())
224 viewCommand = "true";
229 editCommand = lex.GetString();
230 // For security reasons, a command may not be empty!
231 if (editCommand.empty())
232 editCommand = "true";
237 automaticProduction = lex.GetBool();
242 formats[lex.GetString()].readFormat(lex);
249 lyxerr << "Default: " << lex.GetString() << endl;
257 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
267 keyword_item formattags[] = {
268 { "formatend", FO_END },
269 { "preamble", FO_PREAMBLE },
270 { "product", FO_PRODUCT },
271 { "requirement", FO_REQUIREMENT },
272 { "updatecommand", FO_UPDATECMD }
275 pushpophelper pph(lex, formattags, FO_END);
281 product = lex.GetString();
286 updateCommand = lex.GetString();
287 // For security reasons, a command may not be empty!
288 if (updateCommand.empty())
289 updateCommand = "true";
294 requirement = lex.GetString();
298 preamble = lex.getLongString("preambleend");
302 lyxerr << "FormatEnd: " << lex.GetString() << endl;