2 * \file ExternalTemplate.cpp
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
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "ExternalTemplate.h"
16 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/Package.h"
22 #include "support/Path.h"
36 typedef Translator<TransformID, string> TransformIDTranslator;
38 static TransformIDTranslator const initIDTranslator()
40 TransformIDTranslator translator(TransformID(-1), "");
41 translator.addPair(Rotate, "Rotate");
42 translator.addPair(Resize, "Resize");
43 translator.addPair(Clip, "Clip");
44 translator.addPair(Extra, "Extra");
48 static TransformIDTranslator const & transformIDTranslator()
50 static TransformIDTranslator const translator = initIDTranslator();
54 // We have to have dummy default commands for security reasons!
60 Template::Format::Format()
64 TemplateManager::TemplateManager()
66 readTemplates(support::package().user_support());
67 if (lyxerr.debugging(Debug::EXTERNAL)) {
68 dumpPreambleDefs(lyxerr);
70 dumpTemplates(lyxerr);
75 class DumpPreambleDef {
77 typedef TemplateManager::PreambleDefs::value_type value_type;
79 DumpPreambleDef(ostream & o) : ost(o) {}
81 void operator()(value_type const & vt) {
82 ost << "PreambleDef " << vt.first << '\n'
84 << "PreambleDefEnd" << endl;
94 typedef TemplateManager::Templates::value_type value_type;
96 DumpTemplate(ostream & o) : ost(o) {}
98 void operator()(value_type const & vt) {
99 Template const & et = vt.second;
101 ost << "Template " << et.lyxName << '\n'
102 << "\tGuiName " << et.guiName << '\n'
106 << "\tInputFormat " << et.inputFormat << '\n'
107 << "\tFileFilter " << et.fileRegExp << '\n'
108 << "\tAutomaticProduction " << et.automaticProduction << '\n';
110 typedef vector<TransformID> IDs;
111 IDs::const_iterator it = et.transformIds.begin();
112 IDs::const_iterator end = et.transformIds.end();
113 for (; it != end; ++it) {
114 ost << "\tTransform "
115 << transformIDTranslator().find(*it) << '\n';
119 ost << "TemplateEnd" << endl;
129 typedef Template::Formats::value_type value_type;
131 DumpFormat(ostream & o) : ost(o) {}
133 void operator()(value_type const & vt) const {
134 Template::Format const & ft = vt.second;
135 ost << "\tFormat " << vt.first << '\n'
136 << "\t\tProduct " << ft.product << '\n'
137 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
138 << "\t\tUpdateResult " << ft.updateResult << '\n';
140 vector<string>::const_iterator qit = ft.requirements.begin();
141 vector<string>::const_iterator qend = ft.requirements.end();
142 for (; qit != qend; ++qit) {
143 lyxerr << "req:" << *qit << std::endl;
144 ost << "\t\tRequirement " << *qit << '\n';
147 typedef vector<Template::Option> Options;
148 Options::const_iterator oit = ft.options.begin();
149 Options::const_iterator oend = ft.options.end();
150 for (; oit != oend; ++oit) {
158 vector<string>::const_iterator pit = ft.preambleNames.begin();
159 vector<string>::const_iterator pend = ft.preambleNames.end();
160 for (; pit != pend; ++pit) {
161 ost << "\t\tPreamble " << *pit << '\n';
164 typedef Template::Format::FileMap FileMap;
165 FileMap::const_iterator rit = ft.referencedFiles.begin();
166 FileMap::const_iterator rend = ft.referencedFiles.end();
167 for (; rit != rend; ++rit) {
168 vector<string>::const_iterator fit = rit->second.begin();
169 vector<string>::const_iterator fend = rit->second.end();
170 for (; fit != fend; ++fit) {
171 ost << "\t\tReferencedFile " << rit->first
172 << " \"" << *fit << "\"\n";
176 ost << "\tFormatEnd\n";
183 void Template::dumpFormats(ostream & os) const
185 for_each(formats.begin(), formats.end(), DumpFormat(os));
189 void TemplateManager::dumpPreambleDefs(ostream & os) const
191 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
195 void TemplateManager::dumpTemplates(ostream & os) const
197 for_each(templates.begin(), templates.end(), DumpTemplate(os));
201 TemplateManager & TemplateManager::get()
203 static TemplateManager externalTemplateManager;
204 return externalTemplateManager;
208 TemplateManager::Templates const &
209 TemplateManager::getTemplates() const
216 TemplateManager::getTemplateByName(string const & name) const
218 Templates::const_iterator it = templates.find(name);
219 return (it == templates.end()) ? 0 : &it->second;
224 TemplateManager::getPreambleDefByName(string const & name) const
226 string const trimmed_name = support::trim(name);
227 if (trimmed_name.empty())
230 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
231 if (it == preambledefs.end())
238 void TemplateManager::readTemplates(support::FileName const & path)
240 support::PathChanger p(path);
249 keyword_item templatetags[] = {
250 { "preambledef", TM_PREAMBLEDEF },
251 { "preambledefend", TM_PREAMBLEDEF_END },
252 { "template", TM_TEMPLATE },
253 { "templateend", TM_TEMPLATE_END }
256 Lexer lex(templatetags, TM_TEMPLATE_END);
258 support::FileName const filename = support::libFileSearch("", "external_templates");
259 if (filename.empty() || !lex.setFile(filename)) {
260 lex.printError("external::TemplateManager::readTemplates: "
265 char const * const preamble_end_tag =
266 templatetags[TM_PREAMBLEDEF_END-1].tag;
270 case TM_PREAMBLEDEF: {
272 string const name = lex.getString();
273 preambledefs[name] = lex.getLongString(preamble_end_tag);
279 string const name = lex.getString();
280 Template & tmp = templates[name];
282 tmp.readTemplate(lex);
286 case TM_TEMPLATE_END:
287 lex.printError("Warning: End outside Template.");
290 case TM_PREAMBLEDEF_END:
291 lex.printError("Warning: End outside PreambleDef.");
300 void add(vector<TransformID> & ids, string const & name)
302 TransformID id = transformIDTranslator().find(name);
304 lyxerr << "external::Template::readTemplate\n"
305 << "Transform " << name << " is not recognized"
315 void Template::readTemplate(Lexer & lex)
317 enum TemplateOptionTags {
328 keyword_item templateoptiontags[] = {
329 { "automaticproduction", TO_AUTOMATIC },
330 { "filefilter", TO_FILTER },
331 { "format", TO_FORMAT },
332 { "guiname", TO_GUINAME },
333 { "helptext", TO_HELPTEXT },
334 { "inputformat", TO_INPUTFORMAT },
335 { "templateend", TO_END },
336 { "transform", TO_TRANSFORM }
339 PushPopHelper pph(lex, templateoptiontags, TO_END);
345 guiName = lex.getString();
349 helpText = lex.getLongString("HelpTextEnd");
354 inputFormat = lex.getString();
359 fileRegExp = lex.getString();
364 automaticProduction = lex.getBool();
369 add(transformIds, lex.getString());
374 formats[lex.getString()].readFormat(lex);
381 lex.printError("external::Template::readTemplate: "
382 "Wrong tag: $$Token");
392 void transform_not_found(std::ostream & os, string const & transform)
394 os << "external::Format::readFormat. Transformation \""
395 << transform << "\" is unrecognized." << std::endl;
399 void transform_class_not_found(std::ostream & os, string const & tclass)
401 os << "external::Format::readFormat. Transformation class \""
402 << tclass << "\" is unrecognized." << std::endl;
406 void setCommandFactory(Template::Format & format, string const & transform,
407 string const & transformer_class)
409 bool class_found = false;
410 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
412 ResizeCommandFactory factory = ResizeLatexCommand::factory;
413 format.command_transformers[Resize] =
414 TransformStore(Resize, factory);
416 } else if (transform == "Rotate" &&
417 transformer_class == "RotationLatexCommand") {
419 RotationCommandFactory factory = RotationLatexCommand::factory;
420 format.command_transformers[Rotate] =
421 TransformStore(Rotate, factory);
424 transform_not_found(lyxerr, transform);
427 transform_class_not_found(lyxerr, transformer_class);
431 void setOptionFactory(Template::Format & format, string const & transform,
432 string const & transformer_class)
434 bool class_found = false;
435 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
437 ClipOptionFactory factory = ClipLatexOption::factory;
438 format.option_transformers[Clip] =
439 TransformStore(Clip, factory);
441 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
443 ExtraOptionFactory factory = ExtraOption::factory;
444 format.option_transformers[Extra] =
445 TransformStore(Extra, factory);
447 } else if (transform == "Resize" &&
448 transformer_class == "ResizeLatexOption") {
450 ResizeOptionFactory factory = ResizeLatexOption::factory;
451 format.option_transformers[Resize] =
452 TransformStore(Resize, factory);
454 } else if (transform == "Rotate" &&
455 transformer_class == "RotationLatexOption") {
457 RotationOptionFactory factory = RotationLatexOption::factory;
458 format.option_transformers[Rotate] =
459 TransformStore(Rotate, factory);
462 transform_not_found(lyxerr, transform);
465 transform_class_not_found(lyxerr, transformer_class);
471 void Template::Format::readFormat(Lexer & lex)
486 keyword_item formattags[] = {
487 { "formatend", FO_END },
488 { "option", FO_OPTION },
489 { "preamble", FO_PREAMBLE },
490 { "product", FO_PRODUCT },
491 { "referencedfile", FO_REFERENCEDFILE },
492 { "requirement", FO_REQUIREMENT },
493 { "transformcommand", FO_TRANSFORMCOMMAND },
494 { "transformoption", FO_TRANSFORMOPTION },
495 { "updateformat", FO_UPDATEFORMAT },
496 { "updateresult", FO_UPDATERESULT }
499 PushPopHelper pph(lex, formattags, FO_END);
505 product = lex.getString();
508 case FO_UPDATEFORMAT:
510 updateFormat = lex.getString();
513 case FO_UPDATERESULT:
515 updateResult = lex.getString();
520 requirements.push_back(lex.getString());
525 preambleNames.push_back(lex.getString());
528 case FO_TRANSFORMCOMMAND: {
530 string const name = lex.getString();
532 setCommandFactory(*this, name, lex.getString());
536 case FO_TRANSFORMOPTION: {
538 string const name = lex.getString();
540 setOptionFactory(*this, name, lex.getString());
546 string const name = lex.getString();
548 string const opt = lex.getString();
549 options.push_back(Option(name, opt));
553 case FO_REFERENCEDFILE: {
555 string const format = lex.getString();
557 string const file = lex.getString();
558 referencedFiles[format].push_back(file);
568 } // namespace external