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"
23 #include "support/Translator.h"
28 using namespace lyx::support;
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!
54 : inputFormat("*"), automaticProduction(false), preview_mode(PREVIEW_OFF)
58 Template::Format::Format()
62 TemplateManager::TemplateManager()
64 readTemplates(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 & os) : os_(os) {}
79 void operator()(value_type const & vt) {
80 os_ << "PreambleDef " << vt.first << '\n'
82 << "PreambleDefEnd" << endl;
92 typedef TemplateManager::Templates::value_type value_type;
94 DumpTemplate(ostream & os) : os_(os) {}
96 void operator()(value_type const & vt) {
97 Template const & et = vt.second;
99 os_ << "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 switch (et.preview_mode) {
112 case PREVIEW_GRAPHICS:
115 case PREVIEW_INSTANT:
116 os_ << "InstantPreview\n";
119 typedef vector<TransformID> IDs;
120 IDs::const_iterator it = et.transformIds.begin();
121 IDs::const_iterator end = et.transformIds.end();
122 for (; it != end; ++it) {
123 os_ << "\tTransform "
124 << transformIDTranslator().find(*it) << '\n';
128 os_ << "TemplateEnd" << endl;
138 typedef Template::Formats::value_type value_type;
140 DumpFormat(ostream & o) : os_(o) {}
142 void operator()(value_type const & vt) const {
143 Template::Format const & ft = vt.second;
144 os_ << "\tFormat " << vt.first << '\n'
145 << "\t\tProduct " << ft.product << '\n'
146 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
147 << "\t\tUpdateResult " << ft.updateResult << '\n';
149 vector<string>::const_iterator qit = ft.requirements.begin();
150 vector<string>::const_iterator qend = ft.requirements.end();
151 for (; qit != qend; ++qit) {
152 lyxerr << "req:" << *qit << endl;
153 os_ << "\t\tRequirement " << *qit << '\n';
156 typedef vector<Template::Option> Options;
157 Options::const_iterator oit = ft.options.begin();
158 Options::const_iterator oend = ft.options.end();
159 for (; oit != oend; ++oit) {
167 vector<string>::const_iterator pit = ft.preambleNames.begin();
168 vector<string>::const_iterator pend = ft.preambleNames.end();
169 for (; pit != pend; ++pit) {
170 os_ << "\t\tPreamble " << *pit << '\n';
173 typedef Template::Format::FileMap FileMap;
174 FileMap::const_iterator rit = ft.referencedFiles.begin();
175 FileMap::const_iterator rend = ft.referencedFiles.end();
176 for (; rit != rend; ++rit) {
177 vector<string>::const_iterator fit = rit->second.begin();
178 vector<string>::const_iterator fend = rit->second.end();
179 for (; fit != fend; ++fit) {
180 os_ << "\t\tReferencedFile " << rit->first
181 << " \"" << *fit << "\"\n";
185 os_ << "\tFormatEnd\n";
192 void Template::dumpFormats(ostream & os) const
194 for_each(formats.begin(), formats.end(), DumpFormat(os));
198 void TemplateManager::dumpPreambleDefs(ostream & os) const
200 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
204 void TemplateManager::dumpTemplates(ostream & os) const
206 for_each(templates.begin(), templates.end(), DumpTemplate(os));
210 TemplateManager & TemplateManager::get()
212 static TemplateManager externalTemplateManager;
213 return externalTemplateManager;
217 TemplateManager::Templates const & TemplateManager::getTemplates() const
224 TemplateManager::getTemplateByName(string const & name) const
226 Templates::const_iterator it = templates.find(name);
227 return (it == templates.end()) ? 0 : &it->second;
232 TemplateManager::getPreambleDefByName(string const & name) const
234 string const trimmed_name = trim(name);
235 if (trimmed_name.empty())
238 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
239 if (it == preambledefs.end())
246 void TemplateManager::readTemplates(FileName const & path)
257 LexerKeyword templatetags[] = {
258 { "preambledef", TM_PREAMBLEDEF },
259 { "preambledefend", TM_PREAMBLEDEF_END },
260 { "template", TM_TEMPLATE },
261 { "templateend", TM_TEMPLATE_END }
264 Lexer lex(templatetags);
266 FileName const filename = libFileSearch("", "external_templates");
267 if (filename.empty() || !lex.setFile(filename)) {
268 lex.printError("external::TemplateManager::readTemplates: "
273 char const * const preamble_end_tag =
274 templatetags[TM_PREAMBLEDEF_END-1].tag;
278 case TM_PREAMBLEDEF: {
280 string const name = lex.getString();
281 preambledefs[name] = lex.getLongString(preamble_end_tag);
287 string const name = lex.getString();
288 Template & tmp = templates[name];
290 tmp.readTemplate(lex);
294 case TM_TEMPLATE_END:
295 lex.printError("Warning: End outside Template.");
298 case TM_PREAMBLEDEF_END:
299 lex.printError("Warning: End outside PreambleDef.");
306 void Template::readTemplate(Lexer & lex)
320 LexerKeyword templateoptiontags[] = {
321 { "automaticproduction", TO_AUTOMATIC },
322 { "filefilter", TO_FILTER },
323 { "format", TO_FORMAT },
324 { "guiname", TO_GUINAME },
325 { "helptext", TO_HELPTEXT },
326 { "inputformat", TO_INPUTFORMAT },
327 { "preview", TO_PREVIEW },
328 { "templateend", TO_END },
329 { "transform", TO_TRANSFORM }
332 PushPopHelper pph(lex, templateoptiontags);
333 lex.setContext("Template::readTemplate");
340 guiName = lex.getString();
344 helpText = lex.getLongString("HelpTextEnd");
349 inputFormat = lex.getString();
354 fileRegExp = lex.getString();
359 automaticProduction = lex.getBool();
364 if (token == "InstantPreview")
365 preview_mode = PREVIEW_INSTANT;
366 else if (token == "Graphics")
367 preview_mode = PREVIEW_GRAPHICS;
369 preview_mode = PREVIEW_OFF;
374 TransformID id = transformIDTranslator().find(token);
376 LYXERR0("Transform " << token << " is not recognized");
378 transformIds.push_back(id);
384 formats[lex.getString()].readFormat(lex);
391 lex.printError("external::Template::readTemplate: "
392 "Wrong tag: $$Token");
393 LASSERT(false, /**/);
402 void transform_not_found(ostream & os, string const & transform)
404 os << "external::Format::readFormat. Transformation \""
405 << transform << "\" is unrecognized." << endl;
409 void transform_class_not_found(ostream & os, string const & tclass)
411 os << "external::Format::readFormat. Transformation class \""
412 << tclass << "\" is unrecognized." << endl;
416 void setCommandFactory(Template::Format & format, string const & transform,
417 string const & transformer_class)
419 bool class_found = false;
420 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
422 ResizeCommandFactory factory = ResizeLatexCommand::factory;
423 format.command_transformers[Resize] =
424 TransformStore(Resize, factory);
426 } else if (transform == "Rotate" &&
427 transformer_class == "RotationLatexCommand") {
429 RotationCommandFactory factory = RotationLatexCommand::factory;
430 format.command_transformers[Rotate] =
431 TransformStore(Rotate, factory);
434 transform_not_found(lyxerr, transform);
437 transform_class_not_found(lyxerr, transformer_class);
441 void setOptionFactory(Template::Format & format, string const & transform,
442 string const & transformer_class)
444 bool class_found = false;
445 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
447 ClipOptionFactory factory = ClipLatexOption::factory;
448 format.option_transformers[Clip] =
449 TransformStore(Clip, factory);
451 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
453 ExtraOptionFactory factory = ExtraOption::factory;
454 format.option_transformers[Extra] =
455 TransformStore(Extra, factory);
457 } else if (transform == "Resize" &&
458 transformer_class == "ResizeLatexOption") {
460 ResizeOptionFactory factory = ResizeLatexOption::factory;
461 format.option_transformers[Resize] =
462 TransformStore(Resize, factory);
464 } else if (transform == "Rotate" &&
465 transformer_class == "RotationLatexOption") {
467 RotationOptionFactory factory = RotationLatexOption::factory;
468 format.option_transformers[Rotate] =
469 TransformStore(Rotate, factory);
472 transform_not_found(lyxerr, transform);
475 transform_class_not_found(lyxerr, transformer_class);
481 void Template::Format::readFormat(Lexer & lex)
496 LexerKeyword formattags[] = {
497 { "formatend", FO_END },
498 { "option", FO_OPTION },
499 { "preamble", FO_PREAMBLE },
500 { "product", FO_PRODUCT },
501 { "referencedfile", FO_REFERENCEDFILE },
502 { "requirement", FO_REQUIREMENT },
503 { "transformcommand", FO_TRANSFORMCOMMAND },
504 { "transformoption", FO_TRANSFORMOPTION },
505 { "updateformat", FO_UPDATEFORMAT },
506 { "updateresult", FO_UPDATERESULT }
509 PushPopHelper pph(lex, formattags);
515 product = lex.getString();
518 case FO_UPDATEFORMAT:
520 updateFormat = lex.getString();
523 case FO_UPDATERESULT:
525 updateResult = lex.getString();
530 requirements.push_back(lex.getString());
535 preambleNames.push_back(lex.getString());
538 case FO_TRANSFORMCOMMAND: {
540 string const name = lex.getString();
542 setCommandFactory(*this, name, lex.getString());
546 case FO_TRANSFORMOPTION: {
548 string const name = lex.getString();
550 setOptionFactory(*this, name, lex.getString());
556 string const name = lex.getString();
558 string const opt = lex.getString();
559 options.push_back(Option(name, opt));
563 case FO_REFERENCEDFILE: {
565 string const format = lex.getString();
567 string const file = lex.getString();
568 referencedFiles[format].push_back(file);
578 } // namespace external