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"
32 typedef Translator<TransformID, string> TransformIDTranslator;
34 static TransformIDTranslator const initIDTranslator()
36 TransformIDTranslator translator(TransformID(-1), "");
37 translator.addPair(Rotate, "Rotate");
38 translator.addPair(Resize, "Resize");
39 translator.addPair(Clip, "Clip");
40 translator.addPair(Extra, "Extra");
44 static TransformIDTranslator const & transformIDTranslator()
46 static TransformIDTranslator const translator = initIDTranslator();
50 // We have to have dummy default commands for security reasons!
56 Template::Format::Format()
60 TemplateManager::TemplateManager()
62 readTemplates(support::package().user_support());
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';
136 vector<string>::const_iterator qit = ft.requirements.begin();
137 vector<string>::const_iterator qend = ft.requirements.end();
138 for (; qit != qend; ++qit) {
139 lyxerr << "req:" << *qit << std::endl;
140 ost << "\t\tRequirement " << *qit << '\n';
143 typedef vector<Template::Option> Options;
144 Options::const_iterator oit = ft.options.begin();
145 Options::const_iterator oend = ft.options.end();
146 for (; oit != oend; ++oit) {
154 vector<string>::const_iterator pit = ft.preambleNames.begin();
155 vector<string>::const_iterator pend = ft.preambleNames.end();
156 for (; pit != pend; ++pit) {
157 ost << "\t\tPreamble " << *pit << '\n';
160 typedef Template::Format::FileMap FileMap;
161 FileMap::const_iterator rit = ft.referencedFiles.begin();
162 FileMap::const_iterator rend = ft.referencedFiles.end();
163 for (; rit != rend; ++rit) {
164 vector<string>::const_iterator fit = rit->second.begin();
165 vector<string>::const_iterator fend = rit->second.end();
166 for (; fit != fend; ++fit) {
167 ost << "\t\tReferencedFile " << rit->first
168 << " \"" << *fit << "\"\n";
172 ost << "\tFormatEnd\n";
179 void Template::dumpFormats(ostream & os) const
181 for_each(formats.begin(), formats.end(), DumpFormat(os));
185 void TemplateManager::dumpPreambleDefs(ostream & os) const
187 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
191 void TemplateManager::dumpTemplates(ostream & os) const
193 for_each(templates.begin(), templates.end(), DumpTemplate(os));
197 TemplateManager & TemplateManager::get()
199 static TemplateManager externalTemplateManager;
200 return externalTemplateManager;
204 TemplateManager::Templates const &
205 TemplateManager::getTemplates() const
212 TemplateManager::getTemplateByName(string const & name) const
214 Templates::const_iterator it = templates.find(name);
215 return (it == templates.end()) ? 0 : &it->second;
220 TemplateManager::getPreambleDefByName(string const & name) const
222 string const trimmed_name = support::trim(name);
223 if (trimmed_name.empty())
226 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
227 if (it == preambledefs.end())
234 void TemplateManager::readTemplates(support::FileName const & path)
236 support::PathChanger p(path);
245 keyword_item templatetags[] = {
246 { "preambledef", TM_PREAMBLEDEF },
247 { "preambledefend", TM_PREAMBLEDEF_END },
248 { "template", TM_TEMPLATE },
249 { "templateend", TM_TEMPLATE_END }
252 Lexer lex(templatetags, TM_TEMPLATE_END);
254 support::FileName const filename = support::libFileSearch("", "external_templates");
255 if (filename.empty() || !lex.setFile(filename)) {
256 lex.printError("external::TemplateManager::readTemplates: "
261 char const * const preamble_end_tag =
262 templatetags[TM_PREAMBLEDEF_END-1].tag;
266 case TM_PREAMBLEDEF: {
268 string const name = lex.getString();
269 preambledefs[name] = lex.getLongString(preamble_end_tag);
275 string const name = lex.getString();
276 Template & tmp = templates[name];
278 tmp.readTemplate(lex);
282 case TM_TEMPLATE_END:
283 lex.printError("Warning: End outside Template.");
286 case TM_PREAMBLEDEF_END:
287 lex.printError("Warning: End outside PreambleDef.");
296 void add(vector<TransformID> & ids, string const & name)
298 TransformID id = transformIDTranslator().find(name);
300 lyxerr << "external::Template::readTemplate\n"
301 << "Transform " << name << " is not recognized"
311 void Template::readTemplate(Lexer & lex)
313 enum TemplateOptionTags {
324 keyword_item templateoptiontags[] = {
325 { "automaticproduction", TO_AUTOMATIC },
326 { "filefilter", TO_FILTER },
327 { "format", TO_FORMAT },
328 { "guiname", TO_GUINAME },
329 { "helptext", TO_HELPTEXT },
330 { "inputformat", TO_INPUTFORMAT },
331 { "templateend", TO_END },
332 { "transform", TO_TRANSFORM }
335 PushPopHelper pph(lex, templateoptiontags, TO_END);
341 guiName = lex.getString();
345 helpText = lex.getLongString("HelpTextEnd");
350 inputFormat = lex.getString();
355 fileRegExp = lex.getString();
360 automaticProduction = lex.getBool();
365 add(transformIds, lex.getString());
370 formats[lex.getString()].readFormat(lex);
377 lex.printError("external::Template::readTemplate: "
378 "Wrong tag: $$Token");
388 void transform_not_found(std::ostream & os, string const & transform)
390 os << "external::Format::readFormat. Transformation \""
391 << transform << "\" is unrecognized." << std::endl;
395 void transform_class_not_found(std::ostream & os, string const & tclass)
397 os << "external::Format::readFormat. Transformation class \""
398 << tclass << "\" is unrecognized." << std::endl;
402 void setCommandFactory(Template::Format & format, string const & transform,
403 string const & transformer_class)
405 bool class_found = false;
406 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
408 ResizeCommandFactory factory = ResizeLatexCommand::factory;
409 format.command_transformers[Resize] =
410 TransformStore(Resize, factory);
412 } else if (transform == "Rotate" &&
413 transformer_class == "RotationLatexCommand") {
415 RotationCommandFactory factory = RotationLatexCommand::factory;
416 format.command_transformers[Rotate] =
417 TransformStore(Rotate, factory);
420 transform_not_found(lyxerr, transform);
423 transform_class_not_found(lyxerr, transformer_class);
427 void setOptionFactory(Template::Format & format, string const & transform,
428 string const & transformer_class)
430 bool class_found = false;
431 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
433 ClipOptionFactory factory = ClipLatexOption::factory;
434 format.option_transformers[Clip] =
435 TransformStore(Clip, factory);
437 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
439 ExtraOptionFactory factory = ExtraOption::factory;
440 format.option_transformers[Extra] =
441 TransformStore(Extra, factory);
443 } else if (transform == "Resize" &&
444 transformer_class == "ResizeLatexOption") {
446 ResizeOptionFactory factory = ResizeLatexOption::factory;
447 format.option_transformers[Resize] =
448 TransformStore(Resize, factory);
450 } else if (transform == "Rotate" &&
451 transformer_class == "RotationLatexOption") {
453 RotationOptionFactory factory = RotationLatexOption::factory;
454 format.option_transformers[Rotate] =
455 TransformStore(Rotate, factory);
458 transform_not_found(lyxerr, transform);
461 transform_class_not_found(lyxerr, transformer_class);
467 void Template::Format::readFormat(Lexer & lex)
482 keyword_item formattags[] = {
483 { "formatend", FO_END },
484 { "option", FO_OPTION },
485 { "preamble", FO_PREAMBLE },
486 { "product", FO_PRODUCT },
487 { "referencedfile", FO_REFERENCEDFILE },
488 { "requirement", FO_REQUIREMENT },
489 { "transformcommand", FO_TRANSFORMCOMMAND },
490 { "transformoption", FO_TRANSFORMOPTION },
491 { "updateformat", FO_UPDATEFORMAT },
492 { "updateresult", FO_UPDATERESULT }
495 PushPopHelper pph(lex, formattags, FO_END);
501 product = lex.getString();
504 case FO_UPDATEFORMAT:
506 updateFormat = lex.getString();
509 case FO_UPDATERESULT:
511 updateResult = lex.getString();
516 requirements.push_back(lex.getString());
521 preambleNames.push_back(lex.getString());
524 case FO_TRANSFORMCOMMAND: {
526 string const name = lex.getString();
528 setCommandFactory(*this, name, lex.getString());
532 case FO_TRANSFORMOPTION: {
534 string const name = lex.getString();
536 setOptionFactory(*this, name, lex.getString());
542 string const name = lex.getString();
544 string const opt = lex.getString();
545 options.push_back(Option(name, opt));
549 case FO_REFERENCEDFILE: {
551 string const format = lex.getString();
553 string const file = lex.getString();
554 referencedFiles[format].push_back(file);
564 } // namespace external