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"
23 #include "support/path_defines.h"
25 using namespace lyx::support;
31 // We have to have dummy default commands for security reasons!
33 ExternalTemplate::ExternalTemplate()
38 ExternalTemplate::FormatTemplate::FormatTemplate()
42 ExternalTemplateManager::ExternalTemplateManager()
44 // gimp gnuchess gnuplot ical netscape tetris xpaint
45 readTemplates(user_lyxdir());
46 if (lyxerr.debugging())
53 dumpTemplate(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 << "\tInputFormat " << et.inputFormat << "\n"
64 << "\tFileFilter " << et.fileRegExp << "\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\tUpdateFormat " << ft.updateFormat << "\n"
85 << "\t\tUpdateResult " << ft.updateResult << "\n"
86 << "\t\tRequirement " << ft.requirement << "\n"
89 << "\t\tPreambleEnd\n"
97 void ExternalTemplate::dumpFormats(ostream & os) const
99 for_each(formats.begin(), formats.end(), dumpFormat(os));
103 void ExternalTemplateManager::dumpTemplates() const
105 for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
109 ExternalTemplateManager & ExternalTemplateManager::get()
111 static ExternalTemplateManager externalTemplateManager;
112 return externalTemplateManager;
116 ExternalTemplateManager::Templates &
117 ExternalTemplateManager::getTemplates()
123 ExternalTemplateManager::Templates const &
124 ExternalTemplateManager::getTemplates() const
130 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
132 return templates[name];
136 void ExternalTemplateManager::readTemplates(string const & path)
145 keyword_item templatetags[] = {
146 { "template", TM_TEMPLATE },
147 { "templateend", TM_END }
150 string filename = LibFileSearch("", "external_templates");
151 if (filename.empty()) {
152 lyxerr << "ExternalTemplateManager::readTemplates: "
153 "No template file" << endl;
157 LyXLex lex(templatetags, TM_END);
158 if (!lex.setFile(filename)) {
159 lyxerr << "ExternalTemplateManager::readTemplates: "
160 "No template file" << endl;
168 string const temp = lex.getString();
169 ExternalTemplate & tmp = templates[temp];
171 tmp.readTemplate(lex);
176 lex.printError("Warning: End outside Template.");
183 void ExternalTemplate::readTemplate(LyXLex & lex)
185 enum TemplateOptionTags {
196 keyword_item templateoptiontags[] = {
197 { "automaticproduction", TO_AUTOMATIC },
198 { "editcommand", TO_EDITCMD },
199 { "filefilter", TO_FILTER },
200 { "format", TO_FORMAT },
201 { "guiname", TO_GUINAME },
202 { "helptext", TO_HELPTEXT },
203 { "inputformat", TO_INPUTFORMAT },
204 { "templateend", TO_END }
207 pushpophelper pph(lex, templateoptiontags, TO_END);
213 guiName = lex.getString();
217 helpText = lex.getLongString("HelpTextEnd");
222 inputFormat = lex.getString();
227 fileRegExp = lex.getString();
232 editCommand = lex.getString();
237 automaticProduction = lex.getBool();
242 formats[lex.getString()].readFormat(lex);
249 lex.printError("ExternalTemplate::readTemplate: "
250 "Wrong tag: $$Token");
258 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
269 keyword_item formattags[] = {
270 { "formatend", FO_END },
271 { "preamble", FO_PREAMBLE },
272 { "product", FO_PRODUCT },
273 { "requirement", FO_REQUIREMENT },
274 { "updateformat", FO_UPDATEFORMAT },
275 { "updateresult", FO_UPDATERESULT }
278 pushpophelper pph(lex, formattags, FO_END);
284 product = lex.getString();
287 case FO_UPDATEFORMAT:
289 updateFormat = lex.getString();
292 case FO_UPDATERESULT:
294 updateResult = lex.getString();
299 requirement = lex.getString();
303 preamble = lex.getLongString("preambleend");
307 if (lyxerr.debugging())
308 lex.printError("FormatEnd");