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 // Read the templates list
264 vector<string> templateslist;
265 FileName const real_file = libFileSearch("", "xtemplates.lst");
266 LYXERR(Debug::EXTERNAL, "Reading external templates from `" << real_file << '\'');
268 if (real_file.empty()) {
269 LYXERR0("unable to find external templates file `xtemplates.lst'.\n"
270 << "No external templates will be available.");
275 if (!tlex.setFile(real_file)) {
276 LYXERR0("lyxlex was not able to set file: "
277 << real_file << ".\nNo external templates will be available.");
282 LYXERR0("unable to open external templates file `"
283 << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
284 << "'\nNo external templates will be available.");
288 bool finished = false;
289 // Parse external templates files
290 LYXERR(Debug::EXTERNAL, "Starting parsing of xtemplates.lst");
291 while (tlex.isOK() && !finished) {
292 LYXERR(Debug::EXTERNAL, "\tline by line");
293 switch (tlex.lex()) {
294 case Lexer::LEX_FEOF:
298 string const name = tlex.getString();
299 LYXERR(Debug::EXTERNAL, "Template name: " << name);
300 templateslist.push_back(name);
305 LYXERR(Debug::EXTERNAL, "End of parsing of xtemplates.lst");
307 for (vector<string>::const_iterator it = templateslist.begin(); it != templateslist.end(); ++it) {
308 FileName const filename = libFileSearch("xtemplates", *it);
309 LYXERR(Debug::EXTERNAL, "Reading template file " << filename.absFileName());
310 Lexer lex(templatetags);
311 if (filename.empty() || !lex.setFile(filename)) {
312 lex.printError("external::TemplateManager::readTemplates: "
317 char const * const preamble_end_tag =
318 templatetags[TM_PREAMBLEDEF_END-1].tag;
322 case TM_PREAMBLEDEF: {
324 string const name = lex.getString();
325 preambledefs[name] = lex.getLongString(from_ascii(preamble_end_tag));
331 string const name = lex.getString();
332 Template & tmp = templates[name];
334 tmp.readTemplate(lex);
338 case TM_TEMPLATE_END:
339 lex.printError("Warning: End outside Template.");
342 case TM_PREAMBLEDEF_END:
343 lex.printError("Warning: End outside PreambleDef.");
353 void Template::readTemplate(Lexer & lex)
367 LexerKeyword templateoptiontags[] = {
368 { "automaticproduction", TO_AUTOMATIC },
369 { "filefilter", TO_FILTER },
370 { "format", TO_FORMAT },
371 { "guiname", TO_GUINAME },
372 { "helptext", TO_HELPTEXT },
373 { "inputformat", TO_INPUTFORMAT },
374 { "preview", TO_PREVIEW },
375 { "templateend", TO_END },
376 { "transform", TO_TRANSFORM }
379 PushPopHelper pph(lex, templateoptiontags);
380 lex.setContext("Template::readTemplate");
387 guiName = lex.getString();
391 helpText = lex.getLongString(from_ascii("HelpTextEnd"));
396 inputFormat = lex.getString();
401 fileRegExp = lex.getString();
406 automaticProduction = lex.getBool();
411 if (token == "InstantPreview")
412 preview_mode = PREVIEW_INSTANT;
413 else if (token == "Graphics")
414 preview_mode = PREVIEW_GRAPHICS;
416 preview_mode = PREVIEW_OFF;
421 TransformID id = transformIDTranslator().find(token);
423 LYXERR0("Transform " << token << " is not recognized");
425 transformIds.push_back(id);
431 formats[lex.getString()].readFormat(lex);
443 void transform_not_found(ostream & os, string const & transform)
445 os << "external::Format::readFormat. Transformation \""
446 << transform << "\" is unrecognized." << endl;
450 void transform_class_not_found(ostream & os, string const & tclass)
452 os << "external::Format::readFormat. Transformation class \""
453 << tclass << "\" is unrecognized." << endl;
457 void setCommandFactory(Template::Format & format, string const & transform,
458 string const & transformer_class)
460 bool class_found = false;
461 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
463 ResizeCommandFactory factory = ResizeLatexCommand::factory;
464 format.command_transformers[Resize] =
465 TransformStore(Resize, factory);
467 } else if (transform == "Rotate" &&
468 transformer_class == "RotationLatexCommand") {
470 RotationCommandFactory factory = RotationLatexCommand::factory;
471 format.command_transformers[Rotate] =
472 TransformStore(Rotate, factory);
475 transform_not_found(lyxerr, transform);
478 transform_class_not_found(lyxerr, transformer_class);
482 void setOptionFactory(Template::Format & format, string const & transform,
483 string const & transformer_class)
485 bool class_found = false;
486 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
488 ClipOptionFactory factory = ClipLatexOption::factory;
489 format.option_transformers[Clip] =
490 TransformStore(Clip, factory);
492 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
494 ExtraOptionFactory factory = ExtraOption::factory;
495 format.option_transformers[Extra] =
496 TransformStore(Extra, factory);
498 } else if (transform == "Resize" &&
499 transformer_class == "ResizeLatexOption") {
501 ResizeOptionFactory factory = ResizeLatexOption::factory;
502 format.option_transformers[Resize] =
503 TransformStore(Resize, factory);
505 } else if (transform == "Rotate" &&
506 transformer_class == "RotationLatexOption") {
508 RotationOptionFactory factory = RotationLatexOption::factory;
509 format.option_transformers[Rotate] =
510 TransformStore(Rotate, factory);
513 transform_not_found(lyxerr, transform);
516 transform_class_not_found(lyxerr, transformer_class);
522 void Template::Format::readFormat(Lexer & lex)
537 LexerKeyword formattags[] = {
538 { "formatend", FO_END },
539 { "option", FO_OPTION },
540 { "preamble", FO_PREAMBLE },
541 { "product", FO_PRODUCT },
542 { "referencedfile", FO_REFERENCEDFILE },
543 { "requirement", FO_REQUIREMENT },
544 { "transformcommand", FO_TRANSFORMCOMMAND },
545 { "transformoption", FO_TRANSFORMOPTION },
546 { "updateformat", FO_UPDATEFORMAT },
547 { "updateresult", FO_UPDATERESULT }
550 PushPopHelper pph(lex, formattags);
556 product = lex.getString();
559 case FO_UPDATEFORMAT:
561 updateFormat = lex.getString();
564 case FO_UPDATERESULT:
566 updateResult = lex.getString();
571 requirements.push_back(lex.getString());
576 preambleNames.push_back(lex.getString());
579 case FO_TRANSFORMCOMMAND: {
581 string const name = lex.getString();
583 setCommandFactory(*this, name, lex.getString());
587 case FO_TRANSFORMOPTION: {
589 string const name = lex.getString();
591 setOptionFactory(*this, name, lex.getString());
597 string const name = lex.getString();
599 string const opt = lex.getString();
600 options.push_back(Option(name, opt));
604 case FO_REFERENCEDFILE: {
606 string const format = lex.getString();
608 string const file = lex.getString();
609 referencedFiles[format].push_back(file);
619 } // namespace external