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;
37 using lyx::support::addName;
48 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
50 LayoutNamesEqual(string const & name)
53 bool operator()(LyXLayout_ptr const & c) const
55 return c->name() == name_;
65 bool layout2layout(string const & filename, string const & tempfile)
67 string const script = libFileSearch("scripts", "layout2layout.py");
69 lyxerr << "Could not find layout conversion "
70 "script layout2layout.py." << endl;
74 std::ostringstream command;
75 command << "python " << quoteName(script)
76 << ' ' << quoteName(filename)
77 << ' ' << quoteName(tempfile);
78 string const command_str = command.str();
80 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
82 lyx::support::cmd_ret const ret =
83 lyx::support::runCommand(command_str);
85 lyxerr << "Could not run layout conversion "
86 "script layout2layout.py." << endl;
95 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
96 string const & desc, bool texClassAvail )
97 : name_(fn), latexname_(cln), description_(desc),
98 floatlist_(new FloatList), ctrs_(new Counters),
99 texClassAvail_(texClassAvail)
106 pagestyle_ = "default";
107 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
108 opt_fontsize_ = "10|11|12";
109 opt_pagestyle_ = "empty|plain|headings|fancy";
111 titletype_ = TITLE_COMMAND_AFTER;
112 titlename_ = "maketitle";
117 bool LyXTextClass::isTeXClassAvailable() const
119 return texClassAvail_;
123 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
125 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
126 if (!lay.read(lexrc, *this)) {
128 lay.resfont = lay.font;
129 lay.resfont.realize(defaultfont());
130 lay.reslabelfont = lay.labelfont;
131 lay.reslabelfont.realize(defaultfont());
132 return false; // no errors
134 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
170 // Reads a textclass structure from file.
171 bool LyXTextClass::read(string const & filename, bool merge)
173 if (!lyx::support::isFileReadable(filename)) {
174 lyxerr << "Cannot read layout file `" << filename << "'."
179 keyword_item textClassTags[] = {
180 { "charstyle", TC_CHARSTYLE },
181 { "classoptions", TC_CLASSOPTIONS },
182 { "columns", TC_COLUMNS },
183 { "counter", TC_COUNTER },
184 { "defaultfont", TC_DEFAULTFONT },
185 { "defaultstyle", TC_DEFAULTSTYLE },
186 { "environment", TC_ENVIRONMENT },
187 { "float", TC_FLOAT },
188 { "format", TC_FORMAT },
189 { "input", TC_INPUT },
190 { "leftmargin", TC_LEFTMARGIN },
191 { "nofloat", TC_NOFLOAT },
192 { "nostyle", TC_NOSTYLE },
193 { "outputtype", TC_OUTPUTTYPE },
194 { "pagestyle", TC_PAGESTYLE },
195 { "preamble", TC_PREAMBLE },
196 { "providesamsmath", TC_PROVIDESAMSMATH },
197 { "providesmakeidx", TC_PROVIDESMAKEIDX },
198 { "providesnatbib", TC_PROVIDESNATBIB },
199 { "providesurl", TC_PROVIDESURL },
200 { "rightmargin", TC_RIGHTMARGIN },
201 { "secnumdepth", TC_SECNUMDEPTH },
202 { "sides", TC_SIDES },
203 { "style", TC_STYLE },
204 { "titlelatexname", TC_TITLELATEXNAME },
205 { "titlelatextype", TC_TITLELATEXTYPE },
206 { "tocdepth", TC_TOCDEPTH }
210 lyxerr[Debug::TCLASS] << "Reading textclass "
211 << makeDisplayPath(filename)
214 lyxerr[Debug::TCLASS] << "Reading input file "
215 << makeDisplayPath(filename)
218 LyXLex lexrc(textClassTags,
219 sizeof(textClassTags) / sizeof(textClassTags[0]));
221 lexrc.setFile(filename);
222 bool error = !lexrc.isOK();
224 // Format of files before the 'Format' tag was introduced
228 while (lexrc.isOK() && !error) {
229 int le = lexrc.lex();
232 case LyXLex::LEX_FEOF:
235 case LyXLex::LEX_UNDEF:
236 lexrc.printError("Unknown TextClass tag `$$Token'");
244 switch (static_cast<TextClassTags>(le)) {
248 format = lexrc.getInteger();
251 case TC_OUTPUTTYPE: // output type definition
252 readOutputType(lexrc);
255 case TC_INPUT: // Include file
257 string tmp = libFileSearch("layouts",
261 if (read(tmp, true)) {
262 lexrc.printError("Error reading input"
269 case TC_DEFAULTSTYLE:
271 string const name = subst(lexrc.getString(),
273 defaultlayout_ = name;
280 string const name = subst(lexrc.getString(),
282 if (hasLayout(name)) {
283 LyXLayout * lay = operator[](name).get();
284 error = do_readStyle(lexrc, *lay);
288 if (le == TC_ENVIRONMENT)
289 lay.is_environment = true;
290 error = do_readStyle(lexrc, lay);
292 layoutlist_.push_back(
293 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
296 if (defaultlayout_.empty()) {
297 // We do not have a default
298 // layout yet, so we choose
299 // the first layout we
301 defaultlayout_ = name;
306 lexrc.printError("No name given for style: `$$Token'.");
313 string const style = subst(lexrc.getString(),
315 if (!delete_layout(style))
316 lyxerr << "Cannot delete style `"
317 << style << '\'' << endl;
318 // lexrc.printError("Cannot delete style"
325 columns_ = lexrc.getInteger();
330 switch (lexrc.getInteger()) {
331 case 1: sides_ = OneSide; break;
332 case 2: sides_ = TwoSides; break;
334 lyxerr << "Impossible number of page"
335 " sides, setting to one."
345 pagestyle_ = rtrim(lexrc.getString());
349 defaultfont_.lyxRead(lexrc);
350 if (!defaultfont_.resolved()) {
351 lexrc.printError("Warning: defaultfont should "
352 "be fully instantiated!");
353 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
359 secnumdepth_ = lexrc.getInteger();
364 tocdepth_ = lexrc.getInteger();
367 // First step to support options
368 case TC_CLASSOPTIONS:
369 readClassOptions(lexrc);
373 preamble_ = lexrc.getLongString("EndPreamble");
376 case TC_PROVIDESAMSMATH:
377 if (lexrc.next() && lexrc.getInteger())
378 provides_ |= amsmath;
381 case TC_PROVIDESNATBIB:
382 if (lexrc.next() && lexrc.getInteger())
386 case TC_PROVIDESMAKEIDX:
387 if (lexrc.next() && lexrc.getInteger())
388 provides_ |= makeidx;
392 if (lexrc.next() && lexrc.getInteger())
396 case TC_LEFTMARGIN: // left margin type
398 leftmargin_ = lexrc.getString();
401 case TC_RIGHTMARGIN: // right margin type
403 rightmargin_ = lexrc.getString();
407 string const name = subst(lexrc.getString(), '_', ' ');
408 readCharStyle(lexrc, name);
417 case TC_TITLELATEXTYPE:
418 readTitleType(lexrc);
420 case TC_TITLELATEXNAME:
422 titlename_ = lexrc.getString();
426 string const nofloat = lexrc.getString();
427 floatlist_->erase(nofloat);
431 if (format != FORMAT)
435 if (format != FORMAT) {
436 lyxerr[Debug::TCLASS] << "Converting layout file from format "
437 << format << " to " << FORMAT << endl;
438 string const tempfile = lyx::support::tempName();
439 error = !layout2layout(filename, tempfile);
441 error = read(tempfile, merge);
442 lyx::support::unlink(tempfile);
446 if (!merge) { // we are at top level here.
447 lyxerr[Debug::TCLASS] << "Finished reading textclass "
448 << makeDisplayPath(filename)
450 if (defaultlayout_.empty()) {
451 lyxerr << "Error: Textclass '" << name_
452 << "' is missing a defaultstyle." << endl;
456 min_toclevel_ = LyXLayout::NOT_IN_TOC;
457 max_toclevel_ = LyXLayout::NOT_IN_TOC;
458 const_iterator cit = begin();
459 const_iterator the_end = end();
460 for ( ; cit != the_end ; ++cit) {
461 int const toclevel = (*cit)->toclevel;
462 if (toclevel != LyXLayout::NOT_IN_TOC) {
463 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
464 min_toclevel_ = toclevel;
466 min_toclevel_ = std::min(min_toclevel_,
468 max_toclevel_ = std::max(max_toclevel_,
472 lyxerr[Debug::TCLASS]
473 << "Minimum TocLevel is " << min_toclevel_
474 << ", maximum is " << max_toclevel_ <<endl;
477 lyxerr[Debug::TCLASS] << "Finished reading input file "
478 << makeDisplayPath(filename)
485 void LyXTextClass::readTitleType(LyXLex & lexrc)
487 keyword_item titleTypeTags[] = {
488 { "commandafter", TITLE_COMMAND_AFTER },
489 { "environment", TITLE_ENVIRONMENT }
492 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
494 int le = lexrc.lex();
496 case LyXLex::LEX_UNDEF:
497 lexrc.printError("Unknown output type `$$Token'");
499 case TITLE_COMMAND_AFTER:
500 case TITLE_ENVIRONMENT:
501 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
504 lyxerr << "Unhandled value " << le
505 << " in LyXTextClass::readTitleType." << endl;
512 void LyXTextClass::readOutputType(LyXLex & lexrc)
514 keyword_item outputTypeTags[] = {
515 { "docbook", DOCBOOK },
517 { "linuxdoc", LINUXDOC },
518 { "literate", LITERATE }
521 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
523 int le = lexrc.lex();
525 case LyXLex::LEX_UNDEF:
526 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: