]> git.lyx.org Git - lyx.git/blob - src/insets/ExternalTemplate.C
update copyright year
[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-2001 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 ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
133 {
134         return templates[name];
135 }
136
137  
138 void ExternalTemplateManager::readTemplates(string const & path) 
139 {
140         Path p(path);
141
142         enum TemplateTags {
143                 TM_TEMPLATE = 1,
144                 TM_END
145         };
146         
147         keyword_item templatetags[] = {
148                 { "template", TM_TEMPLATE },
149                 { "templateend", TM_END }
150         };
151
152         string filename = LibFileSearch("", "external_templates");
153         if (filename.empty()) {
154                 lyxerr << "ExternalTemplateManager::readTemplates: "
155                         "No template file" << endl;
156                 return;
157         }
158
159         LyXLex lex(templatetags, TM_END);
160         if (!lex.setFile(filename)) {
161                 lyxerr << "ExternalTemplateManager::readTemplates: "
162                         "No template file" << endl;
163                 return;
164         }
165         
166         while (lex.IsOK()) {
167                 switch (lex.lex()) {
168                 case TM_TEMPLATE: {
169                         lex.next();
170                         string temp = lex.GetString();
171                         ExternalTemplate & tmp = templates[temp];
172                         tmp.lyxName = temp;
173                         tmp.readTemplate(lex);
174                 }
175                 break;
176                 
177                 case TM_END:
178                         lex.printError("Warning: End outside Template.");
179                 break;
180                 }
181         }
182 }
183
184
185 void ExternalTemplate::readTemplate(LyXLex & lex)
186 {
187         enum TemplateOptionTags {
188                 TO_GUINAME = 1,
189                 TO_HELPTEXT,
190                 TO_FILTER,
191                 TO_VIEWCMD,
192                 TO_EDITCMD,
193                 TO_AUTOMATIC,
194                 TO_FORMAT,
195                 TO_END
196         };
197
198         keyword_item templateoptiontags[] = {
199                 { "automaticproduction", TO_AUTOMATIC },
200                 { "editcommand", TO_EDITCMD },
201                 { "filefilter", TO_FILTER },
202                 { "format", TO_FORMAT },
203                 { "guiname", TO_GUINAME },
204                 { "helptext", TO_HELPTEXT },
205                 { "templateend", TO_END },
206                 { "viewcommand", TO_VIEWCMD }
207         };
208
209         pushpophelper pph(lex, templateoptiontags, TO_END);
210         
211         while (lex.IsOK()) {
212                 switch (lex.lex()) {
213                 case TO_GUINAME:
214                         lex.next(true);
215                         guiName = lex.GetString();
216                         break;
217                         
218                 case TO_HELPTEXT:
219                         helpText = lex.getLongString("HelpTextEnd");
220                         break;
221                         
222                 case TO_FILTER:
223                         lex.next(true);
224                         fileRegExp = lex.GetString();
225                         break;
226                         
227                 case TO_VIEWCMD:
228                         lex.next(true);
229                         viewCommand = lex.GetString();
230                         // For security reasons, a command may not be empty!
231                         if (viewCommand.empty())
232                                 viewCommand = "true";
233                         break;
234                         
235                 case TO_EDITCMD:
236                         lex.next(true);
237                         editCommand = lex.GetString();
238                         // For security reasons, a command may not be empty!
239                         if (editCommand.empty())
240                                 editCommand = "true";
241                         break;
242                         
243                 case TO_AUTOMATIC:
244                         lex.next();
245                         automaticProduction = lex.GetBool();
246                         break;
247                         
248                 case TO_FORMAT:
249                         lex.next(true);
250                         formats[lex.GetString()].readFormat(lex);
251                         break;
252                         
253                 case TO_END:
254                         return;
255                         
256                 default:
257                         lex.printError("ExternalTemplate::readTemplate: "
258                                        "Wrong tag: $$Token");
259                         lyx::Assert(false);
260                         break;
261                 }
262         }
263 }
264
265
266 void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex) 
267 {
268         enum FormatTags {
269                 FO_PRODUCT = 1,
270                 FO_UPDATECMD,
271                 FO_REQUIREMENT,
272                 FO_PREAMBLE,
273                 FO_END
274         };
275         
276         keyword_item formattags[] = {
277                 { "formatend", FO_END },
278                 { "preamble", FO_PREAMBLE },
279                 { "product", FO_PRODUCT },
280                 { "requirement", FO_REQUIREMENT },
281                 { "updatecommand", FO_UPDATECMD }
282         };
283
284         pushpophelper pph(lex, formattags, FO_END);
285         
286         while (lex.IsOK()) {
287                 switch (lex.lex()) {
288                 case FO_PRODUCT:
289                         lex.next(true);
290                         product = lex.GetString();
291                         break;
292                         
293                 case FO_UPDATECMD:
294                         lex.next(true);
295                         updateCommand = lex.GetString();
296                         // For security reasons, a command may not be empty!
297                         if (updateCommand.empty())
298                                 updateCommand = "true";
299                         break;
300                         
301                 case FO_REQUIREMENT:
302                         lex.next(true);
303                         requirement = lex.GetString();
304                         break;
305                         
306                 case FO_PREAMBLE:
307                         preamble = lex.getLongString("preambleend");
308                         break;
309                         
310                 case FO_END:
311                         if (lyxerr.debugging())
312                                 lex.printError("FormatEnd");
313                         return;
314                 }
315         }
316 }
317