X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2FExternalTemplate.cpp;h=e7eca8b274fd303a4aef185c8e6ca75fbd95fc08;hb=997f0621c5346bb623cf86713c4fe1be0c941104;hp=cede8a1d4ee18b77cfc3acaaf80ff03f8210bac8;hpb=3b7b39b614556398bf79a0f4f4f2a6294ec5fe80;p=lyx.git diff --git a/src/insets/ExternalTemplate.cpp b/src/insets/ExternalTemplate.cpp index cede8a1d4e..e7eca8b274 100644 --- a/src/insets/ExternalTemplate.cpp +++ b/src/insets/ExternalTemplate.cpp @@ -19,8 +19,10 @@ #include "support/filetools.h" #include "support/lstrings.h" #include "support/Package.h" -#include "support/Path.h" +#include "support/PathChanger.h" +#include "support/Translator.h" +#include #include using namespace std; @@ -34,7 +36,7 @@ typedef Translator TransformIDTranslator; static TransformIDTranslator const initIDTranslator() { - TransformIDTranslator translator(TransformID(-1), ""); + TransformIDTranslator translator(None, ""); translator.addPair(Rotate, "Rotate"); translator.addPair(Resize, "Resize"); translator.addPair(Clip, "Clip"); @@ -50,7 +52,7 @@ static TransformIDTranslator const & transformIDTranslator() // We have to have dummy default commands for security reasons! Template::Template() - : inputFormat("*") + : inputFormat("*"), automaticProduction(false), preview_mode(PREVIEW_OFF) {} @@ -69,67 +71,66 @@ TemplateManager::TemplateManager() } -class DumpPreambleDef { -public: - typedef TemplateManager::PreambleDefs::value_type value_type; - - DumpPreambleDef(ostream & os) : os_(os) {} - - void operator()(value_type const & vt) { - os_ << "PreambleDef " << vt.first << '\n' - << vt.second - << "PreambleDefEnd" << endl; - } - -private: - ostream & os_; -}; - - -class DumpTemplate { -public: - typedef TemplateManager::Templates::value_type value_type; +std::function + DumpPreambleDef(ostream & os) +{ + return [&os](TemplateManager::PreambleDefs::value_type const & vt) + { + os << "PreambleDef " << vt.first << '\n' + << to_utf8(vt.second) + << "PreambleDefEnd" << endl; + }; +} - DumpTemplate(ostream & os) : os_(os) {} - void operator()(value_type const & vt) { +std::function + DumpTemplate(ostream & os) +{ + return [&os](TemplateManager::Templates::value_type const & vt) { Template const & et = vt.second; - os_ << "Template " << et.lyxName << '\n' + os << "Template " << et.lyxName << '\n' << "\tGuiName " << et.guiName << '\n' << "\tHelpText\n" - << et.helpText + << to_utf8(et.helpText) << "\tHelpTextEnd\n" << "\tInputFormat " << et.inputFormat << '\n' << "\tFileFilter " << et.fileRegExp << '\n' - << "\tAutomaticProduction " << et.automaticProduction << '\n'; - + << "\tAutomaticProduction " << et.automaticProduction << '\n' + << "\tPreview "; + switch (et.preview_mode) { + case PREVIEW_OFF: + os << "Off\n"; + break; + case PREVIEW_GRAPHICS: + os << "Graphics\n"; + break; + case PREVIEW_INSTANT: + os << "InstantPreview\n"; + break; + } typedef vector IDs; IDs::const_iterator it = et.transformIds.begin(); IDs::const_iterator end = et.transformIds.end(); for (; it != end; ++it) { - os_ << "\tTransform " + os << "\tTransform " << transformIDTranslator().find(*it) << '\n'; } - et.dumpFormats(os_); - os_ << "TemplateEnd" << endl; - - } - -private: - ostream & os_; -}; + et.dumpFormats(os); + os << "TemplateEnd" << endl; -class DumpFormat { -public: - typedef Template::Formats::value_type value_type; + }; +} - DumpFormat(ostream & o) : os_(o) {} - void operator()(value_type const & vt) const { +std::function + DumpFormat (ostream & os) +{ + return [&os](Template::Formats::value_type const & vt) + { Template::Format const & ft = vt.second; - os_ << "\tFormat " << vt.first << '\n' + os << "\tFormat " << vt.first << '\n' << "\t\tProduct " << ft.product << '\n' << "\t\tUpdateFormat " << ft.updateFormat << '\n' << "\t\tUpdateResult " << ft.updateResult << '\n'; @@ -138,14 +139,14 @@ public: vector::const_iterator qend = ft.requirements.end(); for (; qit != qend; ++qit) { lyxerr << "req:" << *qit << endl; - os_ << "\t\tRequirement " << *qit << '\n'; + os << "\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) { - os_ << "\t\tOption " + os << "\t\tOption " << oit->name << ": " << oit->option @@ -155,7 +156,7 @@ public: vector::const_iterator pit = ft.preambleNames.begin(); vector::const_iterator pend = ft.preambleNames.end(); for (; pit != pend; ++pit) { - os_ << "\t\tPreamble " << *pit << '\n'; + os << "\t\tPreamble " << *pit << '\n'; } typedef Template::Format::FileMap FileMap; @@ -165,16 +166,14 @@ public: vector::const_iterator fit = rit->second.begin(); vector::const_iterator fend = rit->second.end(); for (; fit != fend; ++fit) { - os_ << "\t\tReferencedFile " << rit->first + os << "\t\tReferencedFile " << rit->first << " \"" << *fit << "\"\n"; } } - os_ << "\tFormatEnd\n"; - } -private: - ostream & os_; -}; + os << "\tFormatEnd\n"; + }; +} void Template::dumpFormats(ostream & os) const @@ -216,16 +215,15 @@ TemplateManager::getTemplateByName(string const & name) const } -string const -TemplateManager::getPreambleDefByName(string const & name) const +docstring TemplateManager::getPreambleDefByName(string const & name) const { string const trimmed_name = trim(name); if (trimmed_name.empty()) - return string(); + return docstring(); PreambleDefs::const_iterator it = preambledefs.find(trimmed_name); if (it == preambledefs.end()) - return string(); + return docstring(); return it->second; } @@ -249,43 +247,91 @@ void TemplateManager::readTemplates(FileName const & path) { "templateend", TM_TEMPLATE_END } }; - Lexer lex(templatetags); + // Read the templates list + vector templateslist; + FileName const real_file = libFileSearch("", "xtemplates.lst"); + LYXERR(Debug::EXTERNAL, "Reading external templates from `" << real_file << '\''); - FileName const filename = libFileSearch("", "external_templates"); - if (filename.empty() || !lex.setFile(filename)) { - lex.printError("external::TemplateManager::readTemplates: " - "No template file"); + if (real_file.empty()) { + LYXERR0("unable to find external templates file `xtemplates.lst'.\n" + << "No external templates will be available."); return; } - char const * const preamble_end_tag = - templatetags[TM_PREAMBLEDEF_END-1].tag; + Lexer tlex; + if (!tlex.setFile(real_file)) { + LYXERR0("lyxlex was not able to set file: " + << real_file << ".\nNo external templates will be available."); + return; + } - while (lex.isOK()) { - switch (lex.lex()) { - case TM_PREAMBLEDEF: { - lex.next(); - string const name = lex.getString(); - preambledefs[name] = lex.getLongString(preamble_end_tag); + if (!tlex.isOK()) { + LYXERR0("unable to open external templates file `" + << to_utf8(makeDisplayPath(real_file.absFileName(), 1000)) + << "'\nNo external templates will be available."); + return; + } + + bool finished = false; + // Parse external templates files + LYXERR(Debug::EXTERNAL, "Starting parsing of xtemplates.lst"); + while (tlex.isOK() && !finished) { + LYXERR(Debug::EXTERNAL, "\tline by line"); + switch (tlex.lex()) { + case Lexer::LEX_FEOF: + finished = true; + break; + default: + string const name = tlex.getString(); + LYXERR(Debug::EXTERNAL, "Template name: " << name); + templateslist.push_back(name); + break; } - break; + } - case TM_TEMPLATE: { - lex.next(); - string const name = lex.getString(); - Template & tmp = templates[name]; - tmp.lyxName = name; - tmp.readTemplate(lex); + LYXERR(Debug::EXTERNAL, "End of parsing of xtemplates.lst"); + + for (vector::const_iterator it = templateslist.begin(); it != templateslist.end(); ++it) { + FileName const filename = libFileSearch("xtemplates", *it); + LYXERR(Debug::EXTERNAL, "Reading template file " << filename.absFileName()); + Lexer lex(templatetags); + if (filename.empty() || !lex.setFile(filename)) { + lex.printError("external::TemplateManager::readTemplates: " + "No template file"); + return; } - break; - case TM_TEMPLATE_END: - lex.printError("Warning: End outside Template."); - break; + char const * const preamble_end_tag = + templatetags[TM_PREAMBLEDEF_END-1].tag; - case TM_PREAMBLEDEF_END: - lex.printError("Warning: End outside PreambleDef."); - break; + while (lex.isOK()) { + switch (lex.lex()) { + case TM_PREAMBLEDEF: { + lex.next(); + string const name = lex.getString(); + preambledefs[name] = lex.getLongString(from_ascii(preamble_end_tag)); + break; + } + + case TM_TEMPLATE: { + lex.next(); + string const name = lex.getString(); + Template & tmp = templates[name]; + tmp.lyxName = name; + tmp.readTemplate(lex); + break; + } + + case TM_TEMPLATE_END: + lex.printError("Warning: End outside Template."); + break; + + case TM_PREAMBLEDEF_END: + lex.printError("Warning: End outside PreambleDef."); + break; + default: + break; + } } } } @@ -299,6 +345,7 @@ void Template::readTemplate(Lexer & lex) TO_INPUTFORMAT, TO_FILTER, TO_AUTOMATIC, + TO_PREVIEW, TO_TRANSFORM, TO_FORMAT, TO_END @@ -311,6 +358,7 @@ void Template::readTemplate(Lexer & lex) { "guiname", TO_GUINAME }, { "helptext", TO_HELPTEXT }, { "inputformat", TO_INPUTFORMAT }, + { "preview", TO_PREVIEW }, { "templateend", TO_END }, { "transform", TO_TRANSFORM } }; @@ -327,7 +375,7 @@ void Template::readTemplate(Lexer & lex) break; case TO_HELPTEXT: - helpText = lex.getLongString("HelpTextEnd"); + helpText = lex.getLongString(from_ascii("HelpTextEnd")); break; case TO_INPUTFORMAT: @@ -345,6 +393,16 @@ void Template::readTemplate(Lexer & lex) automaticProduction = lex.getBool(); break; + case TO_PREVIEW: + lex >> token; + if (token == "InstantPreview") + preview_mode = PREVIEW_INSTANT; + else if (token == "Graphics") + preview_mode = PREVIEW_GRAPHICS; + else + preview_mode = PREVIEW_OFF; + break; + case TO_TRANSFORM: { lex >> token; TransformID id = transformIDTranslator().find(token); @@ -362,12 +420,6 @@ void Template::readTemplate(Lexer & lex) case TO_END: return; - - default: - lex.printError("external::Template::readTemplate: " - "Wrong tag: $$Token"); - BOOST_ASSERT(false); - break; } } } @@ -451,7 +503,7 @@ void setOptionFactory(Template::Format & format, string const & transform, transform_class_not_found(lyxerr, transformer_class); } -} // namespace anon +} // namespace void Template::Format::readFormat(Lexer & lex)