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