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"
29 using namespace lyx::support;
35 typedef Translator<TransformID, string> TransformIDTranslator;
37 static TransformIDTranslator const initIDTranslator()
39 TransformIDTranslator translator(TransformID(-1), "");
40 translator.addPair(Rotate, "Rotate");
41 translator.addPair(Resize, "Resize");
42 translator.addPair(Clip, "Clip");
43 translator.addPair(Extra, "Extra");
47 static TransformIDTranslator const & transformIDTranslator()
49 static TransformIDTranslator const translator = initIDTranslator();
53 // We have to have dummy default commands for security reasons!
55 : inputFormat("*"), automaticProduction(false), preview_mode(PREVIEW_OFF)
59 Template::Format::Format()
63 TemplateManager::TemplateManager()
65 readTemplates(package().user_support());
66 if (lyxerr.debugging(Debug::EXTERNAL)) {
67 dumpPreambleDefs(lyxerr);
69 dumpTemplates(lyxerr);
74 std::function<void (TemplateManager::PreambleDefs::value_type const &)>
75 DumpPreambleDef(ostream & os)
77 return [&os](TemplateManager::PreambleDefs::value_type const & vt)
79 os << "PreambleDef " << vt.first << '\n'
81 << "PreambleDefEnd" << endl;
86 std::function<void (TemplateManager::Templates::value_type const &) >
87 DumpTemplate(ostream & os)
89 return [&os](TemplateManager::Templates::value_type const & vt) {
90 Template const & et = vt.second;
92 os << "Template " << et.lyxName << '\n'
93 << "\tGuiName " << et.guiName << '\n'
95 << to_utf8(et.helpText)
97 << "\tInputFormat " << et.inputFormat << '\n'
98 << "\tFileFilter " << et.fileRegExp << '\n'
99 << "\tAutomaticProduction " << et.automaticProduction << '\n'
101 switch (et.preview_mode) {
105 case PREVIEW_GRAPHICS:
108 case PREVIEW_INSTANT:
109 os << "InstantPreview\n";
112 typedef vector<TransformID> IDs;
113 IDs::const_iterator it = et.transformIds.begin();
114 IDs::const_iterator end = et.transformIds.end();
115 for (; it != end; ++it) {
117 << transformIDTranslator().find(*it) << '\n';
121 os << "TemplateEnd" << endl;
127 std::function<void (Template::Formats::value_type const &) >
128 DumpFormat (ostream & os)
130 return [&os](Template::Formats::value_type const & vt)
132 Template::Format const & ft = vt.second;
133 os << "\tFormat " << vt.first << '\n'
134 << "\t\tProduct " << ft.product << '\n'
135 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
136 << "\t\tUpdateResult " << ft.updateResult << '\n';
138 vector<string>::const_iterator qit = ft.requirements.begin();
139 vector<string>::const_iterator qend = ft.requirements.end();
140 for (; qit != qend; ++qit) {
141 lyxerr << "req:" << *qit << endl;
142 os << "\t\tRequirement " << *qit << '\n';
145 typedef vector<Template::Option> Options;
146 Options::const_iterator oit = ft.options.begin();
147 Options::const_iterator oend = ft.options.end();
148 for (; oit != oend; ++oit) {
156 vector<string>::const_iterator pit = ft.preambleNames.begin();
157 vector<string>::const_iterator pend = ft.preambleNames.end();
158 for (; pit != pend; ++pit) {
159 os << "\t\tPreamble " << *pit << '\n';
162 typedef Template::Format::FileMap FileMap;
163 FileMap::const_iterator rit = ft.referencedFiles.begin();
164 FileMap::const_iterator rend = ft.referencedFiles.end();
165 for (; rit != rend; ++rit) {
166 vector<string>::const_iterator fit = rit->second.begin();
167 vector<string>::const_iterator fend = rit->second.end();
168 for (; fit != fend; ++fit) {
169 os << "\t\tReferencedFile " << rit->first
170 << " \"" << *fit << "\"\n";
174 os << "\tFormatEnd\n";
179 void Template::dumpFormats(ostream & os) const
181 for_each(formats.begin(), formats.end(), DumpFormat(os));
185 void TemplateManager::dumpPreambleDefs(ostream & os) const
187 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
191 void TemplateManager::dumpTemplates(ostream & os) const
193 for_each(templates.begin(), templates.end(), DumpTemplate(os));
197 TemplateManager & TemplateManager::get()
199 static TemplateManager externalTemplateManager;
200 return externalTemplateManager;
204 TemplateManager::Templates const & TemplateManager::getTemplates() const
211 TemplateManager::getTemplateByName(string const & name) const
213 Templates::const_iterator it = templates.find(name);
214 return (it == templates.end()) ? 0 : &it->second;
218 docstring TemplateManager::getPreambleDefByName(string const & name) const
220 string const trimmed_name = trim(name);
221 if (trimmed_name.empty())
224 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
225 if (it == preambledefs.end())
232 void TemplateManager::readTemplates(FileName const & path)
243 LexerKeyword templatetags[] = {
244 { "preambledef", TM_PREAMBLEDEF },
245 { "preambledefend", TM_PREAMBLEDEF_END },
246 { "template", TM_TEMPLATE },
247 { "templateend", TM_TEMPLATE_END }
250 // Read the templates list
251 vector<string> templateslist;
252 FileName const real_file = libFileSearch("", "xtemplates.lst");
253 LYXERR(Debug::EXTERNAL, "Reading external templates from `" << real_file << '\'');
255 if (real_file.empty()) {
256 LYXERR0("unable to find external templates file `xtemplates.lst'.\n"
257 << "No external templates will be available.");
262 if (!tlex.setFile(real_file)) {
263 LYXERR0("lyxlex was not able to set file: "
264 << real_file << ".\nNo external templates will be available.");
269 LYXERR0("unable to open external templates file `"
270 << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
271 << "'\nNo external templates will be available.");
275 bool finished = false;
276 // Parse external templates files
277 LYXERR(Debug::EXTERNAL, "Starting parsing of xtemplates.lst");
278 while (tlex.isOK() && !finished) {
279 LYXERR(Debug::EXTERNAL, "\tline by line");
280 switch (tlex.lex()) {
281 case Lexer::LEX_FEOF:
285 string const name = tlex.getString();
286 LYXERR(Debug::EXTERNAL, "Template name: " << name);
287 templateslist.push_back(name);
292 LYXERR(Debug::EXTERNAL, "End of parsing of xtemplates.lst");
294 for (vector<string>::const_iterator it = templateslist.begin(); it != templateslist.end(); ++it) {
295 FileName const filename = libFileSearch("xtemplates", *it);
296 LYXERR(Debug::EXTERNAL, "Reading template file " << filename.absFileName());
297 Lexer lex(templatetags);
298 if (filename.empty() || !lex.setFile(filename)) {
299 lex.printError("external::TemplateManager::readTemplates: "
304 char const * const preamble_end_tag =
305 templatetags[TM_PREAMBLEDEF_END-1].tag;
309 case TM_PREAMBLEDEF: {
311 string const name = lex.getString();
312 preambledefs[name] = lex.getLongString(from_ascii(preamble_end_tag));
318 string const name = lex.getString();
319 Template & tmp = templates[name];
321 tmp.readTemplate(lex);
325 case TM_TEMPLATE_END:
326 lex.printError("Warning: End outside Template.");
329 case TM_PREAMBLEDEF_END:
330 lex.printError("Warning: End outside PreambleDef.");
340 void Template::readTemplate(Lexer & lex)
354 LexerKeyword templateoptiontags[] = {
355 { "automaticproduction", TO_AUTOMATIC },
356 { "filefilter", TO_FILTER },
357 { "format", TO_FORMAT },
358 { "guiname", TO_GUINAME },
359 { "helptext", TO_HELPTEXT },
360 { "inputformat", TO_INPUTFORMAT },
361 { "preview", TO_PREVIEW },
362 { "templateend", TO_END },
363 { "transform", TO_TRANSFORM }
366 PushPopHelper pph(lex, templateoptiontags);
367 lex.setContext("Template::readTemplate");
374 guiName = lex.getString();
378 helpText = lex.getLongString(from_ascii("HelpTextEnd"));
383 inputFormat = lex.getString();
388 fileRegExp = lex.getString();
393 automaticProduction = lex.getBool();
398 if (token == "InstantPreview")
399 preview_mode = PREVIEW_INSTANT;
400 else if (token == "Graphics")
401 preview_mode = PREVIEW_GRAPHICS;
403 preview_mode = PREVIEW_OFF;
408 TransformID id = transformIDTranslator().find(token);
410 LYXERR0("Transform " << token << " is not recognized");
412 transformIds.push_back(id);
418 formats[lex.getString()].readFormat(lex);
430 void transform_not_found(ostream & os, string const & transform)
432 os << "external::Format::readFormat. Transformation \""
433 << transform << "\" is unrecognized." << endl;
437 void transform_class_not_found(ostream & os, string const & tclass)
439 os << "external::Format::readFormat. Transformation class \""
440 << tclass << "\" is unrecognized." << endl;
444 void setCommandFactory(Template::Format & format, string const & transform,
445 string const & transformer_class)
447 bool class_found = false;
448 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
450 ResizeCommandFactory factory = ResizeLatexCommand::factory;
451 format.command_transformers[Resize] =
452 TransformStore(Resize, factory);
454 } else if (transform == "Rotate" &&
455 transformer_class == "RotationLatexCommand") {
457 RotationCommandFactory factory = RotationLatexCommand::factory;
458 format.command_transformers[Rotate] =
459 TransformStore(Rotate, factory);
462 transform_not_found(lyxerr, transform);
465 transform_class_not_found(lyxerr, transformer_class);
469 void setOptionFactory(Template::Format & format, string const & transform,
470 string const & transformer_class)
472 bool class_found = false;
473 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
475 ClipOptionFactory factory = ClipLatexOption::factory;
476 format.option_transformers[Clip] =
477 TransformStore(Clip, factory);
479 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
481 ExtraOptionFactory factory = ExtraOption::factory;
482 format.option_transformers[Extra] =
483 TransformStore(Extra, factory);
485 } else if (transform == "Resize" &&
486 transformer_class == "ResizeLatexOption") {
488 ResizeOptionFactory factory = ResizeLatexOption::factory;
489 format.option_transformers[Resize] =
490 TransformStore(Resize, factory);
492 } else if (transform == "Rotate" &&
493 transformer_class == "RotationLatexOption") {
495 RotationOptionFactory factory = RotationLatexOption::factory;
496 format.option_transformers[Rotate] =
497 TransformStore(Rotate, factory);
500 transform_not_found(lyxerr, transform);
503 transform_class_not_found(lyxerr, transformer_class);
509 void Template::Format::readFormat(Lexer & lex)
524 LexerKeyword formattags[] = {
525 { "formatend", FO_END },
526 { "option", FO_OPTION },
527 { "preamble", FO_PREAMBLE },
528 { "product", FO_PRODUCT },
529 { "referencedfile", FO_REFERENCEDFILE },
530 { "requirement", FO_REQUIREMENT },
531 { "transformcommand", FO_TRANSFORMCOMMAND },
532 { "transformoption", FO_TRANSFORMOPTION },
533 { "updateformat", FO_UPDATEFORMAT },
534 { "updateresult", FO_UPDATERESULT }
537 PushPopHelper pph(lex, formattags);
543 product = lex.getString();
546 case FO_UPDATEFORMAT:
548 updateFormat = lex.getString();
551 case FO_UPDATERESULT:
553 updateResult = lex.getString();
558 requirements.push_back(lex.getString());
563 preambleNames.push_back(lex.getString());
566 case FO_TRANSFORMCOMMAND: {
568 string const name = lex.getString();
570 setCommandFactory(*this, name, lex.getString());
574 case FO_TRANSFORMOPTION: {
576 string const name = lex.getString();
578 setOptionFactory(*this, name, lex.getString());
584 string const name = lex.getString();
586 string const opt = lex.getString();
587 options.push_back(Option(name, opt));
591 case FO_REFERENCEDFILE: {
593 string const format = lex.getString();
595 string const file = lex.getString();
596 referencedFiles[format].push_back(file);
606 } // namespace external