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"
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/Package.h"
22 #include "support/Path.h"
27 using namespace lyx::support;
33 typedef Translator<TransformID, string> TransformIDTranslator;
35 static TransformIDTranslator const initIDTranslator()
37 TransformIDTranslator translator(TransformID(-1), "");
38 translator.addPair(Rotate, "Rotate");
39 translator.addPair(Resize, "Resize");
40 translator.addPair(Clip, "Clip");
41 translator.addPair(Extra, "Extra");
45 static TransformIDTranslator const & transformIDTranslator()
47 static TransformIDTranslator const translator = initIDTranslator();
51 // We have to have dummy default commands for security reasons!
57 Template::Format::Format()
61 TemplateManager::TemplateManager()
63 readTemplates(package().user_support());
64 if (lyxerr.debugging(Debug::EXTERNAL)) {
65 dumpPreambleDefs(lyxerr);
67 dumpTemplates(lyxerr);
72 class DumpPreambleDef {
74 typedef TemplateManager::PreambleDefs::value_type value_type;
76 DumpPreambleDef(ostream & os) : os_(os) {}
78 void operator()(value_type const & vt) {
79 os_ << "PreambleDef " << vt.first << '\n'
81 << "PreambleDefEnd" << endl;
91 typedef TemplateManager::Templates::value_type value_type;
93 DumpTemplate(ostream & os) : os_(os) {}
95 void operator()(value_type const & vt) {
96 Template const & et = vt.second;
98 os_ << "Template " << et.lyxName << '\n'
99 << "\tGuiName " << et.guiName << '\n'
103 << "\tInputFormat " << et.inputFormat << '\n'
104 << "\tFileFilter " << et.fileRegExp << '\n'
105 << "\tAutomaticProduction " << et.automaticProduction << '\n';
107 typedef vector<TransformID> IDs;
108 IDs::const_iterator it = et.transformIds.begin();
109 IDs::const_iterator end = et.transformIds.end();
110 for (; it != end; ++it) {
111 os_ << "\tTransform "
112 << transformIDTranslator().find(*it) << '\n';
116 os_ << "TemplateEnd" << endl;
126 typedef Template::Formats::value_type value_type;
128 DumpFormat(ostream & o) : os_(o) {}
130 void operator()(value_type const & vt) const {
131 Template::Format const & ft = vt.second;
132 os_ << "\tFormat " << vt.first << '\n'
133 << "\t\tProduct " << ft.product << '\n'
134 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
135 << "\t\tUpdateResult " << ft.updateResult << '\n';
137 vector<string>::const_iterator qit = ft.requirements.begin();
138 vector<string>::const_iterator qend = ft.requirements.end();
139 for (; qit != qend; ++qit) {
140 lyxerr << "req:" << *qit << endl;
141 os_ << "\t\tRequirement " << *qit << '\n';
144 typedef vector<Template::Option> Options;
145 Options::const_iterator oit = ft.options.begin();
146 Options::const_iterator oend = ft.options.end();
147 for (; oit != oend; ++oit) {
155 vector<string>::const_iterator pit = ft.preambleNames.begin();
156 vector<string>::const_iterator pend = ft.preambleNames.end();
157 for (; pit != pend; ++pit) {
158 os_ << "\t\tPreamble " << *pit << '\n';
161 typedef Template::Format::FileMap FileMap;
162 FileMap::const_iterator rit = ft.referencedFiles.begin();
163 FileMap::const_iterator rend = ft.referencedFiles.end();
164 for (; rit != rend; ++rit) {
165 vector<string>::const_iterator fit = rit->second.begin();
166 vector<string>::const_iterator fend = rit->second.end();
167 for (; fit != fend; ++fit) {
168 os_ << "\t\tReferencedFile " << rit->first
169 << " \"" << *fit << "\"\n";
173 os_ << "\tFormatEnd\n";
180 void Template::dumpFormats(ostream & os) const
182 for_each(formats.begin(), formats.end(), DumpFormat(os));
186 void TemplateManager::dumpPreambleDefs(ostream & os) const
188 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
192 void TemplateManager::dumpTemplates(ostream & os) const
194 for_each(templates.begin(), templates.end(), DumpTemplate(os));
198 TemplateManager & TemplateManager::get()
200 static TemplateManager externalTemplateManager;
201 return externalTemplateManager;
205 TemplateManager::Templates const & 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 = 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(FileName const & path)
245 LexerKeyword templatetags[] = {
246 { "preambledef", TM_PREAMBLEDEF },
247 { "preambledefend", TM_PREAMBLEDEF_END },
248 { "template", TM_TEMPLATE },
249 { "templateend", TM_TEMPLATE_END }
252 Lexer lex(templatetags);
254 FileName const filename = 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.");
294 void Template::readTemplate(Lexer & lex)
307 LexerKeyword templateoptiontags[] = {
308 { "automaticproduction", TO_AUTOMATIC },
309 { "filefilter", TO_FILTER },
310 { "format", TO_FORMAT },
311 { "guiname", TO_GUINAME },
312 { "helptext", TO_HELPTEXT },
313 { "inputformat", TO_INPUTFORMAT },
314 { "templateend", TO_END },
315 { "transform", TO_TRANSFORM }
318 PushPopHelper pph(lex, templateoptiontags);
319 lex.setContext("Template::readTemplate");
326 guiName = lex.getString();
330 helpText = lex.getLongString("HelpTextEnd");
335 inputFormat = lex.getString();
340 fileRegExp = lex.getString();
345 automaticProduction = lex.getBool();
350 TransformID id = transformIDTranslator().find(token);
352 LYXERR0("Transform " << token << " is not recognized");
354 transformIds.push_back(id);
360 formats[lex.getString()].readFormat(lex);
367 lex.printError("external::Template::readTemplate: "
368 "Wrong tag: $$Token");
369 LASSERT(false, /**/);
378 void transform_not_found(ostream & os, string const & transform)
380 os << "external::Format::readFormat. Transformation \""
381 << transform << "\" is unrecognized." << endl;
385 void transform_class_not_found(ostream & os, string const & tclass)
387 os << "external::Format::readFormat. Transformation class \""
388 << tclass << "\" is unrecognized." << endl;
392 void setCommandFactory(Template::Format & format, string const & transform,
393 string const & transformer_class)
395 bool class_found = false;
396 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
398 ResizeCommandFactory factory = ResizeLatexCommand::factory;
399 format.command_transformers[Resize] =
400 TransformStore(Resize, factory);
402 } else if (transform == "Rotate" &&
403 transformer_class == "RotationLatexCommand") {
405 RotationCommandFactory factory = RotationLatexCommand::factory;
406 format.command_transformers[Rotate] =
407 TransformStore(Rotate, factory);
410 transform_not_found(lyxerr, transform);
413 transform_class_not_found(lyxerr, transformer_class);
417 void setOptionFactory(Template::Format & format, string const & transform,
418 string const & transformer_class)
420 bool class_found = false;
421 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
423 ClipOptionFactory factory = ClipLatexOption::factory;
424 format.option_transformers[Clip] =
425 TransformStore(Clip, factory);
427 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
429 ExtraOptionFactory factory = ExtraOption::factory;
430 format.option_transformers[Extra] =
431 TransformStore(Extra, factory);
433 } else if (transform == "Resize" &&
434 transformer_class == "ResizeLatexOption") {
436 ResizeOptionFactory factory = ResizeLatexOption::factory;
437 format.option_transformers[Resize] =
438 TransformStore(Resize, factory);
440 } else if (transform == "Rotate" &&
441 transformer_class == "RotationLatexOption") {
443 RotationOptionFactory factory = RotationLatexOption::factory;
444 format.option_transformers[Rotate] =
445 TransformStore(Rotate, factory);
448 transform_not_found(lyxerr, transform);
451 transform_class_not_found(lyxerr, transformer_class);
457 void Template::Format::readFormat(Lexer & lex)
472 LexerKeyword formattags[] = {
473 { "formatend", FO_END },
474 { "option", FO_OPTION },
475 { "preamble", FO_PREAMBLE },
476 { "product", FO_PRODUCT },
477 { "referencedfile", FO_REFERENCEDFILE },
478 { "requirement", FO_REQUIREMENT },
479 { "transformcommand", FO_TRANSFORMCOMMAND },
480 { "transformoption", FO_TRANSFORMOPTION },
481 { "updateformat", FO_UPDATEFORMAT },
482 { "updateresult", FO_UPDATERESULT }
485 PushPopHelper pph(lex, formattags);
491 product = lex.getString();
494 case FO_UPDATEFORMAT:
496 updateFormat = lex.getString();
499 case FO_UPDATERESULT:
501 updateResult = lex.getString();
506 requirements.push_back(lex.getString());
511 preambleNames.push_back(lex.getString());
514 case FO_TRANSFORMCOMMAND: {
516 string const name = lex.getString();
518 setCommandFactory(*this, name, lex.getString());
522 case FO_TRANSFORMOPTION: {
524 string const name = lex.getString();
526 setOptionFactory(*this, name, lex.getString());
532 string const name = lex.getString();
534 string const opt = lex.getString();
535 options.push_back(Option(name, opt));
539 case FO_REFERENCEDFILE: {
541 string const format = lex.getString();
543 string const file = lex.getString();
544 referencedFiles[format].push_back(file);
554 } // namespace external