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 switch (et.preview_mode) {
111 case PREVIEW_GRAPHICS:
114 case PREVIEW_INSTANT:
115 os_ << "InstantPreview\n";
118 typedef vector<TransformID> IDs;
119 IDs::const_iterator it = et.transformIds.begin();
120 IDs::const_iterator end = et.transformIds.end();
121 for (; it != end; ++it) {
122 os_ << "\tTransform "
123 << transformIDTranslator().find(*it) << '\n';
127 os_ << "TemplateEnd" << endl;
137 typedef Template::Formats::value_type value_type;
139 DumpFormat(ostream & o) : os_(o) {}
141 void operator()(value_type const & vt) const {
142 Template::Format const & ft = vt.second;
143 os_ << "\tFormat " << vt.first << '\n'
144 << "\t\tProduct " << ft.product << '\n'
145 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
146 << "\t\tUpdateResult " << ft.updateResult << '\n';
148 vector<string>::const_iterator qit = ft.requirements.begin();
149 vector<string>::const_iterator qend = ft.requirements.end();
150 for (; qit != qend; ++qit) {
151 lyxerr << "req:" << *qit << endl;
152 os_ << "\t\tRequirement " << *qit << '\n';
155 typedef vector<Template::Option> Options;
156 Options::const_iterator oit = ft.options.begin();
157 Options::const_iterator oend = ft.options.end();
158 for (; oit != oend; ++oit) {
166 vector<string>::const_iterator pit = ft.preambleNames.begin();
167 vector<string>::const_iterator pend = ft.preambleNames.end();
168 for (; pit != pend; ++pit) {
169 os_ << "\t\tPreamble " << *pit << '\n';
172 typedef Template::Format::FileMap FileMap;
173 FileMap::const_iterator rit = ft.referencedFiles.begin();
174 FileMap::const_iterator rend = ft.referencedFiles.end();
175 for (; rit != rend; ++rit) {
176 vector<string>::const_iterator fit = rit->second.begin();
177 vector<string>::const_iterator fend = rit->second.end();
178 for (; fit != fend; ++fit) {
179 os_ << "\t\tReferencedFile " << rit->first
180 << " \"" << *fit << "\"\n";
184 os_ << "\tFormatEnd\n";
191 void Template::dumpFormats(ostream & os) const
193 for_each(formats.begin(), formats.end(), DumpFormat(os));
197 void TemplateManager::dumpPreambleDefs(ostream & os) const
199 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
203 void TemplateManager::dumpTemplates(ostream & os) const
205 for_each(templates.begin(), templates.end(), DumpTemplate(os));
209 TemplateManager & TemplateManager::get()
211 static TemplateManager externalTemplateManager;
212 return externalTemplateManager;
216 TemplateManager::Templates const & TemplateManager::getTemplates() const
223 TemplateManager::getTemplateByName(string const & name) const
225 Templates::const_iterator it = templates.find(name);
226 return (it == templates.end()) ? 0 : &it->second;
231 TemplateManager::getPreambleDefByName(string const & name) const
233 string const trimmed_name = trim(name);
234 if (trimmed_name.empty())
237 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
238 if (it == preambledefs.end())
245 void TemplateManager::readTemplates(FileName const & path)
256 LexerKeyword templatetags[] = {
257 { "preambledef", TM_PREAMBLEDEF },
258 { "preambledefend", TM_PREAMBLEDEF_END },
259 { "template", TM_TEMPLATE },
260 { "templateend", TM_TEMPLATE_END }
263 Lexer lex(templatetags);
265 FileName const filename = libFileSearch("", "external_templates");
266 if (filename.empty() || !lex.setFile(filename)) {
267 lex.printError("external::TemplateManager::readTemplates: "
272 char const * const preamble_end_tag =
273 templatetags[TM_PREAMBLEDEF_END-1].tag;
277 case TM_PREAMBLEDEF: {
279 string const name = lex.getString();
280 preambledefs[name] = lex.getLongString(preamble_end_tag);
286 string const name = lex.getString();
287 Template & tmp = templates[name];
289 tmp.readTemplate(lex);
293 case TM_TEMPLATE_END:
294 lex.printError("Warning: End outside Template.");
297 case TM_PREAMBLEDEF_END:
298 lex.printError("Warning: End outside PreambleDef.");
305 void Template::readTemplate(Lexer & lex)
319 LexerKeyword templateoptiontags[] = {
320 { "automaticproduction", TO_AUTOMATIC },
321 { "filefilter", TO_FILTER },
322 { "format", TO_FORMAT },
323 { "guiname", TO_GUINAME },
324 { "helptext", TO_HELPTEXT },
325 { "inputformat", TO_INPUTFORMAT },
326 { "preview", TO_PREVIEW },
327 { "templateend", TO_END },
328 { "transform", TO_TRANSFORM }
331 PushPopHelper pph(lex, templateoptiontags);
332 lex.setContext("Template::readTemplate");
339 guiName = lex.getString();
343 helpText = lex.getLongString("HelpTextEnd");
348 inputFormat = lex.getString();
353 fileRegExp = lex.getString();
358 automaticProduction = lex.getBool();
363 if (token == "InstantPreview")
364 preview_mode = PREVIEW_INSTANT;
365 else if (token == "Graphics")
366 preview_mode = PREVIEW_GRAPHICS;
368 preview_mode = PREVIEW_OFF;
373 TransformID id = transformIDTranslator().find(token);
375 LYXERR0("Transform " << token << " is not recognized");
377 transformIds.push_back(id);
383 formats[lex.getString()].readFormat(lex);
390 lex.printError("external::Template::readTemplate: "
391 "Wrong tag: $$Token");
392 LASSERT(false, /**/);
401 void transform_not_found(ostream & os, string const & transform)
403 os << "external::Format::readFormat. Transformation \""
404 << transform << "\" is unrecognized." << endl;
408 void transform_class_not_found(ostream & os, string const & tclass)
410 os << "external::Format::readFormat. Transformation class \""
411 << tclass << "\" is unrecognized." << endl;
415 void setCommandFactory(Template::Format & format, string const & transform,
416 string const & transformer_class)
418 bool class_found = false;
419 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
421 ResizeCommandFactory factory = ResizeLatexCommand::factory;
422 format.command_transformers[Resize] =
423 TransformStore(Resize, factory);
425 } else if (transform == "Rotate" &&
426 transformer_class == "RotationLatexCommand") {
428 RotationCommandFactory factory = RotationLatexCommand::factory;
429 format.command_transformers[Rotate] =
430 TransformStore(Rotate, factory);
433 transform_not_found(lyxerr, transform);
436 transform_class_not_found(lyxerr, transformer_class);
440 void setOptionFactory(Template::Format & format, string const & transform,
441 string const & transformer_class)
443 bool class_found = false;
444 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
446 ClipOptionFactory factory = ClipLatexOption::factory;
447 format.option_transformers[Clip] =
448 TransformStore(Clip, factory);
450 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
452 ExtraOptionFactory factory = ExtraOption::factory;
453 format.option_transformers[Extra] =
454 TransformStore(Extra, factory);
456 } else if (transform == "Resize" &&
457 transformer_class == "ResizeLatexOption") {
459 ResizeOptionFactory factory = ResizeLatexOption::factory;
460 format.option_transformers[Resize] =
461 TransformStore(Resize, factory);
463 } else if (transform == "Rotate" &&
464 transformer_class == "RotationLatexOption") {
466 RotationOptionFactory factory = RotationLatexOption::factory;
467 format.option_transformers[Rotate] =
468 TransformStore(Rotate, factory);
471 transform_not_found(lyxerr, transform);
474 transform_class_not_found(lyxerr, transformer_class);
480 void Template::Format::readFormat(Lexer & lex)
495 LexerKeyword formattags[] = {
496 { "formatend", FO_END },
497 { "option", FO_OPTION },
498 { "preamble", FO_PREAMBLE },
499 { "product", FO_PRODUCT },
500 { "referencedfile", FO_REFERENCEDFILE },
501 { "requirement", FO_REQUIREMENT },
502 { "transformcommand", FO_TRANSFORMCOMMAND },
503 { "transformoption", FO_TRANSFORMOPTION },
504 { "updateformat", FO_UPDATEFORMAT },
505 { "updateresult", FO_UPDATERESULT }
508 PushPopHelper pph(lex, formattags);
514 product = lex.getString();
517 case FO_UPDATEFORMAT:
519 updateFormat = lex.getString();
522 case FO_UPDATERESULT:
524 updateResult = lex.getString();
529 requirements.push_back(lex.getString());
534 preambleNames.push_back(lex.getString());
537 case FO_TRANSFORMCOMMAND: {
539 string const name = lex.getString();
541 setCommandFactory(*this, name, lex.getString());
545 case FO_TRANSFORMOPTION: {
547 string const name = lex.getString();
549 setOptionFactory(*this, name, lex.getString());
555 string const name = lex.getString();
557 string const opt = lex.getString();
558 options.push_back(Option(name, opt));
562 case FO_REFERENCEDFILE: {
564 string const format = lex.getString();
566 string const file = lex.getString();
567 referencedFiles[format].push_back(file);
577 } // namespace external