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 "support/os.h"
29 #include <boost/filesystem/operations.hpp>
30 namespace fs = boost::filesystem;
34 using lyx::support::libFileSearch;
35 using lyx::support::makeDisplayPath;
36 using lyx::support::quoteName;
37 using lyx::support::rtrim;
38 using lyx::support::subst;
39 using lyx::support::addName;
50 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
52 LayoutNamesEqual(string const & name)
55 bool operator()(LyXLayout_ptr const & c) const
57 return c->name() == name_;
67 bool layout2layout(string const & filename, string const & tempfile)
69 string const script = libFileSearch("scripts", "layout2layout.py");
71 lyxerr << "Could not find layout conversion "
72 "script layout2layout.py." << endl;
76 std::ostringstream command;
77 command << lyx::support::os::python() << ' ' << quoteName(script)
78 << ' ' << quoteName(filename)
79 << ' ' << quoteName(tempfile);
80 string const command_str = command.str();
82 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
84 lyx::support::cmd_ret const ret =
85 lyx::support::runCommand(command_str);
87 lyxerr << "Could not run layout conversion "
88 "script layout2layout.py." << endl;
97 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
98 string const & desc, bool texClassAvail )
99 : name_(fn), latexname_(cln), description_(desc),
100 floatlist_(new FloatList), ctrs_(new Counters),
101 texClassAvail_(texClassAvail)
108 pagestyle_ = "default";
109 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
110 opt_fontsize_ = "10|11|12";
111 opt_pagestyle_ = "empty|plain|headings|fancy";
113 titletype_ = TITLE_COMMAND_AFTER;
114 titlename_ = "maketitle";
119 bool LyXTextClass::isTeXClassAvailable() const
121 return texClassAvail_;
125 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
127 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
128 if (!lay.read(lexrc, *this)) {
130 lay.resfont = lay.font;
131 lay.resfont.realize(defaultfont());
132 lay.reslabelfont = lay.labelfont;
133 lay.reslabelfont.realize(defaultfont());
134 return false; // no errors
136 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
172 // Reads a textclass structure from file.
173 bool LyXTextClass::read(string const & filename, bool merge)
175 if (!lyx::support::isFileReadable(filename)) {
176 lyxerr << "Cannot read layout file `" << filename << "'."
181 keyword_item textClassTags[] = {
182 { "charstyle", TC_CHARSTYLE },
183 { "classoptions", TC_CLASSOPTIONS },
184 { "columns", TC_COLUMNS },
185 { "counter", TC_COUNTER },
186 { "defaultfont", TC_DEFAULTFONT },
187 { "defaultstyle", TC_DEFAULTSTYLE },
188 { "environment", TC_ENVIRONMENT },
189 { "float", TC_FLOAT },
190 { "format", TC_FORMAT },
191 { "input", TC_INPUT },
192 { "leftmargin", TC_LEFTMARGIN },
193 { "nofloat", TC_NOFLOAT },
194 { "nostyle", TC_NOSTYLE },
195 { "outputtype", TC_OUTPUTTYPE },
196 { "pagestyle", TC_PAGESTYLE },
197 { "preamble", TC_PREAMBLE },
198 { "providesamsmath", TC_PROVIDESAMSMATH },
199 { "providesmakeidx", TC_PROVIDESMAKEIDX },
200 { "providesnatbib", TC_PROVIDESNATBIB },
201 { "providesurl", TC_PROVIDESURL },
202 { "rightmargin", TC_RIGHTMARGIN },
203 { "secnumdepth", TC_SECNUMDEPTH },
204 { "sides", TC_SIDES },
205 { "style", TC_STYLE },
206 { "titlelatexname", TC_TITLELATEXNAME },
207 { "titlelatextype", TC_TITLELATEXTYPE },
208 { "tocdepth", TC_TOCDEPTH }
212 lyxerr[Debug::TCLASS] << "Reading textclass "
213 << makeDisplayPath(filename)
216 lyxerr[Debug::TCLASS] << "Reading input file "
217 << makeDisplayPath(filename)
220 LyXLex lexrc(textClassTags,
221 sizeof(textClassTags) / sizeof(textClassTags[0]));
223 lexrc.setFile(filename);
224 bool error = !lexrc.isOK();
226 // Format of files before the 'Format' tag was introduced
230 while (lexrc.isOK() && !error) {
231 int le = lexrc.lex();
234 case LyXLex::LEX_FEOF:
237 case LyXLex::LEX_UNDEF:
238 lexrc.printError("Unknown TextClass tag `$$Token'");
246 switch (static_cast<TextClassTags>(le)) {
250 format = lexrc.getInteger();
253 case TC_OUTPUTTYPE: // output type definition
254 readOutputType(lexrc);
257 case TC_INPUT: // Include file
259 string tmp = libFileSearch("layouts",
263 if (read(tmp, true)) {
264 lexrc.printError("Error reading input"
271 case TC_DEFAULTSTYLE:
273 string const name = subst(lexrc.getString(),
275 defaultlayout_ = name;
282 string const name = subst(lexrc.getString(),
284 if (hasLayout(name)) {
285 LyXLayout * lay = operator[](name).get();
286 error = do_readStyle(lexrc, *lay);
290 if (le == TC_ENVIRONMENT)
291 lay.is_environment = true;
292 error = do_readStyle(lexrc, lay);
294 layoutlist_.push_back(
295 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
298 if (defaultlayout_.empty()) {
299 // We do not have a default
300 // layout yet, so we choose
301 // the first layout we
303 defaultlayout_ = name;
308 lexrc.printError("No name given for style: `$$Token'.");
315 string const style = subst(lexrc.getString(),
317 if (!delete_layout(style))
318 lyxerr << "Cannot delete style `"
319 << style << '\'' << endl;
320 // lexrc.printError("Cannot delete style"
327 columns_ = lexrc.getInteger();
332 switch (lexrc.getInteger()) {
333 case 1: sides_ = OneSide; break;
334 case 2: sides_ = TwoSides; break;
336 lyxerr << "Impossible number of page"
337 " sides, setting to one."
347 pagestyle_ = rtrim(lexrc.getString());
351 defaultfont_.lyxRead(lexrc);
352 if (!defaultfont_.resolved()) {
353 lexrc.printError("Warning: defaultfont should "
354 "be fully instantiated!");
355 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
361 secnumdepth_ = lexrc.getInteger();
366 tocdepth_ = lexrc.getInteger();
369 // First step to support options
370 case TC_CLASSOPTIONS:
371 readClassOptions(lexrc);
375 preamble_ = lexrc.getLongString("EndPreamble");
378 case TC_PROVIDESAMSMATH:
379 if (lexrc.next() && lexrc.getInteger())
380 provides_ |= amsmath;
383 case TC_PROVIDESNATBIB:
384 if (lexrc.next() && lexrc.getInteger())
388 case TC_PROVIDESMAKEIDX:
389 if (lexrc.next() && lexrc.getInteger())
390 provides_ |= makeidx;
394 if (lexrc.next() && lexrc.getInteger())
398 case TC_LEFTMARGIN: // left margin type
400 leftmargin_ = lexrc.getString();
403 case TC_RIGHTMARGIN: // right margin type
405 rightmargin_ = lexrc.getString();
409 string const name = subst(lexrc.getString(), '_', ' ');
410 readCharStyle(lexrc, name);
419 case TC_TITLELATEXTYPE:
420 readTitleType(lexrc);
422 case TC_TITLELATEXNAME:
424 titlename_ = lexrc.getString();
428 string const nofloat = lexrc.getString();
429 floatlist_->erase(nofloat);
433 if (format != FORMAT)
437 if (format != FORMAT) {
438 lyxerr[Debug::TCLASS] << "Converting layout file from format "
439 << format << " to " << FORMAT << endl;
440 string const tempfile = lyx::support::tempName();
441 error = !layout2layout(filename, tempfile);
443 error = read(tempfile, merge);
444 lyx::support::unlink(tempfile);
448 if (!merge) { // we are at top level here.
449 lyxerr[Debug::TCLASS] << "Finished reading textclass "
450 << makeDisplayPath(filename)
452 if (defaultlayout_.empty()) {
453 lyxerr << "Error: Textclass '" << name_
454 << "' is missing a defaultstyle." << endl;
458 min_toclevel_ = LyXLayout::NOT_IN_TOC;
459 max_toclevel_ = LyXLayout::NOT_IN_TOC;
460 const_iterator cit = begin();
461 const_iterator the_end = end();
462 for ( ; cit != the_end ; ++cit) {
463 int const toclevel = (*cit)->toclevel;
464 if (toclevel != LyXLayout::NOT_IN_TOC) {
465 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
466 min_toclevel_ = toclevel;
468 min_toclevel_ = std::min(min_toclevel_,
470 max_toclevel_ = std::max(max_toclevel_,
474 lyxerr[Debug::TCLASS]
475 << "Minimum TocLevel is " << min_toclevel_
476 << ", maximum is " << max_toclevel_ <<endl;
479 lyxerr[Debug::TCLASS] << "Finished reading input file "
480 << makeDisplayPath(filename)
487 void LyXTextClass::readTitleType(LyXLex & lexrc)
489 keyword_item titleTypeTags[] = {
490 { "commandafter", TITLE_COMMAND_AFTER },
491 { "environment", TITLE_ENVIRONMENT }
494 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
496 int le = lexrc.lex();
498 case LyXLex::LEX_UNDEF:
499 lexrc.printError("Unknown output type `$$Token'");
501 case TITLE_COMMAND_AFTER:
502 case TITLE_ENVIRONMENT:
503 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
506 lyxerr << "Unhandled value " << le
507 << " in LyXTextClass::readTitleType." << endl;
514 void LyXTextClass::readOutputType(LyXLex & lexrc)
516 keyword_item outputTypeTags[] = {
517 { "docbook", DOCBOOK },
519 { "literate", LITERATE }
522 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
524 int le = lexrc.lex();
526 case LyXLex::LEX_UNDEF:
527 lexrc.printError("Unknown output type `$$Token'");
532 outputType_ = static_cast<OutputType>(le);
535 lyxerr << "Unhandled value " << le
536 << " in LyXTextClass::readOutputType." << endl;
543 enum ClassOptionsTags {
552 void LyXTextClass::readClassOptions(LyXLex & lexrc)
554 keyword_item classOptionsTags[] = {
556 {"fontsize", CO_FONTSIZE },
557 {"header", CO_HEADER },
558 {"other", CO_OTHER },
559 {"pagestyle", CO_PAGESTYLE }
562 lexrc.pushTable(classOptionsTags, CO_END);
564 while (!getout && lexrc.isOK()) {
565 int le = lexrc.lex();
567 case LyXLex::LEX_UNDEF:
568 lexrc.printError("Unknown ClassOption tag `$$Token'");
572 switch (static_cast<ClassOptionsTags>(le)) {
575 opt_fontsize_ = rtrim(lexrc.getString());
579 opt_pagestyle_ = rtrim(lexrc.getString());
583 options_ = lexrc.getString();
587 class_header_ = subst(lexrc.getString(), """, "\"");
608 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
610 keyword_item elementTags[] = {
613 { "labelfont", CS_LABELFONT },
614 { "latexname", CS_LATEXNAME },
615 { "latexparam", CS_LATEXPARAM },
616 { "latextype", CS_LATEXTYPE },
617 { "preamble", CS_PREAMBLE}
620 lexrc.pushTable(elementTags, CS_END);
625 LyXFont font(LyXFont::ALL_INHERIT);
626 LyXFont labelfont(LyXFont::ALL_INHERIT);
630 while (!getout && lexrc.isOK()) {
631 int le = lexrc.lex();
633 case LyXLex::LEX_UNDEF:
634 lexrc.printError("Unknown ClassOption tag `$$Token'");
638 switch (static_cast<CharStyleTags>(le)) {
641 latextype = lexrc.getString();
645 latexname = lexrc.getString();
649 latexparam = subst(lexrc.getString(), """, "\"");
652 labelfont.lyxRead(lexrc);
659 preamble = lexrc.getLongString("EndPreamble");
668 // Here add element to list if getout == true
672 cs.latextype = latextype;
673 cs.latexname = latexname;
674 cs.latexparam = latexparam;
676 cs.labelfont = labelfont;
677 cs.preamble = preamble;
678 charstyles().push_back(cs);
698 void LyXTextClass::readFloat(LyXLex & lexrc)
700 keyword_item floatTags[] = {
702 { "extension", FT_EXT },
703 { "guiname", FT_NAME },
704 { "latexbuiltin", FT_BUILTIN },
705 { "listname", FT_LISTNAME },
706 { "numberwithin", FT_WITHIN },
707 { "placement", FT_PLACEMENT },
708 { "style", FT_STYLE },
712 lexrc.pushTable(floatTags, FT_END);
721 bool builtin = false;
724 while (!getout && lexrc.isOK()) {
725 int le = lexrc.lex();
727 case LyXLex::LEX_UNDEF:
728 lexrc.printError("Unknown ClassOption tag `$$Token'");
732 switch (static_cast<FloatTags>(le)) {
735 type = lexrc.getString();
736 // Here we could check if this type is already defined
737 // and modify it with the rest of the vars instead.
741 name = lexrc.getString();
745 placement = lexrc.getString();
749 ext = lexrc.getString();
753 within = lexrc.getString();
754 if (within == "none")
759 style = lexrc.getString();
763 listname = lexrc.getString();
767 builtin = lexrc.getBool();
775 // Here if have a full float if getout == true
777 Floating newfloat(type, placement, ext, within,
778 style, name, listname, builtin);
779 floatlist_->newFloat(newfloat);
792 void LyXTextClass::readCounter(LyXLex & lexrc)
794 keyword_item counterTags[] = {
797 { "within", CT_WITHIN }
800 lexrc.pushTable(counterTags, CT_END);
806 while (!getout && lexrc.isOK()) {
807 int le = lexrc.lex();
809 case LyXLex::LEX_UNDEF:
810 lexrc.printError("Unknown ClassOption tag `$$Token'");
814 switch (static_cast<CounterTags>(le)) {
817 name = lexrc.getString();
821 within = lexrc.getString();
822 if (within == "none")
831 // Here if have a full counter if getout == true
833 if (within.empty()) {
834 ctrs_->newCounter(name);
836 ctrs_->newCounter(name, within);
844 LyXFont const & LyXTextClass::defaultfont() const
850 string const & LyXTextClass::leftmargin() const
856 string const & LyXTextClass::rightmargin() const
862 bool LyXTextClass::hasLayout(string const & n) const
864 string const name = (n.empty() ? defaultLayoutName() : n);
866 return find_if(layoutlist_.begin(), layoutlist_.end(),
867 LayoutNamesEqual(name))
868 != layoutlist_.end();
873 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
875 BOOST_ASSERT(!name.empty());
877 LayoutList::const_iterator cit =
878 find_if(layoutlist_.begin(),
880 LayoutNamesEqual(name));
882 if (cit == layoutlist_.end()) {
883 lyxerr << "We failed to find the layout '" << name
884 << "' in the layout list. You MUST investigate!"
886 for (LayoutList::const_iterator it = layoutlist_.begin();
887 it != layoutlist_.end(); ++it)
888 lyxerr << " " << it->get()->name() << endl;
890 // we require the name to exist
899 bool LyXTextClass::delete_layout(string const & name)
901 if (name == defaultLayoutName())
904 LayoutList::iterator it =
905 remove_if(layoutlist_.begin(), layoutlist_.end(),
906 LayoutNamesEqual(name));
908 LayoutList::iterator end = layoutlist_.end();
909 bool const ret = (it != end);
910 layoutlist_.erase(it, end);
915 // Load textclass info if not loaded yet
916 bool LyXTextClass::load(string const & path) const
921 // Read style-file, provided path is searched before the system ones
924 layout_file = addName(path, name_ + ".layout");
925 if (layout_file.empty() || !fs::exists(layout_file))
926 layout_file = libFileSearch("layouts", name_, "layout");
927 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
930 lyxerr << "Error reading `"
931 << makeDisplayPath(layout_file)
932 << "'\n(Check `" << name_
933 << "')\nCheck your installation and "
934 "try Options/Reconfigure..." << endl;
941 FloatList & LyXTextClass::floats()
943 return *floatlist_.get();
947 FloatList const & LyXTextClass::floats() const
949 return *floatlist_.get();
953 Counters & LyXTextClass::counters() const
959 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
961 CharStyles::iterator cs = charstyles().begin();
962 CharStyles::iterator csend = charstyles().end();
963 for (; cs != csend; ++cs) {
971 string const & LyXTextClass::defaultLayoutName() const
973 // This really should come from the actual layout... (Lgb)
974 return defaultlayout_;
978 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
980 return operator[](defaultLayoutName());
984 string const & LyXTextClass::name() const
990 string const & LyXTextClass::latexname() const
992 const_cast<LyXTextClass*>(this)->load();
997 string const & LyXTextClass::description() const
1003 string const & LyXTextClass::opt_fontsize() const
1005 return opt_fontsize_;
1009 string const & LyXTextClass::opt_pagestyle() const
1011 return opt_pagestyle_;
1015 string const & LyXTextClass::options() const
1021 string const & LyXTextClass::class_header() const
1023 return class_header_;
1027 string const & LyXTextClass::pagestyle() const
1033 string const & LyXTextClass::preamble() const
1039 LyXTextClass::PageSides LyXTextClass::sides() const
1045 int LyXTextClass::secnumdepth() const
1047 return secnumdepth_;
1051 int LyXTextClass::tocdepth() const
1057 OutputType LyXTextClass::outputType() const
1063 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1065 return provides_ & p;
1069 unsigned int LyXTextClass::columns() const
1075 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1081 string const & LyXTextClass::titlename() const
1087 int LyXTextClass::size() const
1089 return layoutlist_.size();
1093 int LyXTextClass::min_toclevel() const
1095 return min_toclevel_;
1099 int LyXTextClass::max_toclevel() const
1101 return max_toclevel_;
1105 bool LyXTextClass::hasTocLevels() const
1107 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1111 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1114 case LyXTextClass::OneSide:
1117 case LyXTextClass::TwoSides: