]> git.lyx.org Git - lyx.git/blobdiff - src/insets/ExternalTemplate.C
Re-add the RasterImage template.
[lyx.git] / src / insets / ExternalTemplate.C
index a323f8a0cd398621ab8a4a61fb56f98f91e18457..49d9e3cf7f88a8234e46bc9e624232bc04e114b7 100644 (file)
@@ -1,74 +1,98 @@
-/* This file is part of
- * ======================================================
- * 
- *           LyX, The Document Processor
- *      
- *         Copyright 1995 Matthias Ettrich
- *          Copyright 1995-2000 The LyX Team.
+/**
+ * \file ExternalTemplate.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
- * ====================================================== */
+ * \author Asger Alstrup Nielsen
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
-#include <algorithm>
-
 #include "ExternalTemplate.h"
 
-#include "lyxlex.h"
 #include "debug.h"
-#include "support/path.h"
-#include "support/LAssert.h"
+#include "lyxlex.h"
+
 #include "support/filetools.h"
+#include "support/lstrings.h"
+#include "support/path.h"
+#include "support/path_defines.h"
+
+#include <boost/assert.hpp>
+
+#include <algorithm>
+
+namespace support = lyx::support;
 
 using std::endl;
-using std::ostream;
 using std::for_each;
 
-extern string user_lyxdir;
+using std::ostream;
+using std::vector;
 
 
 // We have to have dummy default commands for security reasons!
 
 ExternalTemplate::ExternalTemplate()
-       : viewCommand("true"), editCommand("true")
+       : inputFormat("*")
 {}
 
 
 ExternalTemplate::FormatTemplate::FormatTemplate()
-       : updateCommand("true") {}
+{}
 
 
 ExternalTemplateManager::ExternalTemplateManager()
 {
-       // gimp gnuchess gnuplot ical netscape tetris xpaint
-       readTemplates(user_lyxdir);
-       dumpTemplates();
+       readTemplates(support::user_lyxdir());
+       if (lyxerr.debugging(Debug::EXTERNAL)) {
+               dumpPreambleDefs(lyxerr);
+               lyxerr << '\n';
+               dumpTemplates(lyxerr);
+       }
 }
 
 
+class dumpPreambleDef {
+public:
+       typedef ExternalTemplateManager::PreambleDefs::value_type value_type;
+
+       dumpPreambleDef(ostream & o) : ost(o) {}
+
+       void operator()(value_type const & vt) {
+               ost << "PreambleDef " << vt.first << '\n'
+                   << vt.second
+                   << "PreambleDefEnd" << endl;
+       }
+
+private:
+       ostream & ost;
+};
+
+
 class dumpTemplate {
 public:
-       dumpTemplate(std::ostream & o) 
-               : ost(o) {}
-       void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
+       typedef ExternalTemplateManager::Templates::value_type value_type;
+
+       dumpTemplate(ostream & o) : ost(o) {}
+
+       void operator()(value_type const & vt) {
                ExternalTemplate const & et = vt.second;
-               
-               ost << "Template " << et.lyxName << "\n"
-                   << "\tGuiName " << et.guiName << "\n"
+
+               ost << "Template " << et.lyxName << '\n'
+                   << "\tGuiName " << et.guiName << '\n'
                    << "\tHelpText\n"
                    << et.helpText
                    << "\tHelpTextEnd\n"
-                   << "\tFileFilter " << et.fileRegExp << "\n"
-                   << "\tViewCommand " << et.viewCommand << "\n"
-                   << "\tEditCommand " << et.editCommand << "\n"
-                   << "\tAutomaticProduction " << et.automaticProduction << "\n";
+                   << "\tInputFormat " << et.inputFormat << '\n'
+                   << "\tFileFilter " << et.fileRegExp << '\n'
+                   << "\tEditCommand " << et.editCommand << '\n'
+                   << "\tAutomaticProduction " << et.automaticProduction << '\n';
                et.dumpFormats(ost);
                ost << "TemplateEnd" << endl;
-               
+
        }
 
 private:
@@ -77,33 +101,46 @@ private:
 
 class dumpFormat {
 public:
-       dumpFormat(ostream & o) 
-               : ost(o) {}
-       void operator()(ExternalTemplate::Formats::value_type const & vt) const{
+       typedef ExternalTemplate::Formats::value_type value_type;
+
+       dumpFormat(ostream & o) : ost(o) {}
+
+       void operator()(value_type const & vt) const{
                ExternalTemplate::FormatTemplate const & ft = vt.second;
-               ost << "\tFormat " << vt.first << "\n"
-                   << "\t\tProduct " << ft.product << "\n"
-                   << "\t\tUpdateCommand " << ft.updateCommand << "\n"
-                   << "\t\tRequirement " << ft.requirement << "\n"
-                   << "\t\tPreamble\n"
-                   << ft.preamble
-                   << "\t\tPreambleEnd\n"
-                   << "\tFormatEnd\n";
+               ost << "\tFormat " << vt.first << '\n'
+                   << "\t\tProduct " << ft.product << '\n'
+                   << "\t\tUpdateFormat " << ft.updateFormat << '\n'
+                   << "\t\tUpdateResult " << ft.updateResult << '\n'
+                   << "\t\tRequirement " << ft.requirement << '\n';
+
+               vector<string>::const_iterator it  = ft.preambleNames.begin();
+               vector<string>::const_iterator end = ft.preambleNames.end();
+               for (; it != end; ++it) {
+                       ost << "\t\tPreamble " << *it << '\n';
+               }
+
+               ost << "\tFormatEnd\n";
        }
 private:
        ostream & ost;
 };
 
 
-void ExternalTemplate::dumpFormats(ostream & os) const 
+void ExternalTemplate::dumpFormats(ostream & os) const
 {
        for_each(formats.begin(), formats.end(), dumpFormat(os));
 }
 
 
-void ExternalTemplateManager::dumpTemplates() const 
+void ExternalTemplateManager::dumpPreambleDefs(ostream & os) const
+{
+       for_each(preambledefs.begin(), preambledefs.end(), dumpPreambleDef(os));
+}
+
+
+void ExternalTemplateManager::dumpTemplates(ostream & os) const
 {
-       for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
+       for_each(templates.begin(), templates.end(), dumpTemplate(os));
 }
 
 
@@ -128,46 +165,83 @@ ExternalTemplateManager::getTemplates() const
 }
 
 
-void ExternalTemplateManager::readTemplates(string const & path) 
+ExternalTemplate const *
+ExternalTemplateManager::getTemplateByName(string const & name) const
 {
-       Path p(path);
+       Templates::const_iterator it = templates.find(name);
+       return (it == templates.end()) ? 0 : &it->second;
+}
+
+
+string const
+ExternalTemplateManager::getPreambleDefByName(string const & name) const
+{
+       string const trimmed_name = support::trim(name);
+       if (trimmed_name.empty())
+               return string();
+
+       PreambleDefs::const_iterator it = preambledefs.find(trimmed_name);
+       if (it == preambledefs.end())
+               return string();
+
+       return it->second;
+}
+
+
+void ExternalTemplateManager::readTemplates(string const & path)
+{
+       support::Path p(path);
 
        enum TemplateTags {
-               TM_TEMPLATE = 1,
-               TM_END
+               TM_PREAMBLEDEF = 1,
+               TM_PREAMBLEDEF_END,
+               TM_TEMPLATE,
+               TM_TEMPLATE_END
        };
-       
+
        keyword_item templatetags[] = {
+               { "preambledef", TM_PREAMBLEDEF },
+               { "preambledefend", TM_PREAMBLEDEF_END },
                { "template", TM_TEMPLATE },
-               { "templateend", TM_END }
+               { "templateend", TM_TEMPLATE_END }
        };
 
-       string filename = LibFileSearch("", "external_templates");
-       if (filename.empty()) {
-               lyxerr << "No template file" << endl;
-               return;
-       }
+       LyXLex lex(templatetags, TM_TEMPLATE_END);
 
-       LyXLex lex(templatetags, TM_END);
-       if (!lex.setFile(filename)) {
-               lyxerr << "No template file" << endl;
+       string filename = support::LibFileSearch("", "external_templates");
+       if (filename.empty() || !lex.setFile(filename)) {
+               lex.printError("ExternalTemplateManager::readTemplates: "
+                              "No template file");
                return;
        }
-       
-       while (lex.IsOK()) {
+
+       char const * const preamble_end_tag =
+               templatetags[TM_PREAMBLEDEF_END-1].tag;
+
+       while (lex.isOK()) {
                switch (lex.lex()) {
+               case TM_PREAMBLEDEF: {
+                       lex.next();
+                       string const name = lex.getString();
+                       preambledefs[name] = lex.getLongString(preamble_end_tag);
+               }
+               break;
+
                case TM_TEMPLATE: {
                        lex.next();
-                       string temp = lex.GetString();
-                       ExternalTemplate & tmp = templates[temp];
-                       tmp.lyxName = temp;
+                       string const name = lex.getString();
+                       ExternalTemplate & tmp = templates[name];
+                       tmp.lyxName = name;
                        tmp.readTemplate(lex);
                }
                break;
-               
-               case TM_END:
-                       lyxerr << "TemplateEnd: " << lex.GetString() << endl;
-                       lyxerr << "Warning: End outside Template." << endl;
+
+               case TM_TEMPLATE_END:
+                       lex.printError("Warning: End outside Template.");
+               break;
+
+               case TM_PREAMBLEDEF_END:
+                       lex.printError("Warning: End outside PreambleDef.");
                break;
                }
        }
@@ -179,8 +253,8 @@ void ExternalTemplate::readTemplate(LyXLex & lex)
        enum TemplateOptionTags {
                TO_GUINAME = 1,
                TO_HELPTEXT,
+               TO_INPUTFORMAT,
                TO_FILTER,
-               TO_VIEWCMD,
                TO_EDITCMD,
                TO_AUTOMATIC,
                TO_FORMAT,
@@ -194,114 +268,112 @@ void ExternalTemplate::readTemplate(LyXLex & lex)
                { "format", TO_FORMAT },
                { "guiname", TO_GUINAME },
                { "helptext", TO_HELPTEXT },
-               { "templateend", TO_END },
-               { "viewcommand", TO_VIEWCMD }
+               { "inputformat", TO_INPUTFORMAT },
+               { "templateend", TO_END }
        };
 
        pushpophelper pph(lex, templateoptiontags, TO_END);
-       
-       while (lex.IsOK()) {
+
+       while (lex.isOK()) {
                switch (lex.lex()) {
                case TO_GUINAME:
                        lex.next(true);
-                       guiName = lex.GetString();
+                       guiName = lex.getString();
                        break;
-                       
+
                case TO_HELPTEXT:
                        helpText = lex.getLongString("HelpTextEnd");
                        break;
-                       
-               case TO_FILTER:
+
+               case TO_INPUTFORMAT:
                        lex.next(true);
-                       fileRegExp = lex.GetString();
+                       inputFormat = lex.getString();
                        break;
-                       
-               case TO_VIEWCMD:
+
+               case TO_FILTER:
                        lex.next(true);
-                       viewCommand = lex.GetString();
-                       // For security reasons, a command may not be empty!
-                       if (viewCommand.empty())
-                               viewCommand = "true";
+                       fileRegExp = lex.getString();
                        break;
-                       
+
                case TO_EDITCMD:
                        lex.next(true);
-                       editCommand = lex.GetString();
-                       // For security reasons, a command may not be empty!
-                       if (editCommand.empty())
-                               editCommand = "true";
+                       editCommand = lex.getString();
                        break;
-                       
+
                case TO_AUTOMATIC:
                        lex.next();
-                       automaticProduction = lex.GetBool();
+                       automaticProduction = lex.getBool();
                        break;
-                       
+
                case TO_FORMAT:
                        lex.next(true);
-                       formats[lex.GetString()].readFormat(lex);
+                       formats[lex.getString()].readFormat(lex);
                        break;
-                       
+
                case TO_END:
                        return;
-                       
+
                default:
-                       lyxerr << "Default: " << lex.GetString() << endl;
-                       Assert(false);
+                       lex.printError("ExternalTemplate::readTemplate: "
+                                      "Wrong tag: $$Token");
+                       BOOST_ASSERT(false);
                        break;
                }
        }
 }
 
 
-void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex) 
+void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
 {
        enum FormatTags {
                FO_PRODUCT = 1,
-               FO_UPDATECMD,
+               FO_UPDATEFORMAT,
+               FO_UPDATERESULT,
                FO_REQUIREMENT,
                FO_PREAMBLE,
                FO_END
        };
-       
+
        keyword_item formattags[] = {
                { "formatend", FO_END },
                { "preamble", FO_PREAMBLE },
                { "product", FO_PRODUCT },
                { "requirement", FO_REQUIREMENT },
-               { "updatecommand", FO_UPDATECMD }
+               { "updateformat", FO_UPDATEFORMAT },
+               { "updateresult", FO_UPDATERESULT }
        };
 
        pushpophelper pph(lex, formattags, FO_END);
-       
-       while (lex.IsOK()) {
+
+       while (lex.isOK()) {
                switch (lex.lex()) {
                case FO_PRODUCT:
                        lex.next(true);
-                       product = lex.GetString();
+                       product = lex.getString();
                        break;
-                       
-               case FO_UPDATECMD:
+
+               case FO_UPDATEFORMAT:
+                       lex.next(true);
+                       updateFormat = lex.getString();
+                       break;
+
+               case FO_UPDATERESULT:
                        lex.next(true);
-                       updateCommand = lex.GetString();
-                       // For security reasons, a command may not be empty!
-                       if (updateCommand.empty())
-                               updateCommand = "true";
+                       updateResult = lex.getString();
                        break;
-                       
+
                case FO_REQUIREMENT:
                        lex.next(true);
-                       requirement = lex.GetString();
+                       requirement = lex.getString();
                        break;
-                       
+
                case FO_PREAMBLE:
-                       preamble = lex.getLongString("preambleend");
+                       lex.next(true);
+                       preambleNames.push_back(lex.getString());
                        break;
-                       
+
                case FO_END:
-                       lyxerr << "FormatEnd: " << lex.GetString() << endl;
                        return;
                }
        }
 }
-