]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.cpp
Context menu item to add unknown branch (rest of #7643)
[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 #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                     << 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                     << 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 string const
232 TemplateManager::getPreambleDefByName(string const & name) const
233 {
234         string const trimmed_name = trim(name);
235         if (trimmed_name.empty())
236                 return string();
237
238         PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
239         if (it == preambledefs.end())
240                 return string();
241
242         return it->second;
243 }
244
245
246 void TemplateManager::readTemplates(FileName const & path)
247 {
248         PathChanger p(path);
249
250         enum {
251                 TM_PREAMBLEDEF = 1,
252                 TM_PREAMBLEDEF_END,
253                 TM_TEMPLATE,
254                 TM_TEMPLATE_END
255         };
256
257         LexerKeyword templatetags[] = {
258                 { "preambledef", TM_PREAMBLEDEF },
259                 { "preambledefend", TM_PREAMBLEDEF_END },
260                 { "template", TM_TEMPLATE },
261                 { "templateend", TM_TEMPLATE_END }
262         };
263
264         Lexer lex(templatetags);
265
266         FileName const filename = libFileSearch("", "external_templates");
267         if (filename.empty() || !lex.setFile(filename)) {
268                 lex.printError("external::TemplateManager::readTemplates: "
269                                "No template file");
270                 return;
271         }
272
273         char const * const preamble_end_tag =
274                 templatetags[TM_PREAMBLEDEF_END-1].tag;
275
276         while (lex.isOK()) {
277                 switch (lex.lex()) {
278                 case TM_PREAMBLEDEF: {
279                         lex.next();
280                         string const name = lex.getString();
281                         preambledefs[name] = lex.getLongString(preamble_end_tag);
282                 }
283                 break;
284
285                 case TM_TEMPLATE: {
286                         lex.next();
287                         string const name = lex.getString();
288                         Template & tmp = templates[name];
289                         tmp.lyxName = name;
290                         tmp.readTemplate(lex);
291                 }
292                 break;
293
294                 case TM_TEMPLATE_END:
295                         lex.printError("Warning: End outside Template.");
296                 break;
297
298                 case TM_PREAMBLEDEF_END:
299                         lex.printError("Warning: End outside PreambleDef.");
300                 break;
301                 }
302         }
303 }
304
305
306 void Template::readTemplate(Lexer & lex)
307 {
308         enum {
309                 TO_GUINAME = 1,
310                 TO_HELPTEXT,
311                 TO_INPUTFORMAT,
312                 TO_FILTER,
313                 TO_AUTOMATIC,
314                 TO_PREVIEW,
315                 TO_TRANSFORM,
316                 TO_FORMAT,
317                 TO_END
318         };
319
320         LexerKeyword templateoptiontags[] = {
321                 { "automaticproduction", TO_AUTOMATIC },
322                 { "filefilter", TO_FILTER },
323                 { "format", TO_FORMAT },
324                 { "guiname", TO_GUINAME },
325                 { "helptext", TO_HELPTEXT },
326                 { "inputformat", TO_INPUTFORMAT },
327                 { "preview", TO_PREVIEW },
328                 { "templateend", TO_END },
329                 { "transform", TO_TRANSFORM }
330         };
331
332         PushPopHelper pph(lex, templateoptiontags);
333         lex.setContext("Template::readTemplate");
334
335         string token;
336         while (lex.isOK()) {
337                 switch (lex.lex()) {
338                 case TO_GUINAME:
339                         lex.next(true);
340                         guiName = lex.getString();
341                         break;
342
343                 case TO_HELPTEXT:
344                         helpText = lex.getLongString("HelpTextEnd");
345                         break;
346
347                 case TO_INPUTFORMAT:
348                         lex.next(true);
349                         inputFormat = lex.getString();
350                         break;
351
352                 case TO_FILTER:
353                         lex.next(true);
354                         fileRegExp = lex.getString();
355                         break;
356
357                 case TO_AUTOMATIC:
358                         lex.next();
359                         automaticProduction = lex.getBool();
360                         break;
361
362                 case TO_PREVIEW:
363                         lex >> token;
364                         if (token == "InstantPreview")
365                                 preview_mode = PREVIEW_INSTANT;
366                         else if (token == "Graphics")
367                                 preview_mode = PREVIEW_GRAPHICS;
368                         else
369                                 preview_mode = PREVIEW_OFF;
370                         break;
371
372                 case TO_TRANSFORM: {
373                         lex >> token;
374                         TransformID id = transformIDTranslator().find(token);
375                         if (int(id) == -1)
376                                 LYXERR0("Transform " << token << " is not recognized");
377                         else
378                                 transformIds.push_back(id);
379                         break;
380                 }
381
382                 case TO_FORMAT:
383                         lex.next(true);
384                         formats[lex.getString()].readFormat(lex);
385                         break;
386
387                 case TO_END:
388                         return;
389
390                 default:
391                         lex.printError("external::Template::readTemplate: "
392                                        "Wrong tag: $$Token");
393                         LASSERT(false, /**/);
394                         break;
395                 }
396         }
397 }
398
399
400 namespace {
401
402 void transform_not_found(ostream & os, string const & transform)
403 {
404         os << "external::Format::readFormat. Transformation \""
405            << transform << "\" is unrecognized." << endl;
406 }
407
408
409 void transform_class_not_found(ostream & os, string const & tclass)
410 {
411         os << "external::Format::readFormat. Transformation class \""
412            << tclass << "\" is unrecognized." << endl;
413 }
414
415
416 void setCommandFactory(Template::Format & format, string const & transform,
417                        string const & transformer_class)
418 {
419         bool class_found = false;
420         if (transform == "Resize" && transformer_class == "ResizeLatexCommand") {
421                 class_found = true;
422                 ResizeCommandFactory factory = ResizeLatexCommand::factory;
423                 format.command_transformers[Resize] =
424                         TransformStore(Resize, factory);
425
426         } else if (transform == "Rotate" &&
427                    transformer_class == "RotationLatexCommand") {
428                 class_found = true;
429                 RotationCommandFactory factory = RotationLatexCommand::factory;
430                 format.command_transformers[Rotate] =
431                         TransformStore(Rotate, factory);
432
433         } else
434                 transform_not_found(lyxerr, transform);
435
436         if (!class_found)
437                 transform_class_not_found(lyxerr, transformer_class);
438 }
439
440
441 void setOptionFactory(Template::Format & format, string const & transform,
442                 string const & transformer_class)
443 {
444         bool class_found = false;
445         if (transform == "Clip" && transformer_class == "ClipLatexOption") {
446                 class_found = true;
447                 ClipOptionFactory factory = ClipLatexOption::factory;
448                 format.option_transformers[Clip] =
449                                 TransformStore(Clip, factory);
450
451         } else if (transform == "Extra" && transformer_class == "ExtraOption") {
452                 class_found = true;
453                 ExtraOptionFactory factory = ExtraOption::factory;
454                 format.option_transformers[Extra] =
455                         TransformStore(Extra, factory);
456
457         } else if (transform == "Resize" &&
458                    transformer_class == "ResizeLatexOption") {
459                 class_found = true;
460                 ResizeOptionFactory factory = ResizeLatexOption::factory;
461                 format.option_transformers[Resize] =
462                         TransformStore(Resize, factory);
463
464         } else if (transform == "Rotate" &&
465                    transformer_class == "RotationLatexOption") {
466                 class_found = true;
467                 RotationOptionFactory factory = RotationLatexOption::factory;
468                 format.option_transformers[Rotate] =
469                         TransformStore(Rotate, factory);
470
471         } else
472                 transform_not_found(lyxerr, transform);
473
474         if (!class_found)
475                 transform_class_not_found(lyxerr, transformer_class);
476 }
477
478 } // namespace anon
479
480
481 void Template::Format::readFormat(Lexer & lex)
482 {
483         enum {
484                 FO_PRODUCT = 1,
485                 FO_UPDATEFORMAT,
486                 FO_UPDATERESULT,
487                 FO_REQUIREMENT,
488                 FO_OPTION,
489                 FO_PREAMBLE,
490                 FO_TRANSFORMCOMMAND,
491                 FO_TRANSFORMOPTION,
492                 FO_REFERENCEDFILE,
493                 FO_END
494         };
495
496         LexerKeyword formattags[] = {
497                 { "formatend", FO_END },
498                 { "option", FO_OPTION },
499                 { "preamble", FO_PREAMBLE },
500                 { "product", FO_PRODUCT },
501                 { "referencedfile", FO_REFERENCEDFILE },
502                 { "requirement", FO_REQUIREMENT },
503                 { "transformcommand", FO_TRANSFORMCOMMAND },
504                 { "transformoption", FO_TRANSFORMOPTION },
505                 { "updateformat", FO_UPDATEFORMAT },
506                 { "updateresult", FO_UPDATERESULT }
507         };
508
509         PushPopHelper pph(lex, formattags);
510
511         while (lex.isOK()) {
512                 switch (lex.lex()) {
513                 case FO_PRODUCT:
514                         lex.next(true);
515                         product = lex.getString();
516                         break;
517
518                 case FO_UPDATEFORMAT:
519                         lex.next(true);
520                         updateFormat = lex.getString();
521                         break;
522
523                 case FO_UPDATERESULT:
524                         lex.next(true);
525                         updateResult = lex.getString();
526                         break;
527
528                 case FO_REQUIREMENT:
529                         lex.next(true);
530                         requirements.push_back(lex.getString());
531                         break;
532
533                 case FO_PREAMBLE:
534                         lex.next(true);
535                         preambleNames.push_back(lex.getString());
536                         break;
537
538                 case FO_TRANSFORMCOMMAND: {
539                         lex.next(true);
540                         string const name = lex.getString();
541                         lex.next(true);
542                         setCommandFactory(*this, name, lex.getString());
543                         break;
544                 }
545
546                 case FO_TRANSFORMOPTION: {
547                         lex.next(true);
548                         string const name = lex.getString();
549                         lex.next(true);
550                         setOptionFactory(*this, name, lex.getString());
551                         break;
552                 }
553
554                 case FO_OPTION: {
555                         lex.next(true);
556                         string const name = lex.getString();
557                         lex.next(true);
558                         string const opt = lex.getString();
559                         options.push_back(Option(name, opt));
560                         break;
561                 }
562
563                 case FO_REFERENCEDFILE: {
564                         lex.next(true);
565                         string const format = lex.getString();
566                         lex.next(true);
567                         string const file = lex.getString();
568                         referencedFiles[format].push_back(file);
569                         break;
570                 }
571
572                 case FO_END:
573                         return;
574                 }
575         }
576 }
577
578 } // namespace external
579 } // namespace lyx