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 "TextClass.h"
23 #include "FloatList.h"
25 #include "frontends/alert.h"
27 #include "support/lstrings.h"
28 #include "support/lyxlib.h"
29 #include "support/filetools.h"
30 #include "support/os.h"
32 #include <boost/filesystem/operations.hpp>
33 namespace fs = boost::filesystem;
40 using support::FileName;
41 using support::libFileSearch;
42 using support::makeDisplayPath;
43 using support::quoteName;
46 using support::addName;
57 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
59 LayoutNamesEqual(docstring const & name)
62 bool operator()(LayoutPtr const & c) const
64 return c->name() == name_;
74 bool layout2layout(FileName const & filename, FileName const & tempfile)
76 FileName const script = libFileSearch("scripts", "layout2layout.py");
78 lyxerr << "Could not find layout conversion "
79 "script layout2layout.py." << endl;
83 std::ostringstream command;
84 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
85 << ' ' << quoteName(filename.toFilesystemEncoding())
86 << ' ' << quoteName(tempfile.toFilesystemEncoding());
87 string const command_str = command.str();
89 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
91 support::cmd_ret const ret =
92 support::runCommand(command_str);
94 lyxerr << "Could not run layout conversion "
95 "script layout2layout.py." << endl;
104 TextClass::TextClass(string const & fn, string const & cln,
105 string const & desc, bool texClassAvail )
106 : name_(fn), latexname_(cln), description_(desc),
107 floatlist_(new FloatList), counters_(new Counters),
108 texClassAvail_(texClassAvail)
115 pagestyle_ = "default";
116 defaultfont_ = Font(Font::ALL_SANE);
117 opt_fontsize_ = "10|11|12";
118 opt_pagestyle_ = "empty|plain|headings|fancy";
119 titletype_ = TITLE_COMMAND_AFTER;
120 titlename_ = "maketitle";
125 bool TextClass::isTeXClassAvailable() const
127 return texClassAvail_;
131 bool TextClass::do_readStyle(Lexer & lexrc, Layout & lay)
133 LYXERR(Debug::TCLASS) << "Reading style " << to_utf8(lay.name()) << endl;
134 if (!lay.read(lexrc, *this)) {
136 lay.resfont = lay.font;
137 lay.resfont.realize(defaultfont());
138 lay.reslabelfont = lay.labelfont;
139 lay.reslabelfont.realize(defaultfont());
140 return false; // no errors
142 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
175 // Reads a textclass structure from file.
176 bool TextClass::read(FileName const & filename, bool merge)
178 if (!support::isFileReadable(filename)) {
179 lyxerr << "Cannot read layout file `" << filename << "'."
184 keyword_item textClassTags[] = {
185 { "classoptions", TC_CLASSOPTIONS },
186 { "columns", TC_COLUMNS },
187 { "counter", TC_COUNTER },
188 { "defaultfont", TC_DEFAULTFONT },
189 { "defaultstyle", TC_DEFAULTSTYLE },
190 { "environment", TC_ENVIRONMENT },
191 { "float", TC_FLOAT },
192 { "format", TC_FORMAT },
193 { "input", TC_INPUT },
194 { "insetlayout", TC_INSETLAYOUT },
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 { "provides", TC_PROVIDES },
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 << to_utf8(makeDisplayPath(filename.absFilename()))
216 LYXERR(Debug::TCLASS) << "Reading input file "
217 << to_utf8(makeDisplayPath(filename.absFilename()))
220 Lexer 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 Lexer::LEX_FEOF:
237 case Lexer::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 const inc = lexrc.getString();
260 FileName tmp = libFileSearch("layouts", inc,
264 lexrc.printError("Could not find input"
267 } else if (read(tmp, true)) {
268 lexrc.printError("Error reading input"
269 "file: " + tmp.absFilename());
275 case TC_DEFAULTSTYLE:
277 docstring const name = from_utf8(subst(lexrc.getString(),
279 defaultlayout_ = name;
286 docstring const name = from_utf8(subst(lexrc.getString(),
289 string s = "Could not read name for style: `$$Token' "
290 + lexrc.getString() + " is probably not valid UTF-8!";
291 lexrc.printError(s.c_str());
293 error = do_readStyle(lexrc, lay);
294 } else if (hasLayout(name)) {
295 Layout * lay = operator[](name).get();
296 error = do_readStyle(lexrc, *lay);
300 if (le == TC_ENVIRONMENT)
301 lay.is_environment = true;
302 error = do_readStyle(lexrc, lay);
304 layoutlist_.push_back(
305 boost::shared_ptr<Layout>(new Layout(lay))
308 if (defaultlayout_.empty()) {
309 // We do not have a default
310 // layout yet, so we choose
311 // the first layout we
313 defaultlayout_ = name;
318 lexrc.printError("No name given for style: `$$Token'.");
325 docstring const style = from_utf8(subst(lexrc.getString(),
327 if (!delete_layout(style))
328 lyxerr << "Cannot delete style `"
329 << to_utf8(style) << '\'' << endl;
330 // lexrc.printError("Cannot delete style"
337 columns_ = lexrc.getInteger();
342 switch (lexrc.getInteger()) {
343 case 1: sides_ = OneSide; break;
344 case 2: sides_ = TwoSides; break;
346 lyxerr << "Impossible number of page"
347 " sides, setting to one."
357 pagestyle_ = rtrim(lexrc.getString());
361 defaultfont_.lyxRead(lexrc);
362 if (!defaultfont_.resolved()) {
363 lexrc.printError("Warning: defaultfont should "
364 "be fully instantiated!");
365 defaultfont_.realize(Font(Font::ALL_SANE));
371 secnumdepth_ = lexrc.getInteger();
376 tocdepth_ = lexrc.getInteger();
379 // First step to support options
380 case TC_CLASSOPTIONS:
381 readClassOptions(lexrc);
385 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
390 string const feature = lexrc.getString();
392 if (lexrc.getInteger())
393 provides_.insert(feature);
395 provides_.erase(feature);
399 case TC_LEFTMARGIN: // left margin type
401 leftmargin_ = lexrc.getDocString();
404 case TC_RIGHTMARGIN: // right margin type
406 rightmargin_ = lexrc.getDocString();
410 docstring const name = subst(lexrc.getDocString(), '_', ' ');
411 readInsetLayout(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 FileName const tempfile(support::tempName());
442 error = !layout2layout(filename, tempfile);
444 error = read(tempfile, merge);
445 support::unlink(tempfile);
449 if (!merge) { // we are at top level here.
450 LYXERR(Debug::TCLASS) << "Finished reading textclass "
451 << to_utf8(makeDisplayPath(filename.absFilename()))
453 if (defaultlayout_.empty()) {
454 lyxerr << "Error: Textclass '" << name_
455 << "' is missing a defaultstyle." << endl;
459 min_toclevel_ = Layout::NOT_IN_TOC;
460 max_toclevel_ = Layout::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 != Layout::NOT_IN_TOC) {
466 if (min_toclevel_ == Layout::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 << to_utf8(makeDisplayPath(filename.absFilename()))
488 void TextClass::readTitleType(Lexer & 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 Lexer::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 TextClass::readTitleType." << endl;
515 void TextClass::readOutputType(Lexer & lexrc)
517 keyword_item outputTypeTags[] = {
518 { "docbook", DOCBOOK },
520 { "literate", LITERATE }
523 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
525 int le = lexrc.lex();
527 case Lexer::LEX_UNDEF:
528 lexrc.printError("Unknown output type `$$Token'");
533 outputType_ = static_cast<OutputType>(le);
536 lyxerr << "Unhandled value " << le
537 << " in TextClass::readOutputType." << endl;
544 enum ClassOptionsTags {
553 void TextClass::readClassOptions(Lexer & 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 Lexer::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(), """, "\"");
599 enum InsetLayoutTags {
614 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
616 keyword_item elementTags[] = {
617 { "bgcolor", IL_BGCOLOR },
618 { "decoration", IL_DECORATION },
621 { "labelfont", IL_LABELFONT },
622 { "labelstring", IL_LABELSTRING },
623 { "latexname", IL_LATEXNAME },
624 { "latexparam", IL_LATEXPARAM },
625 { "latextype", IL_LATEXTYPE },
626 { "lyxtype", IL_LYXTYPE },
627 { "preamble", IL_PREAMBLE }
630 lexrc.pushTable(elementTags, IL_END);
633 docstring labelstring;
638 Font font(Font::ALL_INHERIT);
639 Font labelfont(Font::ALL_INHERIT);
640 Color::color bgcolor(Color::background);
644 while (!getout && lexrc.isOK()) {
645 int le = lexrc.lex();
647 case Lexer::LEX_UNDEF:
648 lexrc.printError("Unknown ClassOption tag `$$Token'");
652 switch (static_cast<InsetLayoutTags>(le)) {
655 lyxtype = lexrc.getString();
659 latextype = lexrc.getString();
663 labelstring = lexrc.getDocString();
667 decoration = lexrc.getString();
671 latexname = lexrc.getString();
675 latexparam = subst(lexrc.getString(), """, "\"");
678 labelfont.lyxRead(lexrc);
679 labelfont.realize(defaultfont());
683 font.realize(defaultfont());
688 string const token = lexrc.getString();
689 bgcolor = lcolor.getFromLyXName(token);
693 preamble = lexrc.getLongString("EndPreamble");
702 // Here add element to list if getout == true
705 il.name = to_ascii(name);
706 il.lyxtype = lyxtype;
707 il.labelstring = labelstring;
708 il.decoration = decoration;
709 il.latextype = latextype;
710 il.latexname = latexname;
711 il.latexparam = latexparam;
713 il.labelfont = labelfont;
714 il.bgcolor = bgcolor;
715 il.preamble = preamble;
716 insetlayoutlist_[name] = il;
737 void TextClass::readFloat(Lexer & lexrc)
739 keyword_item floatTags[] = {
741 { "extension", FT_EXT },
742 { "guiname", FT_NAME },
743 { "latexbuiltin", FT_BUILTIN },
744 { "listname", FT_LISTNAME },
745 { "numberwithin", FT_WITHIN },
746 { "placement", FT_PLACEMENT },
747 { "style", FT_STYLE },
751 lexrc.pushTable(floatTags, FT_END);
760 bool builtin = false;
763 while (!getout && lexrc.isOK()) {
764 int le = lexrc.lex();
766 case Lexer::LEX_UNDEF:
767 lexrc.printError("Unknown ClassOption tag `$$Token'");
771 switch (static_cast<FloatTags>(le)) {
774 type = lexrc.getString();
775 if (floatlist_->typeExist(type)) {
776 Floating const & fl = floatlist_->getType(type);
777 placement = fl.placement();
779 within = fl.within();
782 listName = fl.listName();
783 builtin = fl.builtin();
788 name = lexrc.getString();
792 placement = lexrc.getString();
796 ext = lexrc.getString();
800 within = lexrc.getString();
801 if (within == "none")
806 style = lexrc.getString();
810 listName = lexrc.getString();
814 builtin = lexrc.getBool();
822 // Here if have a full float if getout == true
824 Floating fl(type, placement, ext, within,
825 style, name, listName, builtin);
826 floatlist_->newFloat(fl);
827 // each float has its own counter
828 counters_->newCounter(from_ascii(type), from_ascii(within),
829 docstring(), docstring());
840 CT_LABELSTRING_APPENDIX,
844 void TextClass::readCounter(Lexer & lexrc)
846 keyword_item counterTags[] = {
848 { "labelstring", CT_LABELSTRING },
849 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
851 { "within", CT_WITHIN }
854 lexrc.pushTable(counterTags, CT_END);
858 docstring labelstring;
859 docstring labelstring_appendix;
862 while (!getout && lexrc.isOK()) {
863 int le = lexrc.lex();
865 case Lexer::LEX_UNDEF:
866 lexrc.printError("Unknown ClassOption tag `$$Token'");
870 switch (static_cast<CounterTags>(le)) {
873 name = lexrc.getDocString();
874 if (counters_->hasCounter(name))
875 LYXERR(Debug::TCLASS)
876 << "Reading existing counter "
877 << to_utf8(name) << endl;
879 LYXERR(Debug::TCLASS)
880 << "Reading new counter "
881 << to_utf8(name) << endl;
885 within = lexrc.getDocString();
886 if (within == "none")
891 labelstring = lexrc.getDocString();
892 labelstring_appendix = labelstring;
894 case CT_LABELSTRING_APPENDIX:
896 labelstring_appendix = lexrc.getDocString();
904 // Here if have a full counter if getout == true
906 counters_->newCounter(name, within,
907 labelstring, labelstring_appendix);
913 Font const & TextClass::defaultfont() const
919 docstring const & TextClass::leftmargin() const
925 docstring const & TextClass::rightmargin() const
931 bool TextClass::hasLayout(docstring const & n) const
933 docstring const name = n.empty() ? defaultLayoutName() : n;
935 return find_if(layoutlist_.begin(), layoutlist_.end(),
936 LayoutNamesEqual(name))
937 != layoutlist_.end();
942 LayoutPtr const & TextClass::operator[](docstring const & name) const
944 BOOST_ASSERT(!name.empty());
946 LayoutList::const_iterator cit =
947 find_if(layoutlist_.begin(),
949 LayoutNamesEqual(name));
951 if (cit == layoutlist_.end()) {
952 lyxerr << "We failed to find the layout '" << to_utf8(name)
953 << "' in the layout list. You MUST investigate!"
955 for (LayoutList::const_iterator it = layoutlist_.begin();
956 it != layoutlist_.end(); ++it)
957 lyxerr << " " << to_utf8(it->get()->name()) << endl;
959 // we require the name to exist
968 bool TextClass::delete_layout(docstring const & name)
970 if (name == defaultLayoutName())
973 LayoutList::iterator it =
974 remove_if(layoutlist_.begin(), layoutlist_.end(),
975 LayoutNamesEqual(name));
977 LayoutList::iterator end = layoutlist_.end();
978 bool const ret = (it != end);
979 layoutlist_.erase(it, end);
984 // Load textclass info if not loaded yet
985 bool TextClass::load(string const & path) const
990 // Read style-file, provided path is searched before the system ones
991 FileName layout_file;
993 layout_file = FileName(addName(path, name_ + ".layout"));
994 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
995 layout_file = libFileSearch("layouts", name_, "layout");
996 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
999 lyxerr << "Error reading `"
1000 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1001 << "'\n(Check `" << name_
1002 << "')\nCheck your installation and "
1003 "try Options/Reconfigure..." << endl;
1010 FloatList & TextClass::floats()
1012 return *floatlist_.get();
1016 FloatList const & TextClass::floats() const
1018 return *floatlist_.get();
1022 Counters & TextClass::counters() const
1024 return *counters_.get();
1028 // Return the layout object of an inset given by name. If the name
1029 // is not found as such, the part after the ':' is stripped off, and
1030 // searched again. In this way, an error fallback can be provided:
1031 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1032 // will invoke the layout object defined by name = 'CharStyle'.
1033 // If that doesn't work either, an empty object returns (shouldn't
1034 // happen). -- Idea JMarc, comment MV
1035 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1038 while (!n.empty()) {
1039 if (insetlayoutlist_.count(n) > 0)
1040 return insetlayoutlist_[n];
1041 docstring::size_type i = n.find(':');
1042 if (i == string::npos)
1046 static InsetLayout empty;
1047 empty.labelstring = from_utf8("UNDEFINED");
1052 docstring const & TextClass::defaultLayoutName() const
1054 // This really should come from the actual layout... (Lgb)
1055 return defaultlayout_;
1059 LayoutPtr const & TextClass::defaultLayout() const
1061 return operator[](defaultLayoutName());
1065 string const & TextClass::name() const
1071 string const & TextClass::latexname() const
1073 const_cast<TextClass*>(this)->load();
1078 string const & TextClass::description() const
1080 return description_;
1084 string const & TextClass::opt_fontsize() const
1086 return opt_fontsize_;
1090 string const & TextClass::opt_pagestyle() const
1092 return opt_pagestyle_;
1096 string const & TextClass::options() const
1102 string const & TextClass::class_header() const
1104 return class_header_;
1108 string const & TextClass::pagestyle() const
1114 docstring const & TextClass::preamble() const
1120 TextClass::PageSides TextClass::sides() const
1126 int TextClass::secnumdepth() const
1128 return secnumdepth_;
1132 int TextClass::tocdepth() const
1138 OutputType TextClass::outputType() const
1144 bool TextClass::provides(string const & p) const
1146 return provides_.find(p) != provides_.end();
1150 unsigned int TextClass::columns() const
1156 LYX_TITLE_LATEX_TYPES TextClass::titletype() const
1162 string const & TextClass::titlename() const
1168 int TextClass::size() const
1170 return layoutlist_.size();
1174 int TextClass::min_toclevel() const
1176 return min_toclevel_;
1180 int TextClass::max_toclevel() const
1182 return max_toclevel_;
1186 bool TextClass::hasTocLevels() const
1188 return min_toclevel_ != Layout::NOT_IN_TOC;
1192 ostream & operator<<(ostream & os, TextClass::PageSides p)
1195 case TextClass::OneSide:
1198 case TextClass::TwoSides: