2 * \file LyXTextClass.cpp
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::FileName;
38 using support::libFileSearch;
39 using support::makeDisplayPath;
40 using support::quoteName;
43 using support::addName;
54 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
56 LayoutNamesEqual(string const & name)
59 bool operator()(LyXLayout_ptr const & c) const
61 return c->name() == name_;
71 bool layout2layout(FileName const & filename, FileName const & tempfile)
73 FileName const script = libFileSearch("scripts", "layout2layout.py");
75 lyxerr << "Could not find layout conversion "
76 "script layout2layout.py." << endl;
80 std::ostringstream command;
81 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
82 << ' ' << quoteName(filename.toFilesystemEncoding())
83 << ' ' << quoteName(tempfile.toFilesystemEncoding());
84 string const command_str = command.str();
86 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
88 support::cmd_ret const ret =
89 support::runCommand(command_str);
91 lyxerr << "Could not run layout conversion "
92 "script layout2layout.py." << endl;
101 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
102 string const & desc, bool texClassAvail )
103 : name_(fn), latexname_(cln), description_(desc),
104 floatlist_(new FloatList), ctrs_(new Counters),
105 texClassAvail_(texClassAvail)
112 pagestyle_ = "default";
113 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
114 opt_fontsize_ = "10|11|12";
115 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;
172 // Reads a textclass structure from file.
173 bool LyXTextClass::read(FileName const & filename, bool merge)
175 if (!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 { "provides", TC_PROVIDES },
199 { "rightmargin", TC_RIGHTMARGIN },
200 { "secnumdepth", TC_SECNUMDEPTH },
201 { "sides", TC_SIDES },
202 { "style", TC_STYLE },
203 { "titlelatexname", TC_TITLELATEXNAME },
204 { "titlelatextype", TC_TITLELATEXTYPE },
205 { "tocdepth", TC_TOCDEPTH }
209 LYXERR(Debug::TCLASS) << "Reading textclass "
210 << to_utf8(makeDisplayPath(filename.absFilename()))
213 LYXERR(Debug::TCLASS) << "Reading input file "
214 << to_utf8(makeDisplayPath(filename.absFilename()))
217 LyXLex lexrc(textClassTags,
218 sizeof(textClassTags) / sizeof(textClassTags[0]));
220 lexrc.setFile(filename);
221 bool error = !lexrc.isOK();
223 // Format of files before the 'Format' tag was introduced
227 while (lexrc.isOK() && !error) {
228 int le = lexrc.lex();
231 case LyXLex::LEX_FEOF:
234 case LyXLex::LEX_UNDEF:
235 lexrc.printError("Unknown TextClass tag `$$Token'");
243 switch (static_cast<TextClassTags>(le)) {
247 format = lexrc.getInteger();
250 case TC_OUTPUTTYPE: // output type definition
251 readOutputType(lexrc);
254 case TC_INPUT: // Include file
256 string const inc = lexrc.getString();
257 FileName tmp = libFileSearch("layouts", inc,
261 lexrc.printError("Could not find input"
264 } else if (read(tmp, true)) {
265 lexrc.printError("Error reading input"
266 "file: " + tmp.absFilename());
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_ = from_utf8(lexrc.getLongString("EndPreamble"));
381 string const feature = lexrc.getString();
383 if (lexrc.getInteger())
384 provides_.insert(feature);
386 provides_.erase(feature);
390 case TC_LEFTMARGIN: // left margin type
392 leftmargin_ = lexrc.getString();
395 case TC_RIGHTMARGIN: // right margin type
397 rightmargin_ = lexrc.getString();
401 string const name = subst(lexrc.getString(), '_', ' ');
402 readCharStyle(lexrc, name);
411 case TC_TITLELATEXTYPE:
412 readTitleType(lexrc);
414 case TC_TITLELATEXNAME:
416 titlename_ = lexrc.getString();
420 string const nofloat = lexrc.getString();
421 floatlist_->erase(nofloat);
425 if (format != FORMAT)
429 if (format != FORMAT) {
430 LYXERR(Debug::TCLASS) << "Converting layout file from format "
431 << format << " to " << FORMAT << endl;
432 FileName const tempfile(support::tempName());
433 error = !layout2layout(filename, tempfile);
435 error = read(tempfile, merge);
436 support::unlink(tempfile);
440 if (!merge) { // we are at top level here.
441 LYXERR(Debug::TCLASS) << "Finished reading textclass "
442 << to_utf8(makeDisplayPath(filename.absFilename()))
444 if (defaultlayout_.empty()) {
445 lyxerr << "Error: Textclass '" << name_
446 << "' is missing a defaultstyle." << endl;
450 min_toclevel_ = LyXLayout::NOT_IN_TOC;
451 max_toclevel_ = LyXLayout::NOT_IN_TOC;
452 const_iterator cit = begin();
453 const_iterator the_end = end();
454 for ( ; cit != the_end ; ++cit) {
455 int const toclevel = (*cit)->toclevel;
456 if (toclevel != LyXLayout::NOT_IN_TOC) {
457 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
458 min_toclevel_ = toclevel;
460 min_toclevel_ = std::min(min_toclevel_,
462 max_toclevel_ = std::max(max_toclevel_,
466 LYXERR(Debug::TCLASS)
467 << "Minimum TocLevel is " << min_toclevel_
468 << ", maximum is " << max_toclevel_ <<endl;
471 LYXERR(Debug::TCLASS) << "Finished reading input file "
472 << to_utf8(makeDisplayPath(filename.absFilename()))
479 void LyXTextClass::readTitleType(LyXLex & lexrc)
481 keyword_item titleTypeTags[] = {
482 { "commandafter", TITLE_COMMAND_AFTER },
483 { "environment", TITLE_ENVIRONMENT }
486 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
488 int le = lexrc.lex();
490 case LyXLex::LEX_UNDEF:
491 lexrc.printError("Unknown output type `$$Token'");
493 case TITLE_COMMAND_AFTER:
494 case TITLE_ENVIRONMENT:
495 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
498 lyxerr << "Unhandled value " << le
499 << " in LyXTextClass::readTitleType." << endl;
506 void LyXTextClass::readOutputType(LyXLex & lexrc)
508 keyword_item outputTypeTags[] = {
509 { "docbook", DOCBOOK },
511 { "literate", LITERATE }
514 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
516 int le = lexrc.lex();
518 case LyXLex::LEX_UNDEF:
519 lexrc.printError("Unknown output type `$$Token'");
524 outputType_ = static_cast<OutputType>(le);
527 lyxerr << "Unhandled value " << le
528 << " in LyXTextClass::readOutputType." << endl;
535 enum ClassOptionsTags {
544 void LyXTextClass::readClassOptions(LyXLex & lexrc)
546 keyword_item classOptionsTags[] = {
548 {"fontsize", CO_FONTSIZE },
549 {"header", CO_HEADER },
550 {"other", CO_OTHER },
551 {"pagestyle", CO_PAGESTYLE }
554 lexrc.pushTable(classOptionsTags, CO_END);
556 while (!getout && lexrc.isOK()) {
557 int le = lexrc.lex();
559 case LyXLex::LEX_UNDEF:
560 lexrc.printError("Unknown ClassOption tag `$$Token'");
564 switch (static_cast<ClassOptionsTags>(le)) {
567 opt_fontsize_ = rtrim(lexrc.getString());
571 opt_pagestyle_ = rtrim(lexrc.getString());
575 options_ = lexrc.getString();
579 class_header_ = subst(lexrc.getString(), """, "\"");
600 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
602 keyword_item elementTags[] = {
605 { "labelfont", CS_LABELFONT },
606 { "latexname", CS_LATEXNAME },
607 { "latexparam", CS_LATEXPARAM },
608 { "latextype", CS_LATEXTYPE },
609 { "preamble", CS_PREAMBLE}
612 lexrc.pushTable(elementTags, CS_END);
617 LyXFont font(LyXFont::ALL_INHERIT);
618 LyXFont labelfont(LyXFont::ALL_INHERIT);
622 while (!getout && lexrc.isOK()) {
623 int le = lexrc.lex();
625 case LyXLex::LEX_UNDEF:
626 lexrc.printError("Unknown ClassOption tag `$$Token'");
630 switch (static_cast<CharStyleTags>(le)) {
633 latextype = lexrc.getString();
637 latexname = lexrc.getString();
641 latexparam = subst(lexrc.getString(), """, "\"");
644 labelfont.lyxRead(lexrc);
651 preamble = lexrc.getLongString("EndPreamble");
660 // Here add element to list if getout == true
664 cs.latextype = latextype;
665 cs.latexname = latexname;
666 cs.latexparam = latexparam;
668 cs.labelfont = labelfont;
669 cs.preamble = from_utf8(preamble);
670 charstyles().push_back(cs);
690 void LyXTextClass::readFloat(LyXLex & lexrc)
692 keyword_item floatTags[] = {
694 { "extension", FT_EXT },
695 { "guiname", FT_NAME },
696 { "latexbuiltin", FT_BUILTIN },
697 { "listname", FT_LISTNAME },
698 { "numberwithin", FT_WITHIN },
699 { "placement", FT_PLACEMENT },
700 { "style", FT_STYLE },
704 lexrc.pushTable(floatTags, FT_END);
713 bool builtin = false;
716 while (!getout && lexrc.isOK()) {
717 int le = lexrc.lex();
719 case LyXLex::LEX_UNDEF:
720 lexrc.printError("Unknown ClassOption tag `$$Token'");
724 switch (static_cast<FloatTags>(le)) {
727 type = lexrc.getString();
728 // Here we could check if this type is already defined
729 // and modify it with the rest of the vars instead.
733 name = lexrc.getString();
737 placement = lexrc.getString();
741 ext = lexrc.getString();
745 within = lexrc.getString();
746 if (within == "none")
751 style = lexrc.getString();
755 listname = lexrc.getString();
759 builtin = lexrc.getBool();
767 // Here if have a full float if getout == true
769 Floating newfloat(type, placement, ext, within,
770 style, name, listname, builtin);
771 floatlist_->newFloat(newfloat);
784 void LyXTextClass::readCounter(LyXLex & lexrc)
786 keyword_item counterTags[] = {
789 { "within", CT_WITHIN }
792 lexrc.pushTable(counterTags, CT_END);
798 while (!getout && lexrc.isOK()) {
799 int le = lexrc.lex();
801 case LyXLex::LEX_UNDEF:
802 lexrc.printError("Unknown ClassOption tag `$$Token'");
806 switch (static_cast<CounterTags>(le)) {
809 name = from_ascii(lexrc.getString());
813 within = from_ascii(lexrc.getString());
814 if (within == "none")
823 // Here if have a full counter if getout == true
826 ctrs_->newCounter(name);
828 ctrs_->newCounter(name, within);
835 LyXFont const & LyXTextClass::defaultfont() const
841 string const & LyXTextClass::leftmargin() const
847 string const & LyXTextClass::rightmargin() const
853 bool LyXTextClass::hasLayout(string const & n) const
855 string const name = (n.empty() ? defaultLayoutName() : n);
857 return find_if(layoutlist_.begin(), layoutlist_.end(),
858 LayoutNamesEqual(name))
859 != layoutlist_.end();
864 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
866 BOOST_ASSERT(!name.empty());
868 LayoutList::const_iterator cit =
869 find_if(layoutlist_.begin(),
871 LayoutNamesEqual(name));
873 if (cit == layoutlist_.end()) {
874 lyxerr << "We failed to find the layout '" << name
875 << "' in the layout list. You MUST investigate!"
877 for (LayoutList::const_iterator it = layoutlist_.begin();
878 it != layoutlist_.end(); ++it)
879 lyxerr << " " << it->get()->name() << endl;
881 // we require the name to exist
890 bool LyXTextClass::delete_layout(string const & name)
892 if (name == defaultLayoutName())
895 LayoutList::iterator it =
896 remove_if(layoutlist_.begin(), layoutlist_.end(),
897 LayoutNamesEqual(name));
899 LayoutList::iterator end = layoutlist_.end();
900 bool const ret = (it != end);
901 layoutlist_.erase(it, end);
906 // Load textclass info if not loaded yet
907 bool LyXTextClass::load(string const & path) const
912 // Read style-file, provided path is searched before the system ones
913 FileName layout_file;
915 layout_file = FileName(addName(path, name_ + ".layout"));
916 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
917 layout_file = libFileSearch("layouts", name_, "layout");
918 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
921 lyxerr << "Error reading `"
922 << to_utf8(makeDisplayPath(layout_file.absFilename()))
923 << "'\n(Check `" << name_
924 << "')\nCheck your installation and "
925 "try Options/Reconfigure..." << endl;
932 FloatList & LyXTextClass::floats()
934 return *floatlist_.get();
938 FloatList const & LyXTextClass::floats() const
940 return *floatlist_.get();
944 Counters & LyXTextClass::counters() const
950 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
952 CharStyles::iterator cs = charstyles().begin();
953 CharStyles::iterator csend = charstyles().end();
954 for (; cs != csend; ++cs) {
962 string const & LyXTextClass::defaultLayoutName() const
964 // This really should come from the actual layout... (Lgb)
965 return defaultlayout_;
969 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
971 return operator[](defaultLayoutName());
975 string const & LyXTextClass::name() const
981 string const & LyXTextClass::latexname() const
983 const_cast<LyXTextClass*>(this)->load();
988 string const & LyXTextClass::description() const
994 string const & LyXTextClass::opt_fontsize() const
996 return opt_fontsize_;
1000 string const & LyXTextClass::opt_pagestyle() const
1002 return opt_pagestyle_;
1006 string const & LyXTextClass::options() const
1012 string const & LyXTextClass::class_header() const
1014 return class_header_;
1018 string const & LyXTextClass::pagestyle() const
1024 docstring const & LyXTextClass::preamble() const
1030 LyXTextClass::PageSides LyXTextClass::sides() const
1036 int LyXTextClass::secnumdepth() const
1038 return secnumdepth_;
1042 int LyXTextClass::tocdepth() const
1048 OutputType LyXTextClass::outputType() const
1054 bool LyXTextClass::provides(string const & p) const
1056 return provides_.find(p) != provides_.end();
1060 unsigned int LyXTextClass::columns() const
1066 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1072 string const & LyXTextClass::titlename() const
1078 int LyXTextClass::size() const
1080 return layoutlist_.size();
1084 int LyXTextClass::min_toclevel() const
1086 return min_toclevel_;
1090 int LyXTextClass::max_toclevel() const
1092 return max_toclevel_;
1096 bool LyXTextClass::hasTocLevels() const
1098 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1102 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1105 case LyXTextClass::OneSide:
1108 case LyXTextClass::TwoSides: