X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FExternalTemplate.C;h=b27255d48dabffac411b0459834242459655f2eb;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=3c997c56baf1045b3d3d5e9c4bfe55662feae009;hpb=27f298812d089d601ba1a2f96d9b584d25268497;p=lyx.git diff --git a/src/insets/ExternalTemplate.C b/src/insets/ExternalTemplate.C index 3c997c56ba..b27255d48d 100644 --- a/src/insets/ExternalTemplate.C +++ b/src/insets/ExternalTemplate.C @@ -4,6 +4,7 @@ * Licence details can be found in the file COPYING. * * \author Asger Alstrup Nielsen + * \author Angus Leeming * * Full author contact details are available in file CREDITS. */ @@ -17,25 +18,37 @@ #include "support/filetools.h" #include "support/lstrings.h" +#include "support/package.h" #include "support/path.h" -#include "support/path_defines.h" - -#include - -#include - -namespace support = lyx::support; using std::endl; using std::for_each; - +using std::string; using std::ostream; using std::vector; - namespace lyx { namespace external { + +typedef Translator TransformIDTranslator; + +static TransformIDTranslator const initIDTranslator() +{ + TransformIDTranslator translator(TransformID(-1), ""); + translator.addPair(Rotate, "Rotate"); + translator.addPair(Resize, "Resize"); + translator.addPair(Clip, "Clip"); + translator.addPair(Extra, "Extra"); + return translator; +} + +static TransformIDTranslator const & transformIDTranslator() +{ + static TransformIDTranslator const translator = initIDTranslator(); + return translator; +} + // We have to have dummy default commands for security reasons! Template::Template() : inputFormat("*") @@ -48,7 +61,7 @@ Template::Format::Format() TemplateManager::TemplateManager() { - readTemplates(support::user_lyxdir()); + readTemplates(support::package().user_support()); if (lyxerr.debugging(Debug::EXTERNAL)) { dumpPreambleDefs(lyxerr); lyxerr << '\n'; @@ -90,8 +103,16 @@ public: << "\tHelpTextEnd\n" << "\tInputFormat " << et.inputFormat << '\n' << "\tFileFilter " << et.fileRegExp << '\n' - << "\tEditCommand " << et.editCommand << '\n' << "\tAutomaticProduction " << et.automaticProduction << '\n'; + + typedef vector IDs; + IDs::const_iterator it = et.transformIds.begin(); + IDs::const_iterator end = et.transformIds.end(); + for (; it != end; ++it) { + ost << "\tTransform " + << transformIDTranslator().find(*it) << '\n'; + } + et.dumpFormats(ost); ost << "TemplateEnd" << endl; @@ -107,18 +128,47 @@ public: dumpFormat(ostream & o) : ost(o) {} - void operator()(value_type const & vt) const{ + void operator()(value_type const & vt) const { Template::Format const & ft = vt.second; 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'; + << "\t\tUpdateResult " << ft.updateResult << '\n'; - vector::const_iterator it = ft.preambleNames.begin(); - vector::const_iterator end = ft.preambleNames.end(); - for (; it != end; ++it) { - ost << "\t\tPreamble " << *it << '\n'; + vector::const_iterator qit = ft.requirements.begin(); + vector::const_iterator qend = ft.requirements.end(); + for (; qit != qend; ++qit) { + lyxerr << "req:" << *qit << std::endl; + ost << "\t\tRequirement " << *qit << '\n'; + } + + typedef vector Options; + Options::const_iterator oit = ft.options.begin(); + Options::const_iterator oend = ft.options.end(); + for (; oit != oend; ++oit) { + ost << "\t\tOption " + << oit->name + << ": " + << oit->option + << '\n'; + } + + vector::const_iterator pit = ft.preambleNames.begin(); + vector::const_iterator pend = ft.preambleNames.end(); + for (; pit != pend; ++pit) { + ost << "\t\tPreamble " << *pit << '\n'; + } + + typedef Template::Format::FileMap FileMap; + FileMap::const_iterator rit = ft.referencedFiles.begin(); + FileMap::const_iterator rend = ft.referencedFiles.end(); + for (; rit != rend; ++rit) { + vector::const_iterator fit = rit->second.begin(); + vector::const_iterator fend = rit->second.end(); + for (; fit != fend; ++fit) { + ost << "\t\tReferencedFile " << rit->first + << " \"" << *fit << "\"\n"; + } } ost << "\tFormatEnd\n"; @@ -153,13 +203,6 @@ TemplateManager & TemplateManager::get() } -TemplateManager::Templates & -TemplateManager::getTemplates() -{ - return templates; -} - - TemplateManager::Templates const & TemplateManager::getTemplates() const { @@ -210,7 +253,7 @@ void TemplateManager::readTemplates(string const & path) LyXLex lex(templatetags, TM_TEMPLATE_END); - string filename = support::LibFileSearch("", "external_templates"); + support::FileName const filename = support::libFileSearch("", "external_templates"); if (filename.empty() || !lex.setFile(filename)) { lex.printError("external::TemplateManager::readTemplates: " "No template file"); @@ -250,6 +293,23 @@ void TemplateManager::readTemplates(string const & path) } +namespace { + +void add(vector & ids, string const & name) +{ + TransformID id = transformIDTranslator().find(name); + if (int(id) == -1) { + lyxerr << "external::Template::readTemplate\n" + << "Transform " << name << " is not recognized" + << std::endl; + } else { + ids.push_back(id); + } +} + +} // namespace anon + + void Template::readTemplate(LyXLex & lex) { enum TemplateOptionTags { @@ -257,21 +317,21 @@ void Template::readTemplate(LyXLex & lex) TO_HELPTEXT, TO_INPUTFORMAT, TO_FILTER, - TO_EDITCMD, TO_AUTOMATIC, + TO_TRANSFORM, 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 }, { "inputformat", TO_INPUTFORMAT }, - { "templateend", TO_END } + { "templateend", TO_END }, + { "transform", TO_TRANSFORM } }; pushpophelper pph(lex, templateoptiontags, TO_END); @@ -297,16 +357,16 @@ void Template::readTemplate(LyXLex & lex) fileRegExp = lex.getString(); break; - case TO_EDITCMD: - lex.next(true); - editCommand = lex.getString(); - break; - case TO_AUTOMATIC: lex.next(); automaticProduction = lex.getBool(); break; + case TO_TRANSFORM: + lex.next(true); + add(transformIds, lex.getString()); + break; + case TO_FORMAT: lex.next(true); formats[lex.getString()].readFormat(lex); @@ -325,6 +385,87 @@ void Template::readTemplate(LyXLex & lex) } +namespace { + +void transform_not_found(std::ostream & os, string const & transform) +{ + os << "external::Format::readFormat. Transformation \"" + << transform << "\" is unrecognized." << std::endl; +} + + +void transform_class_not_found(std::ostream & os, string const & tclass) +{ + os << "external::Format::readFormat. Transformation class \"" + << tclass << "\" is unrecognized." << std::endl; +} + + +void setCommandFactory(Template::Format & format, string const & transform, + string const & transformer_class) +{ + bool class_found = false; + if (transform == "Resize" && transformer_class == "ResizeLatexCommand") { + class_found = true; + ResizeCommandFactory factory = ResizeLatexCommand::factory; + format.command_transformers[Resize] = + TransformStore(Resize, factory); + + } else if (transform == "Rotate" && + transformer_class == "RotationLatexCommand") { + class_found = true; + RotationCommandFactory factory = RotationLatexCommand::factory; + format.command_transformers[Rotate] = + TransformStore(Rotate, factory); + + } else + transform_not_found(lyxerr, transform); + + if (!class_found) + transform_class_not_found(lyxerr, transformer_class); +} + + +void setOptionFactory(Template::Format & format, string const & transform, + string const & transformer_class) +{ + bool class_found = false; + if (transform == "Clip" && transformer_class == "ClipLatexOption") { + class_found = true; + ClipOptionFactory factory = ClipLatexOption::factory; + format.option_transformers[Clip] = + TransformStore(Clip, factory); + + } else if (transform == "Extra" && transformer_class == "ExtraOption") { + class_found = true; + ExtraOptionFactory factory = ExtraOption::factory; + format.option_transformers[Extra] = + TransformStore(Extra, factory); + + } else if (transform == "Resize" && + transformer_class == "ResizeLatexOption") { + class_found = true; + ResizeOptionFactory factory = ResizeLatexOption::factory; + format.option_transformers[Resize] = + TransformStore(Resize, factory); + + } else if (transform == "Rotate" && + transformer_class == "RotationLatexOption") { + class_found = true; + RotationOptionFactory factory = RotationLatexOption::factory; + format.option_transformers[Rotate] = + TransformStore(Rotate, factory); + + } else + transform_not_found(lyxerr, transform); + + if (!class_found) + transform_class_not_found(lyxerr, transformer_class); +} + +} // namespace anon + + void Template::Format::readFormat(LyXLex & lex) { enum FormatTags { @@ -332,15 +473,23 @@ void Template::Format::readFormat(LyXLex & lex) FO_UPDATEFORMAT, FO_UPDATERESULT, FO_REQUIREMENT, + FO_OPTION, FO_PREAMBLE, + FO_TRANSFORMCOMMAND, + FO_TRANSFORMOPTION, + FO_REFERENCEDFILE, FO_END }; keyword_item formattags[] = { { "formatend", FO_END }, + { "option", FO_OPTION }, { "preamble", FO_PREAMBLE }, { "product", FO_PRODUCT }, + { "referencedfile", FO_REFERENCEDFILE }, { "requirement", FO_REQUIREMENT }, + { "transformcommand", FO_TRANSFORMCOMMAND }, + { "transformoption", FO_TRANSFORMOPTION }, { "updateformat", FO_UPDATEFORMAT }, { "updateresult", FO_UPDATERESULT } }; @@ -366,7 +515,7 @@ void Template::Format::readFormat(LyXLex & lex) case FO_REQUIREMENT: lex.next(true); - requirement = lex.getString(); + requirements.push_back(lex.getString()); break; case FO_PREAMBLE: @@ -374,6 +523,40 @@ void Template::Format::readFormat(LyXLex & lex) preambleNames.push_back(lex.getString()); break; + case FO_TRANSFORMCOMMAND: { + lex.next(true); + string const name = lex.getString(); + lex.next(true); + setCommandFactory(*this, name, lex.getString()); + break; + } + + case FO_TRANSFORMOPTION: { + lex.next(true); + string const name = lex.getString(); + lex.next(true); + setOptionFactory(*this, name, lex.getString()); + break; + } + + case FO_OPTION: { + lex.next(true); + string const name = lex.getString(); + lex.next(true); + string const opt = lex.getString(); + options.push_back(Option(name, opt)); + break; + } + + case FO_REFERENCEDFILE: { + lex.next(true); + string const format = lex.getString(); + lex.next(true); + string const file = lex.getString(); + referencedFiles[format].push_back(file); + break; + } + case FO_END: return; }