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