]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.cpp
ExternalTemplate.cpp:
[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                     << "\tPreview ";
107                 switch (et.preview_mode) {
108                         case PREVIEW_OFF:
109                                 os_ << "Off\n";
110                                 break;
111                         case PREVIEW_GRAPHICS:
112                                 os_ << "Graphics\n";
113                                 break;
114                         case PREVIEW_INSTANT:
115                                 os_ << "InstantPreview\n";
116                                 break;
117                 }
118                 typedef vector<TransformID> IDs;
119                 IDs::const_iterator it  = et.transformIds.begin();
120                 IDs::const_iterator end = et.transformIds.end();
121                 for (; it != end; ++it) {
122                         os_ << "\tTransform "
123                             << transformIDTranslator().find(*it) << '\n';
124                 }
125
126                 et.dumpFormats(os_);
127                 os_ << "TemplateEnd" << endl;
128
129         }
130
131 private:
132         ostream & os_;
133 };
134
135 class DumpFormat {
136 public:
137         typedef Template::Formats::value_type value_type;
138
139         DumpFormat(ostream & o) : os_(o) {}
140
141         void operator()(value_type const & vt) const {
142                 Template::Format const & ft = vt.second;
143                 os_ << "\tFormat " << vt.first << '\n'
144                     << "\t\tProduct " << ft.product << '\n'
145                     << "\t\tUpdateFormat " << ft.updateFormat << '\n'
146                     << "\t\tUpdateResult " << ft.updateResult << '\n';
147
148                 vector<string>::const_iterator qit = ft.requirements.begin();
149                 vector<string>::const_iterator qend = ft.requirements.end();
150                 for (; qit != qend; ++qit) {
151                         lyxerr << "req:" << *qit << endl;
152                         os_ << "\t\tRequirement " << *qit << '\n';
153                 }
154
155                 typedef vector<Template::Option> Options;
156                 Options::const_iterator oit  = ft.options.begin();
157                 Options::const_iterator oend = ft.options.end();
158                 for (; oit != oend; ++oit) {
159                         os_ << "\t\tOption "
160                             << oit->name
161                             << ": "
162                             << oit->option
163                             << '\n';
164                 }
165
166                 vector<string>::const_iterator pit  = ft.preambleNames.begin();
167                 vector<string>::const_iterator pend = ft.preambleNames.end();
168                 for (; pit != pend; ++pit) {
169                         os_ << "\t\tPreamble " << *pit << '\n';
170                 }
171
172                 typedef Template::Format::FileMap FileMap;
173                 FileMap::const_iterator rit  = ft.referencedFiles.begin();
174                 FileMap::const_iterator rend = ft.referencedFiles.end();
175                 for (; rit != rend; ++rit) {
176                         vector<string>::const_iterator fit  = rit->second.begin();
177                         vector<string>::const_iterator fend = rit->second.end();
178                         for (; fit != fend; ++fit) {
179                                 os_ << "\t\tReferencedFile " << rit->first
180                                     << " \"" << *fit << "\"\n";
181                         }
182                 }
183
184                 os_ << "\tFormatEnd\n";
185         }
186 private:
187         ostream & os_;
188 };
189
190
191 void Template::dumpFormats(ostream & os) const
192 {
193         for_each(formats.begin(), formats.end(), DumpFormat(os));
194 }
195
196
197 void TemplateManager::dumpPreambleDefs(ostream & os) const
198 {
199         for_each(preambledefs.begin(), preambledefs.end(), DumpPreambleDef(os));
200 }
201
202
203 void TemplateManager::dumpTemplates(ostream & os) const
204 {
205         for_each(templates.begin(), templates.end(), DumpTemplate(os));
206 }
207
208
209 TemplateManager & TemplateManager::get()
210 {
211         static TemplateManager externalTemplateManager;
212         return externalTemplateManager;
213 }
214
215
216 TemplateManager::Templates const & TemplateManager::getTemplates() const
217 {
218         return templates;
219 }
220
221
222 Template const *
223 TemplateManager::getTemplateByName(string const & name) const
224 {
225         Templates::const_iterator it = templates.find(name);
226         return (it == templates.end()) ? 0 : &it->second;
227 }
228
229
230 string const
231 TemplateManager::getPreambleDefByName(string const & name) const
232 {
233         string const trimmed_name = trim(name);
234         if (trimmed_name.empty())
235                 return string();
236
237         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
238         if (it == preambledefs.end())
239                 return string();
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(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("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                 default:
390                         lex.printError("external::Template::readTemplate: "
391                                        "Wrong tag: $$Token");
392                         LASSERT(false, /**/);
393                         break;
394                 }
395         }
396 }
397
398
399 namespace {
400
401 void transform_not_found(ostream & os, string const & transform)
402 {
403         os << "external::Format::readFormat. Transformation \""
404            << transform << "\" is unrecognized." << endl;
405 }
406
407
408 void transform_class_not_found(ostream & os, string const & tclass)
409 {
410         os << "external::Format::readFormat. Transformation class \""
411            << tclass << "\" is unrecognized." << endl;
412 }
413
414
415 void setCommandFactory(Template::Format & format, string const & transform,
416                        string const & transformer_class)
417 {
418         bool class_found = false;
419         if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
420                 class_found = true;
421                 ResizeCommandFactory factory = ResizeLatexCommand::factory;
422                 format.command_transformers[Resize] =
423                         TransformStore(Resize, factory);
424
425         } else if (transform == "Rotate" &&
426                    transformer_class == "RotationLatexCommand") {
427                 class_found = true;
428                 RotationCommandFactory factory = RotationLatexCommand::factory;
429                 format.command_transformers[Rotate] =
430                         TransformStore(Rotate, factory);
431
432         } else
433                 transform_not_found(lyxerr, transform);
434
435         if (!class_found)
436                 transform_class_not_found(lyxerr, transformer_class);
437 }
438
439
440 void setOptionFactory(Template::Format & format, string const & transform,
441                 string const & transformer_class)
442 {
443         bool class_found = false;
444         if (transform == "Clip" && transformer_class == "ClipLatexOption") {
445                 class_found = true;
446                 ClipOptionFactory factory = ClipLatexOption::factory;
447                 format.option_transformers[Clip] =
448                                 TransformStore(Clip, factory);
449
450         } else if (transform == "Extra" && transformer_class == "ExtraOption") {
451                 class_found = true;
452                 ExtraOptionFactory factory = ExtraOption::factory;
453                 format.option_transformers[Extra] =
454                         TransformStore(Extra, factory);
455
456         } else if (transform == "Resize" &&
457                    transformer_class == "ResizeLatexOption") {
458                 class_found = true;
459                 ResizeOptionFactory factory = ResizeLatexOption::factory;
460                 format.option_transformers[Resize] =
461                         TransformStore(Resize, factory);
462
463         } else if (transform == "Rotate" &&
464                    transformer_class == "RotationLatexOption") {
465                 class_found = true;
466                 RotationOptionFactory factory = RotationLatexOption::factory;
467                 format.option_transformers[Rotate] =
468                         TransformStore(Rotate, factory);
469
470         } else
471                 transform_not_found(lyxerr, transform);
472
473         if (!class_found)
474                 transform_class_not_found(lyxerr, transformer_class);
475 }
476
477 } // namespace anon
478
479
480 void Template::Format::readFormat(Lexer & lex)
481 {
482         enum {
483                 FO_PRODUCT = 1,
484                 FO_UPDATEFORMAT,
485                 FO_UPDATERESULT,
486                 FO_REQUIREMENT,
487                 FO_OPTION,
488                 FO_PREAMBLE,
489                 FO_TRANSFORMCOMMAND,
490                 FO_TRANSFORMOPTION,
491                 FO_REFERENCEDFILE,
492                 FO_END
493         };
494
495         LexerKeyword formattags[] = {
496                 { "formatend", FO_END },
497                 { "option", FO_OPTION },
498                 { "preamble", FO_PREAMBLE },
499                 { "product", FO_PRODUCT },
500                 { "referencedfile", FO_REFERENCEDFILE },
501                 { "requirement", FO_REQUIREMENT },
502                 { "transformcommand", FO_TRANSFORMCOMMAND },
503                 { "transformoption", FO_TRANSFORMOPTION },
504                 { "updateformat", FO_UPDATEFORMAT },
505                 { "updateresult", FO_UPDATERESULT }
506         };
507
508         PushPopHelper pph(lex, formattags);
509
510         while (lex.isOK()) {
511                 switch (lex.lex()) {
512                 case FO_PRODUCT:
513                         lex.next(true);
514                         product = lex.getString();
515                         break;
516
517                 case FO_UPDATEFORMAT:
518                         lex.next(true);
519                         updateFormat = lex.getString();
520                         break;
521
522                 case FO_UPDATERESULT:
523                         lex.next(true);
524                         updateResult = lex.getString();
525                         break;
526
527                 case FO_REQUIREMENT:
528                         lex.next(true);
529                         requirements.push_back(lex.getString());
530                         break;
531
532                 case FO_PREAMBLE:
533                         lex.next(true);
534                         preambleNames.push_back(lex.getString());
535                         break;
536
537                 case FO_TRANSFORMCOMMAND: {
538                         lex.next(true);
539                         string const name = lex.getString();
540                         lex.next(true);
541                         setCommandFactory(*this, name, lex.getString());
542                         break;
543                 }
544
545                 case FO_TRANSFORMOPTION: {
546                         lex.next(true);
547                         string const name = lex.getString();
548                         lex.next(true);
549                         setOptionFactory(*this, name, lex.getString());
550                         break;
551                 }
552
553                 case FO_OPTION: {
554                         lex.next(true);
555                         string const name = lex.getString();
556                         lex.next(true);
557                         string const opt = lex.getString();
558                         options.push_back(Option(name, opt));
559                         break;
560                 }
561
562                 case FO_REFERENCEDFILE: {
563                         lex.next(true);
564                         string const format = lex.getString();
565                         lex.next(true);
566                         string const file = lex.getString();
567                         referencedFiles[format].push_back(file);
568                         break;
569                 }
570
571                 case FO_END:
572                         return;
573                 }
574         }
575 }
576
577 } // namespace external
578 } // namespace lyx