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"
16 #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 & o) : ost(o) {}
78 void operator()(value_type const & vt) {
79 ost << "PreambleDef " << vt.first << '\n'
81 << "PreambleDefEnd" << endl;
91 typedef TemplateManager::Templates::value_type value_type;
93 DumpTemplate(ostream & o) : ost(o) {}
95 void operator()(value_type const & vt) {
96 Template const & et = vt.second;
98 ost << "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 typedef vector<TransformID> IDs;
108 IDs::const_iterator it = et.transformIds.begin();
109 IDs::const_iterator end = et.transformIds.end();
110 for (; it != end; ++it) {
111 ost << "\tTransform "
112 << transformIDTranslator().find(*it) << '\n';
116 ost << "TemplateEnd" << endl;
126 typedef Template::Formats::value_type value_type;
128 DumpFormat(ostream & o) : ost(o) {}
130 void operator()(value_type const & vt) const {
131 Template::Format const & ft = vt.second;
132 ost << "\tFormat " << vt.first << '\n'
133 << "\t\tProduct " << ft.product << '\n'
134 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
135 << "\t\tUpdateResult " << ft.updateResult << '\n';
137 vector<string>::const_iterator qit = ft.requirements.begin();
138 vector<string>::const_iterator qend = ft.requirements.end();
139 for (; qit != qend; ++qit) {
140 lyxerr << "req:" << *qit << endl;
141 ost << "\t\tRequirement " << *qit << '\n';
144 typedef vector<Template::Option> Options;
145 Options::const_iterator oit = ft.options.begin();
146 Options::const_iterator oend = ft.options.end();
147 for (; oit != oend; ++oit) {
155 vector<string>::const_iterator pit = ft.preambleNames.begin();
156 vector<string>::const_iterator pend = ft.preambleNames.end();
157 for (; pit != pend; ++pit) {
158 ost << "\t\tPreamble " << *pit << '\n';
161 typedef Template::Format::FileMap FileMap;
162 FileMap::const_iterator rit = ft.referencedFiles.begin();
163 FileMap::const_iterator rend = ft.referencedFiles.end();
164 for (; rit != rend; ++rit) {
165 vector<string>::const_iterator fit = rit->second.begin();
166 vector<string>::const_iterator fend = rit->second.end();
167 for (; fit != fend; ++fit) {
168 ost << "\t\tReferencedFile " << rit->first
169 << " \"" << *fit << "\"\n";
173 ost << "\tFormatEnd\n";
180 void Template::dumpFormats(ostream & os) const
182 for_each(formats.begin(), formats.end(), DumpFormat(os));
186 void TemplateManager::dumpPreambleDefs(ostream & os) const
188 for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
192 void TemplateManager::dumpTemplates(ostream & os) const
194 for_each(templates.begin(), templates.end(), DumpTemplate(os));
198 TemplateManager & TemplateManager::get()
200 static TemplateManager externalTemplateManager;
201 return externalTemplateManager;
205 TemplateManager::Templates const &
206 TemplateManager::getTemplates() const
213 TemplateManager::getTemplateByName(string const & name) const
215 Templates::const_iterator it = templates.find(name);
216 return (it == templates.end()) ? 0 : &it->second;
221 TemplateManager::getPreambleDefByName(string const & name) const
223 string const trimmed_name = trim(name);
224 if (trimmed_name.empty())
227 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
228 if (it == preambledefs.end())
235 void TemplateManager::readTemplates(FileName const & path)
246 keyword_item templatetags[] = {
247 { "preambledef", TM_PREAMBLEDEF },
248 { "preambledefend", TM_PREAMBLEDEF_END },
249 { "template", TM_TEMPLATE },
250 { "templateend", TM_TEMPLATE_END }
253 Lexer lex(templatetags, TM_TEMPLATE_END);
255 FileName const filename = libFileSearch("", "external_templates");
256 if (filename.empty() || !lex.setFile(filename)) {
257 lex.printError("external::TemplateManager::readTemplates: "
262 char const * const preamble_end_tag =
263 templatetags[TM_PREAMBLEDEF_END-1].tag;
267 case TM_PREAMBLEDEF: {
269 string const name = lex.getString();
270 preambledefs[name] = lex.getLongString(preamble_end_tag);
276 string const name = lex.getString();
277 Template & tmp = templates[name];
279 tmp.readTemplate(lex);
283 case TM_TEMPLATE_END:
284 lex.printError("Warning: End outside Template.");
287 case TM_PREAMBLEDEF_END:
288 lex.printError("Warning: End outside PreambleDef.");
297 void add(vector<TransformID> & ids, string const & name)
299 TransformID id = transformIDTranslator().find(name);
301 lyxerr << "external::Template::readTemplate\n"
302 << "Transform " << name << " is not recognized"
312 void Template::readTemplate(Lexer & lex)
314 enum TemplateOptionTags {
325 keyword_item templateoptiontags[] = {
326 { "automaticproduction", TO_AUTOMATIC },
327 { "filefilter", TO_FILTER },
328 { "format", TO_FORMAT },
329 { "guiname", TO_GUINAME },
330 { "helptext", TO_HELPTEXT },
331 { "inputformat", TO_INPUTFORMAT },
332 { "templateend", TO_END },
333 { "transform", TO_TRANSFORM }
336 PushPopHelper pph(lex, templateoptiontags, TO_END);
342 guiName = lex.getString();
346 helpText = lex.getLongString("HelpTextEnd");
351 inputFormat = lex.getString();
356 fileRegExp = lex.getString();
361 automaticProduction = lex.getBool();
366 add(transformIds, lex.getString());
371 formats[lex.getString()].readFormat(lex);
378 lex.printError("external::Template::readTemplate: "
379 "Wrong tag: $$Token");
389 void transform_not_found(ostream & os, string const & transform)
391 os << "external::Format::readFormat. Transformation \""
392 << transform << "\" is unrecognized." << endl;
396 void transform_class_not_found(ostream & os, string const & tclass)
398 os << "external::Format::readFormat. Transformation class \""
399 << tclass << "\" is unrecognized." << endl;
403 void setCommandFactory(Template::Format & format, string const & transform,
404 string const & transformer_class)
406 bool class_found = false;
407 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
409 ResizeCommandFactory factory = ResizeLatexCommand::factory;
410 format.command_transformers[Resize] =
411 TransformStore(Resize, factory);
413 } else if (transform == "Rotate" &&
414 transformer_class == "RotationLatexCommand") {
416 RotationCommandFactory factory = RotationLatexCommand::factory;
417 format.command_transformers[Rotate] =
418 TransformStore(Rotate, factory);
421 transform_not_found(lyxerr, transform);
424 transform_class_not_found(lyxerr, transformer_class);
428 void setOptionFactory(Template::Format & format, string const & transform,
429 string const & transformer_class)
431 bool class_found = false;
432 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
434 ClipOptionFactory factory = ClipLatexOption::factory;
435 format.option_transformers[Clip] =
436 TransformStore(Clip, factory);
438 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
440 ExtraOptionFactory factory = ExtraOption::factory;
441 format.option_transformers[Extra] =
442 TransformStore(Extra, factory);
444 } else if (transform == "Resize" &&
445 transformer_class == "ResizeLatexOption") {
447 ResizeOptionFactory factory = ResizeLatexOption::factory;
448 format.option_transformers[Resize] =
449 TransformStore(Resize, factory);
451 } else if (transform == "Rotate" &&
452 transformer_class == "RotationLatexOption") {
454 RotationOptionFactory factory = RotationLatexOption::factory;
455 format.option_transformers[Rotate] =
456 TransformStore(Rotate, factory);
459 transform_not_found(lyxerr, transform);
462 transform_class_not_found(lyxerr, transformer_class);
468 void Template::Format::readFormat(Lexer & lex)
483 keyword_item formattags[] = {
484 { "formatend", FO_END },
485 { "option", FO_OPTION },
486 { "preamble", FO_PREAMBLE },
487 { "product", FO_PRODUCT },
488 { "referencedfile", FO_REFERENCEDFILE },
489 { "requirement", FO_REQUIREMENT },
490 { "transformcommand", FO_TRANSFORMCOMMAND },
491 { "transformoption", FO_TRANSFORMOPTION },
492 { "updateformat", FO_UPDATEFORMAT },
493 { "updateresult", FO_UPDATERESULT }
496 PushPopHelper pph(lex, formattags, FO_END);
502 product = lex.getString();
505 case FO_UPDATEFORMAT:
507 updateFormat = lex.getString();
510 case FO_UPDATERESULT:
512 updateResult = lex.getString();
517 requirements.push_back(lex.getString());
522 preambleNames.push_back(lex.getString());
525 case FO_TRANSFORMCOMMAND: {
527 string const name = lex.getString();
529 setCommandFactory(*this, name, lex.getString());
533 case FO_TRANSFORMOPTION: {
535 string const name = lex.getString();
537 setOptionFactory(*this, name, lex.getString());
543 string const name = lex.getString();
545 string const opt = lex.getString();
546 options.push_back(Option(name, opt));
550 case FO_REFERENCEDFILE: {
552 string const format = lex.getString();
554 string const file = lex.getString();
555 referencedFiles[format].push_back(file);
565 } // namespace external