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/package.h"
22 #include "support/path.h"
34 typedef Translator<TransformID, string> TransformIDTranslator;
36 static TransformIDTranslator const initIDTranslator()
38 TransformIDTranslator translator(TransformID(-1), "");
39 translator.addPair(Rotate, "Rotate");
40 translator.addPair(Resize, "Resize");
41 translator.addPair(Clip, "Clip");
42 translator.addPair(Extra, "Extra");
46 static TransformIDTranslator const & transformIDTranslator()
48 static TransformIDTranslator const translator = initIDTranslator();
52 // We have to have dummy default commands for security reasons!
58 Template::Format::Format()
62 TemplateManager::TemplateManager()
64 readTemplates(support::package().user_support());
65 if (lyxerr.debugging(Debug::EXTERNAL)) {
66 dumpPreambleDefs(lyxerr);
68 dumpTemplates(lyxerr);
73 class dumpPreambleDef {
75 typedef TemplateManager::PreambleDefs::value_type value_type;
77 dumpPreambleDef(ostream & o) : ost(o) {}
79 void operator()(value_type const & vt) {
80 ost << "PreambleDef " << vt.first << '\n'
82 << "PreambleDefEnd" << endl;
92 typedef TemplateManager::Templates::value_type value_type;
94 dumpTemplate(ostream & o) : ost(o) {}
96 void operator()(value_type const & vt) {
97 Template const & et = vt.second;
99 ost << "Template " << et.lyxName << '\n'
100 << "\tGuiName " << et.guiName << '\n'
104 << "\tInputFormat " << et.inputFormat << '\n'
105 << "\tFileFilter " << et.fileRegExp << '\n'
106 << "\tAutomaticProduction " << et.automaticProduction << '\n';
108 typedef vector<TransformID> IDs;
109 IDs::const_iterator it = et.transformIds.begin();
110 IDs::const_iterator end = et.transformIds.end();
111 for (; it != end; ++it) {
112 ost << "\tTransform "
113 << transformIDTranslator().find(*it) << '\n';
117 ost << "TemplateEnd" << endl;
127 typedef Template::Formats::value_type value_type;
129 dumpFormat(ostream & o) : ost(o) {}
131 void operator()(value_type const & vt) const {
132 Template::Format const & ft = vt.second;
133 ost << "\tFormat " << vt.first << '\n'
134 << "\t\tProduct " << ft.product << '\n'
135 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
136 << "\t\tUpdateResult " << ft.updateResult << '\n';
138 vector<string>::const_iterator qit = ft.requirements.begin();
139 vector<string>::const_iterator qend = ft.requirements.end();
140 for (; qit != qend; ++qit) {
141 lyxerr << "req:" << *qit << std::endl;
142 ost << "\t\tRequirement " << *qit << '\n';
145 typedef vector<Template::Option> Options;
146 Options::const_iterator oit = ft.options.begin();
147 Options::const_iterator oend = ft.options.end();
148 for (; oit != oend; ++oit) {
156 vector<string>::const_iterator pit = ft.preambleNames.begin();
157 vector<string>::const_iterator pend = ft.preambleNames.end();
158 for (; pit != pend; ++pit) {
159 ost << "\t\tPreamble " << *pit << '\n';
162 typedef Template::Format::FileMap FileMap;
163 FileMap::const_iterator rit = ft.referencedFiles.begin();
164 FileMap::const_iterator rend = ft.referencedFiles.end();
165 for (; rit != rend; ++rit) {
166 vector<string>::const_iterator fit = rit->second.begin();
167 vector<string>::const_iterator fend = rit->second.end();
168 for (; fit != fend; ++fit) {
169 ost << "\t\tReferencedFile " << rit->first
170 << " \"" << *fit << "\"\n";
174 ost << "\tFormatEnd\n";
181 void Template::dumpFormats(ostream & os) const
183 for_each(formats.begin(), formats.end(), dumpFormat(os));
187 void TemplateManager::dumpPreambleDefs(ostream & os) const
189 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
193 void TemplateManager::dumpTemplates(ostream & os) const
195 for_each(templates.begin(), templates.end(), dumpTemplate(os));
199 TemplateManager & TemplateManager::get()
201 static TemplateManager externalTemplateManager;
202 return externalTemplateManager;
206 TemplateManager::Templates const &
207 TemplateManager::getTemplates() const
214 TemplateManager::getTemplateByName(string const & name) const
216 Templates::const_iterator it = templates.find(name);
217 return (it == templates.end()) ? 0 : &it->second;
222 TemplateManager::getPreambleDefByName(string const & name) const
224 string const trimmed_name = support::trim(name);
225 if (trimmed_name.empty())
228 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
229 if (it == preambledefs.end())
236 void TemplateManager::readTemplates(string const & path)
238 support::Path p(path);
247 keyword_item templatetags[] = {
248 { "preambledef", TM_PREAMBLEDEF },
249 { "preambledefend", TM_PREAMBLEDEF_END },
250 { "template", TM_TEMPLATE },
251 { "templateend", TM_TEMPLATE_END }
254 LyXLex lex(templatetags, TM_TEMPLATE_END);
256 support::FileName const filename = support::libFileSearch("", "external_templates");
257 if (filename.empty() || !lex.setFile(filename)) {
258 lex.printError("external::TemplateManager::readTemplates: "
263 char const * const preamble_end_tag =
264 templatetags[TM_PREAMBLEDEF_END-1].tag;
268 case TM_PREAMBLEDEF: {
270 string const name = lex.getString();
271 preambledefs[name] = lex.getLongString(preamble_end_tag);
277 string const name = lex.getString();
278 Template & tmp = templates[name];
280 tmp.readTemplate(lex);
284 case TM_TEMPLATE_END:
285 lex.printError("Warning: End outside Template.");
288 case TM_PREAMBLEDEF_END:
289 lex.printError("Warning: End outside PreambleDef.");
298 void add(vector<TransformID> & ids, string const & name)
300 TransformID id = transformIDTranslator().find(name);
302 lyxerr << "external::Template::readTemplate\n"
303 << "Transform " << name << " is not recognized"
313 void Template::readTemplate(LyXLex & lex)
315 enum TemplateOptionTags {
326 keyword_item templateoptiontags[] = {
327 { "automaticproduction", TO_AUTOMATIC },
328 { "filefilter", TO_FILTER },
329 { "format", TO_FORMAT },
330 { "guiname", TO_GUINAME },
331 { "helptext", TO_HELPTEXT },
332 { "inputformat", TO_INPUTFORMAT },
333 { "templateend", TO_END },
334 { "transform", TO_TRANSFORM }
337 pushpophelper pph(lex, templateoptiontags, TO_END);
343 guiName = lex.getString();
347 helpText = lex.getLongString("HelpTextEnd");
352 inputFormat = lex.getString();
357 fileRegExp = lex.getString();
362 automaticProduction = lex.getBool();
367 add(transformIds, lex.getString());
372 formats[lex.getString()].readFormat(lex);
379 lex.printError("external::Template::readTemplate: "
380 "Wrong tag: $$Token");
390 void transform_not_found(std::ostream & os, string const & transform)
392 os << "external::Format::readFormat. Transformation \""
393 << transform << "\" is unrecognized." << std::endl;
397 void transform_class_not_found(std::ostream & os, string const & tclass)
399 os << "external::Format::readFormat. Transformation class \""
400 << tclass << "\" is unrecognized." << std::endl;
404 void setCommandFactory(Template::Format & format, string const & transform,
405 string const & transformer_class)
407 bool class_found = false;
408 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
410 ResizeCommandFactory factory = ResizeLatexCommand::factory;
411 format.command_transformers[Resize] =
412 TransformStore(Resize, factory);
414 } else if (transform == "Rotate" &&
415 transformer_class == "RotationLatexCommand") {
417 RotationCommandFactory factory = RotationLatexCommand::factory;
418 format.command_transformers[Rotate] =
419 TransformStore(Rotate, factory);
422 transform_not_found(lyxerr, transform);
425 transform_class_not_found(lyxerr, transformer_class);
429 void setOptionFactory(Template::Format & format, string const & transform,
430 string const & transformer_class)
432 bool class_found = false;
433 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
435 ClipOptionFactory factory = ClipLatexOption::factory;
436 format.option_transformers[Clip] =
437 TransformStore(Clip, factory);
439 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
441 ExtraOptionFactory factory = ExtraOption::factory;
442 format.option_transformers[Extra] =
443 TransformStore(Extra, factory);
445 } else if (transform == "Resize" &&
446 transformer_class == "ResizeLatexOption") {
448 ResizeOptionFactory factory = ResizeLatexOption::factory;
449 format.option_transformers[Resize] =
450 TransformStore(Resize, factory);
452 } else if (transform == "Rotate" &&
453 transformer_class == "RotationLatexOption") {
455 RotationOptionFactory factory = RotationLatexOption::factory;
456 format.option_transformers[Rotate] =
457 TransformStore(Rotate, factory);
460 transform_not_found(lyxerr, transform);
463 transform_class_not_found(lyxerr, transformer_class);
469 void Template::Format::readFormat(LyXLex & lex)
484 keyword_item formattags[] = {
485 { "formatend", FO_END },
486 { "option", FO_OPTION },
487 { "preamble", FO_PREAMBLE },
488 { "product", FO_PRODUCT },
489 { "referencedfile", FO_REFERENCEDFILE },
490 { "requirement", FO_REQUIREMENT },
491 { "transformcommand", FO_TRANSFORMCOMMAND },
492 { "transformoption", FO_TRANSFORMOPTION },
493 { "updateformat", FO_UPDATEFORMAT },
494 { "updateresult", FO_UPDATERESULT }
497 pushpophelper pph(lex, formattags, FO_END);
503 product = lex.getString();
506 case FO_UPDATEFORMAT:
508 updateFormat = lex.getString();
511 case FO_UPDATERESULT:
513 updateResult = lex.getString();
518 requirements.push_back(lex.getString());
523 preambleNames.push_back(lex.getString());
526 case FO_TRANSFORMCOMMAND: {
528 string const name = lex.getString();
530 setCommandFactory(*this, name, lex.getString());
534 case FO_TRANSFORMOPTION: {
536 string const name = lex.getString();
538 setOptionFactory(*this, name, lex.getString());
544 string const name = lex.getString();
546 string const opt = lex.getString();
547 options.push_back(Option(name, opt));
551 case FO_REFERENCEDFILE: {
553 string const format = lex.getString();
555 string const file = lex.getString();
556 referencedFiles[format].push_back(file);
566 } // namespace external