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;
40 // We have to have dummy default commands for security reasons!
46 Template::Format::Format()
50 TemplateManager::TemplateManager()
52 readTemplates(support::user_lyxdir());
53 if (lyxerr.debugging(Debug::EXTERNAL)) {
54 dumpPreambleDefs(lyxerr);
56 dumpTemplates(lyxerr);
61 class dumpPreambleDef {
63 typedef TemplateManager::PreambleDefs::value_type value_type;
65 dumpPreambleDef(ostream & o) : ost(o) {}
67 void operator()(value_type const & vt) {
68 ost << "PreambleDef " << vt.first << '\n'
70 << "PreambleDefEnd" << endl;
80 typedef TemplateManager::Templates::value_type value_type;
82 dumpTemplate(ostream & o) : ost(o) {}
84 void operator()(value_type const & vt) {
85 Template const & et = vt.second;
87 ost << "Template " << et.lyxName << '\n'
88 << "\tGuiName " << et.guiName << '\n'
92 << "\tInputFormat " << et.inputFormat << '\n'
93 << "\tFileFilter " << et.fileRegExp << '\n'
94 << "\tEditCommand " << et.editCommand << '\n'
95 << "\tAutomaticProduction " << et.automaticProduction << '\n';
97 ost << "TemplateEnd" << endl;
107 typedef Template::Formats::value_type value_type;
109 dumpFormat(ostream & o) : ost(o) {}
111 void operator()(value_type const & vt) const{
112 Template::Format const & ft = vt.second;
113 ost << "\tFormat " << vt.first << '\n'
114 << "\t\tProduct " << ft.product << '\n'
115 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
116 << "\t\tUpdateResult " << ft.updateResult << '\n'
117 << "\t\tRequirement " << ft.requirement << '\n';
119 vector<string>::const_iterator it = ft.preambleNames.begin();
120 vector<string>::const_iterator end = ft.preambleNames.end();
121 for (; it != end; ++it) {
122 ost << "\t\tPreamble " << *it << '\n';
125 ost << "\tFormatEnd\n";
132 void Template::dumpFormats(ostream & os) const
134 for_each(formats.begin(), formats.end(), dumpFormat(os));
138 void TemplateManager::dumpPreambleDefs(ostream & os) const
140 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
144 void TemplateManager::dumpTemplates(ostream & os) const
146 for_each(templates.begin(), templates.end(), dumpTemplate(os));
150 TemplateManager & TemplateManager::get()
152 static TemplateManager externalTemplateManager;
153 return externalTemplateManager;
157 TemplateManager::Templates &
158 TemplateManager::getTemplates()
164 TemplateManager::Templates const &
165 TemplateManager::getTemplates() const
172 TemplateManager::getTemplateByName(string const & name) const
174 Templates::const_iterator it = templates.find(name);
175 return (it == templates.end()) ? 0 : &it->second;
180 TemplateManager::getPreambleDefByName(string const & name) const
182 string const trimmed_name = support::trim(name);
183 if (trimmed_name.empty())
186 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
187 if (it == preambledefs.end())
194 void TemplateManager::readTemplates(string const & path)
196 support::Path p(path);
205 keyword_item templatetags[] = {
206 { "preambledef", TM_PREAMBLEDEF },
207 { "preambledefend", TM_PREAMBLEDEF_END },
208 { "template", TM_TEMPLATE },
209 { "templateend", TM_TEMPLATE_END }
212 LyXLex lex(templatetags, TM_TEMPLATE_END);
214 string filename = support::LibFileSearch("", "external_templates");
215 if (filename.empty() || !lex.setFile(filename)) {
216 lex.printError("external::TemplateManager::readTemplates: "
221 char const * const preamble_end_tag =
222 templatetags[TM_PREAMBLEDEF_END-1].tag;
226 case TM_PREAMBLEDEF: {
228 string const name = lex.getString();
229 preambledefs[name] = lex.getLongString(preamble_end_tag);
235 string const name = lex.getString();
236 Template & tmp = templates[name];
238 tmp.readTemplate(lex);
242 case TM_TEMPLATE_END:
243 lex.printError("Warning: End outside Template.");
246 case TM_PREAMBLEDEF_END:
247 lex.printError("Warning: End outside PreambleDef.");
254 void Template::readTemplate(LyXLex & lex)
256 enum TemplateOptionTags {
267 keyword_item templateoptiontags[] = {
268 { "automaticproduction", TO_AUTOMATIC },
269 { "editcommand", TO_EDITCMD },
270 { "filefilter", TO_FILTER },
271 { "format", TO_FORMAT },
272 { "guiname", TO_GUINAME },
273 { "helptext", TO_HELPTEXT },
274 { "inputformat", TO_INPUTFORMAT },
275 { "templateend", TO_END }
278 pushpophelper pph(lex, templateoptiontags, TO_END);
284 guiName = lex.getString();
288 helpText = lex.getLongString("HelpTextEnd");
293 inputFormat = lex.getString();
298 fileRegExp = lex.getString();
303 editCommand = lex.getString();
308 automaticProduction = lex.getBool();
313 formats[lex.getString()].readFormat(lex);
320 lex.printError("external::Template::readTemplate: "
321 "Wrong tag: $$Token");
329 void Template::Format::readFormat(LyXLex & lex)
340 keyword_item formattags[] = {
341 { "formatend", FO_END },
342 { "preamble", FO_PREAMBLE },
343 { "product", FO_PRODUCT },
344 { "requirement", FO_REQUIREMENT },
345 { "updateformat", FO_UPDATEFORMAT },
346 { "updateresult", FO_UPDATERESULT }
349 pushpophelper pph(lex, formattags, FO_END);
355 product = lex.getString();
358 case FO_UPDATEFORMAT:
360 updateFormat = lex.getString();
363 case FO_UPDATERESULT:
365 updateResult = lex.getString();
370 requirement = lex.getString();
375 preambleNames.push_back(lex.getString());
384 } // namespace external