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
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
14 #include "ExternalTemplate.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/path.h"
22 #include "support/path_defines.h"
24 namespace support = lyx::support;
39 typedef Translator<TransformID, string> TransformIDTranslator;
40 TransformIDTranslator const & transformIDTranslator();
45 // We have to have dummy default commands for security reasons!
51 Template::Format::Format()
55 TemplateManager::TemplateManager()
57 readTemplates(support::user_lyxdir());
58 if (lyxerr.debugging(Debug::EXTERNAL)) {
59 dumpPreambleDefs(lyxerr);
61 dumpTemplates(lyxerr);
66 class dumpPreambleDef {
68 typedef TemplateManager::PreambleDefs::value_type value_type;
70 dumpPreambleDef(ostream & o) : ost(o) {}
72 void operator()(value_type const & vt) {
73 ost << "PreambleDef " << vt.first << '\n'
75 << "PreambleDefEnd" << endl;
85 typedef TemplateManager::Templates::value_type value_type;
87 dumpTemplate(ostream & o) : ost(o) {}
89 void operator()(value_type const & vt) {
90 Template const & et = vt.second;
92 ost << "Template " << et.lyxName << '\n'
93 << "\tGuiName " << et.guiName << '\n'
97 << "\tInputFormat " << et.inputFormat << '\n'
98 << "\tFileFilter " << et.fileRegExp << '\n'
99 << "\tAutomaticProduction " << et.automaticProduction << '\n';
101 typedef vector<TransformID> IDs;
102 IDs::const_iterator it = et.transformIds.begin();
103 IDs::const_iterator end = et.transformIds.end();
104 for (; it != end; ++it) {
105 ost << "\tTransform "
106 << transformIDTranslator().find(*it) << '\n';
110 ost << "TemplateEnd" << endl;
120 typedef Template::Formats::value_type value_type;
122 dumpFormat(ostream & o) : ost(o) {}
124 void operator()(value_type const & vt) const {
125 Template::Format const & ft = vt.second;
126 ost << "\tFormat " << vt.first << '\n'
127 << "\t\tProduct " << ft.product << '\n'
128 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
129 << "\t\tUpdateResult " << ft.updateResult << '\n'
130 << "\t\tRequirement " << ft.requirement << '\n';
132 typedef vector<Template::Option> Options;
133 Options::const_iterator oit = ft.options.begin();
134 Options::const_iterator oend = ft.options.end();
135 for (; oit != oend; ++oit) {
143 vector<string>::const_iterator pit = ft.preambleNames.begin();
144 vector<string>::const_iterator pend = ft.preambleNames.end();
145 for (; pit != pend; ++pit) {
146 ost << "\t\tPreamble " << *pit << '\n';
149 typedef Template::Format::FileMap FileMap;
150 FileMap::const_iterator rit = ft.referencedFiles.begin();
151 FileMap::const_iterator rend = ft.referencedFiles.end();
152 for (; rit != rend; ++rit) {
153 vector<string>::const_iterator fit = rit->second.begin();
154 vector<string>::const_iterator fend = rit->second.end();
155 for (; fit != fend; ++fit) {
156 ost << "\t\tReferencedFile " << rit->first
157 << " \"" << *fit << "\"\n";
161 ost << "\tFormatEnd\n";
168 void Template::dumpFormats(ostream & os) const
170 for_each(formats.begin(), formats.end(), dumpFormat(os));
174 void TemplateManager::dumpPreambleDefs(ostream & os) const
176 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
180 void TemplateManager::dumpTemplates(ostream & os) const
182 for_each(templates.begin(), templates.end(), dumpTemplate(os));
186 TemplateManager & TemplateManager::get()
188 static TemplateManager externalTemplateManager;
189 return externalTemplateManager;
193 TemplateManager::Templates const &
194 TemplateManager::getTemplates() const
201 TemplateManager::getTemplateByName(string const & name) const
203 Templates::const_iterator it = templates.find(name);
204 return (it == templates.end()) ? 0 : &it->second;
209 TemplateManager::getPreambleDefByName(string const & name) const
211 string const trimmed_name = support::trim(name);
212 if (trimmed_name.empty())
215 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
216 if (it == preambledefs.end())
223 void TemplateManager::readTemplates(string const & path)
225 support::Path p(path);
234 keyword_item templatetags[] = {
235 { "preambledef", TM_PREAMBLEDEF },
236 { "preambledefend", TM_PREAMBLEDEF_END },
237 { "template", TM_TEMPLATE },
238 { "templateend", TM_TEMPLATE_END }
241 LyXLex lex(templatetags, TM_TEMPLATE_END);
243 string filename = support::LibFileSearch("", "external_templates");
244 if (filename.empty() || !lex.setFile(filename)) {
245 lex.printError("external::TemplateManager::readTemplates: "
250 char const * const preamble_end_tag =
251 templatetags[TM_PREAMBLEDEF_END-1].tag;
255 case TM_PREAMBLEDEF: {
257 string const name = lex.getString();
258 preambledefs[name] = lex.getLongString(preamble_end_tag);
264 string const name = lex.getString();
265 Template & tmp = templates[name];
267 tmp.readTemplate(lex);
271 case TM_TEMPLATE_END:
272 lex.printError("Warning: End outside Template.");
275 case TM_PREAMBLEDEF_END:
276 lex.printError("Warning: End outside PreambleDef.");
285 void add(vector<TransformID> & ids, string const & name)
287 TransformID id = transformIDTranslator().find(name);
289 lyxerr << "external::Template::readTemplate\n"
290 << "Transform " << name << " is not recognized"
300 void Template::readTemplate(LyXLex & lex)
302 enum TemplateOptionTags {
313 keyword_item templateoptiontags[] = {
314 { "automaticproduction", TO_AUTOMATIC },
315 { "filefilter", TO_FILTER },
316 { "format", TO_FORMAT },
317 { "guiname", TO_GUINAME },
318 { "helptext", TO_HELPTEXT },
319 { "inputformat", TO_INPUTFORMAT },
320 { "templateend", TO_END },
321 { "transform", TO_TRANSFORM }
324 pushpophelper pph(lex, templateoptiontags, TO_END);
330 guiName = lex.getString();
334 helpText = lex.getLongString("HelpTextEnd");
339 inputFormat = lex.getString();
344 fileRegExp = lex.getString();
349 automaticProduction = lex.getBool();
354 add(transformIds, lex.getString());
359 formats[lex.getString()].readFormat(lex);
366 lex.printError("external::Template::readTemplate: "
367 "Wrong tag: $$Token");
377 void transform_not_found(std::ostream & os, string const & transform)
379 os << "external::Format::readFormat. Transformation \""
380 << transform << "\" is unrecognized." << std::endl;
384 void transform_class_not_found(std::ostream & os, string const & tclass)
386 os << "external::Format::readFormat. Transformation class \""
387 << tclass << "\" is unrecognized." << std::endl;
391 void setCommandFactory(Template::Format & format, string const & transform,
392 string const & transformer_class)
394 bool class_found = false;
395 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
397 ResizeCommandFactory factory = ResizeLatexCommand::factory;
398 format.command_transformers[Resize] =
399 TransformStore(Resize, factory);
401 } else if (transform == "Rotate" &&
402 transformer_class == "RotationLatexCommand") {
404 RotationCommandFactory factory = RotationLatexCommand::factory;
405 format.command_transformers[Rotate] =
406 TransformStore(Rotate, factory);
409 transform_not_found(lyxerr, transform);
412 transform_class_not_found(lyxerr, transformer_class);
416 void setOptionFactory(Template::Format & format, string const & transform,
417 string const & transformer_class)
419 bool class_found = false;
420 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
422 ClipOptionFactory factory = ClipLatexOption::factory;
423 format.option_transformers[Clip] =
424 TransformStore(Clip, factory);
426 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
428 ExtraOptionFactory factory = ExtraOption::factory;
429 format.option_transformers[Extra] =
430 TransformStore(Extra, factory);
432 } else if (transform == "Resize" &&
433 transformer_class == "ResizeLatexOption") {
435 ResizeOptionFactory factory = ResizeLatexOption::factory;
436 format.option_transformers[Resize] =
437 TransformStore(Resize, factory);
439 } else if (transform == "Rotate" &&
440 transformer_class == "RotationLatexOption") {
442 RotationOptionFactory factory = RotationLatexOption::factory;
443 format.option_transformers[Rotate] =
444 TransformStore(Rotate, factory);
447 transform_not_found(lyxerr, transform);
450 transform_class_not_found(lyxerr, transformer_class);
456 void Template::Format::readFormat(LyXLex & lex)
471 keyword_item formattags[] = {
472 { "formatend", FO_END },
473 { "option", FO_OPTION },
474 { "preamble", FO_PREAMBLE },
475 { "product", FO_PRODUCT },
476 { "referencedfile", FO_REFERENCEDFILE },
477 { "requirement", FO_REQUIREMENT },
478 { "transformcommand", FO_TRANSFORMCOMMAND },
479 { "transformoption", FO_TRANSFORMOPTION },
480 { "updateformat", FO_UPDATEFORMAT },
481 { "updateresult", FO_UPDATERESULT }
484 pushpophelper pph(lex, formattags, FO_END);
490 product = lex.getString();
493 case FO_UPDATEFORMAT:
495 updateFormat = lex.getString();
498 case FO_UPDATERESULT:
500 updateResult = lex.getString();
505 requirement = lex.getString();
510 preambleNames.push_back(lex.getString());
513 case FO_TRANSFORMCOMMAND: {
515 string const name = lex.getString();
517 setCommandFactory(*this, name, lex.getString());
521 case FO_TRANSFORMOPTION: {
523 string const name = lex.getString();
525 setOptionFactory(*this, name, lex.getString());
531 string const name = lex.getString();
533 string const opt = lex.getString();
534 options.push_back(Option(name, opt));
538 case FO_REFERENCEDFILE: {
540 string const format = lex.getString();
542 string const file = lex.getString();
543 referencedFiles[format].push_back(file);
555 TransformIDTranslator const initIDTranslator()
557 TransformIDTranslator translator(TransformID(-1), "");
558 translator.addPair(Rotate, "Rotate");
559 translator.addPair(Resize, "Resize");
560 translator.addPair(Clip, "Clip");
561 translator.addPair(Extra, "Extra");
566 TransformIDTranslator const & transformIDTranslator()
568 static TransformIDTranslator const translator = initIDTranslator();
574 } // namespace external