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.
13 #include "ExternalTemplate.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20 #include "support/path.h"
21 #include "support/path_defines.h"
23 #include <boost/assert.hpp>
27 namespace support = lyx::support;
39 // We have to have dummy default commands for security reasons!
45 Template::Format::Format()
49 TemplateManager::TemplateManager()
51 readTemplates(support::user_lyxdir());
52 if (lyxerr.debugging(Debug::EXTERNAL)) {
53 dumpPreambleDefs(lyxerr);
55 dumpTemplates(lyxerr);
60 class dumpPreambleDef {
62 typedef TemplateManager::PreambleDefs::value_type value_type;
64 dumpPreambleDef(ostream & o) : ost(o) {}
66 void operator()(value_type const & vt) {
67 ost << "PreambleDef " << vt.first << '\n'
69 << "PreambleDefEnd" << endl;
79 typedef TemplateManager::Templates::value_type value_type;
81 dumpTemplate(ostream & o) : ost(o) {}
83 void operator()(value_type const & vt) {
84 Template const & et = vt.second;
86 ost << "Template " << et.lyxName << '\n'
87 << "\tGuiName " << et.guiName << '\n'
91 << "\tInputFormat " << et.inputFormat << '\n'
92 << "\tFileFilter " << et.fileRegExp << '\n'
93 << "\tEditCommand " << et.editCommand << '\n'
94 << "\tAutomaticProduction " << et.automaticProduction << '\n';
96 ost << "TemplateEnd" << endl;
106 typedef Template::Formats::value_type value_type;
108 dumpFormat(ostream & o) : ost(o) {}
110 void operator()(value_type const & vt) const{
111 Template::Format const & ft = vt.second;
112 ost << "\tFormat " << vt.first << '\n'
113 << "\t\tProduct " << ft.product << '\n'
114 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
115 << "\t\tUpdateResult " << ft.updateResult << '\n'
116 << "\t\tRequirement " << ft.requirement << '\n';
118 vector<string>::const_iterator it = ft.preambleNames.begin();
119 vector<string>::const_iterator end = ft.preambleNames.end();
120 for (; it != end; ++it) {
121 ost << "\t\tPreamble " << *it << '\n';
124 ost << "\tFormatEnd\n";
131 void Template::dumpFormats(ostream & os) const
133 for_each(formats.begin(), formats.end(), dumpFormat(os));
137 void TemplateManager::dumpPreambleDefs(ostream & os) const
139 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
143 void TemplateManager::dumpTemplates(ostream & os) const
145 for_each(templates.begin(), templates.end(), dumpTemplate(os));
149 TemplateManager & TemplateManager::get()
151 static TemplateManager externalTemplateManager;
152 return externalTemplateManager;
156 TemplateManager::Templates &
157 TemplateManager::getTemplates()
163 TemplateManager::Templates const &
164 TemplateManager::getTemplates() const
171 TemplateManager::getTemplateByName(string const & name) const
173 Templates::const_iterator it = templates.find(name);
174 return (it == templates.end()) ? 0 : &it->second;
179 TemplateManager::getPreambleDefByName(string const & name) const
181 string const trimmed_name = support::trim(name);
182 if (trimmed_name.empty())
185 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
186 if (it == preambledefs.end())
193 void TemplateManager::readTemplates(string const & path)
195 support::Path p(path);
204 keyword_item templatetags[] = {
205 { "preambledef", TM_PREAMBLEDEF },
206 { "preambledefend", TM_PREAMBLEDEF_END },
207 { "template", TM_TEMPLATE },
208 { "templateend", TM_TEMPLATE_END }
211 LyXLex lex(templatetags, TM_TEMPLATE_END);
213 string filename = support::LibFileSearch("", "external_templates");
214 if (filename.empty() || !lex.setFile(filename)) {
215 lex.printError("external::TemplateManager::readTemplates: "
220 char const * const preamble_end_tag =
221 templatetags[TM_PREAMBLEDEF_END-1].tag;
225 case TM_PREAMBLEDEF: {
227 string const name = lex.getString();
228 preambledefs[name] = lex.getLongString(preamble_end_tag);
234 string const name = lex.getString();
235 Template & tmp = templates[name];
237 tmp.readTemplate(lex);
241 case TM_TEMPLATE_END:
242 lex.printError("Warning: End outside Template.");
245 case TM_PREAMBLEDEF_END:
246 lex.printError("Warning: End outside PreambleDef.");
253 void Template::readTemplate(LyXLex & lex)
255 enum TemplateOptionTags {
266 keyword_item templateoptiontags[] = {
267 { "automaticproduction", TO_AUTOMATIC },
268 { "editcommand", TO_EDITCMD },
269 { "filefilter", TO_FILTER },
270 { "format", TO_FORMAT },
271 { "guiname", TO_GUINAME },
272 { "helptext", TO_HELPTEXT },
273 { "inputformat", TO_INPUTFORMAT },
274 { "templateend", TO_END }
277 pushpophelper pph(lex, templateoptiontags, TO_END);
283 guiName = lex.getString();
287 helpText = lex.getLongString("HelpTextEnd");
292 inputFormat = lex.getString();
297 fileRegExp = lex.getString();
302 editCommand = lex.getString();
307 automaticProduction = lex.getBool();
312 formats[lex.getString()].readFormat(lex);
319 lex.printError("external::Template::readTemplate: "
320 "Wrong tag: $$Token");
328 void Template::Format::readFormat(LyXLex & lex)
339 keyword_item formattags[] = {
340 { "formatend", FO_END },
341 { "preamble", FO_PREAMBLE },
342 { "product", FO_PRODUCT },
343 { "requirement", FO_REQUIREMENT },
344 { "updateformat", FO_UPDATEFORMAT },
345 { "updateresult", FO_UPDATERESULT }
348 pushpophelper pph(lex, formattags, FO_END);
354 product = lex.getString();
357 case FO_UPDATEFORMAT:
359 updateFormat = lex.getString();
362 case FO_UPDATERESULT:
364 updateResult = lex.getString();
369 requirement = lex.getString();
374 preambleNames.push_back(lex.getString());
383 } // namespace external