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