2002-09-25 14:26:13 +00:00
|
|
|
/**
|
|
|
|
* \file ExternalTemplate.C
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2002-09-25 14:26:13 +00:00
|
|
|
* \author Asger Alstrup Nielsen
|
2002-03-21 17:09:55 +00:00
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-09-25 14:26:13 +00:00
|
|
|
*/
|
2000-06-12 11:27:15 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "ExternalTemplate.h"
|
|
|
|
|
|
|
|
#include "lyxlex.h"
|
2000-11-08 09:39:46 +00:00
|
|
|
#include "debug.h"
|
2000-06-12 11:27:15 +00:00
|
|
|
#include "support/path.h"
|
2000-06-12 12:52:51 +00:00
|
|
|
#include "support/LAssert.h"
|
2000-11-08 09:39:46 +00:00
|
|
|
#include "support/filetools.h"
|
2003-07-27 22:13:29 +00:00
|
|
|
#include "support/path_defines.h"
|
2000-06-12 11:27:15 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
using namespace lyx::support;
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
using std::endl;
|
|
|
|
using std::ostream;
|
2000-06-13 10:33:57 +00:00
|
|
|
using std::for_each;
|
2000-06-12 11:27:15 +00:00
|
|
|
|
|
|
|
// We have to have dummy default commands for security reasons!
|
|
|
|
|
|
|
|
ExternalTemplate::ExternalTemplate()
|
2003-06-04 09:16:29 +00:00
|
|
|
: inputFormat("*")
|
2000-06-12 11:27:15 +00:00
|
|
|
{}
|
|
|
|
|
2000-06-15 15:44:39 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
ExternalTemplate::FormatTemplate::FormatTemplate()
|
2001-11-02 16:18:07 +00:00
|
|
|
{}
|
2000-06-12 11:27:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
ExternalTemplateManager::ExternalTemplateManager()
|
|
|
|
{
|
|
|
|
// gimp gnuchess gnuplot ical netscape tetris xpaint
|
2003-07-27 22:13:29 +00:00
|
|
|
readTemplates(user_lyxdir());
|
2000-12-28 16:22:28 +00:00
|
|
|
if (lyxerr.debugging())
|
|
|
|
dumpTemplates();
|
2000-06-12 11:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class dumpTemplate {
|
|
|
|
public:
|
2002-03-21 17:09:55 +00:00
|
|
|
dumpTemplate(ostream & o)
|
2000-06-12 11:27:15 +00:00
|
|
|
: ost(o) {}
|
|
|
|
void operator()(ExternalTemplateManager::Templates::value_type const & vt) {
|
|
|
|
ExternalTemplate const & et = vt.second;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
ost << "Template " << et.lyxName << "\n"
|
|
|
|
<< "\tGuiName " << et.guiName << "\n"
|
|
|
|
<< "\tHelpText\n"
|
|
|
|
<< et.helpText
|
|
|
|
<< "\tHelpTextEnd\n"
|
2003-06-04 09:16:29 +00:00
|
|
|
<< "\tInputFormat " << et.inputFormat << "\n"
|
2000-06-12 11:27:15 +00:00
|
|
|
<< "\tFileFilter " << et.fileRegExp << "\n"
|
|
|
|
<< "\tEditCommand " << et.editCommand << "\n"
|
|
|
|
<< "\tAutomaticProduction " << et.automaticProduction << "\n";
|
|
|
|
et.dumpFormats(ost);
|
|
|
|
ost << "TemplateEnd" << endl;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ostream & ost;
|
|
|
|
};
|
|
|
|
|
|
|
|
class dumpFormat {
|
|
|
|
public:
|
2002-03-21 17:09:55 +00:00
|
|
|
dumpFormat(ostream & o)
|
2000-06-12 11:27:15 +00:00
|
|
|
: ost(o) {}
|
|
|
|
void operator()(ExternalTemplate::Formats::value_type const & vt) const{
|
|
|
|
ExternalTemplate::FormatTemplate const & ft = vt.second;
|
|
|
|
ost << "\tFormat " << vt.first << "\n"
|
|
|
|
<< "\t\tProduct " << ft.product << "\n"
|
2003-06-04 09:16:29 +00:00
|
|
|
<< "\t\tUpdateFormat " << ft.updateFormat << "\n"
|
2001-11-02 16:18:07 +00:00
|
|
|
<< "\t\tUpdateResult " << ft.updateResult << "\n"
|
2000-06-12 11:27:15 +00:00
|
|
|
<< "\t\tRequirement " << ft.requirement << "\n"
|
|
|
|
<< "\t\tPreamble\n"
|
|
|
|
<< ft.preamble
|
|
|
|
<< "\t\tPreambleEnd\n"
|
|
|
|
<< "\tFormatEnd\n";
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
ostream & ost;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
void ExternalTemplate::dumpFormats(ostream & os) const
|
2000-06-12 11:27:15 +00:00
|
|
|
{
|
|
|
|
for_each(formats.begin(), formats.end(), dumpFormat(os));
|
|
|
|
}
|
|
|
|
|
2000-06-15 15:44:39 +00:00
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
void ExternalTemplateManager::dumpTemplates() const
|
2000-06-12 11:27:15 +00:00
|
|
|
{
|
|
|
|
for_each(templates.begin(), templates.end(), dumpTemplate(lyxerr));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExternalTemplateManager & ExternalTemplateManager::get()
|
|
|
|
{
|
|
|
|
static ExternalTemplateManager externalTemplateManager;
|
|
|
|
return externalTemplateManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExternalTemplateManager::Templates &
|
|
|
|
ExternalTemplateManager::getTemplates()
|
|
|
|
{
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ExternalTemplateManager::Templates const &
|
|
|
|
ExternalTemplateManager::getTemplates() const
|
|
|
|
{
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-12 11:22:26 +00:00
|
|
|
ExternalTemplate const & ExternalTemplateManager::getTemplateByName(string const & name)
|
|
|
|
{
|
|
|
|
return templates[name];
|
|
|
|
}
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
|
|
|
|
void ExternalTemplateManager::readTemplates(string const & path)
|
2000-06-12 11:27:15 +00:00
|
|
|
{
|
|
|
|
Path p(path);
|
|
|
|
|
|
|
|
enum TemplateTags {
|
|
|
|
TM_TEMPLATE = 1,
|
|
|
|
TM_END
|
|
|
|
};
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
keyword_item templatetags[] = {
|
|
|
|
{ "template", TM_TEMPLATE },
|
|
|
|
{ "templateend", TM_END }
|
|
|
|
};
|
|
|
|
|
2000-06-13 17:10:47 +00:00
|
|
|
string filename = LibFileSearch("", "external_templates");
|
|
|
|
if (filename.empty()) {
|
2000-12-28 16:22:28 +00:00
|
|
|
lyxerr << "ExternalTemplateManager::readTemplates: "
|
|
|
|
"No template file" << endl;
|
2000-06-13 17:10:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
LyXLex lex(templatetags, TM_END);
|
2000-06-13 17:10:47 +00:00
|
|
|
if (!lex.setFile(filename)) {
|
2000-12-28 16:22:28 +00:00
|
|
|
lyxerr << "ExternalTemplateManager::readTemplates: "
|
|
|
|
"No template file" << endl;
|
2000-06-12 11:27:15 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-08-06 19:13:25 +00:00
|
|
|
while (lex.isOK()) {
|
2000-11-04 10:00:12 +00:00
|
|
|
switch (lex.lex()) {
|
2000-06-12 11:27:15 +00:00
|
|
|
case TM_TEMPLATE: {
|
|
|
|
lex.next();
|
2001-08-06 19:13:25 +00:00
|
|
|
string const temp = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
ExternalTemplate & tmp = templates[temp];
|
|
|
|
tmp.lyxName = temp;
|
|
|
|
tmp.readTemplate(lex);
|
|
|
|
}
|
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TM_END:
|
2000-12-28 16:22:28 +00:00
|
|
|
lex.printError("Warning: End outside Template.");
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ExternalTemplate::readTemplate(LyXLex & lex)
|
|
|
|
{
|
|
|
|
enum TemplateOptionTags {
|
|
|
|
TO_GUINAME = 1,
|
|
|
|
TO_HELPTEXT,
|
2003-06-04 09:16:29 +00:00
|
|
|
TO_INPUTFORMAT,
|
2000-06-12 11:27:15 +00:00
|
|
|
TO_FILTER,
|
|
|
|
TO_EDITCMD,
|
|
|
|
TO_AUTOMATIC,
|
|
|
|
TO_FORMAT,
|
|
|
|
TO_END
|
|
|
|
};
|
|
|
|
|
|
|
|
keyword_item templateoptiontags[] = {
|
|
|
|
{ "automaticproduction", TO_AUTOMATIC },
|
|
|
|
{ "editcommand", TO_EDITCMD },
|
|
|
|
{ "filefilter", TO_FILTER },
|
|
|
|
{ "format", TO_FORMAT },
|
|
|
|
{ "guiname", TO_GUINAME },
|
|
|
|
{ "helptext", TO_HELPTEXT },
|
2003-06-04 09:16:29 +00:00
|
|
|
{ "inputformat", TO_INPUTFORMAT },
|
|
|
|
{ "templateend", TO_END }
|
2000-06-12 11:27:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pushpophelper pph(lex, templateoptiontags, TO_END);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-08-06 19:13:25 +00:00
|
|
|
while (lex.isOK()) {
|
2000-06-12 11:27:15 +00:00
|
|
|
switch (lex.lex()) {
|
|
|
|
case TO_GUINAME:
|
|
|
|
lex.next(true);
|
2001-08-06 19:13:25 +00:00
|
|
|
guiName = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TO_HELPTEXT:
|
|
|
|
helpText = lex.getLongString("HelpTextEnd");
|
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2003-06-04 09:16:29 +00:00
|
|
|
case TO_INPUTFORMAT:
|
2000-06-12 11:27:15 +00:00
|
|
|
lex.next(true);
|
2003-06-04 09:16:29 +00:00
|
|
|
inputFormat = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2003-06-04 09:16:29 +00:00
|
|
|
case TO_FILTER:
|
2000-06-12 11:27:15 +00:00
|
|
|
lex.next(true);
|
2003-06-04 09:16:29 +00:00
|
|
|
fileRegExp = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TO_EDITCMD:
|
|
|
|
lex.next(true);
|
2001-08-06 19:13:25 +00:00
|
|
|
editCommand = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TO_AUTOMATIC:
|
|
|
|
lex.next();
|
2001-08-06 19:13:25 +00:00
|
|
|
automaticProduction = lex.getBool();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TO_FORMAT:
|
|
|
|
lex.next(true);
|
2001-08-06 19:13:25 +00:00
|
|
|
formats[lex.getString()].readFormat(lex);
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case TO_END:
|
|
|
|
return;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
default:
|
2000-12-28 16:22:28 +00:00
|
|
|
lex.printError("ExternalTemplate::readTemplate: "
|
|
|
|
"Wrong tag: $$Token");
|
2003-06-30 23:56:22 +00:00
|
|
|
Assert(false);
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-21 17:09:55 +00:00
|
|
|
void ExternalTemplate::FormatTemplate::readFormat(LyXLex & lex)
|
2000-06-12 11:27:15 +00:00
|
|
|
{
|
|
|
|
enum FormatTags {
|
|
|
|
FO_PRODUCT = 1,
|
2003-06-04 09:16:29 +00:00
|
|
|
FO_UPDATEFORMAT,
|
2001-11-02 16:18:07 +00:00
|
|
|
FO_UPDATERESULT,
|
2000-06-12 11:27:15 +00:00
|
|
|
FO_REQUIREMENT,
|
|
|
|
FO_PREAMBLE,
|
|
|
|
FO_END
|
|
|
|
};
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
keyword_item formattags[] = {
|
|
|
|
{ "formatend", FO_END },
|
|
|
|
{ "preamble", FO_PREAMBLE },
|
|
|
|
{ "product", FO_PRODUCT },
|
|
|
|
{ "requirement", FO_REQUIREMENT },
|
2003-06-04 09:16:29 +00:00
|
|
|
{ "updateformat", FO_UPDATEFORMAT },
|
2001-11-02 16:18:07 +00:00
|
|
|
{ "updateresult", FO_UPDATERESULT }
|
2000-06-12 11:27:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
pushpophelper pph(lex, formattags, FO_END);
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2001-08-06 19:13:25 +00:00
|
|
|
while (lex.isOK()) {
|
2000-11-04 10:00:12 +00:00
|
|
|
switch (lex.lex()) {
|
2000-06-12 11:27:15 +00:00
|
|
|
case FO_PRODUCT:
|
|
|
|
lex.next(true);
|
2001-08-06 19:13:25 +00:00
|
|
|
product = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2003-06-04 09:16:29 +00:00
|
|
|
case FO_UPDATEFORMAT:
|
2000-06-12 11:27:15 +00:00
|
|
|
lex.next(true);
|
2003-06-04 09:16:29 +00:00
|
|
|
updateFormat = lex.getString();
|
2001-11-02 16:18:07 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FO_UPDATERESULT:
|
|
|
|
lex.next(true);
|
|
|
|
updateResult = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case FO_REQUIREMENT:
|
|
|
|
lex.next(true);
|
2001-08-06 19:13:25 +00:00
|
|
|
requirement = lex.getString();
|
2000-06-12 11:27:15 +00:00
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case FO_PREAMBLE:
|
|
|
|
preamble = lex.getLongString("preambleend");
|
|
|
|
break;
|
2002-03-21 17:09:55 +00:00
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
case FO_END:
|
2000-12-28 16:22:28 +00:00
|
|
|
if (lyxerr.debugging())
|
|
|
|
lex.printError("FormatEnd");
|
2000-06-12 11:27:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|