2 * \file ExternalTemplate.C
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"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/package.h"
22 #include "support/path.h"
24 namespace support = lyx::support;
39 typedef Translator<TransformID, string> TransformIDTranslator;
40 TransformIDTranslator const & transformIDTranslator();
45 // We have to have dummy default commands for security reasons!
51 Template::Format::Format()
55 TemplateManager::TemplateManager()
57 readTemplates(support::package().user_support());
58 if (lyxerr.debugging(Debug::EXTERNAL)) {
59 dumpPreambleDefs(lyxerr);
61 dumpTemplates(lyxerr);
66 class dumpPreambleDef {
68 typedef TemplateManager::PreambleDefs::value_type value_type;
70 dumpPreambleDef(ostream & o) : ost(o) {}
72 void operator()(value_type const & vt) {
73 ost << "PreambleDef " << vt.first << '\n'
75 << "PreambleDefEnd" << endl;
85 typedef TemplateManager::Templates::value_type value_type;
87 dumpTemplate(ostream & o) : ost(o) {}
89 void operator()(value_type const & vt) {
90 Template const & et = vt.second;
92 ost << "Template " << et.lyxName << '\n'
93 << "\tGuiName " << et.guiName << '\n'
97 << "\tInputFormat " << et.inputFormat << '\n'
98 << "\tFileFilter " << et.fileRegExp << '\n'
99 << "\tAutomaticProduction " << et.automaticProduction << '\n';
101 typedef vector<TransformID> IDs;
102 IDs::const_iterator it = et.transformIds.begin();
103 IDs::const_iterator end = et.transformIds.end();
104 for (; it != end; ++it) {
105 ost << "\tTransform "
106 << transformIDTranslator().find(*it) << '\n';
110 ost << "TemplateEnd" << endl;
120 typedef Template::Formats::value_type value_type;
122 dumpFormat(ostream & o) : ost(o) {}
124 void operator()(value_type const & vt) const {
125 Template::Format const & ft = vt.second;
126 ost << "\tFormat " << vt.first << '\n'
127 << "\t\tProduct " << ft.product << '\n'
128 << "\t\tUpdateFormat " << ft.updateFormat << '\n'
129 << "\t\tUpdateResult " << ft.updateResult << '\n';
131 vector<string>::const_iterator qit = ft.requirements.begin();
132 vector<string>::const_iterator qend = ft.requirements.end();
133 for (; qit != qend; ++qit) {
134 lyxerr << "req:" << *qit << std::endl;
135 ost << "\t\tRequirement " << *qit << '\n';
138 typedef vector<Template::Option> Options;
139 Options::const_iterator oit = ft.options.begin();
140 Options::const_iterator oend = ft.options.end();
141 for (; oit != oend; ++oit) {
149 vector<string>::const_iterator pit = ft.preambleNames.begin();
150 vector<string>::const_iterator pend = ft.preambleNames.end();
151 for (; pit != pend; ++pit) {
152 ost << "\t\tPreamble " << *pit << '\n';
155 typedef Template::Format::FileMap FileMap;
156 FileMap::const_iterator rit = ft.referencedFiles.begin();
157 FileMap::const_iterator rend = ft.referencedFiles.end();
158 for (; rit != rend; ++rit) {
159 vector<string>::const_iterator fit = rit->second.begin();
160 vector<string>::const_iterator fend = rit->second.end();
161 for (; fit != fend; ++fit) {
162 ost << "\t\tReferencedFile " << rit->first
163 << " \"" << *fit << "\"\n";
167 ost << "\tFormatEnd\n";
174 void Template::dumpFormats(ostream & os) const
176 for_each(formats.begin(), formats.end(), dumpFormat(os));
180 void TemplateManager::dumpPreambleDefs(ostream & os) const
182 for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
186 void TemplateManager::dumpTemplates(ostream & os) const
188 for_each(templates.begin(), templates.end(), dumpTemplate(os));
192 TemplateManager & TemplateManager::get()
194 static TemplateManager externalTemplateManager;
195 return externalTemplateManager;
199 TemplateManager::Templates const &
200 TemplateManager::getTemplates() const
207 TemplateManager::getTemplateByName(string const & name) const
209 Templates::const_iterator it = templates.find(name);
210 return (it == templates.end()) ? 0 : &it->second;
215 TemplateManager::getPreambleDefByName(string const & name) const
217 string const trimmed_name = support::trim(name);
218 if (trimmed_name.empty())
221 PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
222 if (it == preambledefs.end())
229 void TemplateManager::readTemplates(string const & path)
231 support::Path p(path);
240 keyword_item templatetags[] = {
241 { "preambledef", TM_PREAMBLEDEF },
242 { "preambledefend", TM_PREAMBLEDEF_END },
243 { "template", TM_TEMPLATE },
244 { "templateend", TM_TEMPLATE_END }
247 LyXLex lex(templatetags, TM_TEMPLATE_END);
249 string filename = support::libFileSearch("", "external_templates");
250 if (filename.empty() || !lex.setFile(filename)) {
251 lex.printError("external::TemplateManager::readTemplates: "
256 char const * const preamble_end_tag =
257 templatetags[TM_PREAMBLEDEF_END-1].tag;
261 case TM_PREAMBLEDEF: {
263 string const name = lex.getString();
264 preambledefs[name] = lex.getLongString(preamble_end_tag);
270 string const name = lex.getString();
271 Template & tmp = templates[name];
273 tmp.readTemplate(lex);
277 case TM_TEMPLATE_END:
278 lex.printError("Warning: End outside Template.");
281 case TM_PREAMBLEDEF_END:
282 lex.printError("Warning: End outside PreambleDef.");
291 void add(vector<TransformID> & ids, string const & name)
293 TransformID id = transformIDTranslator().find(name);
295 lyxerr << "external::Template::readTemplate\n"
296 << "Transform " << name << " is not recognized"
306 void Template::readTemplate(LyXLex & lex)
308 enum TemplateOptionTags {
319 keyword_item 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 { "templateend", TO_END },
327 { "transform", TO_TRANSFORM }
330 pushpophelper pph(lex, templateoptiontags, TO_END);
336 guiName = lex.getString();
340 helpText = lex.getLongString("HelpTextEnd");
345 inputFormat = lex.getString();
350 fileRegExp = lex.getString();
355 automaticProduction = lex.getBool();
360 add(transformIds, lex.getString());
365 formats[lex.getString()].readFormat(lex);
372 lex.printError("external::Template::readTemplate: "
373 "Wrong tag: $$Token");
383 void transform_not_found(std::ostream & os, string const & transform)
385 os << "external::Format::readFormat. Transformation \""
386 << transform << "\" is unrecognized." << std::endl;
390 void transform_class_not_found(std::ostream & os, string const & tclass)
392 os << "external::Format::readFormat. Transformation class \""
393 << tclass << "\" is unrecognized." << std::endl;
397 void setCommandFactory(Template::Format & format, string const & transform,
398 string const & transformer_class)
400 bool class_found = false;
401 if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
403 ResizeCommandFactory factory = ResizeLatexCommand::factory;
404 format.command_transformers[Resize] =
405 TransformStore(Resize, factory);
407 } else if (transform == "Rotate" &&
408 transformer_class == "RotationLatexCommand") {
410 RotationCommandFactory factory = RotationLatexCommand::factory;
411 format.command_transformers[Rotate] =
412 TransformStore(Rotate, factory);
415 transform_not_found(lyxerr, transform);
418 transform_class_not_found(lyxerr, transformer_class);
422 void setOptionFactory(Template::Format & format, string const & transform,
423 string const & transformer_class)
425 bool class_found = false;
426 if (transform == "Clip" && transformer_class == "ClipLatexOption") {
428 ClipOptionFactory factory = ClipLatexOption::factory;
429 format.option_transformers[Clip] =
430 TransformStore(Clip, factory);
432 } else if (transform == "Extra" && transformer_class == "ExtraOption") {
434 ExtraOptionFactory factory = ExtraOption::factory;
435 format.option_transformers[Extra] =
436 TransformStore(Extra, factory);
438 } else if (transform == "Resize" &&
439 transformer_class == "ResizeLatexOption") {
441 ResizeOptionFactory factory = ResizeLatexOption::factory;
442 format.option_transformers[Resize] =
443 TransformStore(Resize, factory);
445 } else if (transform == "Rotate" &&
446 transformer_class == "RotationLatexOption") {
448 RotationOptionFactory factory = RotationLatexOption::factory;
449 format.option_transformers[Rotate] =
450 TransformStore(Rotate, factory);
453 transform_not_found(lyxerr, transform);
456 transform_class_not_found(lyxerr, transformer_class);
462 void Template::Format::readFormat(LyXLex & lex)
477 keyword_item formattags[] = {
478 { "formatend", FO_END },
479 { "option", FO_OPTION },
480 { "preamble", FO_PREAMBLE },
481 { "product", FO_PRODUCT },
482 { "referencedfile", FO_REFERENCEDFILE },
483 { "requirement", FO_REQUIREMENT },
484 { "transformcommand", FO_TRANSFORMCOMMAND },
485 { "transformoption", FO_TRANSFORMOPTION },
486 { "updateformat", FO_UPDATEFORMAT },
487 { "updateresult", FO_UPDATERESULT }
490 pushpophelper pph(lex, formattags, FO_END);
496 product = lex.getString();
499 case FO_UPDATEFORMAT:
501 updateFormat = lex.getString();
504 case FO_UPDATERESULT:
506 updateResult = lex.getString();
511 requirements.push_back(lex.getString());
516 preambleNames.push_back(lex.getString());
519 case FO_TRANSFORMCOMMAND: {
521 string const name = lex.getString();
523 setCommandFactory(*this, name, lex.getString());
527 case FO_TRANSFORMOPTION: {
529 string const name = lex.getString();
531 setOptionFactory(*this, name, lex.getString());
537 string const name = lex.getString();
539 string const opt = lex.getString();
540 options.push_back(Option(name, opt));
544 case FO_REFERENCEDFILE: {
546 string const format = lex.getString();
548 string const file = lex.getString();
549 referencedFiles[format].push_back(file);
561 TransformIDTranslator const initIDTranslator()
563 TransformIDTranslator translator(TransformID(-1), "");
564 translator.addPair(Rotate, "Rotate");
565 translator.addPair(Resize, "Resize");
566 translator.addPair(Clip, "Clip");
567 translator.addPair(Extra, "Extra");
572 TransformIDTranslator const & transformIDTranslator()
574 static TransformIDTranslator const translator = initIDTranslator();
580 } // namespace external