]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
c413190929722ce03f8a3cdbcd0789e0808009c3
[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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "ExternalTemplate.h"
14
15 #include "lyxlex.h"
16 #include "debug.h"
17 #include "support/path.h"
18 #include "support/LAssert.h"
19 #include "support/filetools.h"
20 #include "support/path_defines.h"
21
22 #include <algorithm>
23
24 using namespace lyx::support;
25
26 using std::endl;
27 using std::ostream;
28 using std::for_each;
29
30 // We have to have dummy default commands for security reasons!
31
32 ExternalTemplate::ExternalTemplate()
33         : inputFormat("*")
34 {}
35
36
37 ExternalTemplate::FormatTemplate::FormatTemplate()
38 {}
39
40
41 ExternalTemplateManager::ExternalTemplateManager()
42 {
43         // gimp gnuchess gnuplot ical netscape tetris xpaint
44         readTemplates(user_lyxdir());
45         if (lyxerr.debugging())
46                 dumpTemplates();
47 }
48
49
50 class dumpTemplate {
51 public:
52         dumpTemplate(ostream & o)
53                 : ost(o) {}
54         void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
55                 ExternalTemplate const & et = vt.second;
56
57                 ost << "Template " << et.lyxName << "\n"
58                     << "\tGuiName " << et.guiName << "\n"
59                     << "\tHelpText\n"
60                     << et.helpText
61                     << "\tHelpTextEnd\n"
62                     << "\tInputFormat " << et.inputFormat << "\n"
63                     << "\tFileFilter " << et.fileRegExp << "\n"
64                     << "\tEditCommand " << et.editCommand << "\n"
65                     << "\tAutomaticProduction " << et.automaticProduction << "\n";
66                 et.dumpFormats(ost);
67                 ost << "TemplateEnd" << endl;
68
69         }
70
71 private:
72         ostream & ost;
73 };
74
75 class dumpFormat {
76 public:
77         dumpFormat(ostream & o)
78                 : ost(o) {}
79         void operator()(ExternalTemplate::Formats::value_type const & vt) const{
80                 ExternalTemplate::FormatTemplate const & ft = vt.second;
81                 ost << "\tFormat " << vt.first << "\n"
82                     << "\t\tProduct " << ft.product << "\n"
83                     << "\t\tUpdateFormat " << ft.updateFormat << "\n"
84                     << "\t\tUpdateResult " << ft.updateResult << "\n"
85                     << "\t\tRequirement " << ft.requirement << "\n"
86                     << "\t\tPreamble\n"
87                     << ft.preamble
88                     << "\t\tPreambleEnd\n"
89                     << "\tFormatEnd\n";
90         }
91 private:
92         ostream & ost;
93 };
94
95
96 void ExternalTemplate::dumpFormats(ostream & os) const
97 {
98         for_each(formats.begin(), formats.end(), dumpFormat(os));
99 }
100
101
102 void ExternalTemplateManager::dumpTemplates() const
103 {
104         for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
105 }
106
107
108 ExternalTemplateManager & ExternalTemplateManager::get()
109 {
110         static ExternalTemplateManager externalTemplateManager;
111         return externalTemplateManager;
112 }
113
114
115 ExternalTemplateManager::Templates &
116 ExternalTemplateManager::getTemplates()
117 {
118         return templates;
119 }
120
121
122 ExternalTemplateManager::Templates const &
123 ExternalTemplateManager::getTemplates() const
124 {
125         return templates;
126 }
127
128
129 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
130 {
131         return templates[name];
132 }
133
134
135 void ExternalTemplateManager::readTemplates(string const & path)
136 {
137         Path p(path);
138
139         enum TemplateTags {
140                 TM_TEMPLATE = 1,
141                 TM_END
142         };
143
144         keyword_item templatetags[] = {
145                 { "template", TM_TEMPLATE },
146                 { "templateend", TM_END }
147         };
148
149         string filename = LibFileSearch("", "external_templates");
150         if (filename.empty()) {
151                 lyxerr << "ExternalTemplateManager::readTemplates: "
152                         "No template file" << endl;
153                 return;
154         }
155
156         LyXLex lex(templatetags, TM_END);
157         if (!lex.setFile(filename)) {
158                 lyxerr << "ExternalTemplateManager::readTemplates: "
159                         "No template file" << endl;
160                 return;
161         }
162
163         while (lex.isOK()) {
164                 switch (lex.lex()) {
165                 case TM_TEMPLATE: {
166                         lex.next();
167                         string const temp = lex.getString();
168                         ExternalTemplate & tmp = templates[temp];
169                         tmp.lyxName = temp;
170                         tmp.readTemplate(lex);
171                 }
172                 break;
173
174                 case TM_END:
175                         lex.printError("Warning: End outside Template.");
176                 break;
177                 }
178         }
179 }
180
181
182 void ExternalTemplate::readTemplate(LyXLex & lex)
183 {
184         enum TemplateOptionTags {
185                 TO_GUINAME = 1,
186                 TO_HELPTEXT,
187                 TO_INPUTFORMAT,
188                 TO_FILTER,
189                 TO_EDITCMD,
190                 TO_AUTOMATIC,
191                 TO_FORMAT,
192                 TO_END
193         };
194
195         keyword_item templateoptiontags[] = {
196                 { "automaticproduction", TO_AUTOMATIC },
197                 { "editcommand", TO_EDITCMD },
198                 { "filefilter", TO_FILTER },
199                 { "format", TO_FORMAT },
200                 { "guiname", TO_GUINAME },
201                 { "helptext", TO_HELPTEXT },
202                 { "inputformat", TO_INPUTFORMAT },
203                 { "templateend", TO_END }
204         };
205
206         pushpophelper pph(lex, templateoptiontags, TO_END);
207
208         while (lex.isOK()) {
209                 switch (lex.lex()) {
210                 case TO_GUINAME:
211                         lex.next(true);
212                         guiName = lex.getString();
213                         break;
214
215                 case TO_HELPTEXT:
216                         helpText = lex.getLongString("HelpTextEnd");
217                         break;
218
219                 case TO_INPUTFORMAT:
220                         lex.next(true);
221                         inputFormat = lex.getString();
222                         break;
223
224                 case TO_FILTER:
225                         lex.next(true);
226                         fileRegExp = lex.getString();
227                         break;
228
229                 case TO_EDITCMD:
230                         lex.next(true);
231                         editCommand = lex.getString();
232                         break;
233
234                 case TO_AUTOMATIC:
235                         lex.next();
236                         automaticProduction = lex.getBool();
237                         break;
238
239                 case TO_FORMAT:
240                         lex.next(true);
241                         formats[lex.getString()].readFormat(lex);
242                         break;
243
244                 case TO_END:
245                         return;
246
247                 default:
248                         lex.printError("ExternalTemplate::readTemplate: "
249                                        "Wrong tag: $$Token");
250                         Assert(false);
251                         break;
252                 }
253         }
254 }
255
256
257 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
258 {
259         enum FormatTags {
260                 FO_PRODUCT = 1,
261                 FO_UPDATEFORMAT,
262                 FO_UPDATERESULT,
263                 FO_REQUIREMENT,
264                 FO_PREAMBLE,
265                 FO_END
266         };
267
268         keyword_item formattags[] = {
269                 { "formatend", FO_END },
270                 { "preamble", FO_PREAMBLE },
271                 { "product", FO_PRODUCT },
272                 { "requirement", FO_REQUIREMENT },
273                 { "updateformat", FO_UPDATEFORMAT },
274                 { "updateresult", FO_UPDATERESULT }
275         };
276
277         pushpophelper pph(lex, formattags, FO_END);
278
279         while (lex.isOK()) {
280                 switch (lex.lex()) {
281                 case FO_PRODUCT:
282                         lex.next(true);
283                         product = lex.getString();
284                         break;
285
286                 case FO_UPDATEFORMAT:
287                         lex.next(true);
288                         updateFormat = lex.getString();
289                         break;
290
291                 case FO_UPDATERESULT:
292                         lex.next(true);
293                         updateResult = lex.getString();
294                         break;
295
296                 case FO_REQUIREMENT:
297                         lex.next(true);
298                         requirement = lex.getString();
299                         break;
300
301                 case FO_PREAMBLE:
302                         preamble = lex.getLongString("preambleend");
303                         break;
304
305                 case FO_END:
306                         if (lyxerr.debugging())
307                                 lex.printError("FormatEnd");
308                         return;
309                 }
310         }
311 }