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