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