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<Layout_ptr, bool> {
59 LayoutNamesEqual(docstring const & name)
62 bool operator()(Layout_ptr 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), ctrs_(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;
176 // Reads a textclass structure from file.
177 bool TextClass::read(FileName const & filename, bool merge)
179 if (!support::isFileReadable(filename)) {
180 lyxerr << "Cannot read layout file `" << filename << "'."
185 keyword_item textClassTags[] = {
186 { "charstyle", TC_CHARSTYLE },
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 }
214 LYXERR(Debug::TCLASS) << "Reading textclass "
215 << to_utf8(makeDisplayPath(filename.absFilename()))
218 LYXERR(Debug::TCLASS) << "Reading input file "
219 << to_utf8(makeDisplayPath(filename.absFilename()))
222 Lexer lexrc(textClassTags,
223 sizeof(textClassTags) / sizeof(textClassTags[0]));
225 lexrc.setFile(filename);
226 bool error = !lexrc.isOK();
228 // Format of files before the 'Format' tag was introduced
232 while (lexrc.isOK() && !error) {
233 int le = lexrc.lex();
236 case Lexer::LEX_FEOF:
239 case Lexer::LEX_UNDEF:
240 lexrc.printError("Unknown TextClass tag `$$Token'");
248 switch (static_cast<TextClassTags>(le)) {
252 format = lexrc.getInteger();
255 case TC_OUTPUTTYPE: // output type definition
256 readOutputType(lexrc);
259 case TC_INPUT: // Include file
261 string const inc = lexrc.getString();
262 FileName tmp = libFileSearch("layouts", inc,
266 lexrc.printError("Could not find input"
269 } else if (read(tmp, true)) {
270 lexrc.printError("Error reading input"
271 "file: " + tmp.absFilename());
277 case TC_DEFAULTSTYLE:
279 docstring const name = from_utf8(subst(lexrc.getString(),
281 defaultlayout_ = name;
288 docstring const name = from_utf8(subst(lexrc.getString(),
291 string s = "Could not read name for style: `$$Token' "
292 + lexrc.getString() + " is probably not valid UTF-8!";
293 lexrc.printError(s.c_str());
295 error = do_readStyle(lexrc, lay);
296 } else if (hasLayout(name)) {
297 Layout * lay = operator[](name).get();
298 error = do_readStyle(lexrc, *lay);
302 if (le == TC_ENVIRONMENT)
303 lay.is_environment = true;
304 error = do_readStyle(lexrc, lay);
306 layoutlist_.push_back(
307 boost::shared_ptr<Layout>(new Layout(lay))
310 if (defaultlayout_.empty()) {
311 // We do not have a default
312 // layout yet, so we choose
313 // the first layout we
315 defaultlayout_ = name;
320 lexrc.printError("No name given for style: `$$Token'.");
327 docstring const style = from_utf8(subst(lexrc.getString(),
329 if (!delete_layout(style))
330 lyxerr << "Cannot delete style `"
331 << to_utf8(style) << '\'' << endl;
332 // lexrc.printError("Cannot delete style"
339 columns_ = lexrc.getInteger();
344 switch (lexrc.getInteger()) {
345 case 1: sides_ = OneSide; break;
346 case 2: sides_ = TwoSides; break;
348 lyxerr << "Impossible number of page"
349 " sides, setting to one."
359 pagestyle_ = rtrim(lexrc.getString());
363 defaultfont_.lyxRead(lexrc);
364 if (!defaultfont_.resolved()) {
365 lexrc.printError("Warning: defaultfont should "
366 "be fully instantiated!");
367 defaultfont_.realize(Font(Font::ALL_SANE));
373 secnumdepth_ = lexrc.getInteger();
378 tocdepth_ = lexrc.getInteger();
381 // First step to support options
382 case TC_CLASSOPTIONS:
383 readClassOptions(lexrc);
387 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
392 string const feature = lexrc.getString();
394 if (lexrc.getInteger())
395 provides_.insert(feature);
397 provides_.erase(feature);
401 case TC_LEFTMARGIN: // left margin type
403 leftmargin_ = lexrc.getString();
406 case TC_RIGHTMARGIN: // right margin type
408 rightmargin_ = lexrc.getString();
412 string const name = subst(lexrc.getString(), '_', ' ');
413 readCharStyle(lexrc, name);
418 docstring const name = subst(lexrc.getDocString(), '_', ' ');
419 readInsetLayout(lexrc, name);
428 case TC_TITLELATEXTYPE:
429 readTitleType(lexrc);
431 case TC_TITLELATEXNAME:
433 titlename_ = lexrc.getString();
437 string const nofloat = lexrc.getString();
438 floatlist_->erase(nofloat);
442 if (format != FORMAT)
446 if (format != FORMAT) {
447 LYXERR(Debug::TCLASS) << "Converting layout file from format "
448 << format << " to " << FORMAT << endl;
449 FileName const tempfile(support::tempName());
450 error = !layout2layout(filename, tempfile);
452 error = read(tempfile, merge);
453 support::unlink(tempfile);
457 if (!merge) { // we are at top level here.
458 LYXERR(Debug::TCLASS) << "Finished reading textclass "
459 << to_utf8(makeDisplayPath(filename.absFilename()))
461 if (defaultlayout_.empty()) {
462 lyxerr << "Error: Textclass '" << name_
463 << "' is missing a defaultstyle." << endl;
467 min_toclevel_ = Layout::NOT_IN_TOC;
468 max_toclevel_ = Layout::NOT_IN_TOC;
469 const_iterator cit = begin();
470 const_iterator the_end = end();
471 for ( ; cit != the_end ; ++cit) {
472 int const toclevel = (*cit)->toclevel;
473 if (toclevel != Layout::NOT_IN_TOC) {
474 if (min_toclevel_ == Layout::NOT_IN_TOC)
475 min_toclevel_ = toclevel;
477 min_toclevel_ = std::min(min_toclevel_,
479 max_toclevel_ = std::max(max_toclevel_,
483 LYXERR(Debug::TCLASS)
484 << "Minimum TocLevel is " << min_toclevel_
485 << ", maximum is " << max_toclevel_ <<endl;
488 LYXERR(Debug::TCLASS) << "Finished reading input file "
489 << to_utf8(makeDisplayPath(filename.absFilename()))
496 void TextClass::readTitleType(Lexer & lexrc)
498 keyword_item titleTypeTags[] = {
499 { "commandafter", TITLE_COMMAND_AFTER },
500 { "environment", TITLE_ENVIRONMENT }
503 PushPopHelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
505 int le = lexrc.lex();
507 case Lexer::LEX_UNDEF:
508 lexrc.printError("Unknown output type `$$Token'");
510 case TITLE_COMMAND_AFTER:
511 case TITLE_ENVIRONMENT:
512 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
515 lyxerr << "Unhandled value " << le
516 << " in TextClass::readTitleType." << endl;
523 void TextClass::readOutputType(Lexer & lexrc)
525 keyword_item outputTypeTags[] = {
526 { "docbook", DOCBOOK },
528 { "literate", LITERATE }
531 PushPopHelper pph(lexrc, outputTypeTags, LITERATE);
533 int le = lexrc.lex();
535 case Lexer::LEX_UNDEF:
536 lexrc.printError("Unknown output type `$$Token'");
541 outputType_ = static_cast<OutputType>(le);
544 lyxerr << "Unhandled value " << le
545 << " in TextClass::readOutputType." << endl;
552 enum ClassOptionsTags {
561 void TextClass::readClassOptions(Lexer & lexrc)
563 keyword_item classOptionsTags[] = {
565 {"fontsize", CO_FONTSIZE },
566 {"header", CO_HEADER },
567 {"other", CO_OTHER },
568 {"pagestyle", CO_PAGESTYLE }
571 lexrc.pushTable(classOptionsTags, CO_END);
573 while (!getout && lexrc.isOK()) {
574 int le = lexrc.lex();
576 case Lexer::LEX_UNDEF:
577 lexrc.printError("Unknown ClassOption tag `$$Token'");
581 switch (static_cast<ClassOptionsTags>(le)) {
584 opt_fontsize_ = rtrim(lexrc.getString());
588 opt_pagestyle_ = rtrim(lexrc.getString());
592 options_ = lexrc.getString();
596 class_header_ = subst(lexrc.getString(), """, "\"");
617 enum InsetLayoutTags {
630 void TextClass::readCharStyle(Lexer & lexrc, string const & name)
632 keyword_item elementTags[] = {
635 { "labelfont", CS_LABELFONT },
636 { "latexname", CS_LATEXNAME },
637 { "latexparam", CS_LATEXPARAM },
638 { "latextype", CS_LATEXTYPE },
639 { "preamble", CS_PREAMBLE}
642 lexrc.pushTable(elementTags, CS_END);
647 Font font(Font::ALL_INHERIT);
648 Font labelfont(Font::ALL_INHERIT);
652 while (!getout && lexrc.isOK()) {
653 int le = lexrc.lex();
655 case Lexer::LEX_UNDEF:
656 lexrc.printError("Unknown ClassOption tag `$$Token'");
660 switch (static_cast<CharStyleTags>(le)) {
663 latextype = lexrc.getString();
667 latexname = lexrc.getString();
671 latexparam = subst(lexrc.getString(), """, "\"");
674 labelfont.lyxRead(lexrc);
681 preamble = lexrc.getLongString("EndPreamble");
690 // Here add element to list if getout == true
694 cs.latextype = latextype;
695 cs.latexname = latexname;
696 cs.latexparam = latexparam;
698 cs.labelfont = labelfont;
699 cs.preamble = from_utf8(preamble);
700 charstyles().push_back(cs);
707 void TextClass::readInsetLayout(Lexer & lexrc, docstring const & name)
709 keyword_item elementTags[] = {
712 { "labelfont", IL_LABELFONT },
713 { "labelstring", IL_LABELSTRING },
714 { "latexname", IL_LATEXNAME },
715 { "latexparam", IL_LATEXPARAM },
716 { "latextype", IL_LATEXTYPE },
717 { "preamble", IL_PREAMBLE}
720 lexrc.pushTable(elementTags, IL_END);
722 docstring labelstring;
726 Font font(Font::ALL_INHERIT);
727 Font labelfont(Font::ALL_INHERIT);
731 while (!getout && lexrc.isOK()) {
732 int le = lexrc.lex();
734 case Lexer::LEX_UNDEF:
735 lexrc.printError("Unknown ClassOption tag `$$Token'");
739 switch (static_cast<InsetLayoutTags>(le)) {
742 latextype = lexrc.getString();
746 labelstring = lexrc.getDocString();
750 latexname = lexrc.getString();
754 latexparam = subst(lexrc.getString(), """, "\"");
757 labelfont.lyxRead(lexrc);
758 labelfont.realize(defaultfont());
762 font.realize(defaultfont());
766 preamble = lexrc.getLongString("EndPreamble");
775 // Here add element to list if getout == true
778 il.labelstring = labelstring;
779 il.latextype = latextype;
780 il.latexname = latexname;
781 il.latexparam = latexparam;
783 il.labelfont = labelfont;
784 il.preamble = from_utf8(preamble);
785 insetlayoutlist_[name] = il;
806 void TextClass::readFloat(Lexer & lexrc)
808 keyword_item floatTags[] = {
810 { "extension", FT_EXT },
811 { "guiname", FT_NAME },
812 { "latexbuiltin", FT_BUILTIN },
813 { "listname", FT_LISTNAME },
814 { "numberwithin", FT_WITHIN },
815 { "placement", FT_PLACEMENT },
816 { "style", FT_STYLE },
820 lexrc.pushTable(floatTags, FT_END);
829 bool builtin = false;
832 while (!getout && lexrc.isOK()) {
833 int le = lexrc.lex();
835 case Lexer::LEX_UNDEF:
836 lexrc.printError("Unknown ClassOption tag `$$Token'");
840 switch (static_cast<FloatTags>(le)) {
843 type = lexrc.getString();
844 // Here we could check if this type is already defined
845 // and modify it with the rest of the vars instead.
849 name = lexrc.getString();
853 placement = lexrc.getString();
857 ext = lexrc.getString();
861 within = lexrc.getString();
862 if (within == "none")
867 style = lexrc.getString();
871 listname = lexrc.getString();
875 builtin = lexrc.getBool();
883 // Here if have a full float if getout == true
885 Floating newfloat(type, placement, ext, within,
886 style, name, listname, builtin);
887 floatlist_->newFloat(newfloat);
900 void TextClass::readCounter(Lexer & lexrc)
902 keyword_item counterTags[] = {
905 { "within", CT_WITHIN }
908 lexrc.pushTable(counterTags, CT_END);
914 while (!getout && lexrc.isOK()) {
915 int le = lexrc.lex();
917 case Lexer::LEX_UNDEF:
918 lexrc.printError("Unknown ClassOption tag `$$Token'");
922 switch (static_cast<CounterTags>(le)) {
925 name = from_ascii(lexrc.getString());
929 within = from_ascii(lexrc.getString());
930 if (within == "none")
939 // Here if have a full counter if getout == true
942 ctrs_->newCounter(name);
944 ctrs_->newCounter(name, within);
951 Font const & TextClass::defaultfont() const
957 string const & TextClass::leftmargin() const
963 string const & TextClass::rightmargin() const
969 bool TextClass::hasLayout(docstring const & n) const
971 docstring const name = n.empty() ? defaultLayoutName() : n;
973 return find_if(layoutlist_.begin(), layoutlist_.end(),
974 LayoutNamesEqual(name))
975 != layoutlist_.end();
980 Layout_ptr const & TextClass::operator[](docstring const & name) const
982 BOOST_ASSERT(!name.empty());
984 LayoutList::const_iterator cit =
985 find_if(layoutlist_.begin(),
987 LayoutNamesEqual(name));
989 if (cit == layoutlist_.end()) {
990 lyxerr << "We failed to find the layout '" << to_utf8(name)
991 << "' in the layout list. You MUST investigate!"
993 for (LayoutList::const_iterator it = layoutlist_.begin();
994 it != layoutlist_.end(); ++it)
995 lyxerr << " " << to_utf8(it->get()->name()) << endl;
997 // we require the name to exist
1006 bool TextClass::delete_layout(docstring const & name)
1008 if (name == defaultLayoutName())
1011 LayoutList::iterator it =
1012 remove_if(layoutlist_.begin(), layoutlist_.end(),
1013 LayoutNamesEqual(name));
1015 LayoutList::iterator end = layoutlist_.end();
1016 bool const ret = (it != end);
1017 layoutlist_.erase(it, end);
1022 // Load textclass info if not loaded yet
1023 bool TextClass::load(string const & path) const
1028 // Read style-file, provided path is searched before the system ones
1029 FileName layout_file;
1031 layout_file = FileName(addName(path, name_ + ".layout"));
1032 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
1033 layout_file = libFileSearch("layouts", name_, "layout");
1034 loaded_ = const_cast<TextClass*>(this)->read(layout_file) == 0;
1037 lyxerr << "Error reading `"
1038 << to_utf8(makeDisplayPath(layout_file.absFilename()))
1039 << "'\n(Check `" << name_
1040 << "')\nCheck your installation and "
1041 "try Options/Reconfigure..." << endl;
1048 FloatList & TextClass::floats()
1050 return *floatlist_.get();
1054 FloatList const & TextClass::floats() const
1056 return *floatlist_.get();
1060 Counters & TextClass::counters() const
1062 return *ctrs_.get();
1065 InsetLayout const & TextClass::insetlayout(docstring const & name) const
1067 return insetlayoutlist_[name];
1071 CharStyles::iterator TextClass::charstyle(string const & s) const
1073 CharStyles::iterator cs = charstyles().begin();
1074 CharStyles::iterator csend = charstyles().end();
1075 for (; cs != csend; ++cs) {
1083 docstring const & TextClass::defaultLayoutName() const
1085 // This really should come from the actual layout... (Lgb)
1086 return defaultlayout_;
1090 Layout_ptr const & TextClass::defaultLayout() const
1092 return operator[](defaultLayoutName());
1096 string const & TextClass::name() const
1102 string const & TextClass::latexname() const
1104 const_cast<TextClass*>(this)->load();
1109 string const & TextClass::description() const
1111 return description_;
1115 string const & TextClass::opt_fontsize() const
1117 return opt_fontsize_;
1121 string const & TextClass::opt_pagestyle() const
1123 return opt_pagestyle_;
1127 string const & TextClass::options() const
1133 string const & TextClass::class_header() const
1135 return class_header_;
1139 string const & TextClass::pagestyle() const
1145 docstring const & TextClass::preamble() const
1151 TextClass::PageSides TextClass::sides() const
1157 int TextClass::secnumdepth() const
1159 return secnumdepth_;
1163 int TextClass::tocdepth() const
1169 OutputType TextClass::outputType() const
1175 bool TextClass::provides(string const & p) const
1177 return provides_.find(p) != provides_.end();
1181 unsigned int TextClass::columns() const
1187 LYX_TITLE_LATEX_TYPES TextClass::titletype() const
1193 string const & TextClass::titlename() const
1199 int TextClass::size() const
1201 return layoutlist_.size();
1205 int TextClass::min_toclevel() const
1207 return min_toclevel_;
1211 int TextClass::max_toclevel() const
1213 return max_toclevel_;
1217 bool TextClass::hasTocLevels() const
1219 return min_toclevel_ != Layout::NOT_IN_TOC;
1223 ostream & operator<<(ostream & os, TextClass::PageSides p)
1226 case TextClass::OneSide:
1229 case TextClass::TwoSides: