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;
35 using lyx::support::libFileSearch;
36 using lyx::support::makeDisplayPath;
37 using lyx::support::quoteName;
38 using lyx::support::rtrim;
39 using lyx::support::subst;
40 using lyx::support::addName;
51 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
53 LayoutNamesEqual(string const & name)
56 bool operator()(LyXLayout_ptr const & c) const
58 return c->name() == name_;
68 bool layout2layout(string const & filename, string const & tempfile)
70 string const script = libFileSearch("scripts", "layout2layout.py");
72 lyxerr << "Could not find layout conversion "
73 "script layout2layout.py." << endl;
77 std::ostringstream command;
78 command << lyx::support::os::python() << ' ' << quoteName(script)
79 << ' ' << quoteName(filename)
80 << ' ' << quoteName(tempfile);
81 string const command_str = command.str();
83 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
85 lyx::support::cmd_ret const ret =
86 lyx::support::runCommand(command_str);
88 lyxerr << "Could not run layout conversion "
89 "script layout2layout.py." << endl;
98 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
99 string const & desc, bool texClassAvail )
100 : name_(fn), latexname_(cln), description_(desc),
101 floatlist_(new FloatList), ctrs_(new Counters),
102 texClassAvail_(texClassAvail)
109 pagestyle_ = "default";
110 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
111 opt_fontsize_ = "10|11|12";
112 opt_pagestyle_ = "empty|plain|headings|fancy";
114 titletype_ = TITLE_COMMAND_AFTER;
115 titlename_ = "maketitle";
120 bool LyXTextClass::isTeXClassAvailable() const
122 return texClassAvail_;
126 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
128 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
129 if (!lay.read(lexrc, *this)) {
131 lay.resfont = lay.font;
132 lay.resfont.realize(defaultfont());
133 lay.reslabelfont = lay.labelfont;
134 lay.reslabelfont.realize(defaultfont());
135 return false; // no errors
137 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
173 // Reads a textclass structure from file.
174 bool LyXTextClass::read(string const & filename, bool merge)
176 if (!lyx::support::isFileReadable(filename)) {
177 lyxerr << "Cannot read layout file `" << filename << "'."
182 keyword_item textClassTags[] = {
183 { "charstyle", TC_CHARSTYLE },
184 { "classoptions", TC_CLASSOPTIONS },
185 { "columns", TC_COLUMNS },
186 { "counter", TC_COUNTER },
187 { "defaultfont", TC_DEFAULTFONT },
188 { "defaultstyle", TC_DEFAULTSTYLE },
189 { "environment", TC_ENVIRONMENT },
190 { "float", TC_FLOAT },
191 { "format", TC_FORMAT },
192 { "input", TC_INPUT },
193 { "leftmargin", TC_LEFTMARGIN },
194 { "nofloat", TC_NOFLOAT },
195 { "nostyle", TC_NOSTYLE },
196 { "outputtype", TC_OUTPUTTYPE },
197 { "pagestyle", TC_PAGESTYLE },
198 { "preamble", TC_PREAMBLE },
199 { "providesamsmath", TC_PROVIDESAMSMATH },
200 { "providesmakeidx", TC_PROVIDESMAKEIDX },
201 { "providesnatbib", TC_PROVIDESNATBIB },
202 { "providesurl", TC_PROVIDESURL },
203 { "rightmargin", TC_RIGHTMARGIN },
204 { "secnumdepth", TC_SECNUMDEPTH },
205 { "sides", TC_SIDES },
206 { "style", TC_STYLE },
207 { "titlelatexname", TC_TITLELATEXNAME },
208 { "titlelatextype", TC_TITLELATEXTYPE },
209 { "tocdepth", TC_TOCDEPTH }
213 lyxerr[Debug::TCLASS] << "Reading textclass "
214 << lyx::to_utf8(makeDisplayPath(filename))
217 lyxerr[Debug::TCLASS] << "Reading input file "
218 << lyx::to_utf8(makeDisplayPath(filename))
221 LyXLex lexrc(textClassTags,
222 sizeof(textClassTags) / sizeof(textClassTags[0]));
224 lexrc.setFile(filename);
225 bool error = !lexrc.isOK();
227 // Format of files before the 'Format' tag was introduced
231 while (lexrc.isOK() && !error) {
232 int le = lexrc.lex();
235 case LyXLex::LEX_FEOF:
238 case LyXLex::LEX_UNDEF:
239 lexrc.printError("Unknown TextClass tag `$$Token'");
247 switch (static_cast<TextClassTags>(le)) {
251 format = lexrc.getInteger();
254 case TC_OUTPUTTYPE: // output type definition
255 readOutputType(lexrc);
258 case TC_INPUT: // Include file
260 string tmp = libFileSearch("layouts",
264 if (read(tmp, true)) {
265 lexrc.printError("Error reading input"
272 case TC_DEFAULTSTYLE:
274 string const name = subst(lexrc.getString(),
276 defaultlayout_ = name;
283 string const name = subst(lexrc.getString(),
285 if (hasLayout(name)) {
286 LyXLayout * lay = operator[](name).get();
287 error = do_readStyle(lexrc, *lay);
291 if (le == TC_ENVIRONMENT)
292 lay.is_environment = true;
293 error = do_readStyle(lexrc, lay);
295 layoutlist_.push_back(
296 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
299 if (defaultlayout_.empty()) {
300 // We do not have a default
301 // layout yet, so we choose
302 // the first layout we
304 defaultlayout_ = name;
309 lexrc.printError("No name given for style: `$$Token'.");
316 string const style = subst(lexrc.getString(),
318 if (!delete_layout(style))
319 lyxerr << "Cannot delete style `"
320 << style << '\'' << endl;
321 // lexrc.printError("Cannot delete style"
328 columns_ = lexrc.getInteger();
333 switch (lexrc.getInteger()) {
334 case 1: sides_ = OneSide; break;
335 case 2: sides_ = TwoSides; break;
337 lyxerr << "Impossible number of page"
338 " sides, setting to one."
348 pagestyle_ = rtrim(lexrc.getString());
352 defaultfont_.lyxRead(lexrc);
353 if (!defaultfont_.resolved()) {
354 lexrc.printError("Warning: defaultfont should "
355 "be fully instantiated!");
356 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
362 secnumdepth_ = lexrc.getInteger();
367 tocdepth_ = lexrc.getInteger();
370 // First step to support options
371 case TC_CLASSOPTIONS:
372 readClassOptions(lexrc);
376 preamble_ = lexrc.getLongString("EndPreamble");
379 case TC_PROVIDESAMSMATH:
380 if (lexrc.next() && lexrc.getInteger())
381 provides_ |= amsmath;
384 case TC_PROVIDESNATBIB:
385 if (lexrc.next() && lexrc.getInteger())
389 case TC_PROVIDESMAKEIDX:
390 if (lexrc.next() && lexrc.getInteger())
391 provides_ |= makeidx;
395 if (lexrc.next() && lexrc.getInteger())
399 case TC_LEFTMARGIN: // left margin type
401 leftmargin_ = lexrc.getString();
404 case TC_RIGHTMARGIN: // right margin type
406 rightmargin_ = lexrc.getString();
410 string const name = subst(lexrc.getString(), '_', ' ');
411 readCharStyle(lexrc, name);
420 case TC_TITLELATEXTYPE:
421 readTitleType(lexrc);
423 case TC_TITLELATEXNAME:
425 titlename_ = lexrc.getString();
429 string const nofloat = lexrc.getString();
430 floatlist_->erase(nofloat);
434 if (format != FORMAT)
438 if (format != FORMAT) {
439 lyxerr[Debug::TCLASS] << "Converting layout file from format "
440 << format << " to " << FORMAT << endl;
441 string const tempfile = lyx::support::tempName();
442 error = !layout2layout(filename, tempfile);
444 error = read(tempfile, merge);
445 lyx::support::unlink(tempfile);
449 if (!merge) { // we are at top level here.
450 lyxerr[Debug::TCLASS] << "Finished reading textclass "
451 << lyx::to_utf8(makeDisplayPath(filename))
453 if (defaultlayout_.empty()) {
454 lyxerr << "Error: Textclass '" << name_
455 << "' is missing a defaultstyle." << endl;
459 min_toclevel_ = LyXLayout::NOT_IN_TOC;
460 max_toclevel_ = LyXLayout::NOT_IN_TOC;
461 const_iterator cit = begin();
462 const_iterator the_end = end();
463 for ( ; cit != the_end ; ++cit) {
464 int const toclevel = (*cit)->toclevel;
465 if (toclevel != LyXLayout::NOT_IN_TOC) {
466 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
467 min_toclevel_ = toclevel;
469 min_toclevel_ = std::min(min_toclevel_,
471 max_toclevel_ = std::max(max_toclevel_,
475 lyxerr[Debug::TCLASS]
476 << "Minimum TocLevel is " << min_toclevel_
477 << ", maximum is " << max_toclevel_ <<endl;
480 lyxerr[Debug::TCLASS] << "Finished reading input file "
481 << lyx::to_utf8(makeDisplayPath(filename))
488 void LyXTextClass::readTitleType(LyXLex & lexrc)
490 keyword_item titleTypeTags[] = {
491 { "commandafter", TITLE_COMMAND_AFTER },
492 { "environment", TITLE_ENVIRONMENT }
495 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
497 int le = lexrc.lex();
499 case LyXLex::LEX_UNDEF:
500 lexrc.printError("Unknown output type `$$Token'");
502 case TITLE_COMMAND_AFTER:
503 case TITLE_ENVIRONMENT:
504 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
507 lyxerr << "Unhandled value " << le
508 << " in LyXTextClass::readTitleType." << endl;
515 void LyXTextClass::readOutputType(LyXLex & lexrc)
517 keyword_item outputTypeTags[] = {
518 { "docbook", DOCBOOK },
520 { "literate", LITERATE }
523 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
525 int le = lexrc.lex();
527 case LyXLex::LEX_UNDEF:
528 lexrc.printError("Unknown output type `$$Token'");
533 outputType_ = static_cast<OutputType>(le);
536 lyxerr << "Unhandled value " << le
537 << " in LyXTextClass::readOutputType." << endl;
544 enum ClassOptionsTags {
553 void LyXTextClass::readClassOptions(LyXLex & lexrc)
555 keyword_item classOptionsTags[] = {
557 {"fontsize", CO_FONTSIZE },
558 {"header", CO_HEADER },
559 {"other", CO_OTHER },
560 {"pagestyle", CO_PAGESTYLE }
563 lexrc.pushTable(classOptionsTags, CO_END);
565 while (!getout && lexrc.isOK()) {
566 int le = lexrc.lex();
568 case LyXLex::LEX_UNDEF:
569 lexrc.printError("Unknown ClassOption tag `$$Token'");
573 switch (static_cast<ClassOptionsTags>(le)) {
576 opt_fontsize_ = rtrim(lexrc.getString());
580 opt_pagestyle_ = rtrim(lexrc.getString());
584 options_ = lexrc.getString();
588 class_header_ = subst(lexrc.getString(), """, "\"");
609 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
611 keyword_item elementTags[] = {
614 { "labelfont", CS_LABELFONT },
615 { "latexname", CS_LATEXNAME },
616 { "latexparam", CS_LATEXPARAM },
617 { "latextype", CS_LATEXTYPE },
618 { "preamble", CS_PREAMBLE}
621 lexrc.pushTable(elementTags, CS_END);
626 LyXFont font(LyXFont::ALL_INHERIT);
627 LyXFont labelfont(LyXFont::ALL_INHERIT);
631 while (!getout && lexrc.isOK()) {
632 int le = lexrc.lex();
634 case LyXLex::LEX_UNDEF:
635 lexrc.printError("Unknown ClassOption tag `$$Token'");
639 switch (static_cast<CharStyleTags>(le)) {
642 latextype = lexrc.getString();
646 latexname = lexrc.getString();
650 latexparam = subst(lexrc.getString(), """, "\"");
653 labelfont.lyxRead(lexrc);
660 preamble = lexrc.getLongString("EndPreamble");
669 // Here add element to list if getout == true
673 cs.latextype = latextype;
674 cs.latexname = latexname;
675 cs.latexparam = latexparam;
677 cs.labelfont = labelfont;
678 cs.preamble = preamble;
679 charstyles().push_back(cs);
699 void LyXTextClass::readFloat(LyXLex & lexrc)
701 keyword_item floatTags[] = {
703 { "extension", FT_EXT },
704 { "guiname", FT_NAME },
705 { "latexbuiltin", FT_BUILTIN },
706 { "listname", FT_LISTNAME },
707 { "numberwithin", FT_WITHIN },
708 { "placement", FT_PLACEMENT },
709 { "style", FT_STYLE },
713 lexrc.pushTable(floatTags, FT_END);
722 bool builtin = false;
725 while (!getout && lexrc.isOK()) {
726 int le = lexrc.lex();
728 case LyXLex::LEX_UNDEF:
729 lexrc.printError("Unknown ClassOption tag `$$Token'");
733 switch (static_cast<FloatTags>(le)) {
736 type = lexrc.getString();
737 // Here we could check if this type is already defined
738 // and modify it with the rest of the vars instead.
742 name = lexrc.getString();
746 placement = lexrc.getString();
750 ext = lexrc.getString();
754 within = lexrc.getString();
755 if (within == "none")
760 style = lexrc.getString();
764 listname = lexrc.getString();
768 builtin = lexrc.getBool();
776 // Here if have a full float if getout == true
778 Floating newfloat(type, placement, ext, within,
779 style, name, listname, builtin);
780 floatlist_->newFloat(newfloat);
793 void LyXTextClass::readCounter(LyXLex & lexrc)
795 keyword_item counterTags[] = {
798 { "within", CT_WITHIN }
801 lexrc.pushTable(counterTags, CT_END);
807 while (!getout && lexrc.isOK()) {
808 int le = lexrc.lex();
810 case LyXLex::LEX_UNDEF:
811 lexrc.printError("Unknown ClassOption tag `$$Token'");
815 switch (static_cast<CounterTags>(le)) {
818 name = lexrc.getString();
822 within = lexrc.getString();
823 if (within == "none")
832 // Here if have a full counter if getout == true
834 if (within.empty()) {
835 ctrs_->newCounter(name);
837 ctrs_->newCounter(name, within);
845 LyXFont const & LyXTextClass::defaultfont() const
851 string const & LyXTextClass::leftmargin() const
857 string const & LyXTextClass::rightmargin() const
863 bool LyXTextClass::hasLayout(string const & n) const
865 string const name = (n.empty() ? defaultLayoutName() : n);
867 return find_if(layoutlist_.begin(), layoutlist_.end(),
868 LayoutNamesEqual(name))
869 != layoutlist_.end();
874 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
876 BOOST_ASSERT(!name.empty());
878 LayoutList::const_iterator cit =
879 find_if(layoutlist_.begin(),
881 LayoutNamesEqual(name));
883 if (cit == layoutlist_.end()) {
884 lyxerr << "We failed to find the layout '" << name
885 << "' in the layout list. You MUST investigate!"
887 for (LayoutList::const_iterator it = layoutlist_.begin();
888 it != layoutlist_.end(); ++it)
889 lyxerr << " " << it->get()->name() << endl;
891 // we require the name to exist
900 bool LyXTextClass::delete_layout(string const & name)
902 if (name == defaultLayoutName())
905 LayoutList::iterator it =
906 remove_if(layoutlist_.begin(), layoutlist_.end(),
907 LayoutNamesEqual(name));
909 LayoutList::iterator end = layoutlist_.end();
910 bool const ret = (it != end);
911 layoutlist_.erase(it, end);
916 // Load textclass info if not loaded yet
917 bool LyXTextClass::load(string const & path) const
922 // Read style-file, provided path is searched before the system ones
925 layout_file = addName(path, name_ + ".layout");
926 if (layout_file.empty() || !fs::exists(layout_file))
927 layout_file = libFileSearch("layouts", name_, "layout");
928 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
931 lyxerr << "Error reading `"
932 << lyx::to_utf8(makeDisplayPath(layout_file))
933 << "'\n(Check `" << name_
934 << "')\nCheck your installation and "
935 "try Options/Reconfigure..." << endl;
942 FloatList & LyXTextClass::floats()
944 return *floatlist_.get();
948 FloatList const & LyXTextClass::floats() const
950 return *floatlist_.get();
954 Counters & LyXTextClass::counters() const
960 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
962 CharStyles::iterator cs = charstyles().begin();
963 CharStyles::iterator csend = charstyles().end();
964 for (; cs != csend; ++cs) {
972 string const & LyXTextClass::defaultLayoutName() const
974 // This really should come from the actual layout... (Lgb)
975 return defaultlayout_;
979 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
981 return operator[](defaultLayoutName());
985 string const & LyXTextClass::name() const
991 string const & LyXTextClass::latexname() const
993 const_cast<LyXTextClass*>(this)->load();
998 string const & LyXTextClass::description() const
1000 return description_;
1004 string const & LyXTextClass::opt_fontsize() const
1006 return opt_fontsize_;
1010 string const & LyXTextClass::opt_pagestyle() const
1012 return opt_pagestyle_;
1016 string const & LyXTextClass::options() const
1022 string const & LyXTextClass::class_header() const
1024 return class_header_;
1028 string const & LyXTextClass::pagestyle() const
1034 string const & LyXTextClass::preamble() const
1040 LyXTextClass::PageSides LyXTextClass::sides() const
1046 int LyXTextClass::secnumdepth() const
1048 return secnumdepth_;
1052 int LyXTextClass::tocdepth() const
1058 OutputType LyXTextClass::outputType() const
1064 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1066 return provides_ & p;
1070 unsigned int LyXTextClass::columns() const
1076 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1082 string const & LyXTextClass::titlename() const
1088 int LyXTextClass::size() const
1090 return layoutlist_.size();
1094 int LyXTextClass::min_toclevel() const
1096 return min_toclevel_;
1100 int LyXTextClass::max_toclevel() const
1102 return max_toclevel_;
1106 bool LyXTextClass::hasTocLevels() const
1108 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1112 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1115 case LyXTextClass::OneSide:
1118 case LyXTextClass::TwoSides: