3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
8 * \author Angus Leeming
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
17 #include "lyxtextclass.h"
22 #include "FloatList.h"
24 #include "support/lstrings.h"
25 #include "support/lyxlib.h"
26 #include "support/filetools.h"
27 #include <boost/filesystem/operations.hpp>
28 namespace fs = boost::filesystem;
32 using lyx::support::libFileSearch;
33 using lyx::support::makeDisplayPath;
34 using lyx::support::quoteName;
35 using lyx::support::rtrim;
36 using lyx::support::subst;
47 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
49 LayoutNamesEqual(string const & name)
52 bool operator()(LyXLayout_ptr const & c) const
54 return c->name() == name_;
64 bool layout2layout(string const & filename, string const & tempfile)
66 string const script = libFileSearch("scripts", "layout2layout.py");
68 lyxerr << "Could not find layout conversion "
69 "script layout2layout.py." << endl;
73 std::ostringstream command;
74 command << "python " << quoteName(script)
75 << ' ' << quoteName(filename)
76 << ' ' << quoteName(tempfile);
77 string const command_str = command.str();
79 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
81 lyx::support::cmd_ret const ret =
82 lyx::support::runCommand(command_str);
84 lyxerr << "Could not run layout conversion "
85 "script layout2layout.py." << endl;
94 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
95 string const & desc, bool texClassAvail )
96 : name_(fn), latexname_(cln), description_(desc),
97 floatlist_(new FloatList), ctrs_(new Counters),
98 texClassAvail_(texClassAvail)
105 pagestyle_ = "default";
106 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
107 opt_fontsize_ = "10|11|12";
108 opt_pagestyle_ = "empty|plain|headings|fancy";
110 titletype_ = TITLE_COMMAND_AFTER;
111 titlename_ = "maketitle";
116 bool LyXTextClass::isTeXClassAvailable() const
118 return texClassAvail_;
122 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
124 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
125 if (!lay.read(lexrc, *this)) {
127 lay.resfont = lay.font;
128 lay.resfont.realize(defaultfont());
129 lay.reslabelfont = lay.labelfont;
130 lay.reslabelfont.realize(defaultfont());
131 return false; // no errors
133 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
169 // Reads a textclass structure from file.
170 bool LyXTextClass::read(string const & filename, bool merge)
172 if (!lyx::support::isFileReadable(filename)) {
173 lyxerr << "Cannot read layout file `" << filename << "'."
178 keyword_item textClassTags[] = {
179 { "charstyle", TC_CHARSTYLE },
180 { "classoptions", TC_CLASSOPTIONS },
181 { "columns", TC_COLUMNS },
182 { "counter", TC_COUNTER },
183 { "defaultfont", TC_DEFAULTFONT },
184 { "defaultstyle", TC_DEFAULTSTYLE },
185 { "environment", TC_ENVIRONMENT },
186 { "float", TC_FLOAT },
187 { "format", TC_FORMAT },
188 { "input", TC_INPUT },
189 { "leftmargin", TC_LEFTMARGIN },
190 { "nofloat", TC_NOFLOAT },
191 { "nostyle", TC_NOSTYLE },
192 { "outputtype", TC_OUTPUTTYPE },
193 { "pagestyle", TC_PAGESTYLE },
194 { "preamble", TC_PREAMBLE },
195 { "providesamsmath", TC_PROVIDESAMSMATH },
196 { "providesmakeidx", TC_PROVIDESMAKEIDX },
197 { "providesnatbib", TC_PROVIDESNATBIB },
198 { "providesurl", TC_PROVIDESURL },
199 { "rightmargin", TC_RIGHTMARGIN },
200 { "secnumdepth", TC_SECNUMDEPTH },
201 { "sides", TC_SIDES },
202 { "style", TC_STYLE },
203 { "titlelatexname", TC_TITLELATEXNAME },
204 { "titlelatextype", TC_TITLELATEXTYPE },
205 { "tocdepth", TC_TOCDEPTH }
209 lyxerr[Debug::TCLASS] << "Reading textclass "
210 << makeDisplayPath(filename)
213 lyxerr[Debug::TCLASS] << "Reading input file "
214 << makeDisplayPath(filename)
217 LyXLex lexrc(textClassTags,
218 sizeof(textClassTags) / sizeof(textClassTags[0]));
220 lexrc.setFile(filename);
221 bool error = !lexrc.isOK();
223 // Format of files before the 'Format' tag was introduced
227 while (lexrc.isOK() && !error) {
228 int le = lexrc.lex();
231 case LyXLex::LEX_FEOF:
234 case LyXLex::LEX_UNDEF:
235 lexrc.printError("Unknown TextClass tag `$$Token'");
243 switch (static_cast<TextClassTags>(le)) {
247 format = lexrc.getInteger();
250 case TC_OUTPUTTYPE: // output type definition
251 readOutputType(lexrc);
254 case TC_INPUT: // Include file
256 string tmp = libFileSearch("layouts",
260 if (read(tmp, true)) {
261 lexrc.printError("Error reading input"
268 case TC_DEFAULTSTYLE:
270 string const name = subst(lexrc.getString(),
272 defaultlayout_ = name;
279 string const name = subst(lexrc.getString(),
281 if (hasLayout(name)) {
282 LyXLayout * lay = operator[](name).get();
283 error = do_readStyle(lexrc, *lay);
287 if (le == TC_ENVIRONMENT)
288 lay.is_environment = true;
289 error = do_readStyle(lexrc, lay);
291 layoutlist_.push_back(
292 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
295 if (defaultlayout_.empty()) {
296 // We do not have a default
297 // layout yet, so we choose
298 // the first layout we
300 defaultlayout_ = name;
305 lexrc.printError("No name given for style: `$$Token'.");
312 string const style = subst(lexrc.getString(),
314 if (!delete_layout(style))
315 lyxerr << "Cannot delete style `"
316 << style << '\'' << endl;
317 // lexrc.printError("Cannot delete style"
324 columns_ = lexrc.getInteger();
329 switch (lexrc.getInteger()) {
330 case 1: sides_ = OneSide; break;
331 case 2: sides_ = TwoSides; break;
333 lyxerr << "Impossible number of page"
334 " sides, setting to one."
344 pagestyle_ = rtrim(lexrc.getString());
348 defaultfont_.lyxRead(lexrc);
349 if (!defaultfont_.resolved()) {
350 lexrc.printError("Warning: defaultfont should "
351 "be fully instantiated!");
352 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
358 secnumdepth_ = lexrc.getInteger();
363 tocdepth_ = lexrc.getInteger();
366 // First step to support options
367 case TC_CLASSOPTIONS:
368 readClassOptions(lexrc);
372 preamble_ = lexrc.getLongString("EndPreamble");
375 case TC_PROVIDESAMSMATH:
376 if (lexrc.next() && lexrc.getInteger())
377 provides_ |= amsmath;
380 case TC_PROVIDESNATBIB:
381 if (lexrc.next() && lexrc.getInteger())
385 case TC_PROVIDESMAKEIDX:
386 if (lexrc.next() && lexrc.getInteger())
387 provides_ |= makeidx;
391 if (lexrc.next() && lexrc.getInteger())
395 case TC_LEFTMARGIN: // left margin type
397 leftmargin_ = lexrc.getString();
400 case TC_RIGHTMARGIN: // right margin type
402 rightmargin_ = lexrc.getString();
406 string const name = subst(lexrc.getString(), '_', ' ');
407 readCharStyle(lexrc, name);
416 case TC_TITLELATEXTYPE:
417 readTitleType(lexrc);
419 case TC_TITLELATEXNAME:
421 titlename_ = lexrc.getString();
425 string const nofloat = lexrc.getString();
426 floatlist_->erase(nofloat);
430 if (format != FORMAT)
434 if (format != FORMAT) {
435 lyxerr[Debug::TCLASS] << "Converting layout file from format "
436 << format << " to " << FORMAT << endl;
437 string const tempfile = lyx::support::tempName();
438 error = !layout2layout(filename, tempfile);
440 error = read(tempfile, merge);
441 lyx::support::unlink(tempfile);
445 if (!merge) { // we are at top level here.
446 lyxerr[Debug::TCLASS] << "Finished reading textclass "
447 << makeDisplayPath(filename)
449 if (defaultlayout_.empty()) {
450 lyxerr << "Error: Textclass '" << name_
451 << "' is missing a defaultstyle." << endl;
455 min_toclevel_ = LyXLayout::NOT_IN_TOC;
456 max_toclevel_ = LyXLayout::NOT_IN_TOC;
457 const_iterator cit = begin();
458 const_iterator the_end = end();
459 for ( ; cit != the_end ; ++cit) {
460 int const toclevel = (*cit)->toclevel;
461 if (toclevel != LyXLayout::NOT_IN_TOC) {
462 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
463 min_toclevel_ = toclevel;
465 min_toclevel_ = std::min(min_toclevel_,
467 max_toclevel_ = std::max(max_toclevel_,
471 lyxerr[Debug::TCLASS]
472 << "Minimum TocLevel is " << min_toclevel_
473 << ", maximum is " << max_toclevel_ <<endl;
476 lyxerr[Debug::TCLASS] << "Finished reading input file "
477 << makeDisplayPath(filename)
484 void LyXTextClass::readTitleType(LyXLex & lexrc)
486 keyword_item titleTypeTags[] = {
487 { "commandafter", TITLE_COMMAND_AFTER },
488 { "environment", TITLE_ENVIRONMENT }
491 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
493 int le = lexrc.lex();
495 case LyXLex::LEX_UNDEF:
496 lexrc.printError("Unknown output type `$$Token'");
498 case TITLE_COMMAND_AFTER:
499 case TITLE_ENVIRONMENT:
500 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
503 lyxerr << "Unhandled value " << le
504 << " in LyXTextClass::readTitleType." << endl;
511 void LyXTextClass::readOutputType(LyXLex & lexrc)
513 keyword_item outputTypeTags[] = {
514 { "docbook", DOCBOOK },
516 { "linuxdoc", LINUXDOC },
517 { "literate", LITERATE }
520 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
522 int le = lexrc.lex();
524 case LyXLex::LEX_UNDEF:
525 lexrc.printError("Unknown output type `$$Token'");
531 outputType_ = static_cast<OutputType>(le);
534 lyxerr << "Unhandled value " << le
535 << " in LyXTextClass::readOutputType." << endl;
542 enum ClassOptionsTags {
551 void LyXTextClass::readClassOptions(LyXLex & lexrc)
553 keyword_item classOptionsTags[] = {
555 {"fontsize", CO_FONTSIZE },
556 {"header", CO_HEADER },
557 {"other", CO_OTHER },
558 {"pagestyle", CO_PAGESTYLE }
561 lexrc.pushTable(classOptionsTags, CO_END);
563 while (!getout && lexrc.isOK()) {
564 int le = lexrc.lex();
566 case LyXLex::LEX_UNDEF:
567 lexrc.printError("Unknown ClassOption tag `$$Token'");
571 switch (static_cast<ClassOptionsTags>(le)) {
574 opt_fontsize_ = rtrim(lexrc.getString());
578 opt_pagestyle_ = rtrim(lexrc.getString());
582 options_ = lexrc.getString();
586 class_header_ = subst(lexrc.getString(), """, "\"");
607 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
609 keyword_item elementTags[] = {
612 { "labelfont", CS_LABELFONT },
613 { "latexname", CS_LATEXNAME },
614 { "latexparam", CS_LATEXPARAM },
615 { "latextype", CS_LATEXTYPE },
616 { "preamble", CS_PREAMBLE}
619 lexrc.pushTable(elementTags, CS_END);
624 LyXFont font(LyXFont::ALL_INHERIT);
625 LyXFont labelfont(LyXFont::ALL_INHERIT);
629 while (!getout && lexrc.isOK()) {
630 int le = lexrc.lex();
632 case LyXLex::LEX_UNDEF:
633 lexrc.printError("Unknown ClassOption tag `$$Token'");
637 switch (static_cast<CharStyleTags>(le)) {
640 latextype = lexrc.getString();
644 latexname = lexrc.getString();
648 latexparam = subst(lexrc.getString(), """, "\"");
651 labelfont.lyxRead(lexrc);
658 preamble = lexrc.getLongString("EndPreamble");
667 // Here add element to list if getout == true
671 cs.latextype = latextype;
672 cs.latexname = latexname;
673 cs.latexparam = latexparam;
675 cs.labelfont = labelfont;
676 cs.preamble = preamble;
677 charstyles().push_back(cs);
697 void LyXTextClass::readFloat(LyXLex & lexrc)
699 keyword_item floatTags[] = {
701 { "extension", FT_EXT },
702 { "guiname", FT_NAME },
703 { "latexbuiltin", FT_BUILTIN },
704 { "listname", FT_LISTNAME },
705 { "numberwithin", FT_WITHIN },
706 { "placement", FT_PLACEMENT },
707 { "style", FT_STYLE },
711 lexrc.pushTable(floatTags, FT_END);
720 bool builtin = false;
723 while (!getout && lexrc.isOK()) {
724 int le = lexrc.lex();
726 case LyXLex::LEX_UNDEF:
727 lexrc.printError("Unknown ClassOption tag `$$Token'");
731 switch (static_cast<FloatTags>(le)) {
734 type = lexrc.getString();
735 // Here we could check if this type is already defined
736 // and modify it with the rest of the vars instead.
740 name = lexrc.getString();
744 placement = lexrc.getString();
748 ext = lexrc.getString();
752 within = lexrc.getString();
753 if (within == "none")
758 style = lexrc.getString();
762 listname = lexrc.getString();
766 builtin = lexrc.getBool();
774 // Here if have a full float if getout == true
776 Floating newfloat(type, placement, ext, within,
777 style, name, listname, builtin);
778 floatlist_->newFloat(newfloat);
791 void LyXTextClass::readCounter(LyXLex & lexrc)
793 keyword_item counterTags[] = {
796 { "within", CT_WITHIN }
799 lexrc.pushTable(counterTags, CT_END);
805 while (!getout && lexrc.isOK()) {
806 int le = lexrc.lex();
808 case LyXLex::LEX_UNDEF:
809 lexrc.printError("Unknown ClassOption tag `$$Token'");
813 switch (static_cast<CounterTags>(le)) {
816 name = lexrc.getString();
820 within = lexrc.getString();
821 if (within == "none")
830 // Here if have a full counter if getout == true
832 if (within.empty()) {
833 ctrs_->newCounter(name);
835 ctrs_->newCounter(name, within);
843 LyXFont const & LyXTextClass::defaultfont() const
849 string const & LyXTextClass::leftmargin() const
855 string const & LyXTextClass::rightmargin() const
861 bool LyXTextClass::hasLayout(string const & n) const
863 string const name = (n.empty() ? defaultLayoutName() : n);
865 return find_if(layoutlist_.begin(), layoutlist_.end(),
866 LayoutNamesEqual(name))
867 != layoutlist_.end();
872 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
874 BOOST_ASSERT(!name.empty());
876 LayoutList::const_iterator cit =
877 find_if(layoutlist_.begin(),
879 LayoutNamesEqual(name));
881 if (cit == layoutlist_.end()) {
882 lyxerr << "We failed to find the layout '" << name
883 << "' in the layout list. You MUST investigate!"
885 for (LayoutList::const_iterator it = layoutlist_.begin();
886 it != layoutlist_.end(); ++it)
887 lyxerr << " " << it->get()->name() << endl;
889 // we require the name to exist
898 bool LyXTextClass::delete_layout(string const & name)
900 if (name == defaultLayoutName())
903 LayoutList::iterator it =
904 remove_if(layoutlist_.begin(), layoutlist_.end(),
905 LayoutNamesEqual(name));
907 LayoutList::iterator end = layoutlist_.end();
908 bool const ret = (it != end);
909 layoutlist_.erase(it, end);
914 // Load textclass info if not loaded yet
915 bool LyXTextClass::load(string const & path) const
920 // Read style-file, current directory is searched before system ones
921 string real_file = path + "/" + name_ + ".layout";
922 if (!fs::exists(real_file))
923 real_file = libFileSearch("layouts", name_, "layout");
924 loaded_ = const_cast<LyXTextClass*>(this)->read(real_file) == 0;
927 lyxerr << "Error reading `"
928 << makeDisplayPath(real_file)
929 << "'\n(Check `" << name_
930 << "')\nCheck your installation and "
931 "try Options/Reconfigure..." << endl;
938 FloatList & LyXTextClass::floats()
940 return *floatlist_.get();
944 FloatList const & LyXTextClass::floats() const
946 return *floatlist_.get();
950 Counters & LyXTextClass::counters() const
956 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
958 CharStyles::iterator cs = charstyles().begin();
959 CharStyles::iterator csend = charstyles().end();
960 for (; cs != csend; ++cs) {
968 string const & LyXTextClass::defaultLayoutName() const
970 // This really should come from the actual layout... (Lgb)
971 return defaultlayout_;
975 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
977 return operator[](defaultLayoutName());
981 string const & LyXTextClass::name() const
987 string const & LyXTextClass::latexname() const
989 const_cast<LyXTextClass*>(this)->load();
994 string const & LyXTextClass::description() const
1000 string const & LyXTextClass::opt_fontsize() const
1002 return opt_fontsize_;
1006 string const & LyXTextClass::opt_pagestyle() const
1008 return opt_pagestyle_;
1012 string const & LyXTextClass::options() const
1018 string const & LyXTextClass::class_header() const
1020 return class_header_;
1024 string const & LyXTextClass::pagestyle() const
1030 string const & LyXTextClass::preamble() const
1036 LyXTextClass::PageSides LyXTextClass::sides() const
1042 int LyXTextClass::secnumdepth() const
1044 return secnumdepth_;
1048 int LyXTextClass::tocdepth() const
1054 OutputType LyXTextClass::outputType() const
1060 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1062 return provides_ & p;
1066 unsigned int LyXTextClass::columns() const
1072 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1078 string const & LyXTextClass::titlename() const
1084 int LyXTextClass::size() const
1086 return layoutlist_.size();
1090 int LyXTextClass::min_toclevel() const
1092 return min_toclevel_;
1096 int LyXTextClass::max_toclevel() const
1098 return max_toclevel_;
1102 bool LyXTextClass::hasTocLevels() const
1104 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1108 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1111 case LyXTextClass::OneSide:
1114 case LyXTextClass::TwoSides: