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"
24 #include "FloatList.h"
26 #include "frontends/alert.h"
28 #include "support/lstrings.h"
29 #include "support/lyxlib.h"
30 #include "support/filetools.h"
31 #include "support/os.h"
33 #include <boost/filesystem/operations.hpp>
37 namespace fs = boost::filesystem;
41 using support::FileName;
42 using support::libFileSearch;
43 using support::makeDisplayPath;
44 using support::quoteName;
47 using support::addName;
58 class LayoutNamesEqual : public std::unary_function<LayoutPtr, bool> {
60 LayoutNamesEqual(docstring const & name)
63 bool operator()(LayoutPtr const & c) const
65 return c->name() == name_;
75 bool layout2layout(FileName const & filename, FileName const & tempfile)
77 FileName const script = libFileSearch("scripts", "layout2layout.py");
79 lyxerr << "Could not find layout conversion "
80 "script layout2layout.py." << endl;
84 std::ostringstream command;
85 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
86 << ' ' << quoteName(filename.toFilesystemEncoding())
87 << ' ' << quoteName(tempfile.toFilesystemEncoding());
88 string const command_str = command.str();
90 LYXERR(Debug::TCLASS) << "Running `" << command_str << '\'' << endl;
92 support::cmd_ret const ret =
93 support::runCommand(command_str);
95 lyxerr << "Could not run layout conversion "
96 "script layout2layout.py." << endl;
105 TextClass::TextClass(string const & fn, string const & cln,
106 string const & desc, bool texClassAvail )
107 : name_(fn), latexname_(cln), description_(desc),
108 floatlist_(new FloatList), counters_(new Counters),
109 texClassAvail_(texClassAvail)
117 pagestyle_ = "default";
118 defaultfont_ = Font(Font::ALL_SANE);
119 opt_fontsize_ = "10|11|12";
120 opt_pagestyle_ = "empty|plain|headings|fancy";
121 titletype_ = TITLE_COMMAND_AFTER;
122 titlename_ = "maketitle";
127 bool TextClass::isTeXClassAvailable() const
129 return texClassAvail_;
133 bool TextClass::do_readStyle(Lexer & lexrc, Layout & lay)
135 LYXERR(Debug::TCLASS) << "Reading style " << to_utf8(lay.name()) << endl;
136 if (!lay.read(lexrc, *this)) {
138 lay.resfont = lay.font;
139 lay.resfont.realize(defaultfont());
140 lay.reslabelfont = lay.labelfont;
141 lay.reslabelfont.realize(defaultfont());
142 return false; // no errors
144 lyxerr << "Error parsing style `" << to_utf8(lay.name()) << '\'' << endl;
177 // Reads a textclass structure from file.
178 bool TextClass::read(FileName const & filename, ReadType rt)
180 if (!support::isFileReadable(filename)) {
181 lyxerr << "Cannot read layout file `" << filename << "'."
186 keyword_item textClassTags[] = {
187 { "classoptions", TC_CLASSOPTIONS },
188 { "columns", TC_COLUMNS },
189 { "counter", TC_COUNTER },
190 { "defaultfont", TC_DEFAULTFONT },
191 { "defaultstyle", TC_DEFAULTSTYLE },
192 { "environment", TC_ENVIRONMENT },
193 { "float", TC_FLOAT },
194 { "format", TC_FORMAT },
195 { "input", TC_INPUT },
196 { "insetlayout", TC_INSETLAYOUT },
197 { "leftmargin", TC_LEFTMARGIN },
198 { "nofloat", TC_NOFLOAT },
199 { "nostyle", TC_NOSTYLE },
200 { "outputtype", TC_OUTPUTTYPE },
201 { "pagestyle", TC_PAGESTYLE },
202 { "preamble", TC_PREAMBLE },
203 { "provides", TC_PROVIDES },
204 { "rightmargin", TC_RIGHTMARGIN },
205 { "secnumdepth", TC_SECNUMDEPTH },
206 { "sides", TC_SIDES },
207 { "style", TC_STYLE },
208 { "titlelatexname", TC_TITLELATEXNAME },
209 { "titlelatextype", TC_TITLELATEXTYPE },
210 { "tocdepth", TC_TOCDEPTH }
215 LYXERR(Debug::TCLASS) << "Reading textclass ";
218 LYXERR(Debug::TCLASS) << "Reading input file ";
221 LYXERR(Debug::TCLASS) << "Reading module file ";
226 LYXERR(Debug::TCLASS) << to_utf8(makeDisplayPath(filename.absFilename()))
229 Lexer lexrc(textClassTags,
230 sizeof(textClassTags) / sizeof(textClassTags[0]));
232 lexrc.setFile(filename);
233 bool error = !lexrc.isOK();
235 // Format of files before the 'Format' tag was introduced
239 while (lexrc.isOK() && !error) {
240 int le = lexrc.lex();
243 case Lexer::LEX_FEOF:
246 case Lexer::LEX_UNDEF:
247 lexrc.printError("Unknown TextClass tag `$$Token'");
255 switch (static_cast<TextClassTags>(le)) {
259 format = lexrc.getInteger();
262 case TC_OUTPUTTYPE: // output type definition
263 readOutputType(lexrc);
266 case TC_INPUT: // Include file
268 string const inc = lexrc.getString();
269 FileName tmp = libFileSearch("layouts", inc,
273 lexrc.printError("Could not find input"
276 } else if (read(tmp, MERGE)) {
277 lexrc.printError("Error reading input"
278 "file: " + tmp.absFilename());
284 case TC_DEFAULTSTYLE:
286 docstring const name = from_utf8(subst(lexrc.getString(),
288 defaultlayout_ = name;
295 docstring const name = from_utf8(subst(lexrc.getString(),
298 string s = "Could not read name for style: `$$Token' "
299 + lexrc.getString() + " is probably not valid UTF-8!";
300 lexrc.printError(s.c_str());
302 error = do_readStyle(lexrc, lay);
303 } else if (hasLayout(name)) {
304 Layout * lay = operator[](name).get();
305 error = do_readStyle(lexrc, *lay);
309 if (le == TC_ENVIRONMENT)
310 lay.is_environment = true;
311 error = do_readStyle(lexrc, lay);
313 layoutlist_.push_back(
314 boost::shared_ptr<Layout>(new Layout(lay))
317 if (defaultlayout_.empty()) {
318 // We do not have a default
319 // layout yet, so we choose
320 // the first layout we
322 defaultlayout_ = name;
327 lexrc.printError("No name given for style: `$$Token'.");
334 docstring const style = from_utf8(subst(lexrc.getString(),
336 if (!delete_layout(style))
337 lyxerr << "Cannot delete style `"
338 << to_utf8(style) << '\'' << endl;
339 // lexrc.printError("Cannot delete style"
346 columns_ = lexrc.getInteger();
351 switch (lexrc.getInteger()) {
352 case 1: sides_ = OneSide; break;
353 case 2: sides_ = TwoSides; break;
355 lyxerr << "Impossible number of page"
356 " sides, setting to one."
366 pagestyle_ = rtrim(lexrc.getString());
370 defaultfont_.lyxRead(lexrc);
371 if (!defaultfont_.resolved()) {
372 lexrc.printError("Warning: defaultfont should "
373 "be fully instantiated!");
374 defaultfont_.realize(Font(Font::ALL_SANE));
380 secnumdepth_ = lexrc.getInteger();
385 tocdepth_ = lexrc.getInteger();
388 // First step to support options
389 case TC_CLASSOPTIONS:
390 readClassOptions(lexrc);
394 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
399 string const feature = lexrc.getString();
401 if (lexrc.getInteger())
402 provides_.insert(feature);
404 provides_.erase(feature);
408 case TC_LEFTMARGIN: // left margin type
410 leftmargin_ = lexrc.getDocString();
413 case TC_RIGHTMARGIN: // right margin type
415 rightmargin_ = lexrc.getDocString();
419 docstring const name = subst(lexrc.getDocString(), '_', ' ');
420 readInsetLayout(lexrc, name);
429 case TC_TITLELATEXTYPE:
430 readTitleType(lexrc);
432 case TC_TITLELATEXNAME:
434 titlename_ = lexrc.getString();
438 string const nofloat = lexrc.getString();
439 floatlist_->erase(nofloat);
443 if (format != FORMAT)
447 if (format != FORMAT) {
448 LYXERR(Debug::TCLASS) << "Converting layout file from format "
449 << format << " to " << FORMAT << endl;
450 FileName const tempfile(support::tempName());
451 error = !layout2layout(filename, tempfile);
453 error = read(tempfile, rt);
454 support::unlink(tempfile);
459 LYXERR(Debug::TCLASS) << "Finished reading module file "
460 << to_utf8(makeDisplayPath(filename.absFilename()))
462 else if (rt == MERGE)
463 LYXERR(Debug::TCLASS) << "Finished reading input file "
464 << to_utf8(makeDisplayPath(filename.absFilename()))
466 else { // we are at top level here.
467 LYXERR(Debug::TCLASS) << "Finished reading textclass "
468 << to_utf8(makeDisplayPath(filename.absFilename()))
470 if (defaultlayout_.empty()) {
471 lyxerr << "Error: Textclass '" << name_
472 << "' is missing a defaultstyle." << endl;
476 min_toclevel_ = Layout::NOT_IN_TOC;
477 max_toclevel_ = Layout::NOT_IN_TOC;
478 const_iterator cit = begin();
479 const_iterator the_end = end();
480 for ( ; cit != the_end ; ++cit) {
481 int const toclevel = (*cit)->toclevel;
482 if (toclevel != Layout::NOT_IN_TOC) {
483 if (min_toclevel_ == Layout::NOT_IN_TOC)
484 min_toclevel_ = toclevel;
486 min_toclevel_ = std::min(min_toclevel_,
488 max_toclevel_ = std::max(max_toclevel_,
492 LYXERR(Debug::TCLASS)
493 << "Minimum TocLevel is " << min_toclevel_
494 << ", maximum is " << max_toclevel_ <<endl;
502 void TextClass::readTitleType(Lexer & lexrc)
504 keyword_item titleTypeTags[] = {
505 { "commandafter", TITLE_COMMAND_AFTER },
506 { "environment", TITLE_ENVIRONMENT }
509 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
511 int le = lexrc.lex();
513 case Lexer::LEX_UNDEF:
514 lexrc.printError("Unknown output type `$$Token'");
516 case TITLE_COMMAND_AFTER:
517 case TITLE_ENVIRONMENT:
518 titletype_ = static_cast<TitleLatexType>(le);
521 lyxerr << "Unhandled value " << le
522 << " in TextClass::readTitleType." << endl;
529 void TextClass::readOutputType(Lexer & lexrc)
531 keyword_item outputTypeTags[] = {
532 { "docbook", DOCBOOK },
534 { "literate", LITERATE }
537 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
539 int le = lexrc.lex();
541 case Lexer::LEX_UNDEF:
542 lexrc.printError("Unknown output type `$$Token'");
547 outputType_ = static_cast<OutputType>(le);
550 lyxerr << "Unhandled value " << le
551 << " in TextClass::readOutputType." << endl;
558 enum ClassOptionsTags {
567 void TextClass::readClassOptions(Lexer & lexrc)
569 keyword_item classOptionsTags[] = {
571 {"fontsize", CO_FONTSIZE },
572 {"header", CO_HEADER },
573 {"other", CO_OTHER },
574 {"pagestyle", CO_PAGESTYLE }
577 lexrc.pushTable(classOptionsTags, CO_END);
579 while (!getout && lexrc.isOK()) {
580 int le = lexrc.lex();
582 case Lexer::LEX_UNDEF:
583 lexrc.printError("Unknown ClassOption tag `$$Token'");
587 switch (static_cast<ClassOptionsTags>(le)) {
590 opt_fontsize_ = rtrim(lexrc.getString());
594 opt_pagestyle_ = rtrim(lexrc.getString());
598 options_ = lexrc.getString();
602 class_header_ = subst(lexrc.getString(), """, "\"");
613 enum InsetLayoutTags {
628 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
630 keyword_item elementTags[] = {
631 { "bgcolor", IL_BGCOLOR },
632 { "decoration", IL_DECORATION },
635 { "labelfont", IL_LABELFONT },
636 { "labelstring", IL_LABELSTRING },
637 { "latexname", IL_LATEXNAME },
638 { "latexparam", IL_LATEXPARAM },
639 { "latextype", IL_LATEXTYPE },
640 { "lyxtype", IL_LYXTYPE },
641 { "preamble", IL_PREAMBLE }
644 lexrc.pushTable(elementTags, IL_END);
647 docstring labelstring;
652 Font font(Font::ALL_INHERIT);
653 Font labelfont(Font::ALL_INHERIT);
654 Color::color bgcolor(Color::background);
658 while (!getout && lexrc.isOK()) {
659 int le = lexrc.lex();
661 case Lexer::LEX_UNDEF:
662 lexrc.printError("Unknown ClassOption tag `$$Token'");
666 switch (static_cast<InsetLayoutTags>(le)) {
669 lyxtype = lexrc.getString();
673 latextype = lexrc.getString();
677 labelstring = lexrc.getDocString();
681 decoration = lexrc.getString();
685 latexname = lexrc.getString();
689 latexparam = subst(lexrc.getString(), """, "\"");
692 labelfont.lyxRead(lexrc);
693 labelfont.realize(defaultfont());
697 font.realize(defaultfont());
698 // So: define font before labelfont
703 string const token = lexrc.getString();
704 bgcolor = lcolor.getFromLyXName(token);
708 preamble = lexrc.getLongString("EndPreamble");
717 // Here add element to list if getout == true
720 il.name = to_ascii(name);
721 il.lyxtype = lyxtype;
722 il.labelstring = labelstring;
723 il.decoration = decoration;
724 il.latextype = latextype;
725 il.latexname = latexname;
726 il.latexparam = latexparam;
728 il.labelfont = labelfont;
729 il.bgcolor = bgcolor;
730 il.preamble = preamble;
731 insetlayoutlist_[name] = il;
752 void TextClass::readFloat(Lexer & lexrc)
754 keyword_item floatTags[] = {
756 { "extension", FT_EXT },
757 { "guiname", FT_NAME },
758 { "latexbuiltin", FT_BUILTIN },
759 { "listname", FT_LISTNAME },
760 { "numberwithin", FT_WITHIN },
761 { "placement", FT_PLACEMENT },
762 { "style", FT_STYLE },
766 lexrc.pushTable(floatTags, FT_END);
775 bool builtin = false;
778 while (!getout && lexrc.isOK()) {
779 int le = lexrc.lex();
781 case Lexer::LEX_UNDEF:
782 lexrc.printError("Unknown ClassOption tag `$$Token'");
786 switch (static_cast<FloatTags>(le)) {
789 type = lexrc.getString();
790 if (floatlist_->typeExist(type)) {
791 Floating const & fl = floatlist_->getType(type);
792 placement = fl.placement();
794 within = fl.within();
797 listName = fl.listName();
798 builtin = fl.builtin();
803 name = lexrc.getString();
807 placement = lexrc.getString();
811 ext = lexrc.getString();
815 within = lexrc.getString();
816 if (within == "none")
821 style = lexrc.getString();
825 listName = lexrc.getString();
829 builtin = lexrc.getBool();
837 // Here if have a full float if getout == true
839 Floating fl(type, placement, ext, within,
840 style, name, listName, builtin);
841 floatlist_->newFloat(fl);
842 // each float has its own counter
843 counters_->newCounter(from_ascii(type), from_ascii(within),
844 docstring(), docstring());
855 CT_LABELSTRING_APPENDIX,
859 void TextClass::readCounter(Lexer & lexrc)
861 keyword_item counterTags[] = {
863 { "labelstring", CT_LABELSTRING },
864 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
866 { "within", CT_WITHIN }
869 lexrc.pushTable(counterTags, CT_END);
873 docstring labelstring;
874 docstring labelstring_appendix;
877 while (!getout && lexrc.isOK()) {
878 int le = lexrc.lex();
880 case Lexer::LEX_UNDEF:
881 lexrc.printError("Unknown ClassOption tag `$$Token'");
885 switch (static_cast<CounterTags>(le)) {
888 name = lexrc.getDocString();
889 if (counters_->hasCounter(name))
890 LYXERR(Debug::TCLASS)
891 << "Reading existing counter "
892 << to_utf8(name) << endl;
894 LYXERR(Debug::TCLASS)
895 << "Reading new counter "
896 << to_utf8(name) << endl;
900 within = lexrc.getDocString();
901 if (within == "none")
906 labelstring = lexrc.getDocString();
907 labelstring_appendix = labelstring;
909 case CT_LABELSTRING_APPENDIX:
911 labelstring_appendix = lexrc.getDocString();
919 // Here if have a full counter if getout == true
921 counters_->newCounter(name, within,
922 labelstring, labelstring_appendix);
928 Font const & TextClass::defaultfont() const
934 docstring const & TextClass::leftmargin() const
940 docstring const & TextClass::rightmargin() const
946 bool TextClass::hasLayout(docstring const & n) const
948 docstring const name = n.empty() ? defaultLayoutName() : n;
950 return find_if(layoutlist_.begin(), layoutlist_.end(),
951 LayoutNamesEqual(name))
952 != layoutlist_.end();
957 LayoutPtr const & TextClass::operator[](docstring const & name) const
959 BOOST_ASSERT(!name.empty());
961 LayoutList::const_iterator cit =
962 find_if(layoutlist_.begin(),
964 LayoutNamesEqual(name));
966 if (cit == layoutlist_.end()) {
967 lyxerr << "We failed to find the layout '" << to_utf8(name)
968 << "' in the layout list. You MUST investigate!"
970 for (LayoutList::const_iterator it = layoutlist_.begin();
971 it != layoutlist_.end(); ++it)
972 lyxerr << " " << to_utf8(it->get()->name()) << endl;
974 // we require the name to exist
983 bool TextClass::delete_layout(docstring const & name)
985 if (name == defaultLayoutName())
988 LayoutList::iterator it =
989 remove_if(layoutlist_.begin(), layoutlist_.end(),
990 LayoutNamesEqual(name));
992 LayoutList::iterator end = layoutlist_.end();
993 bool const ret = (it != end);
994 layoutlist_.erase(it, end);
999 // Load textclass info if not loaded yet
1000 bool TextClass::load(string const & path) const
1005 // Read style-file, provided path is searched before the system ones
1006 FileName layout_file;
1008 layout_file = FileName(addName(path, name_ + ".layout"));
1009 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
1010 layout_file = libFileSearch("layouts", name_, "layout");
1011 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1014 lyxerr << "Error reading `"
1015 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1016 << "'\n(Check `" << name_
1017 << "')\nCheck your installation and "
1018 "try Options/Reconfigure..." << endl;
1025 FloatList & TextClass::floats()
1027 return *floatlist_.get();
1031 FloatList const & TextClass::floats() const
1033 return *floatlist_.get();
1037 Counters & TextClass::counters() const
1039 return *counters_.get();
1043 // Return the layout object of an inset given by name. If the name
1044 // is not found as such, the part after the ':' is stripped off, and
1045 // searched again. In this way, an error fallback can be provided:
1046 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1047 // will invoke the layout object defined by name = 'CharStyle'.
1048 // If that doesn't work either, an empty object returns (shouldn't
1049 // happen). -- Idea JMarc, comment MV
1050 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1053 while (!n.empty()) {
1054 if (insetlayoutlist_.count(n) > 0)
1055 return insetlayoutlist_[n];
1056 docstring::size_type i = n.find(':');
1057 if (i == string::npos)
1061 static InsetLayout empty;
1062 empty.labelstring = from_utf8("UNDEFINED");
1063 empty.bgcolor = Color::error;
1068 docstring const & TextClass::defaultLayoutName() const
1070 // This really should come from the actual layout... (Lgb)
1071 return defaultlayout_;
1075 LayoutPtr const & TextClass::defaultLayout() const
1077 return operator[](defaultLayoutName());
1081 string const & TextClass::name() const
1087 string const & TextClass::latexname() const
1089 const_cast<TextClass*>(this)->load();
1094 string const & TextClass::description() const
1096 return description_;
1100 string const & TextClass::opt_fontsize() const
1102 return opt_fontsize_;
1106 string const & TextClass::opt_pagestyle() const
1108 return opt_pagestyle_;
1112 string const & TextClass::options() const
1118 string const & TextClass::class_header() const
1120 return class_header_;
1124 string const & TextClass::pagestyle() const
1130 docstring const & TextClass::preamble() const
1136 TextClass::PageSides TextClass::sides() const
1142 int TextClass::secnumdepth() const
1144 return secnumdepth_;
1148 int TextClass::tocdepth() const
1154 OutputType TextClass::outputType() const
1160 bool TextClass::provides(string const & p) const
1162 return provides_.find(p) != provides_.end();
1166 unsigned int TextClass::columns() const
1172 TitleLatexType TextClass::titletype() const
1178 string const & TextClass::titlename() const
1184 int TextClass::size() const
1186 return layoutlist_.size();
1190 int TextClass::min_toclevel() const
1192 return min_toclevel_;
1196 int TextClass::max_toclevel() const
1198 return max_toclevel_;
1202 bool TextClass::hasTocLevels() const
1204 return min_toclevel_ != Layout::NOT_IN_TOC;
1208 ostream & operator<<(ostream & os, TextClass::PageSides p)
1211 case TextClass::OneSide:
1214 case TextClass::TwoSides: