]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.cpp
Inset::addToToc(): change signature. Use DocIterator instead of ParConstIterator...
[lyx.git] / src / insets / ExternalTemplate.cpp
1 /**
2  * \file ExternalTemplate.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup Nielsen
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ExternalTemplate.h"
15
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/Package.h"
22 #include "support/Path.h"
23
24 #include <ostream>
25
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30 namespace external {
31
32
33 typedef Translator<TransformID, string> TransformIDTranslator;
34
35 static TransformIDTranslator const initIDTranslator()
36 {
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");
42         return translator;
43 }
44
45 static TransformIDTranslator const & transformIDTranslator()
46 {
47         static TransformIDTranslator const translator = initIDTranslator();
48         return translator;
49 }
50
51 // We have to have dummy default commands for security reasons!
52 Template::Template()
53         : inputFormat("*")
54 {}
55
56
57 Template::Format::Format()
58 {}
59
60
61 TemplateManager::TemplateManager()
62 {
63         readTemplates(package().user_support());
64         if (lyxerr.debugging(Debug::EXTERNAL)) {
65                 dumpPreambleDefs(lyxerr);
66                 lyxerr << '\n';
67                 dumpTemplates(lyxerr);
68         }
69 }
70
71
72 class DumpPreambleDef {
73 public:
74         typedef TemplateManager::PreambleDefs::value_type value_type;
75
76         DumpPreambleDef(ostream & os) : os_(os) {}
77
78         void operator()(value_type const & vt) {
79                 os_ << "PreambleDef " << vt.first << '\n'
80                     << vt.second
81                     << "PreambleDefEnd" << endl;
82         }
83
84 private:
85         ostream & os_;
86 };
87
88
89 class DumpTemplate {
90 public:
91         typedef TemplateManager::Templates::value_type value_type;
92
93         DumpTemplate(ostream & os) : os_(os) {}
94
95         void operator()(value_type const & vt) {
96                 Template const & et = vt.second;
97
98                 os_ << "Template " << et.lyxName << '\n'
99                     << "\tGuiName " << et.guiName << '\n'
100                     << "\tHelpText\n"
101                     << et.helpText
102                     << "\tHelpTextEnd\n"
103                     << "\tInputFormat " << et.inputFormat << '\n'
104                     << "\tFileFilter " << et.fileRegExp << '\n'
105                     << "\tAutomaticProduction " << et.automaticProduction << '\n';
106
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                         os_ << "\tTransform "
112                             << transformIDTranslator().find(*it) << '\n';
113                 }
114
115                 et.dumpFormats(os_);
116                 os_ << "TemplateEnd" << endl;
117
118         }
119
120 private:
121         ostream & os_;
122 };
123
124 class DumpFormat {
125 public:
126         typedef Template::Formats::value_type value_type;
127
128         DumpFormat(ostream & o) : os_(o) {}
129
130         void operator()(value_type const & vt) const {
131                 Template::Format const & ft = vt.second;
132                 os_ << "\tFormat " << vt.first << '\n'
133                     << "\t\tProduct " << ft.product << '\n'
134                     << "\t\tUpdateFormat " << ft.updateFormat << '\n'
135                     << "\t\tUpdateResult " << ft.updateResult << '\n';
136
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                         os_ << "\t\tRequirement " << *qit << '\n';
142                 }
143
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) {
148                         os_ << "\t\tOption "
149                             << oit->name
150                             << ": "
151                             << oit->option
152                             << '\n';
153                 }
154
155                 vector<string>::const_iterator pit  = ft.preambleNames.begin();
156                 vector<string>::const_iterator pend = ft.preambleNames.end();
157                 for (; pit != pend; ++pit) {
158                         os_ << "\t\tPreamble " << *pit << '\n';
159                 }
160
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                                 os_ << "\t\tReferencedFile " << rit->first
169                                     << " \"" << *fit << "\"\n";
170                         }
171                 }
172
173                 os_ << "\tFormatEnd\n";
174         }
175 private:
176         ostream & os_;
177 };
178
179
180 void Template::dumpFormats(ostream & os) const
181 {
182         for_each(formats.begin(), formats.end(), DumpFormat(os));
183 }
184
185
186 void TemplateManager::dumpPreambleDefs(ostream & os) const
187 {
188         for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
189 }
190
191
192 void TemplateManager::dumpTemplates(ostream & os) const
193 {
194         for_each(templates.begin(), templates.end(), DumpTemplate(os));
195 }
196
197
198 TemplateManager & TemplateManager::get()
199 {
200         static TemplateManager externalTemplateManager;
201         return externalTemplateManager;
202 }
203
204
205 TemplateManager::Templates const & TemplateManager::getTemplates() const
206 {
207         return templates;
208 }
209
210
211 Template const *
212 TemplateManager::getTemplateByName(string const & name) const
213 {
214         Templates::const_iterator it = templates.find(name);
215         return (it == templates.end()) ? 0 : &it->second;
216 }
217
218
219 string const
220 TemplateManager::getPreambleDefByName(string const & name) const
221 {
222         string const trimmed_name = trim(name);
223         if (trimmed_name.empty())
224                 return string();
225
226         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
227         if (it == preambledefs.end())
228                 return string();
229
230         return it->second;
231 }
232
233
234 void TemplateManager::readTemplates(FileName const & path)
235 {
236         PathChanger p(path);
237
238         enum {
239                 TM_PREAMBLEDEF = 1,
240                 TM_PREAMBLEDEF_END,
241                 TM_TEMPLATE,
242                 TM_TEMPLATE_END
243         };
244
245         LexerKeyword templatetags[] = {
246                 { "preambledef", TM_PREAMBLEDEF },
247                 { "preambledefend", TM_PREAMBLEDEF_END },
248                 { "template", TM_TEMPLATE },
249                 { "templateend", TM_TEMPLATE_END }
250         };
251
252         Lexer lex(templatetags);
253
254         FileName const filename = libFileSearch("", "external_templates");
255         if (filename.empty() || !lex.setFile(filename)) {
256                 lex.printError("external::TemplateManager::readTemplates: "
257                                "No template file");
258                 return;
259         }
260
261         char const * const preamble_end_tag =
262                 templatetags[TM_PREAMBLEDEF_END-1].tag;
263
264         while (lex.isOK()) {
265                 switch (lex.lex()) {
266                 case TM_PREAMBLEDEF: {
267                         lex.next();
268                         string const name = lex.getString();
269                         preambledefs[name] = lex.getLongString(preamble_end_tag);
270                 }
271                 break;
272
273                 case TM_TEMPLATE: {
274                         lex.next();
275                         string const name = lex.getString();
276                         Template & tmp = templates[name];
277                         tmp.lyxName = name;
278                         tmp.readTemplate(lex);
279                 }
280                 break;
281
282                 case TM_TEMPLATE_END:
283                         lex.printError("Warning: End outside Template.");
284                 break;
285
286                 case TM_PREAMBLEDEF_END:
287                         lex.printError("Warning: End outside PreambleDef.");
288                 break;
289                 }
290         }
291 }
292
293
294 void Template::readTemplate(Lexer & lex)
295 {
296         enum {
297                 TO_GUINAME = 1,
298                 TO_HELPTEXT,
299                 TO_INPUTFORMAT,
300                 TO_FILTER,
301                 TO_AUTOMATIC,
302                 TO_TRANSFORM,
303                 TO_FORMAT,
304                 TO_END
305         };
306
307         LexerKeyword templateoptiontags[] = {
308                 { "automaticproduction", TO_AUTOMATIC },
309                 { "filefilter", TO_FILTER },
310                 { "format", TO_FORMAT },
311                 { "guiname", TO_GUINAME },
312                 { "helptext", TO_HELPTEXT },
313                 { "inputformat", TO_INPUTFORMAT },
314                 { "templateend", TO_END },
315                 { "transform", TO_TRANSFORM }
316         };
317
318         PushPopHelper pph(lex, templateoptiontags);
319         lex.setContext("Template::readTemplate");
320
321         string token;
322         while (lex.isOK()) {
323                 switch (lex.lex()) {
324                 case TO_GUINAME:
325                         lex.next(true);
326                         guiName = lex.getString();
327                         break;
328
329                 case TO_HELPTEXT:
330                         helpText = lex.getLongString("HelpTextEnd");
331                         break;
332
333                 case TO_INPUTFORMAT:
334                         lex.next(true);
335                         inputFormat = lex.getString();
336                         break;
337
338                 case TO_FILTER:
339                         lex.next(true);
340                         fileRegExp = lex.getString();
341                         break;
342
343                 case TO_AUTOMATIC:
344                         lex.next();
345                         automaticProduction = lex.getBool();
346                         break;
347
348                 case TO_TRANSFORM: {
349                         lex >> token;
350                         TransformID id = transformIDTranslator().find(token);
351                         if (int(id) == -1)
352                                 LYXERR0("Transform " << token << " is not recognized");
353                         else
354                                 transformIds.push_back(id);
355                         break;
356                 }
357
358                 case TO_FORMAT:
359                         lex.next(true);
360                         formats[lex.getString()].readFormat(lex);
361                         break;
362
363                 case TO_END:
364                         return;
365
366                 default:
367                         lex.printError("external::Template::readTemplate: "
368                                        "Wrong tag: $$Token");
369                         LASSERT(false, /**/);
370                         break;
371                 }
372         }
373 }
374
375
376 namespace {
377
378 void transform_not_found(ostream & os, string const & transform)
379 {
380         os << "external::Format::readFormat. Transformation \""
381            << transform << "\" is unrecognized." << endl;
382 }
383
384
385 void transform_class_not_found(ostream & os, string const & tclass)
386 {
387         os << "external::Format::readFormat. Transformation class \""
388            << tclass << "\" is unrecognized." << endl;
389 }
390
391
392 void setCommandFactory(Template::Format & format, string const & transform,
393                        string const & transformer_class)
394 {
395         bool class_found = false;
396         if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
397                 class_found = true;
398                 ResizeCommandFactory factory = ResizeLatexCommand::factory;
399                 format.command_transformers[Resize] =
400                         TransformStore(Resize, factory);
401
402         } else if (transform == "Rotate" &&
403                    transformer_class == "RotationLatexCommand") {
404                 class_found = true;
405                 RotationCommandFactory factory = RotationLatexCommand::factory;
406                 format.command_transformers[Rotate] =
407                         TransformStore(Rotate, factory);
408
409         } else
410                 transform_not_found(lyxerr, transform);
411
412         if (!class_found)
413                 transform_class_not_found(lyxerr, transformer_class);
414 }
415
416
417 void setOptionFactory(Template::Format & format, string const & transform,
418                 string const & transformer_class)
419 {
420         bool class_found = false;
421         if (transform == "Clip" && transformer_class == "ClipLatexOption") {
422                 class_found = true;
423                 ClipOptionFactory factory = ClipLatexOption::factory;
424                 format.option_transformers[Clip] =
425                                 TransformStore(Clip, factory);
426
427         } else if (transform == "Extra" && transformer_class == "ExtraOption") {
428                 class_found = true;
429                 ExtraOptionFactory factory = ExtraOption::factory;
430                 format.option_transformers[Extra] =
431                         TransformStore(Extra, factory);
432
433         } else if (transform == "Resize" &&
434                    transformer_class == "ResizeLatexOption") {
435                 class_found = true;
436                 ResizeOptionFactory factory = ResizeLatexOption::factory;
437                 format.option_transformers[Resize] =
438                         TransformStore(Resize, factory);
439
440         } else if (transform == "Rotate" &&
441                    transformer_class == "RotationLatexOption") {
442                 class_found = true;
443                 RotationOptionFactory factory = RotationLatexOption::factory;
444                 format.option_transformers[Rotate] =
445                         TransformStore(Rotate, factory);
446
447         } else
448                 transform_not_found(lyxerr, transform);
449
450         if (!class_found)
451                 transform_class_not_found(lyxerr, transformer_class);
452 }
453
454 } // namespace anon
455
456
457 void Template::Format::readFormat(Lexer & lex)
458 {
459         enum {
460                 FO_PRODUCT = 1,
461                 FO_UPDATEFORMAT,
462                 FO_UPDATERESULT,
463                 FO_REQUIREMENT,
464                 FO_OPTION,
465                 FO_PREAMBLE,
466                 FO_TRANSFORMCOMMAND,
467                 FO_TRANSFORMOPTION,
468                 FO_REFERENCEDFILE,
469                 FO_END
470         };
471
472         LexerKeyword formattags[] = {
473                 { "formatend", FO_END },
474                 { "option", FO_OPTION },
475                 { "preamble", FO_PREAMBLE },
476                 { "product", FO_PRODUCT },
477                 { "referencedfile", FO_REFERENCEDFILE },
478                 { "requirement", FO_REQUIREMENT },
479                 { "transformcommand", FO_TRANSFORMCOMMAND },
480                 { "transformoption", FO_TRANSFORMOPTION },
481                 { "updateformat", FO_UPDATEFORMAT },
482                 { "updateresult", FO_UPDATERESULT }
483         };
484
485         PushPopHelper pph(lex, formattags);
486
487         while (lex.isOK()) {
488                 switch (lex.lex()) {
489                 case FO_PRODUCT:
490                         lex.next(true);
491                         product = lex.getString();
492                         break;
493
494                 case FO_UPDATEFORMAT:
495                         lex.next(true);
496                         updateFormat = lex.getString();
497                         break;
498
499                 case FO_UPDATERESULT:
500                         lex.next(true);
501                         updateResult = lex.getString();
502                         break;
503
504                 case FO_REQUIREMENT:
505                         lex.next(true);
506                         requirements.push_back(lex.getString());
507                         break;
508
509                 case FO_PREAMBLE:
510                         lex.next(true);
511                         preambleNames.push_back(lex.getString());
512                         break;
513
514                 case FO_TRANSFORMCOMMAND: {
515                         lex.next(true);
516                         string const name = lex.getString();
517                         lex.next(true);
518                         setCommandFactory(*this, name, lex.getString());
519                         break;
520                 }
521
522                 case FO_TRANSFORMOPTION: {
523                         lex.next(true);
524                         string const name = lex.getString();
525                         lex.next(true);
526                         setOptionFactory(*this, name, lex.getString());
527                         break;
528                 }
529
530                 case FO_OPTION: {
531                         lex.next(true);
532                         string const name = lex.getString();
533                         lex.next(true);
534                         string const opt = lex.getString();
535                         options.push_back(Option(name, opt));
536                         break;
537                 }
538
539                 case FO_REFERENCEDFILE: {
540                         lex.next(true);
541                         string const format = lex.getString();
542                         lex.next(true);
543                         string const file = lex.getString();
544                         referencedFiles[format].push_back(file);
545                         break;
546                 }
547
548                 case FO_END:
549                         return;
550                 }
551         }
552 }
553
554 } // namespace external
555 } // namespace lyx