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 { "linuxdoc", LINUXDOC },
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'");
534 outputType_ = static_cast<OutputType>(le);
537 lyxerr << "Unhandled value " << le
538 << " in LyXTextClass::readOutputType." << endl;
545 enum ClassOptionsTags {
554 void LyXTextClass::readClassOptions(LyXLex & lexrc)
556 keyword_item classOptionsTags[] = {
558 {"fontsize", CO_FONTSIZE },
559 {"header", CO_HEADER },
560 {"other", CO_OTHER },
561 {"pagestyle", CO_PAGESTYLE }
564 lexrc.pushTable(classOptionsTags, CO_END);
566 while (!getout && lexrc.isOK()) {
567 int le = lexrc.lex();
569 case LyXLex::LEX_UNDEF:
570 lexrc.printError("Unknown ClassOption tag `$$Token'");
574 switch (static_cast<ClassOptionsTags>(le)) {
577 opt_fontsize_ = rtrim(lexrc.getString());
581 opt_pagestyle_ = rtrim(lexrc.getString());
585 options_ = lexrc.getString();
589 class_header_ = subst(lexrc.getString(), """, "\"");
610 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
612 keyword_item elementTags[] = {
615 { "labelfont", CS_LABELFONT },
616 { "latexname", CS_LATEXNAME },
617 { "latexparam", CS_LATEXPARAM },
618 { "latextype", CS_LATEXTYPE },
619 { "preamble", CS_PREAMBLE}
622 lexrc.pushTable(elementTags, CS_END);
627 LyXFont font(LyXFont::ALL_INHERIT);
628 LyXFont labelfont(LyXFont::ALL_INHERIT);
632 while (!getout && lexrc.isOK()) {
633 int le = lexrc.lex();
635 case LyXLex::LEX_UNDEF:
636 lexrc.printError("Unknown ClassOption tag `$$Token'");
640 switch (static_cast<CharStyleTags>(le)) {
643 latextype = lexrc.getString();
647 latexname = lexrc.getString();
651 latexparam = subst(lexrc.getString(), """, "\"");
654 labelfont.lyxRead(lexrc);
661 preamble = lexrc.getLongString("EndPreamble");
670 // Here add element to list if getout == true
674 cs.latextype = latextype;
675 cs.latexname = latexname;
676 cs.latexparam = latexparam;
678 cs.labelfont = labelfont;
679 cs.preamble = preamble;
680 charstyles().push_back(cs);
700 void LyXTextClass::readFloat(LyXLex & lexrc)
702 keyword_item floatTags[] = {
704 { "extension", FT_EXT },
705 { "guiname", FT_NAME },
706 { "latexbuiltin", FT_BUILTIN },
707 { "listname", FT_LISTNAME },
708 { "numberwithin", FT_WITHIN },
709 { "placement", FT_PLACEMENT },
710 { "style", FT_STYLE },
714 lexrc.pushTable(floatTags, FT_END);
723 bool builtin = false;
726 while (!getout && lexrc.isOK()) {
727 int le = lexrc.lex();
729 case LyXLex::LEX_UNDEF:
730 lexrc.printError("Unknown ClassOption tag `$$Token'");
734 switch (static_cast<FloatTags>(le)) {
737 type = lexrc.getString();
738 // Here we could check if this type is already defined
739 // and modify it with the rest of the vars instead.
743 name = lexrc.getString();
747 placement = lexrc.getString();
751 ext = lexrc.getString();
755 within = lexrc.getString();
756 if (within == "none")
761 style = lexrc.getString();
765 listname = lexrc.getString();
769 builtin = lexrc.getBool();
777 // Here if have a full float if getout == true
779 Floating newfloat(type, placement, ext, within,
780 style, name, listname, builtin);
781 floatlist_->newFloat(newfloat);
794 void LyXTextClass::readCounter(LyXLex & lexrc)
796 keyword_item counterTags[] = {
799 { "within", CT_WITHIN }
802 lexrc.pushTable(counterTags, CT_END);
808 while (!getout && lexrc.isOK()) {
809 int le = lexrc.lex();
811 case LyXLex::LEX_UNDEF:
812 lexrc.printError("Unknown ClassOption tag `$$Token'");
816 switch (static_cast<CounterTags>(le)) {
819 name = lexrc.getString();
823 within = lexrc.getString();
824 if (within == "none")
833 // Here if have a full counter if getout == true
835 if (within.empty()) {
836 ctrs_->newCounter(name);
838 ctrs_->newCounter(name, within);
846 LyXFont const & LyXTextClass::defaultfont() const
852 string const & LyXTextClass::leftmargin() const
858 string const & LyXTextClass::rightmargin() const
864 bool LyXTextClass::hasLayout(string const & n) const
866 string const name = (n.empty() ? defaultLayoutName() : n);
868 return find_if(layoutlist_.begin(), layoutlist_.end(),
869 LayoutNamesEqual(name))
870 != layoutlist_.end();
875 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
877 BOOST_ASSERT(!name.empty());
879 LayoutList::const_iterator cit =
880 find_if(layoutlist_.begin(),
882 LayoutNamesEqual(name));
884 if (cit == layoutlist_.end()) {
885 lyxerr << "We failed to find the layout '" << name
886 << "' in the layout list. You MUST investigate!"
888 for (LayoutList::const_iterator it = layoutlist_.begin();
889 it != layoutlist_.end(); ++it)
890 lyxerr << " " << it->get()->name() << endl;
892 // we require the name to exist
901 bool LyXTextClass::delete_layout(string const & name)
903 if (name == defaultLayoutName())
906 LayoutList::iterator it =
907 remove_if(layoutlist_.begin(), layoutlist_.end(),
908 LayoutNamesEqual(name));
910 LayoutList::iterator end = layoutlist_.end();
911 bool const ret = (it != end);
912 layoutlist_.erase(it, end);
917 // Load textclass info if not loaded yet
918 bool LyXTextClass::load(string const & path) const
923 // Read style-file, provided path is searched before the system ones
926 layout_file = addName(path, name_ + ".layout");
927 if (layout_file.empty() || !fs::exists(layout_file))
928 layout_file = libFileSearch("layouts", name_, "layout");
929 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
932 lyxerr << "Error reading `"
933 << makeDisplayPath(layout_file)
934 << "'\n(Check `" << name_
935 << "')\nCheck your installation and "
936 "try Options/Reconfigure..." << endl;
943 FloatList & LyXTextClass::floats()
945 return *floatlist_.get();
949 FloatList const & LyXTextClass::floats() const
951 return *floatlist_.get();
955 Counters & LyXTextClass::counters() const
961 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
963 CharStyles::iterator cs = charstyles().begin();
964 CharStyles::iterator csend = charstyles().end();
965 for (; cs != csend; ++cs) {
973 string const & LyXTextClass::defaultLayoutName() const
975 // This really should come from the actual layout... (Lgb)
976 return defaultlayout_;
980 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
982 return operator[](defaultLayoutName());
986 string const & LyXTextClass::name() const
992 string const & LyXTextClass::latexname() const
994 const_cast<LyXTextClass*>(this)->load();
999 string const & LyXTextClass::description() const
1001 return description_;
1005 string const & LyXTextClass::opt_fontsize() const
1007 return opt_fontsize_;
1011 string const & LyXTextClass::opt_pagestyle() const
1013 return opt_pagestyle_;
1017 string const & LyXTextClass::options() const
1023 string const & LyXTextClass::class_header() const
1025 return class_header_;
1029 string const & LyXTextClass::pagestyle() const
1035 string const & LyXTextClass::preamble() const
1041 LyXTextClass::PageSides LyXTextClass::sides() const
1047 int LyXTextClass::secnumdepth() const
1049 return secnumdepth_;
1053 int LyXTextClass::tocdepth() const
1059 OutputType LyXTextClass::outputType() const
1065 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1067 return provides_ & p;
1071 unsigned int LyXTextClass::columns() const
1077 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1083 string const & LyXTextClass::titlename() const
1089 int LyXTextClass::size() const
1091 return layoutlist_.size();
1095 int LyXTextClass::min_toclevel() const
1097 return min_toclevel_;
1101 int LyXTextClass::max_toclevel() const
1103 return max_toclevel_;
1107 bool LyXTextClass::hasTocLevels() const
1109 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1113 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1116 case LyXTextClass::OneSide:
1119 case LyXTextClass::TwoSides: