X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Flyxtextclass.C;h=ffcf4741761976e09c2dbf8b44588daeeaa886f6;hb=6c2e78e12855d8661eb289ded3dc8722748863b8;hp=5231cf12ac9860a158c5e07124aed14c1d173a55;hpb=92a24f6f9966dba3a09b625d4d3d81fb2dd888ba;p=lyx.git diff --git a/src/lyxtextclass.C b/src/lyxtextclass.C index 5231cf12ac..ffcf474176 100644 --- a/src/lyxtextclass.C +++ b/src/lyxtextclass.C @@ -22,44 +22,78 @@ #include "FloatList.h" #include "support/lstrings.h" +#include "support/lyxlib.h" #include "support/filetools.h" +#include + using lyx::support::LibFileSearch; using lyx::support::MakeDisplayPath; +using lyx::support::QuoteName; using lyx::support::rtrim; using lyx::support::subst; using std::endl; using std::find_if; using std::remove_if; - +using std::string; using std::ostream; -namespace { // anon +namespace { -struct compare_name { - - compare_name(string const & name) +class LayoutNamesEqual : public std::unary_function { +public: + LayoutNamesEqual(string const & name) : name_(name) {} - - bool operator()(boost::shared_ptr const & c) + bool operator()(LyXLayout_ptr const & c) const { return c->name() == name_; } - +private: string name_; - }; -} // anon + +int const FORMAT = 2; + + +bool layout2layout(string const & filename, string const & tempfile) +{ + string const script = LibFileSearch("scripts", "layout2layout.py"); + if (script.empty()) { + lyxerr << "Could not find layout conversion " + "script layout2layout.py." << endl; + return false; + } + + std::ostringstream command; + command << "python " << QuoteName(script) + << ' ' << QuoteName(filename) + << ' ' << QuoteName(tempfile); + string const command_str = command.str(); + + lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl; + + lyx::support::cmd_ret const ret = + lyx::support::RunCommand(command_str); + if (ret.first != 0) { + lyxerr << "Could not run layout conversion " + "script layout2layout.py." << endl; + return false; + } + return true; +} + +} // namespace anon LyXTextClass::LyXTextClass(string const & fn, string const & cln, string const & desc, bool texClassAvail ) : name_(fn), latexname_(cln), description_(desc), - floatlist_(new FloatList), ctrs_(new Counters), texClassAvail_(texClassAvail) + floatlist_(new FloatList), ctrs_(new Counters), + texClassAvail_(texClassAvail) { outputType_ = LATEX; columns_ = 1; @@ -67,14 +101,13 @@ LyXTextClass::LyXTextClass(string const & fn, string const & cln, secnumdepth_ = 3; tocdepth_ = 3; pagestyle_ = "default"; - maxcounter_ = 4; defaultfont_ = LyXFont(LyXFont::ALL_SANE); opt_fontsize_ = "10|11|12"; opt_pagestyle_ = "empty|plain|headings|fancy"; provides_ = nothing; titletype_ = TITLE_COMMAND_AFTER; titlename_ = "maketitle"; - loaded = false; + loaded_ = false; } @@ -105,13 +138,13 @@ enum TextClassTags { TC_INPUT, TC_STYLE, TC_DEFAULTSTYLE, + TC_CHARSTYLE, TC_ENVIRONMENT, TC_NOSTYLE, TC_COLUMNS, TC_SIDES, TC_PAGESTYLE, TC_DEFAULTFONT, - TC_MAXCOUNTER, TC_SECNUMDEPTH, TC_TOCDEPTH, TC_CLASSOPTIONS, @@ -126,13 +159,22 @@ enum TextClassTags { TC_COUNTER, TC_NOFLOAT, TC_TITLELATEXNAME, - TC_TITLELATEXTYPE + TC_TITLELATEXTYPE, + TC_FORMAT }; + // Reads a textclass structure from file. bool LyXTextClass::Read(string const & filename, bool merge) { + if (!lyx::support::IsFileReadable(filename)) { + lyxerr << "Cannot read layout file `" << filename << "'." + << endl; + return true; + } + keyword_item textClassTags[] = { + { "charstyle", TC_CHARSTYLE }, { "classoptions", TC_CLASSOPTIONS }, { "columns", TC_COLUMNS }, { "counter", TC_COUNTER }, @@ -140,9 +182,9 @@ bool LyXTextClass::Read(string const & filename, bool merge) { "defaultstyle", TC_DEFAULTSTYLE }, { "environment", TC_ENVIRONMENT }, { "float", TC_FLOAT }, + { "format", TC_FORMAT }, { "input", TC_INPUT }, { "leftmargin", TC_LEFTMARGIN }, - { "maxcounter", TC_MAXCOUNTER }, { "nofloat", TC_NOFLOAT }, { "nostyle", TC_NOSTYLE }, { "outputtype", TC_OUTPUTTYPE }, @@ -172,10 +214,12 @@ bool LyXTextClass::Read(string const & filename, bool merge) LyXLex lexrc(textClassTags, sizeof(textClassTags) / sizeof(textClassTags[0])); - bool error = false; lexrc.setFile(filename); - if (!lexrc.isOK()) error = true; + bool error = !lexrc.isOK(); + + // Format of files before the 'Format' tag was introduced + int format = 1; // parsing while (lexrc.isOK() && !error) { @@ -196,6 +240,11 @@ bool LyXTextClass::Read(string const & filename, bool merge) switch (static_cast(le)) { + case TC_FORMAT: + if (lexrc.next()) + format = lexrc.getInteger(); + break; + case TC_OUTPUTTYPE: // output type definition readOutputType(lexrc); break; @@ -235,9 +284,12 @@ bool LyXTextClass::Read(string const & filename, bool merge) lay.setName(name); if (le == TC_ENVIRONMENT) lay.is_environment = true; - if (!(error = do_readStyle(lexrc, lay))) - layoutlist_.push_back - (boost::shared_ptr(new LyXLayout(lay))); + error = do_readStyle(lexrc, lay); + if (!error) + layoutlist_.push_back( + boost::shared_ptr(new LyXLayout(lay)) + ); + if (defaultlayout_.empty()) { // We do not have a default // layout yet, so we choose @@ -299,11 +351,6 @@ bool LyXTextClass::Read(string const & filename, bool merge) } break; - case TC_MAXCOUNTER: - lexrc.next(); - maxcounter_ = lexrc.getInteger(); - break; - case TC_SECNUMDEPTH: lexrc.next(); secnumdepth_ = lexrc.getInteger(); @@ -352,6 +399,12 @@ bool LyXTextClass::Read(string const & filename, bool merge) if (lexrc.next()) rightmargin_ = lexrc.getString(); break; + case TC_CHARSTYLE: + if (lexrc.next()) { + string const name = subst(lexrc.getString(), '_', ' '); + readCharStyle(lexrc, name); + } + break; case TC_FLOAT: readFloat(lexrc); break; @@ -372,6 +425,19 @@ bool LyXTextClass::Read(string const & filename, bool merge) } break; } + if (format != FORMAT) + break; + } + + if (format != FORMAT) { + lyxerr[Debug::TCLASS] << "Converting layout file from format " + << format << " to " << FORMAT << endl; + string const tempfile = lyx::support::tempName(); + error = !layout2layout(filename, tempfile); + if (!error) + error = Read(tempfile, merge); + lyx::support::unlink(tempfile); + return error; } if (!merge) { // we are at top level here. @@ -383,6 +449,27 @@ bool LyXTextClass::Read(string const & filename, bool merge) << "' is missing a defaultstyle." << endl; error = true; } + + min_toclevel_ = LyXLayout::NOT_IN_TOC; + max_toclevel_ = LyXLayout::NOT_IN_TOC; + const_iterator cit = begin(); + const_iterator the_end = end(); + for ( ; cit != the_end ; ++cit) { + int const toclevel = (*cit)->toclevel; + if (toclevel != LyXLayout::NOT_IN_TOC) { + if (min_toclevel_ == LyXLayout::NOT_IN_TOC) + min_toclevel_ = toclevel; + else + min_toclevel_ = std::min(min_toclevel_, + toclevel); + max_toclevel_ = std::max(max_toclevel_, + toclevel); + } + } + lyxerr[Debug::TCLASS] + << "Minimum TocLevel is " << min_toclevel_ + << ", maximum is " << max_toclevel_ <(le)) { + case CS_LATEXTYPE: + lexrc.next(); + latextype = lexrc.getString(); + break; + case CS_LATEXNAME: + lexrc.next(); + latexname = lexrc.getString(); + break; + case CS_LATEXPARAM: + lexrc.next(); + latexparam = subst(lexrc.getString(), """, "\""); + break; + case CS_LABELFONT: + labelfont.lyxRead(lexrc); + break; + case CS_FONT: + font.lyxRead(lexrc); + labelfont = font; + break; + case CS_PREAMBLE: + preamble = lexrc.getLongString("EndPreamble"); + break; + case CS_END: + getout = true; + break; + } + } + + // + // Here add element to list if getout == true + if (getout) { + CharStyle cs; + cs.name = name; + cs.latextype = latextype; + cs.latexname = latexname; + cs.latexparam = latexparam; + cs.font = font; + cs.labelfont = labelfont; + cs.preamble = preamble; + charstyles().push_back(cs); + } + + lexrc.popTable(); +} + enum FloatTags { FT_TYPE = 1, @@ -525,6 +691,7 @@ enum FloatTags { FT_END }; + void LyXTextClass::readFloat(LyXLex & lexrc) { keyword_item floatTags[] = { @@ -694,7 +861,7 @@ bool LyXTextClass::hasLayout(string const & n) const string const name = (n.empty() ? defaultLayoutName() : n); return find_if(layoutlist_.begin(), layoutlist_.end(), - compare_name(name)) + LayoutNamesEqual(name)) != layoutlist_.end(); } @@ -707,7 +874,7 @@ LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const LayoutList::const_iterator cit = find_if(layoutlist_.begin(), layoutlist_.end(), - compare_name(name)); + LayoutNamesEqual(name)); if (cit == layoutlist_.end()) { lyxerr << "We failed to find the layout '" << name @@ -733,7 +900,7 @@ bool LyXTextClass::delete_layout(string const & name) LayoutList::iterator it = remove_if(layoutlist_.begin(), layoutlist_.end(), - compare_name(name)); + LayoutNamesEqual(name)); LayoutList::iterator end = layoutlist_.end(); bool const ret = (it != end); @@ -745,22 +912,22 @@ bool LyXTextClass::delete_layout(string const & name) // Load textclass info if not loaded yet bool LyXTextClass::load() const { - if (loaded) + if (loaded_) return true; // Read style-file string const real_file = LibFileSearch("layouts", name_, "layout"); + loaded_ = const_cast(this)->Read(real_file) == 0; - if (const_cast(this)->Read(real_file)) { + if (!loaded_) { lyxerr << "Error reading `" << MakeDisplayPath(real_file) << "'\n(Check `" << name_ << "')\nCheck your installation and " "try Options/Reconfigure..." << endl; - loaded = false; } - loaded = true; - return loaded; + + return loaded_; } @@ -782,6 +949,18 @@ Counters & LyXTextClass::counters() const } +CharStyles::iterator LyXTextClass::charstyle(string const & s) const +{ + CharStyles::iterator cs = charstyles().begin(); + CharStyles::iterator csend = charstyles().end(); + for (; cs != csend; ++cs) { + if (cs->name == s) + return cs; + } + return csend; +} + + string const & LyXTextClass::defaultLayoutName() const { // This really should come from the actual layout... (Lgb) @@ -832,6 +1011,12 @@ string const & LyXTextClass::options() const } +string const & LyXTextClass::class_header() const +{ + return class_header_; +} + + string const & LyXTextClass::pagestyle() const { return pagestyle_; @@ -880,12 +1065,6 @@ unsigned int LyXTextClass::columns() const } -int LyXTextClass::maxcounter() const -{ - return maxcounter_; -} - - LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const { return titletype_; @@ -898,12 +1077,21 @@ string const & LyXTextClass::titlename() const } +int LyXTextClass::size() const +{ + return layoutlist_.size(); +} + +int LyXTextClass::min_toclevel() const +{ + return min_toclevel_; +} -int LyXTextClass::size() const +int LyXTextClass::max_toclevel() const { - return layoutlist_.size(); + return max_toclevel_; }