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