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"
23 #include "support/translator.h"
25 #include <boost/assert.hpp>
29 namespace support = lyx::support;
44 typedef Translator<TransformID, string> TransformIDTranslator;
45 TransformIDTranslator const & transformIDTranslator();
50 // We have to have dummy default commands for security reasons!
56 Template::Format::Format()
60 TemplateManager::TemplateManager()
62 readTemplates(support::user_lyxdir());
63 if (lyxerr.debugging(Debug::EXTERNAL)) {
64 dumpPreambleDefs(lyxerr);
66 dumpTemplates(lyxerr);
71 class dumpPreambleDef {
73 typedef TemplateManager::PreambleDefs::value_type value_type;
75 dumpPreambleDef(ostream & o) : ost(o) {}
77 void operator()(value_type const & vt) {
78 ost << "PreambleDef " << vt.first << '\n'
80 << "PreambleDefEnd" << endl;
90 typedef TemplateManager::Templates::value_type value_type;
92 dumpTemplate(ostream & o) : ost(o) {}
94 void operator()(value_type const & vt) {
95 Template const & et = vt.second;
97 ost << "Template " << et.lyxName << '\n'
98 << "\tGuiName " << et.guiName << '\n'
102 << "\tInputFormat " << et.inputFormat << '\n'
103 << "\tFileFilter " << et.fileRegExp << '\n'
104 << "\tAutomaticProduction " << et.automaticProduction << '\n';
106 typedef vector<TransformID> IDs;
107 IDs::const_iterator it = et.transformIds.begin();
108 IDs::const_iterator end = et.transformIds.end();
109 for (; it != end; ++it) {
110 ost << "\tTransform "
111 << transformIDTranslator().find(*it) << '\n';
115 ost << "TemplateEnd" << endl;
125 typedef Template::Formats::value_type value_type;
127 dumpFormat(ostream & o) : ost(o) {}
129 void operator()(value_type const & vt) const {
130 Template::Format const & ft = vt.second;
131 ost << "\tFormat " << vt.first << '\n'
132 << "\t\tProduct " << ft.product << '\n'
133 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
134 << "\t\tUpdateResult " << ft.updateResult << '\n'
135 << "\t\tRequirement " << ft.requirement << '\n';
137 typedef vector<Template::Option> Options;
138 Options::const_iterator oit = ft.options.begin();
139 Options::const_iterator oend = ft.options.end();
140 for (; oit != oend; ++oit) {
148 vector<string>::const_iterator pit = ft.preambleNames.begin();
149 vector<string>::const_iterator pend = ft.preambleNames.end();
150 for (; pit != pend; ++pit) {
151 ost << "\t\tPreamble " << *pit << '\n';
154 ost << "\tFormatEnd\n";
161 void Template::dumpFormats(ostream & os) const
163 for_each(formats.begin(), formats.end(), dumpFormat(os));
167 void TemplateManager::dumpPreambleDefs(ostream & os) const
169 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
173 void TemplateManager::dumpTemplates(ostream & os) const
175 for_each(templates.begin(), templates.end(), dumpTemplate(os));
179 TemplateManager & TemplateManager::get()
181 static TemplateManager externalTemplateManager;
182 return externalTemplateManager;
186 TemplateManager::Templates const &
187 TemplateManager::getTemplates() const
194 TemplateManager::getTemplateByName(string const & name) const
196 Templates::const_iterator it = templates.find(name);
197 return (it == templates.end()) ? 0 : &it->second;
202 TemplateManager::getPreambleDefByName(string const & name) const
204 string const trimmed_name = support::trim(name);
205 if (trimmed_name.empty())
208 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
209 if (it == preambledefs.end())
216 void TemplateManager::readTemplates(string const & path)
218 support::Path p(path);
227 keyword_item templatetags[] = {
228 { "preambledef", TM_PREAMBLEDEF },
229 { "preambledefend", TM_PREAMBLEDEF_END },
230 { "template", TM_TEMPLATE },
231 { "templateend", TM_TEMPLATE_END }
234 LyXLex lex(templatetags, TM_TEMPLATE_END);
236 string filename = support::LibFileSearch("", "external_templates");
237 if (filename.empty() || !lex.setFile(filename)) {
238 lex.printError("external::TemplateManager::readTemplates: "
243 char const * const preamble_end_tag =
244 templatetags[TM_PREAMBLEDEF_END-1].tag;
248 case TM_PREAMBLEDEF: {
250 string const name = lex.getString();
251 preambledefs[name] = lex.getLongString(preamble_end_tag);
257 string const name = lex.getString();
258 Template & tmp = templates[name];
260 tmp.readTemplate(lex);
264 case TM_TEMPLATE_END:
265 lex.printError("Warning: End outside Template.");
268 case TM_PREAMBLEDEF_END:
269 lex.printError("Warning: End outside PreambleDef.");
278 void add(vector<TransformID> & ids, string const & name)
280 TransformID id = transformIDTranslator().find(name);
282 lyxerr << "external::Template::readTemplate\n"
283 << "Transform " << name << " is not recognized"
293 void Template::readTemplate(LyXLex & lex)
295 enum TemplateOptionTags {
306 keyword_item templateoptiontags[] = {
307 { "automaticproduction", TO_AUTOMATIC },
308 { "filefilter", TO_FILTER },
309 { "format", TO_FORMAT },
310 { "guiname", TO_GUINAME },
311 { "helptext", TO_HELPTEXT },
312 { "inputformat", TO_INPUTFORMAT },
313 { "templateend", TO_END },
314 { "transform", TO_TRANSFORM }
317 pushpophelper pph(lex, templateoptiontags, TO_END);
323 guiName = lex.getString();
327 helpText = lex.getLongString("HelpTextEnd");
332 inputFormat = lex.getString();
337 fileRegExp = lex.getString();
342 automaticProduction = lex.getBool();
347 add(transformIds, lex.getString());
352 formats[lex.getString()].readFormat(lex);
359 lex.printError("external::Template::readTemplate: "
360 "Wrong tag: $$Token");
370 void transform_not_found(std::ostream & os, string const & transform)
372 os << "external::Format::readFormat. Transformation \""
373 << transform << "\" is unrecognized." << std::endl;
377 void transform_class_not_found(std::ostream & os, string const & tclass)
379 os << "external::Format::readFormat. Transformation class \""
380 << tclass << "\" is unrecognized." << std::endl;
384 void setCommandFactory(Template::Format & format, string const & transform,
385 string const & transformer_class)
387 bool class_found = false;
388 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
390 ResizeCommandFactory factory = ResizeLatexCommand::factory;
391 format.command_transformers[Resize] =
392 TransformStore(Resize, factory);
394 } else if (transform == "Rotate" &&
395 transformer_class == "RotationLatexCommand") {
397 RotationCommandFactory factory = RotationLatexCommand::factory;
398 format.command_transformers[Rotate] =
399 TransformStore(Rotate, factory);
402 transform_not_found(lyxerr, transform);
405 transform_class_not_found(lyxerr, transformer_class);
409 void setOptionFactory(Template::Format & format, string const & transform,
410 string const & transformer_class)
412 bool class_found = false;
413 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
415 ClipOptionFactory factory = ClipLatexOption::factory;
416 format.option_transformers[Clip] =
417 TransformStore(Clip, factory);
419 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
421 ExtraOptionFactory factory = ExtraOption::factory;
422 format.option_transformers[Extra] =
423 TransformStore(Extra, factory);
425 } else if (transform == "Resize" &&
426 transformer_class == "ResizeLatexOption") {
428 ResizeOptionFactory factory = ResizeLatexOption::factory;
429 format.option_transformers[Resize] =
430 TransformStore(Resize, factory);
432 } else if (transform == "Rotate" &&
433 transformer_class == "RotationLatexOption") {
435 RotationOptionFactory factory = RotationLatexOption::factory;
436 format.option_transformers[Rotate] =
437 TransformStore(Rotate, factory);
440 transform_not_found(lyxerr, transform);
443 transform_class_not_found(lyxerr, transformer_class);
449 void Template::Format::readFormat(LyXLex & lex)
463 keyword_item formattags[] = {
464 { "formatend", FO_END },
465 { "option", FO_OPTION },
466 { "preamble", FO_PREAMBLE },
467 { "product", FO_PRODUCT },
468 { "requirement", FO_REQUIREMENT },
469 { "transformcommand", FO_TRANSFORMCOMMAND },
470 { "transformoption", FO_TRANSFORMOPTION },
471 { "updateformat", FO_UPDATEFORMAT },
472 { "updateresult", FO_UPDATERESULT }
475 pushpophelper pph(lex, formattags, FO_END);
481 product = lex.getString();
484 case FO_UPDATEFORMAT:
486 updateFormat = lex.getString();
489 case FO_UPDATERESULT:
491 updateResult = lex.getString();
496 requirement = lex.getString();
501 preambleNames.push_back(lex.getString());
504 case FO_TRANSFORMCOMMAND: {
506 string const name = lex.getString();
508 setCommandFactory(*this, name, lex.getString());
512 case FO_TRANSFORMOPTION: {
514 string const name = lex.getString();
516 setOptionFactory(*this, name, lex.getString());
522 string const name = lex.getString();
524 string const opt = lex.getString();
525 options.push_back(Option(name, opt));
537 TransformIDTranslator const initIDTranslator()
539 TransformIDTranslator translator(TransformID(-1), "");
540 translator.addPair(Rotate, "Rotate");
541 translator.addPair(Resize, "Resize");
542 translator.addPair(Clip, "Clip");
543 translator.addPair(Extra, "Extra");
548 TransformIDTranslator const & transformIDTranslator()
550 static TransformIDTranslator const translator = initIDTranslator();
556 } // namespace external