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/PathChanger.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'
102 << to_utf8(et.helpText)
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;
231 docstring 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(from_ascii(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(from_ascii("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);
395 void transform_not_found(ostream & os, string const & transform)
397 os << "external::Format::readFormat. Transformation \""
398 << transform << "\" is unrecognized." << endl;
402 void transform_class_not_found(ostream & os, string const & tclass)
404 os << "external::Format::readFormat. Transformation class \""
405 << tclass << "\" is unrecognized." << endl;
409 void setCommandFactory(Template::Format & format, string const & transform,
410 string const & transformer_class)
412 bool class_found = false;
413 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
415 ResizeCommandFactory factory = ResizeLatexCommand::factory;
416 format.command_transformers[Resize] =
417 TransformStore(Resize, factory);
419 } else if (transform == "Rotate" &&
420 transformer_class == "RotationLatexCommand") {
422 RotationCommandFactory factory = RotationLatexCommand::factory;
423 format.command_transformers[Rotate] =
424 TransformStore(Rotate, factory);
427 transform_not_found(lyxerr, transform);
430 transform_class_not_found(lyxerr, transformer_class);
434 void setOptionFactory(Template::Format & format, string const & transform,
435 string const & transformer_class)
437 bool class_found = false;
438 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
440 ClipOptionFactory factory = ClipLatexOption::factory;
441 format.option_transformers[Clip] =
442 TransformStore(Clip, factory);
444 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
446 ExtraOptionFactory factory = ExtraOption::factory;
447 format.option_transformers[Extra] =
448 TransformStore(Extra, factory);
450 } else if (transform == "Resize" &&
451 transformer_class == "ResizeLatexOption") {
453 ResizeOptionFactory factory = ResizeLatexOption::factory;
454 format.option_transformers[Resize] =
455 TransformStore(Resize, factory);
457 } else if (transform == "Rotate" &&
458 transformer_class == "RotationLatexOption") {
460 RotationOptionFactory factory = RotationLatexOption::factory;
461 format.option_transformers[Rotate] =
462 TransformStore(Rotate, factory);
465 transform_not_found(lyxerr, transform);
468 transform_class_not_found(lyxerr, transformer_class);
474 void Template::Format::readFormat(Lexer & lex)
489 LexerKeyword formattags[] = {
490 { "formatend", FO_END },
491 { "option", FO_OPTION },
492 { "preamble", FO_PREAMBLE },
493 { "product", FO_PRODUCT },
494 { "referencedfile", FO_REFERENCEDFILE },
495 { "requirement", FO_REQUIREMENT },
496 { "transformcommand", FO_TRANSFORMCOMMAND },
497 { "transformoption", FO_TRANSFORMOPTION },
498 { "updateformat", FO_UPDATEFORMAT },
499 { "updateresult", FO_UPDATERESULT }
502 PushPopHelper pph(lex, formattags);
508 product = lex.getString();
511 case FO_UPDATEFORMAT:
513 updateFormat = lex.getString();
516 case FO_UPDATERESULT:
518 updateResult = lex.getString();
523 requirements.push_back(lex.getString());
528 preambleNames.push_back(lex.getString());
531 case FO_TRANSFORMCOMMAND: {
533 string const name = lex.getString();
535 setCommandFactory(*this, name, lex.getString());
539 case FO_TRANSFORMOPTION: {
541 string const name = lex.getString();
543 setOptionFactory(*this, name, lex.getString());
549 string const name = lex.getString();
551 string const opt = lex.getString();
552 options.push_back(Option(name, opt));
556 case FO_REFERENCEDFILE: {
558 string const format = lex.getString();
560 string const file = lex.getString();
561 referencedFiles[format].push_back(file);
571 } // namespace external