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"
30 using lyx::support::LibFileSearch;
31 using lyx::support::MakeDisplayPath;
32 using lyx::support::QuoteName;
33 using lyx::support::rtrim;
34 using lyx::support::subst;
45 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
47 LayoutNamesEqual(string const & name)
50 bool operator()(LyXLayout_ptr const & c) const
52 return c->name() == name_;
62 bool layout2layout(string const & filename, string const & tempfile)
64 string const script = LibFileSearch("scripts", "layout2layout.py");
66 lyxerr << "Could not find layout conversion "
67 "script layout2layout.py." << endl;
71 std::ostringstream command;
72 command << "python " << QuoteName(script)
73 << ' ' << QuoteName(filename)
74 << ' ' << QuoteName(tempfile);
75 string const command_str = command.str();
77 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
79 lyx::support::cmd_ret const ret =
80 lyx::support::RunCommand(command_str);
82 lyxerr << "Could not run layout conversion "
83 "script layout2layout.py." << endl;
92 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
93 string const & desc, bool texClassAvail )
94 : name_(fn), latexname_(cln), description_(desc),
95 floatlist_(new FloatList), ctrs_(new Counters),
96 texClassAvail_(texClassAvail)
103 pagestyle_ = "default";
104 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
105 opt_fontsize_ = "10|11|12";
106 opt_pagestyle_ = "empty|plain|headings|fancy";
108 titletype_ = TITLE_COMMAND_AFTER;
109 titlename_ = "maketitle";
114 bool LyXTextClass::isTeXClassAvailable() const
116 return texClassAvail_;
120 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
122 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
123 if (!lay.Read(lexrc, *this)) {
125 lay.resfont = lay.font;
126 lay.resfont.realize(defaultfont());
127 lay.reslabelfont = lay.labelfont;
128 lay.reslabelfont.realize(defaultfont());
129 return false; // no errors
131 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
167 // Reads a textclass structure from file.
168 bool LyXTextClass::Read(string const & filename, bool merge)
170 if (!lyx::support::isFileReadable(filename)) {
171 lyxerr << "Cannot read layout file `" << filename << "'."
176 keyword_item textClassTags[] = {
177 { "charstyle", TC_CHARSTYLE },
178 { "classoptions", TC_CLASSOPTIONS },
179 { "columns", TC_COLUMNS },
180 { "counter", TC_COUNTER },
181 { "defaultfont", TC_DEFAULTFONT },
182 { "defaultstyle", TC_DEFAULTSTYLE },
183 { "environment", TC_ENVIRONMENT },
184 { "float", TC_FLOAT },
185 { "format", TC_FORMAT },
186 { "input", TC_INPUT },
187 { "leftmargin", TC_LEFTMARGIN },
188 { "nofloat", TC_NOFLOAT },
189 { "nostyle", TC_NOSTYLE },
190 { "outputtype", TC_OUTPUTTYPE },
191 { "pagestyle", TC_PAGESTYLE },
192 { "preamble", TC_PREAMBLE },
193 { "providesamsmath", TC_PROVIDESAMSMATH },
194 { "providesmakeidx", TC_PROVIDESMAKEIDX },
195 { "providesnatbib", TC_PROVIDESNATBIB },
196 { "providesurl", TC_PROVIDESURL },
197 { "rightmargin", TC_RIGHTMARGIN },
198 { "secnumdepth", TC_SECNUMDEPTH },
199 { "sides", TC_SIDES },
200 { "style", TC_STYLE },
201 { "titlelatexname", TC_TITLELATEXNAME },
202 { "titlelatextype", TC_TITLELATEXTYPE },
203 { "tocdepth", TC_TOCDEPTH }
207 lyxerr[Debug::TCLASS] << "Reading textclass "
208 << MakeDisplayPath(filename)
211 lyxerr[Debug::TCLASS] << "Reading input file "
212 << MakeDisplayPath(filename)
215 LyXLex lexrc(textClassTags,
216 sizeof(textClassTags) / sizeof(textClassTags[0]));
218 lexrc.setFile(filename);
219 bool error = !lexrc.isOK();
221 // Format of files before the 'Format' tag was introduced
225 while (lexrc.isOK() && !error) {
226 int le = lexrc.lex();
229 case LyXLex::LEX_FEOF:
232 case LyXLex::LEX_UNDEF:
233 lexrc.printError("Unknown TextClass tag `$$Token'");
241 switch (static_cast<TextClassTags>(le)) {
245 format = lexrc.getInteger();
248 case TC_OUTPUTTYPE: // output type definition
249 readOutputType(lexrc);
252 case TC_INPUT: // Include file
254 string tmp = LibFileSearch("layouts",
258 if (Read(tmp, true)) {
259 lexrc.printError("Error reading input"
266 case TC_DEFAULTSTYLE:
268 string const name = subst(lexrc.getString(),
270 defaultlayout_ = name;
277 string const name = subst(lexrc.getString(),
279 if (hasLayout(name)) {
280 LyXLayout * lay = operator[](name).get();
281 error = do_readStyle(lexrc, *lay);
285 if (le == TC_ENVIRONMENT)
286 lay.is_environment = true;
287 error = do_readStyle(lexrc, lay);
289 layoutlist_.push_back(
290 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
293 if (defaultlayout_.empty()) {
294 // We do not have a default
295 // layout yet, so we choose
296 // the first layout we
298 defaultlayout_ = name;
303 lexrc.printError("No name given for style: `$$Token'.");
310 string const style = subst(lexrc.getString(),
312 if (!delete_layout(style))
313 lyxerr << "Cannot delete style `"
314 << style << '\'' << endl;
315 // lexrc.printError("Cannot delete style"
322 columns_ = lexrc.getInteger();
327 switch (lexrc.getInteger()) {
328 case 1: sides_ = OneSide; break;
329 case 2: sides_ = TwoSides; break;
331 lyxerr << "Impossible number of page"
332 " sides, setting to one."
342 pagestyle_ = rtrim(lexrc.getString());
346 defaultfont_.lyxRead(lexrc);
347 if (!defaultfont_.resolved()) {
348 lexrc.printError("Warning: defaultfont should "
349 "be fully instantiated!");
350 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
356 secnumdepth_ = lexrc.getInteger();
361 tocdepth_ = lexrc.getInteger();
364 // First step to support options
365 case TC_CLASSOPTIONS:
366 readClassOptions(lexrc);
370 preamble_ = lexrc.getLongString("EndPreamble");
373 case TC_PROVIDESAMSMATH:
374 if (lexrc.next() && lexrc.getInteger())
375 provides_ |= amsmath;
378 case TC_PROVIDESNATBIB:
379 if (lexrc.next() && lexrc.getInteger())
383 case TC_PROVIDESMAKEIDX:
384 if (lexrc.next() && lexrc.getInteger())
385 provides_ |= makeidx;
389 if (lexrc.next() && lexrc.getInteger())
393 case TC_LEFTMARGIN: // left margin type
395 leftmargin_ = lexrc.getString();
398 case TC_RIGHTMARGIN: // right margin type
400 rightmargin_ = lexrc.getString();
404 string const name = subst(lexrc.getString(), '_', ' ');
405 readCharStyle(lexrc, name);
414 case TC_TITLELATEXTYPE:
415 readTitleType(lexrc);
417 case TC_TITLELATEXNAME:
419 titlename_ = lexrc.getString();
423 string const nofloat = lexrc.getString();
424 floatlist_->erase(nofloat);
428 if (format != FORMAT)
432 if (format != FORMAT) {
433 lyxerr[Debug::TCLASS] << "Converting layout file from format "
434 << format << " to " << FORMAT << endl;
435 string const tempfile = lyx::support::tempName();
436 error = !layout2layout(filename, tempfile);
438 error = Read(tempfile, merge);
439 lyx::support::unlink(tempfile);
443 if (!merge) { // we are at top level here.
444 lyxerr[Debug::TCLASS] << "Finished reading textclass "
445 << MakeDisplayPath(filename)
447 if (defaultlayout_.empty()) {
448 lyxerr << "Error: Textclass '" << name_
449 << "' is missing a defaultstyle." << endl;
453 min_toclevel_ = LyXLayout::NOT_IN_TOC;
454 max_toclevel_ = LyXLayout::NOT_IN_TOC;
455 const_iterator cit = begin();
456 const_iterator the_end = end();
457 for ( ; cit != the_end ; ++cit) {
458 int const toclevel = (*cit)->toclevel;
459 if (toclevel != LyXLayout::NOT_IN_TOC) {
460 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
461 min_toclevel_ = toclevel;
463 min_toclevel_ = std::min(min_toclevel_,
465 max_toclevel_ = std::max(max_toclevel_,
469 lyxerr[Debug::TCLASS]
470 << "Minimum TocLevel is " << min_toclevel_
471 << ", maximum is " << max_toclevel_ <<endl;
474 lyxerr[Debug::TCLASS] << "Finished reading input file "
475 << MakeDisplayPath(filename)
482 void LyXTextClass::readTitleType(LyXLex & lexrc)
484 keyword_item titleTypeTags[] = {
485 { "commandafter", TITLE_COMMAND_AFTER },
486 { "environment", TITLE_ENVIRONMENT }
489 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
491 int le = lexrc.lex();
493 case LyXLex::LEX_UNDEF:
494 lexrc.printError("Unknown output type `$$Token'");
496 case TITLE_COMMAND_AFTER:
497 case TITLE_ENVIRONMENT:
498 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
501 lyxerr << "Unhandled value " << le
502 << " in LyXTextClass::readTitleType." << endl;
509 void LyXTextClass::readOutputType(LyXLex & lexrc)
511 keyword_item outputTypeTags[] = {
512 { "docbook", DOCBOOK },
514 { "linuxdoc", LINUXDOC },
515 { "literate", LITERATE }
518 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
520 int le = lexrc.lex();
522 case LyXLex::LEX_UNDEF:
523 lexrc.printError("Unknown output type `$$Token'");
529 outputType_ = static_cast<OutputType>(le);
532 lyxerr << "Unhandled value " << le
533 << " in LyXTextClass::readOutputType." << endl;
540 enum ClassOptionsTags {
549 void LyXTextClass::readClassOptions(LyXLex & lexrc)
551 keyword_item classOptionsTags[] = {
553 {"fontsize", CO_FONTSIZE },
554 {"header", CO_HEADER },
555 {"other", CO_OTHER },
556 {"pagestyle", CO_PAGESTYLE }
559 lexrc.pushTable(classOptionsTags, CO_END);
561 while (!getout && lexrc.isOK()) {
562 int le = lexrc.lex();
564 case LyXLex::LEX_UNDEF:
565 lexrc.printError("Unknown ClassOption tag `$$Token'");
569 switch (static_cast<ClassOptionsTags>(le)) {
572 opt_fontsize_ = rtrim(lexrc.getString());
576 opt_pagestyle_ = rtrim(lexrc.getString());
580 options_ = lexrc.getString();
584 class_header_ = subst(lexrc.getString(), """, "\"");
605 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
607 keyword_item elementTags[] = {
610 { "labelfont", CS_LABELFONT },
611 { "latexname", CS_LATEXNAME },
612 { "latexparam", CS_LATEXPARAM },
613 { "latextype", CS_LATEXTYPE },
614 { "preamble", CS_PREAMBLE}
617 lexrc.pushTable(elementTags, CS_END);
622 LyXFont font(LyXFont::ALL_INHERIT);
623 LyXFont labelfont(LyXFont::ALL_INHERIT);
627 while (!getout && lexrc.isOK()) {
628 int le = lexrc.lex();
630 case LyXLex::LEX_UNDEF:
631 lexrc.printError("Unknown ClassOption tag `$$Token'");
635 switch (static_cast<CharStyleTags>(le)) {
638 latextype = lexrc.getString();
642 latexname = lexrc.getString();
646 latexparam = subst(lexrc.getString(), """, "\"");
649 labelfont.lyxRead(lexrc);
656 preamble = lexrc.getLongString("EndPreamble");
665 // Here add element to list if getout == true
669 cs.latextype = latextype;
670 cs.latexname = latexname;
671 cs.latexparam = latexparam;
673 cs.labelfont = labelfont;
674 cs.preamble = preamble;
675 charstyles().push_back(cs);
695 void LyXTextClass::readFloat(LyXLex & lexrc)
697 keyword_item floatTags[] = {
699 { "extension", FT_EXT },
700 { "guiname", FT_NAME },
701 { "latexbuiltin", FT_BUILTIN },
702 { "listname", FT_LISTNAME },
703 { "numberwithin", FT_WITHIN },
704 { "placement", FT_PLACEMENT },
705 { "style", FT_STYLE },
709 lexrc.pushTable(floatTags, FT_END);
718 bool builtin = false;
721 while (!getout && lexrc.isOK()) {
722 int le = lexrc.lex();
724 case LyXLex::LEX_UNDEF:
725 lexrc.printError("Unknown ClassOption tag `$$Token'");
729 switch (static_cast<FloatTags>(le)) {
732 type = lexrc.getString();
733 // Here we could check if this type is already defined
734 // and modify it with the rest of the vars instead.
738 name = lexrc.getString();
742 placement = lexrc.getString();
746 ext = lexrc.getString();
750 within = lexrc.getString();
751 if (within == "none")
756 style = lexrc.getString();
760 listname = lexrc.getString();
764 builtin = lexrc.getBool();
772 // Here if have a full float if getout == true
774 Floating newfloat(type, placement, ext, within,
775 style, name, listname, builtin);
776 floatlist_->newFloat(newfloat);
789 void LyXTextClass::readCounter(LyXLex & lexrc)
791 keyword_item counterTags[] = {
794 { "within", CT_WITHIN }
797 lexrc.pushTable(counterTags, CT_END);
803 while (!getout && lexrc.isOK()) {
804 int le = lexrc.lex();
806 case LyXLex::LEX_UNDEF:
807 lexrc.printError("Unknown ClassOption tag `$$Token'");
811 switch (static_cast<CounterTags>(le)) {
814 name = lexrc.getString();
818 within = lexrc.getString();
819 if (within == "none")
828 // Here if have a full counter if getout == true
830 if (within.empty()) {
831 ctrs_->newCounter(name);
833 ctrs_->newCounter(name, within);
841 LyXFont const & LyXTextClass::defaultfont() const
847 string const & LyXTextClass::leftmargin() const
853 string const & LyXTextClass::rightmargin() const
859 bool LyXTextClass::hasLayout(string const & n) const
861 string const name = (n.empty() ? defaultLayoutName() : n);
863 return find_if(layoutlist_.begin(), layoutlist_.end(),
864 LayoutNamesEqual(name))
865 != layoutlist_.end();
870 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
872 BOOST_ASSERT(!name.empty());
874 LayoutList::const_iterator cit =
875 find_if(layoutlist_.begin(),
877 LayoutNamesEqual(name));
879 if (cit == layoutlist_.end()) {
880 lyxerr << "We failed to find the layout '" << name
881 << "' in the layout list. You MUST investigate!"
883 for (LayoutList::const_iterator it = layoutlist_.begin();
884 it != layoutlist_.end(); ++it)
885 lyxerr << " " << it->get()->name() << endl;
887 // we require the name to exist
896 bool LyXTextClass::delete_layout(string const & name)
898 if (name == defaultLayoutName())
901 LayoutList::iterator it =
902 remove_if(layoutlist_.begin(), layoutlist_.end(),
903 LayoutNamesEqual(name));
905 LayoutList::iterator end = layoutlist_.end();
906 bool const ret = (it != end);
907 layoutlist_.erase(it, end);
912 // Load textclass info if not loaded yet
913 bool LyXTextClass::load() const
919 string const real_file = LibFileSearch("layouts", name_, "layout");
920 loaded_ = const_cast<LyXTextClass*>(this)->Read(real_file) == 0;
923 lyxerr << "Error reading `"
924 << MakeDisplayPath(real_file)
925 << "'\n(Check `" << name_
926 << "')\nCheck your installation and "
927 "try Options/Reconfigure..." << endl;
934 FloatList & LyXTextClass::floats()
936 return *floatlist_.get();
940 FloatList const & LyXTextClass::floats() const
942 return *floatlist_.get();
946 Counters & LyXTextClass::counters() const
952 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
954 CharStyles::iterator cs = charstyles().begin();
955 CharStyles::iterator csend = charstyles().end();
956 for (; cs != csend; ++cs) {
964 string const & LyXTextClass::defaultLayoutName() const
966 // This really should come from the actual layout... (Lgb)
967 return defaultlayout_;
971 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
973 return operator[](defaultLayoutName());
977 string const & LyXTextClass::name() const
983 string const & LyXTextClass::latexname() const
985 const_cast<LyXTextClass*>(this)->load();
990 string const & LyXTextClass::description() const
996 string const & LyXTextClass::opt_fontsize() const
998 return opt_fontsize_;
1002 string const & LyXTextClass::opt_pagestyle() const
1004 return opt_pagestyle_;
1008 string const & LyXTextClass::options() const
1014 string const & LyXTextClass::class_header() const
1016 return class_header_;
1020 string const & LyXTextClass::pagestyle() const
1026 string const & LyXTextClass::preamble() const
1032 LyXTextClass::PageSides LyXTextClass::sides() const
1038 int LyXTextClass::secnumdepth() const
1040 return secnumdepth_;
1044 int LyXTextClass::tocdepth() const
1050 OutputType LyXTextClass::outputType() const
1056 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1058 return provides_ & p;
1062 unsigned int LyXTextClass::columns() const
1068 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1074 string const & LyXTextClass::titlename() const
1080 int LyXTextClass::size() const
1082 return layoutlist_.size();
1086 int LyXTextClass::min_toclevel() const
1088 return min_toclevel_;
1092 int LyXTextClass::max_toclevel() const
1094 return max_toclevel_;
1098 bool LyXTextClass::hasTocLevels() const
1100 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1104 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1107 case LyXTextClass::OneSide:
1110 case LyXTextClass::TwoSides: