]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
ws changes only
[lyx.git] / src / insets / ExternalTemplate.C
1 /**
2  * \file ExternalTemplate.C
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 "debug.h"
17 #include "lyxlex.h"
18
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21 #include "support/path.h"
22 #include "support/path_defines.h"
23 #include "support/translator.h"
24
25 #include <boost/assert.hpp>
26
27 #include <algorithm>
28
29 namespace support = lyx::support;
30
31 using std::endl;
32 using std::for_each;
33
34 using std::string;
35 using std::ostream;
36 using std::vector;
37
38
39 namespace lyx {
40 namespace external {
41
42 namespace {
43
44 typedef Translator<TransformID, string> TransformIDTranslator;
45 TransformIDTranslator const & transformIDTranslator();
46
47 } // namespace anon
48
49
50 // We have to have dummy default commands for security reasons!
51 Template::Template()
52         : inputFormat("*")
53 {}
54
55
56 Template::Format::Format()
57 {}
58
59
60 TemplateManager::TemplateManager()
61 {
62         readTemplates(support::user_lyxdir());
63         if (lyxerr.debugging(Debug::EXTERNAL)) {
64                 dumpPreambleDefs(lyxerr);
65                 lyxerr << '\n';
66                 dumpTemplates(lyxerr);
67         }
68 }
69
70
71 class dumpPreambleDef {
72 public:
73         typedef TemplateManager::PreambleDefs::value_type value_type;
74
75         dumpPreambleDef(ostream & o) : ost(o) {}
76
77         void operator()(value_type const & vt) {
78                 ost << "PreambleDef " << vt.first << '\n'
79                     << vt.second
80                     << "PreambleDefEnd" << endl;
81         }
82
83 private:
84         ostream & ost;
85 };
86
87
88 class dumpTemplate {
89 public:
90         typedef TemplateManager::Templates::value_type value_type;
91
92         dumpTemplate(ostream & o) : ost(o) {}
93
94         void operator()(value_type const & vt) {
95                 Template const & et = vt.second;
96
97                 ost << "Template " << et.lyxName << '\n'
98                     << "\tGuiName " << et.guiName << '\n'
99                     << "\tHelpText\n"
100                     << et.helpText
101                     << "\tHelpTextEnd\n"
102                     << "\tInputFormat " << et.inputFormat << '\n'
103                     << "\tFileFilter " << et.fileRegExp << '\n'
104                     << "\tEditCommand " << et.editCommand << '\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                         ost << "\tTransform "
112                             << transformIDTranslator().find(*it) << '\n';
113                 }
114
115                 et.dumpFormats(ost);
116                 ost << "TemplateEnd" << endl;
117
118         }
119
120 private:
121         ostream & ost;
122 };
123
124 class dumpFormat {
125 public:
126         typedef Template::Formats::value_type value_type;
127
128         dumpFormat(ostream & o) : ost(o) {}
129
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'
136                     << "\t\tRequirement " << ft.requirement << '\n';
137
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) {
142                         ost << "\t\tOption "
143                             << oit->name
144                             << ": "
145                             << oit->option
146                             << '\n';
147                 }
148
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';
153                 }
154
155                 ost << "\tFormatEnd\n";
156         }
157 private:
158         ostream & ost;
159 };
160
161
162 void Template::dumpFormats(ostream & os) const
163 {
164         for_each(formats.begin(), formats.end(), dumpFormat(os));
165 }
166
167
168 void TemplateManager::dumpPreambleDefs(ostream & os) const
169 {
170         for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
171 }
172
173
174 void TemplateManager::dumpTemplates(ostream & os) const
175 {
176         for_each(templates.begin(), templates.end(), dumpTemplate(os));
177 }
178
179
180 TemplateManager & TemplateManager::get()
181 {
182         static TemplateManager externalTemplateManager;
183         return externalTemplateManager;
184 }
185
186
187 TemplateManager::Templates const &
188 TemplateManager::getTemplates() const
189 {
190         return templates;
191 }
192
193
194 Template const *
195 TemplateManager::getTemplateByName(string const & name) const
196 {
197         Templates::const_iterator it = templates.find(name);
198         return (it == templates.end()) ? 0 : &it->second;
199 }
200
201
202 string const
203 TemplateManager::getPreambleDefByName(string const & name) const
204 {
205         string const trimmed_name = support::trim(name);
206         if (trimmed_name.empty())
207                 return string();
208
209         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
210         if (it == preambledefs.end())
211                 return string();
212
213         return it->second;
214 }
215
216
217 void TemplateManager::readTemplates(string const & path)
218 {
219         support::Path p(path);
220
221         enum TemplateTags {
222                 TM_PREAMBLEDEF = 1,
223                 TM_PREAMBLEDEF_END,
224                 TM_TEMPLATE,
225                 TM_TEMPLATE_END
226         };
227
228         keyword_item templatetags[] = {
229                 { "preambledef", TM_PREAMBLEDEF },
230                 { "preambledefend", TM_PREAMBLEDEF_END },
231                 { "template", TM_TEMPLATE },
232                 { "templateend", TM_TEMPLATE_END }
233         };
234
235         LyXLex lex(templatetags, TM_TEMPLATE_END);
236
237         string filename = support::LibFileSearch("", "external_templates");
238         if (filename.empty() || !lex.setFile(filename)) {
239                 lex.printError("external::TemplateManager::readTemplates: "
240                                "No template file");
241                 return;
242         }
243
244         char const * const preamble_end_tag =
245                 templatetags[TM_PREAMBLEDEF_END-1].tag;
246
247         while (lex.isOK()) {
248                 switch (lex.lex()) {
249                 case TM_PREAMBLEDEF: {
250                         lex.next();
251                         string const name = lex.getString();
252                         preambledefs[name] = lex.getLongString(preamble_end_tag);
253                 }
254                 break;
255
256                 case TM_TEMPLATE: {
257                         lex.next();
258                         string const name = lex.getString();
259                         Template & tmp = templates[name];
260                         tmp.lyxName = name;
261                         tmp.readTemplate(lex);
262                 }
263                 break;
264
265                 case TM_TEMPLATE_END:
266                         lex.printError("Warning: End outside Template.");
267                 break;
268
269                 case TM_PREAMBLEDEF_END:
270                         lex.printError("Warning: End outside PreambleDef.");
271                 break;
272                 }
273         }
274 }
275
276
277 namespace {
278
279 void add(vector<TransformID> & ids, string const & name)
280 {
281         TransformID id = transformIDTranslator().find(name);
282         if (int(id) == -1) {
283                 lyxerr << "external::Template::readTemplate\n"
284                        << "Transform " << name << " is not recognized"
285                        << std::endl;
286         } else {
287                 ids.push_back(id);
288         }
289 }
290
291 } // namespace anon
292
293
294 void Template::readTemplate(LyXLex & lex)
295 {
296         enum TemplateOptionTags {
297                 TO_GUINAME = 1,
298                 TO_HELPTEXT,
299                 TO_INPUTFORMAT,
300                 TO_FILTER,
301                 TO_EDITCMD,
302                 TO_AUTOMATIC,
303                 TO_TRANSFORM,
304                 TO_FORMAT,
305                 TO_END
306         };
307
308         keyword_item templateoptiontags[] = {
309                 { "automaticproduction", TO_AUTOMATIC },
310                 { "editcommand", TO_EDITCMD },
311                 { "filefilter", TO_FILTER },
312                 { "format", TO_FORMAT },
313                 { "guiname", TO_GUINAME },
314                 { "helptext", TO_HELPTEXT },
315                 { "inputformat", TO_INPUTFORMAT },
316                 { "templateend", TO_END },
317                 { "transform", TO_TRANSFORM }
318         };
319
320         pushpophelper pph(lex, templateoptiontags, TO_END);
321
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_EDITCMD:
344                         lex.next(true);
345                         editCommand = lex.getString();
346                         break;
347
348                 case TO_AUTOMATIC:
349                         lex.next();
350                         automaticProduction = lex.getBool();
351                         break;
352
353                 case TO_TRANSFORM:
354                         lex.next(true);
355                         add(transformIds, lex.getString());
356                         break;
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                         BOOST_ASSERT(false);
370                         break;
371                 }
372         }
373 }
374
375
376 namespace {
377
378 void transform_not_found(std::ostream & os, string const & transform)
379 {
380         os << "external::Format::readFormat. Transformation \""
381            << transform << "\" is unrecognized." << std::endl;
382 }
383
384
385 void transform_class_not_found(std::ostream & os, string const & tclass)
386 {
387         os << "external::Format::readFormat. Transformation class \""
388            << tclass << "\" is unrecognized." << std::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(LyXLex & lex)
458 {
459         enum FormatTags {
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_END
469         };
470
471         keyword_item formattags[] = {
472                 { "formatend", FO_END },
473                 { "option", FO_OPTION },
474                 { "preamble", FO_PREAMBLE },
475                 { "product", FO_PRODUCT },
476                 { "requirement", FO_REQUIREMENT },
477                 { "transformcommand", FO_TRANSFORMCOMMAND },
478                 { "transformoption", FO_TRANSFORMOPTION },
479                 { "updateformat", FO_UPDATEFORMAT },
480                 { "updateresult", FO_UPDATERESULT }
481         };
482
483         pushpophelper pph(lex, formattags, FO_END);
484
485         while (lex.isOK()) {
486                 switch (lex.lex()) {
487                 case FO_PRODUCT:
488                         lex.next(true);
489                         product = lex.getString();
490                         break;
491
492                 case FO_UPDATEFORMAT:
493                         lex.next(true);
494                         updateFormat = lex.getString();
495                         break;
496
497                 case FO_UPDATERESULT:
498                         lex.next(true);
499                         updateResult = lex.getString();
500                         break;
501
502                 case FO_REQUIREMENT:
503                         lex.next(true);
504                         requirement = lex.getString();
505                         break;
506
507                 case FO_PREAMBLE:
508                         lex.next(true);
509                         preambleNames.push_back(lex.getString());
510                         break;
511
512                 case FO_TRANSFORMCOMMAND: {
513                         lex.next(true);
514                         string const name = lex.getString();
515                         lex.next(true);
516                         setCommandFactory(*this, name, lex.getString());
517                         break;
518                 }
519
520                 case FO_TRANSFORMOPTION: {
521                         lex.next(true);
522                         string const name = lex.getString();
523                         lex.next(true);
524                         setOptionFactory(*this, name, lex.getString());
525                         break;
526                 }
527
528                 case FO_OPTION: {
529                         lex.next(true);
530                         string const name = lex.getString();
531                         lex.next(true);
532                         string const opt = lex.getString();
533                         options.push_back(Option(name, opt));
534                         break;
535                 }
536
537                 case FO_END:
538                         return;
539                 }
540         }
541 }
542
543 namespace {
544
545 TransformIDTranslator const initIDTranslator()
546 {
547         TransformIDTranslator translator(TransformID(-1), "");
548         translator.addPair(Rotate, "Rotate");
549         translator.addPair(Resize, "Resize");
550         translator.addPair(Clip,   "Clip");
551         translator.addPair(Extra,  "Extra");
552         return translator;
553 }
554
555
556 TransformIDTranslator const & transformIDTranslator()
557 {
558         static TransformIDTranslator const translator = initIDTranslator();
559         return translator;
560 }
561
562 } // namespace anon
563
564 } // namespace external
565 } // namespace lyx