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;
37 using support::libFileSearch;
38 using support::makeDisplayPath;
39 using support::quoteName;
42 using support::addName;
53 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
55 LayoutNamesEqual(string const & name)
58 bool operator()(LyXLayout_ptr const & c) const
60 return c->name() == name_;
70 bool layout2layout(string const & filename, string const & tempfile)
72 string const script = libFileSearch("scripts", "layout2layout.py");
74 lyxerr << "Could not find layout conversion "
75 "script layout2layout.py." << endl;
79 std::ostringstream command;
80 command << support::os::python() << ' ' << quoteName(script)
81 << ' ' << quoteName(filename)
82 << ' ' << quoteName(tempfile);
83 string const command_str = command.str();
85 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
87 support::cmd_ret const ret =
88 support::runCommand(command_str);
90 lyxerr << "Could not run layout conversion "
91 "script layout2layout.py." << endl;
100 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
101 string const & desc, bool texClassAvail )
102 : name_(fn), latexname_(cln), description_(desc),
103 floatlist_(new FloatList), ctrs_(new Counters),
104 texClassAvail_(texClassAvail)
111 pagestyle_ = "default";
112 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
113 opt_fontsize_ = "10|11|12";
114 opt_pagestyle_ = "empty|plain|headings|fancy";
116 titletype_ = TITLE_COMMAND_AFTER;
117 titlename_ = "maketitle";
122 bool LyXTextClass::isTeXClassAvailable() const
124 return texClassAvail_;
128 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
130 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
131 if (!lay.read(lexrc, *this)) {
133 lay.resfont = lay.font;
134 lay.resfont.realize(defaultfont());
135 lay.reslabelfont = lay.labelfont;
136 lay.reslabelfont.realize(defaultfont());
137 return false; // no errors
139 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
175 // Reads a textclass structure from file.
176 bool LyXTextClass::read(string const & filename, bool merge)
178 if (!support::isFileReadable(filename)) {
179 lyxerr << "Cannot read layout file `" << filename << "'."
184 keyword_item textClassTags[] = {
185 { "charstyle", TC_CHARSTYLE },
186 { "classoptions", TC_CLASSOPTIONS },
187 { "columns", TC_COLUMNS },
188 { "counter", TC_COUNTER },
189 { "defaultfont", TC_DEFAULTFONT },
190 { "defaultstyle", TC_DEFAULTSTYLE },
191 { "environment", TC_ENVIRONMENT },
192 { "float", TC_FLOAT },
193 { "format", TC_FORMAT },
194 { "input", TC_INPUT },
195 { "leftmargin", TC_LEFTMARGIN },
196 { "nofloat", TC_NOFLOAT },
197 { "nostyle", TC_NOSTYLE },
198 { "outputtype", TC_OUTPUTTYPE },
199 { "pagestyle", TC_PAGESTYLE },
200 { "preamble", TC_PREAMBLE },
201 { "providesamsmath", TC_PROVIDESAMSMATH },
202 { "providesmakeidx", TC_PROVIDESMAKEIDX },
203 { "providesnatbib", TC_PROVIDESNATBIB },
204 { "providesurl", TC_PROVIDESURL },
205 { "rightmargin", TC_RIGHTMARGIN },
206 { "secnumdepth", TC_SECNUMDEPTH },
207 { "sides", TC_SIDES },
208 { "style", TC_STYLE },
209 { "titlelatexname", TC_TITLELATEXNAME },
210 { "titlelatextype", TC_TITLELATEXTYPE },
211 { "tocdepth", TC_TOCDEPTH }
215 lyxerr[Debug::TCLASS] << "Reading textclass "
216 << to_utf8(makeDisplayPath(filename))
219 lyxerr[Debug::TCLASS] << "Reading input file "
220 << to_utf8(makeDisplayPath(filename))
223 LyXLex lexrc(textClassTags,
224 sizeof(textClassTags) / sizeof(textClassTags[0]));
226 lexrc.setFile(filename);
227 bool error = !lexrc.isOK();
229 // Format of files before the 'Format' tag was introduced
233 while (lexrc.isOK() && !error) {
234 int le = lexrc.lex();
237 case LyXLex::LEX_FEOF:
240 case LyXLex::LEX_UNDEF:
241 lexrc.printError("Unknown TextClass tag `$$Token'");
249 switch (static_cast<TextClassTags>(le)) {
253 format = lexrc.getInteger();
256 case TC_OUTPUTTYPE: // output type definition
257 readOutputType(lexrc);
260 case TC_INPUT: // Include file
262 string const inc = lexrc.getString();
263 string tmp = libFileSearch("layouts", inc,
267 lexrc.printError("Could not find input"
270 } else if (read(tmp, true)) {
271 lexrc.printError("Error reading input"
278 case TC_DEFAULTSTYLE:
280 string const name = subst(lexrc.getString(),
282 defaultlayout_ = name;
289 string const name = subst(lexrc.getString(),
291 if (hasLayout(name)) {
292 LyXLayout * lay = operator[](name).get();
293 error = do_readStyle(lexrc, *lay);
297 if (le == TC_ENVIRONMENT)
298 lay.is_environment = true;
299 error = do_readStyle(lexrc, lay);
301 layoutlist_.push_back(
302 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
305 if (defaultlayout_.empty()) {
306 // We do not have a default
307 // layout yet, so we choose
308 // the first layout we
310 defaultlayout_ = name;
315 lexrc.printError("No name given for style: `$$Token'.");
322 string const style = subst(lexrc.getString(),
324 if (!delete_layout(style))
325 lyxerr << "Cannot delete style `"
326 << style << '\'' << endl;
327 // lexrc.printError("Cannot delete style"
334 columns_ = lexrc.getInteger();
339 switch (lexrc.getInteger()) {
340 case 1: sides_ = OneSide; break;
341 case 2: sides_ = TwoSides; break;
343 lyxerr << "Impossible number of page"
344 " sides, setting to one."
354 pagestyle_ = rtrim(lexrc.getString());
358 defaultfont_.lyxRead(lexrc);
359 if (!defaultfont_.resolved()) {
360 lexrc.printError("Warning: defaultfont should "
361 "be fully instantiated!");
362 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
368 secnumdepth_ = lexrc.getInteger();
373 tocdepth_ = lexrc.getInteger();
376 // First step to support options
377 case TC_CLASSOPTIONS:
378 readClassOptions(lexrc);
382 preamble_ = lexrc.getLongString("EndPreamble");
385 case TC_PROVIDESAMSMATH:
386 if (lexrc.next() && lexrc.getInteger())
387 provides_ |= amsmath;
390 case TC_PROVIDESNATBIB:
391 if (lexrc.next() && lexrc.getInteger())
395 case TC_PROVIDESMAKEIDX:
396 if (lexrc.next() && lexrc.getInteger())
397 provides_ |= makeidx;
401 if (lexrc.next() && lexrc.getInteger())
405 case TC_LEFTMARGIN: // left margin type
407 leftmargin_ = lexrc.getString();
410 case TC_RIGHTMARGIN: // right margin type
412 rightmargin_ = lexrc.getString();
416 string const name = subst(lexrc.getString(), '_', ' ');
417 readCharStyle(lexrc, name);
426 case TC_TITLELATEXTYPE:
427 readTitleType(lexrc);
429 case TC_TITLELATEXNAME:
431 titlename_ = lexrc.getString();
435 string const nofloat = lexrc.getString();
436 floatlist_->erase(nofloat);
440 if (format != FORMAT)
444 if (format != FORMAT) {
445 lyxerr[Debug::TCLASS] << "Converting layout file from format "
446 << format << " to " << FORMAT << endl;
447 string const tempfile = support::tempName();
448 error = !layout2layout(filename, tempfile);
450 error = read(tempfile, merge);
451 support::unlink(tempfile);
455 if (!merge) { // we are at top level here.
456 lyxerr[Debug::TCLASS] << "Finished reading textclass "
457 << to_utf8(makeDisplayPath(filename))
459 if (defaultlayout_.empty()) {
460 lyxerr << "Error: Textclass '" << name_
461 << "' is missing a defaultstyle." << endl;
465 min_toclevel_ = LyXLayout::NOT_IN_TOC;
466 max_toclevel_ = LyXLayout::NOT_IN_TOC;
467 const_iterator cit = begin();
468 const_iterator the_end = end();
469 for ( ; cit != the_end ; ++cit) {
470 int const toclevel = (*cit)->toclevel;
471 if (toclevel != LyXLayout::NOT_IN_TOC) {
472 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
473 min_toclevel_ = toclevel;
475 min_toclevel_ = std::min(min_toclevel_,
477 max_toclevel_ = std::max(max_toclevel_,
481 lyxerr[Debug::TCLASS]
482 << "Minimum TocLevel is " << min_toclevel_
483 << ", maximum is " << max_toclevel_ <<endl;
486 lyxerr[Debug::TCLASS] << "Finished reading input file "
487 << to_utf8(makeDisplayPath(filename))
494 void LyXTextClass::readTitleType(LyXLex & lexrc)
496 keyword_item titleTypeTags[] = {
497 { "commandafter", TITLE_COMMAND_AFTER },
498 { "environment", TITLE_ENVIRONMENT }
501 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
503 int le = lexrc.lex();
505 case LyXLex::LEX_UNDEF:
506 lexrc.printError("Unknown output type `$$Token'");
508 case TITLE_COMMAND_AFTER:
509 case TITLE_ENVIRONMENT:
510 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
513 lyxerr << "Unhandled value " << le
514 << " in LyXTextClass::readTitleType." << endl;
521 void LyXTextClass::readOutputType(LyXLex & lexrc)
523 keyword_item outputTypeTags[] = {
524 { "docbook", DOCBOOK },
526 { "literate", LITERATE }
529 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
531 int le = lexrc.lex();
533 case LyXLex::LEX_UNDEF:
534 lexrc.printError("Unknown output type `$$Token'");
539 outputType_ = static_cast<OutputType>(le);
542 lyxerr << "Unhandled value " << le
543 << " in LyXTextClass::readOutputType." << endl;
550 enum ClassOptionsTags {
559 void LyXTextClass::readClassOptions(LyXLex & lexrc)
561 keyword_item classOptionsTags[] = {
563 {"fontsize", CO_FONTSIZE },
564 {"header", CO_HEADER },
565 {"other", CO_OTHER },
566 {"pagestyle", CO_PAGESTYLE }
569 lexrc.pushTable(classOptionsTags, CO_END);
571 while (!getout && lexrc.isOK()) {
572 int le = lexrc.lex();
574 case LyXLex::LEX_UNDEF:
575 lexrc.printError("Unknown ClassOption tag `$$Token'");
579 switch (static_cast<ClassOptionsTags>(le)) {
582 opt_fontsize_ = rtrim(lexrc.getString());
586 opt_pagestyle_ = rtrim(lexrc.getString());
590 options_ = lexrc.getString();
594 class_header_ = subst(lexrc.getString(), """, "\"");
615 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
617 keyword_item elementTags[] = {
620 { "labelfont", CS_LABELFONT },
621 { "latexname", CS_LATEXNAME },
622 { "latexparam", CS_LATEXPARAM },
623 { "latextype", CS_LATEXTYPE },
624 { "preamble", CS_PREAMBLE}
627 lexrc.pushTable(elementTags, CS_END);
632 LyXFont font(LyXFont::ALL_INHERIT);
633 LyXFont labelfont(LyXFont::ALL_INHERIT);
637 while (!getout && lexrc.isOK()) {
638 int le = lexrc.lex();
640 case LyXLex::LEX_UNDEF:
641 lexrc.printError("Unknown ClassOption tag `$$Token'");
645 switch (static_cast<CharStyleTags>(le)) {
648 latextype = lexrc.getString();
652 latexname = lexrc.getString();
656 latexparam = subst(lexrc.getString(), """, "\"");
659 labelfont.lyxRead(lexrc);
666 preamble = lexrc.getLongString("EndPreamble");
675 // Here add element to list if getout == true
679 cs.latextype = latextype;
680 cs.latexname = latexname;
681 cs.latexparam = latexparam;
683 cs.labelfont = labelfont;
684 cs.preamble = preamble;
685 charstyles().push_back(cs);
705 void LyXTextClass::readFloat(LyXLex & lexrc)
707 keyword_item floatTags[] = {
709 { "extension", FT_EXT },
710 { "guiname", FT_NAME },
711 { "latexbuiltin", FT_BUILTIN },
712 { "listname", FT_LISTNAME },
713 { "numberwithin", FT_WITHIN },
714 { "placement", FT_PLACEMENT },
715 { "style", FT_STYLE },
719 lexrc.pushTable(floatTags, FT_END);
728 bool builtin = false;
731 while (!getout && lexrc.isOK()) {
732 int le = lexrc.lex();
734 case LyXLex::LEX_UNDEF:
735 lexrc.printError("Unknown ClassOption tag `$$Token'");
739 switch (static_cast<FloatTags>(le)) {
742 type = lexrc.getString();
743 // Here we could check if this type is already defined
744 // and modify it with the rest of the vars instead.
748 name = lexrc.getString();
752 placement = lexrc.getString();
756 ext = lexrc.getString();
760 within = lexrc.getString();
761 if (within == "none")
766 style = lexrc.getString();
770 listname = lexrc.getString();
774 builtin = lexrc.getBool();
782 // Here if have a full float if getout == true
784 Floating newfloat(type, placement, ext, within,
785 style, name, listname, builtin);
786 floatlist_->newFloat(newfloat);
799 void LyXTextClass::readCounter(LyXLex & lexrc)
801 keyword_item counterTags[] = {
804 { "within", CT_WITHIN }
807 lexrc.pushTable(counterTags, CT_END);
813 while (!getout && lexrc.isOK()) {
814 int le = lexrc.lex();
816 case LyXLex::LEX_UNDEF:
817 lexrc.printError("Unknown ClassOption tag `$$Token'");
821 switch (static_cast<CounterTags>(le)) {
824 name = lyx::from_ascii(lexrc.getString());
828 within = lyx::from_ascii(lexrc.getString());
829 if (within == lyx::from_ascii("none"))
838 // Here if have a full counter if getout == true
841 ctrs_->newCounter(name);
843 ctrs_->newCounter(name, within);
850 LyXFont const & LyXTextClass::defaultfont() const
856 string const & LyXTextClass::leftmargin() const
862 string const & LyXTextClass::rightmargin() const
868 bool LyXTextClass::hasLayout(string const & n) const
870 string const name = (n.empty() ? defaultLayoutName() : n);
872 return find_if(layoutlist_.begin(), layoutlist_.end(),
873 LayoutNamesEqual(name))
874 != layoutlist_.end();
879 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
881 BOOST_ASSERT(!name.empty());
883 LayoutList::const_iterator cit =
884 find_if(layoutlist_.begin(),
886 LayoutNamesEqual(name));
888 if (cit == layoutlist_.end()) {
889 lyxerr << "We failed to find the layout '" << name
890 << "' in the layout list. You MUST investigate!"
892 for (LayoutList::const_iterator it = layoutlist_.begin();
893 it != layoutlist_.end(); ++it)
894 lyxerr << " " << it->get()->name() << endl;
896 // we require the name to exist
905 bool LyXTextClass::delete_layout(string const & name)
907 if (name == defaultLayoutName())
910 LayoutList::iterator it =
911 remove_if(layoutlist_.begin(), layoutlist_.end(),
912 LayoutNamesEqual(name));
914 LayoutList::iterator end = layoutlist_.end();
915 bool const ret = (it != end);
916 layoutlist_.erase(it, end);
921 // Load textclass info if not loaded yet
922 bool LyXTextClass::load(string const & path) const
927 // Read style-file, provided path is searched before the system ones
930 layout_file = addName(path, name_ + ".layout");
931 if (layout_file.empty() || !fs::exists(layout_file))
932 layout_file = libFileSearch("layouts", name_, "layout");
933 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
936 lyxerr << "Error reading `"
937 << to_utf8(makeDisplayPath(layout_file))
938 << "'\n(Check `" << name_
939 << "')\nCheck your installation and "
940 "try Options/Reconfigure..." << endl;
947 FloatList & LyXTextClass::floats()
949 return *floatlist_.get();
953 FloatList const & LyXTextClass::floats() const
955 return *floatlist_.get();
959 Counters & LyXTextClass::counters() const
965 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
967 CharStyles::iterator cs = charstyles().begin();
968 CharStyles::iterator csend = charstyles().end();
969 for (; cs != csend; ++cs) {
977 string const & LyXTextClass::defaultLayoutName() const
979 // This really should come from the actual layout... (Lgb)
980 return defaultlayout_;
984 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
986 return operator[](defaultLayoutName());
990 string const & LyXTextClass::name() const
996 string const & LyXTextClass::latexname() const
998 const_cast<LyXTextClass*>(this)->load();
1003 string const & LyXTextClass::description() const
1005 return description_;
1009 string const & LyXTextClass::opt_fontsize() const
1011 return opt_fontsize_;
1015 string const & LyXTextClass::opt_pagestyle() const
1017 return opt_pagestyle_;
1021 string const & LyXTextClass::options() const
1027 string const & LyXTextClass::class_header() const
1029 return class_header_;
1033 string const & LyXTextClass::pagestyle() const
1039 string const & LyXTextClass::preamble() const
1045 LyXTextClass::PageSides LyXTextClass::sides() const
1051 int LyXTextClass::secnumdepth() const
1053 return secnumdepth_;
1057 int LyXTextClass::tocdepth() const
1063 OutputType LyXTextClass::outputType() const
1069 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1071 return provides_ & p;
1075 unsigned int LyXTextClass::columns() const
1081 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1087 string const & LyXTextClass::titlename() const
1093 int LyXTextClass::size() const
1095 return layoutlist_.size();
1099 int LyXTextClass::min_toclevel() const
1101 return min_toclevel_;
1105 int LyXTextClass::max_toclevel() const
1107 return max_toclevel_;
1111 bool LyXTextClass::hasTocLevels() const
1113 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1117 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1120 case LyXTextClass::OneSide:
1123 case LyXTextClass::TwoSides: