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