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 {
629 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
631 keyword_item elementTags[] = {
632 { "bgcolor", IL_BGCOLOR },
633 { "decoration", IL_DECORATION },
636 { "labelfont", IL_LABELFONT },
637 { "labelstring", IL_LABELSTRING },
638 { "latexname", IL_LATEXNAME },
639 { "latexparam", IL_LATEXPARAM },
640 { "latextype", IL_LATEXTYPE },
641 { "lyxtype", IL_LYXTYPE },
642 { "multipar", IL_MULTIPAR },
643 { "preamble", IL_PREAMBLE }
646 lexrc.pushTable(elementTags, IL_END);
649 docstring labelstring;
654 Font font(defaultfont());
655 Font labelfont(defaultfont());
656 Color::color bgcolor(Color::background);
658 bool multipar(false);
661 while (!getout && lexrc.isOK()) {
662 int le = lexrc.lex();
664 case Lexer::LEX_UNDEF:
665 lexrc.printError("Unknown ClassOption tag `$$Token'");
669 switch (static_cast<InsetLayoutTags>(le)) {
672 lyxtype = lexrc.getString();
676 latextype = lexrc.getString();
680 labelstring = lexrc.getDocString();
684 decoration = lexrc.getString();
688 latexname = lexrc.getString();
692 latexparam = subst(lexrc.getString(), """, "\"");
695 labelfont.lyxRead(lexrc);
696 labelfont.realize(defaultfont());
700 multipar = lexrc.getBool();
704 font.realize(defaultfont());
705 // So: define font before labelfont
710 string const token = lexrc.getString();
711 bgcolor = lcolor.getFromLyXName(token);
715 preamble = lexrc.getLongString("EndPreamble");
724 // Here add element to list if getout == true
727 il.name = to_ascii(name);
728 il.lyxtype = lyxtype;
729 il.labelstring = labelstring;
730 il.decoration = decoration;
731 il.latextype = latextype;
732 il.latexname = latexname;
733 il.latexparam = latexparam;
734 il.multipar = multipar;
736 il.labelfont = labelfont;
737 il.bgcolor = bgcolor;
738 il.preamble = preamble;
739 insetlayoutlist_[name] = il;
760 void TextClass::readFloat(Lexer & lexrc)
762 keyword_item floatTags[] = {
764 { "extension", FT_EXT },
765 { "guiname", FT_NAME },
766 { "latexbuiltin", FT_BUILTIN },
767 { "listname", FT_LISTNAME },
768 { "numberwithin", FT_WITHIN },
769 { "placement", FT_PLACEMENT },
770 { "style", FT_STYLE },
774 lexrc.pushTable(floatTags, FT_END);
783 bool builtin = false;
786 while (!getout && lexrc.isOK()) {
787 int le = lexrc.lex();
789 case Lexer::LEX_UNDEF:
790 lexrc.printError("Unknown ClassOption tag `$$Token'");
794 switch (static_cast<FloatTags>(le)) {
797 type = lexrc.getString();
798 if (floatlist_->typeExist(type)) {
799 Floating const & fl = floatlist_->getType(type);
800 placement = fl.placement();
802 within = fl.within();
805 listName = fl.listName();
806 builtin = fl.builtin();
811 name = lexrc.getString();
815 placement = lexrc.getString();
819 ext = lexrc.getString();
823 within = lexrc.getString();
824 if (within == "none")
829 style = lexrc.getString();
833 listName = lexrc.getString();
837 builtin = lexrc.getBool();
845 // Here if have a full float if getout == true
847 Floating fl(type, placement, ext, within,
848 style, name, listName, builtin);
849 floatlist_->newFloat(fl);
850 // each float has its own counter
851 counters_->newCounter(from_ascii(type), from_ascii(within),
852 docstring(), docstring());
863 CT_LABELSTRING_APPENDIX,
867 void TextClass::readCounter(Lexer & lexrc)
869 keyword_item counterTags[] = {
871 { "labelstring", CT_LABELSTRING },
872 { "labelstringappendix", CT_LABELSTRING_APPENDIX },
874 { "within", CT_WITHIN }
877 lexrc.pushTable(counterTags, CT_END);
881 docstring labelstring;
882 docstring labelstring_appendix;
885 while (!getout && lexrc.isOK()) {
886 int le = lexrc.lex();
888 case Lexer::LEX_UNDEF:
889 lexrc.printError("Unknown ClassOption tag `$$Token'");
893 switch (static_cast<CounterTags>(le)) {
896 name = lexrc.getDocString();
897 if (counters_->hasCounter(name))
898 LYXERR(Debug::TCLASS)
899 << "Reading existing counter "
900 << to_utf8(name) << endl;
902 LYXERR(Debug::TCLASS)
903 << "Reading new counter "
904 << to_utf8(name) << endl;
908 within = lexrc.getDocString();
909 if (within == "none")
914 labelstring = lexrc.getDocString();
915 labelstring_appendix = labelstring;
917 case CT_LABELSTRING_APPENDIX:
919 labelstring_appendix = lexrc.getDocString();
927 // Here if have a full counter if getout == true
929 counters_->newCounter(name, within,
930 labelstring, labelstring_appendix);
936 Font const & TextClass::defaultfont() const
942 docstring const & TextClass::leftmargin() const
948 docstring const & TextClass::rightmargin() const
954 bool TextClass::hasLayout(docstring const & n) const
956 docstring const name = n.empty() ? defaultLayoutName() : n;
958 return find_if(layoutlist_.begin(), layoutlist_.end(),
959 LayoutNamesEqual(name))
960 != layoutlist_.end();
965 LayoutPtr const & TextClass::operator[](docstring const & name) const
967 BOOST_ASSERT(!name.empty());
969 LayoutList::const_iterator cit =
970 find_if(layoutlist_.begin(),
972 LayoutNamesEqual(name));
974 if (cit == layoutlist_.end()) {
975 lyxerr << "We failed to find the layout '" << to_utf8(name)
976 << "' in the layout list. You MUST investigate!"
978 for (LayoutList::const_iterator it = layoutlist_.begin();
979 it != layoutlist_.end(); ++it)
980 lyxerr << " " << to_utf8(it->get()->name()) << endl;
982 // we require the name to exist
991 bool TextClass::delete_layout(docstring const & name)
993 if (name == defaultLayoutName())
996 LayoutList::iterator it =
997 remove_if(layoutlist_.begin(), layoutlist_.end(),
998 LayoutNamesEqual(name));
1000 LayoutList::iterator end = layoutlist_.end();
1001 bool const ret = (it != end);
1002 layoutlist_.erase(it, end);
1007 // Load textclass info if not loaded yet
1008 bool TextClass::load(string const & path) const
1013 // Read style-file, provided path is searched before the system ones
1014 FileName layout_file;
1016 layout_file = FileName(addName(path, name_ + ".layout"));
1017 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
1018 layout_file = libFileSearch("layouts", name_, "layout");
1019 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1022 lyxerr << "Error reading `"
1023 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1024 << "'\n(Check `" << name_
1025 << "')\nCheck your installation and "
1026 "try Options/Reconfigure..." << endl;
1033 FloatList & TextClass::floats()
1035 return *floatlist_.get();
1039 FloatList const & TextClass::floats() const
1041 return *floatlist_.get();
1045 Counters & TextClass::counters() const
1047 return *counters_.get();
1051 // Return the layout object of an inset given by name. If the name
1052 // is not found as such, the part after the ':' is stripped off, and
1053 // searched again. In this way, an error fallback can be provided:
1054 // An erroneous 'CharStyle:badname' (e.g., after a documentclass switch)
1055 // will invoke the layout object defined by name = 'CharStyle'.
1056 // If that doesn't work either, an empty object returns (shouldn't
1057 // happen). -- Idea JMarc, comment MV
1058 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1061 while (!n.empty()) {
1062 if (insetlayoutlist_.count(n) > 0)
1063 return insetlayoutlist_[n];
1064 docstring::size_type i = n.find(':');
1065 if (i == string::npos)
1069 static InsetLayout empty;
1070 empty.labelstring = from_utf8("UNDEFINED");
1071 empty.bgcolor = Color::error;
1076 docstring const & TextClass::defaultLayoutName() const
1078 // This really should come from the actual layout... (Lgb)
1079 return defaultlayout_;
1083 LayoutPtr const & TextClass::defaultLayout() const
1085 return operator[](defaultLayoutName());
1089 string const & TextClass::name() const
1095 string const & TextClass::latexname() const
1097 const_cast<TextClass*>(this)->load();
1102 string const & TextClass::description() const
1104 return description_;
1108 string const & TextClass::opt_fontsize() const
1110 return opt_fontsize_;
1114 string const & TextClass::opt_pagestyle() const
1116 return opt_pagestyle_;
1120 string const & TextClass::options() const
1126 string const & TextClass::class_header() const
1128 return class_header_;
1132 string const & TextClass::pagestyle() const
1138 docstring const & TextClass::preamble() const
1144 TextClass::PageSides TextClass::sides() const
1150 int TextClass::secnumdepth() const
1152 return secnumdepth_;
1156 int TextClass::tocdepth() const
1162 OutputType TextClass::outputType() const
1168 bool TextClass::provides(string const & p) const
1170 return provides_.find(p) != provides_.end();
1174 unsigned int TextClass::columns() const
1180 TitleLatexType TextClass::titletype() const
1186 string const & TextClass::titlename() const
1192 int TextClass::size() const
1194 return layoutlist_.size();
1198 int TextClass::min_toclevel() const
1200 return min_toclevel_;
1204 int TextClass::max_toclevel() const
1206 return max_toclevel_;
1210 bool TextClass::hasTocLevels() const
1212 return min_toclevel_ != Layout::NOT_IN_TOC;
1216 ostream & operator<<(ostream & os, TextClass::PageSides p)
1219 case TextClass::OneSide:
1222 case TextClass::TwoSides: