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;
35 using lyx::support::libFileSearch;
36 using lyx::support::makeDisplayPath;
37 using lyx::support::quoteName;
38 using lyx::support::rtrim;
39 using lyx::support::subst;
40 using lyx::support::addName;
51 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
53 LayoutNamesEqual(string const & name)
56 bool operator()(LyXLayout_ptr const & c) const
58 return c->name() == name_;
68 bool layout2layout(string const & filename, string const & tempfile)
70 string const script = libFileSearch("scripts", "layout2layout.py");
72 lyxerr << "Could not find layout conversion "
73 "script layout2layout.py." << endl;
77 std::ostringstream command;
78 command << lyx::support::os::python() << ' ' << quoteName(script)
79 << ' ' << quoteName(filename)
80 << ' ' << quoteName(tempfile);
81 string const command_str = command.str();
83 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
85 lyx::support::cmd_ret const ret =
86 lyx::support::runCommand(command_str);
88 lyxerr << "Could not run layout conversion "
89 "script layout2layout.py." << endl;
98 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
99 string const & desc, bool texClassAvail )
100 : name_(fn), latexname_(cln), description_(desc),
101 floatlist_(new FloatList), ctrs_(new Counters),
102 texClassAvail_(texClassAvail)
109 pagestyle_ = "default";
110 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
111 opt_fontsize_ = "10|11|12";
112 opt_pagestyle_ = "empty|plain|headings|fancy";
114 titletype_ = TITLE_COMMAND_AFTER;
115 titlename_ = "maketitle";
120 bool LyXTextClass::isTeXClassAvailable() const
122 return texClassAvail_;
126 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
128 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
129 if (!lay.read(lexrc, *this)) {
131 lay.resfont = lay.font;
132 lay.resfont.realize(defaultfont());
133 lay.reslabelfont = lay.labelfont;
134 lay.reslabelfont.realize(defaultfont());
135 return false; // no errors
137 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
173 // Reads a textclass structure from file.
174 bool LyXTextClass::read(string const & filename, bool merge)
176 if (!lyx::support::isFileReadable(filename)) {
177 lyxerr << "Cannot read layout file `" << filename << "'."
182 keyword_item textClassTags[] = {
183 { "charstyle", TC_CHARSTYLE },
184 { "classoptions", TC_CLASSOPTIONS },
185 { "columns", TC_COLUMNS },
186 { "counter", TC_COUNTER },
187 { "defaultfont", TC_DEFAULTFONT },
188 { "defaultstyle", TC_DEFAULTSTYLE },
189 { "environment", TC_ENVIRONMENT },
190 { "float", TC_FLOAT },
191 { "format", TC_FORMAT },
192 { "input", TC_INPUT },
193 { "leftmargin", TC_LEFTMARGIN },
194 { "nofloat", TC_NOFLOAT },
195 { "nostyle", TC_NOSTYLE },
196 { "outputtype", TC_OUTPUTTYPE },
197 { "pagestyle", TC_PAGESTYLE },
198 { "preamble", TC_PREAMBLE },
199 { "providesamsmath", TC_PROVIDESAMSMATH },
200 { "providesmakeidx", TC_PROVIDESMAKEIDX },
201 { "providesnatbib", TC_PROVIDESNATBIB },
202 { "providesurl", TC_PROVIDESURL },
203 { "rightmargin", TC_RIGHTMARGIN },
204 { "secnumdepth", TC_SECNUMDEPTH },
205 { "sides", TC_SIDES },
206 { "style", TC_STYLE },
207 { "titlelatexname", TC_TITLELATEXNAME },
208 { "titlelatextype", TC_TITLELATEXTYPE },
209 { "tocdepth", TC_TOCDEPTH }
213 lyxerr[Debug::TCLASS] << "Reading textclass "
214 << lyx::to_utf8(makeDisplayPath(filename))
217 lyxerr[Debug::TCLASS] << "Reading input file "
218 << lyx::to_utf8(makeDisplayPath(filename))
221 LyXLex lexrc(textClassTags,
222 sizeof(textClassTags) / sizeof(textClassTags[0]));
224 lexrc.setFile(filename);
225 bool error = !lexrc.isOK();
227 // Format of files before the 'Format' tag was introduced
231 while (lexrc.isOK() && !error) {
232 int le = lexrc.lex();
235 case LyXLex::LEX_FEOF:
238 case LyXLex::LEX_UNDEF:
239 lexrc.printError("Unknown TextClass tag `$$Token'");
247 switch (static_cast<TextClassTags>(le)) {
251 format = lexrc.getInteger();
254 case TC_OUTPUTTYPE: // output type definition
255 readOutputType(lexrc);
258 case TC_INPUT: // Include file
260 string const inc = lexrc.getString();
261 string tmp = libFileSearch("layouts", inc,
265 lexrc.printError("Could not find input"
268 } else if (read(tmp, true)) {
269 lexrc.printError("Error reading input"
276 case TC_DEFAULTSTYLE:
278 string const name = subst(lexrc.getString(),
280 defaultlayout_ = name;
287 string const name = subst(lexrc.getString(),
289 if (hasLayout(name)) {
290 LyXLayout * lay = operator[](name).get();
291 error = do_readStyle(lexrc, *lay);
295 if (le == TC_ENVIRONMENT)
296 lay.is_environment = true;
297 error = do_readStyle(lexrc, lay);
299 layoutlist_.push_back(
300 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
303 if (defaultlayout_.empty()) {
304 // We do not have a default
305 // layout yet, so we choose
306 // the first layout we
308 defaultlayout_ = name;
313 lexrc.printError("No name given for style: `$$Token'.");
320 string const style = subst(lexrc.getString(),
322 if (!delete_layout(style))
323 lyxerr << "Cannot delete style `"
324 << style << '\'' << endl;
325 // lexrc.printError("Cannot delete style"
332 columns_ = lexrc.getInteger();
337 switch (lexrc.getInteger()) {
338 case 1: sides_ = OneSide; break;
339 case 2: sides_ = TwoSides; break;
341 lyxerr << "Impossible number of page"
342 " sides, setting to one."
352 pagestyle_ = rtrim(lexrc.getString());
356 defaultfont_.lyxRead(lexrc);
357 if (!defaultfont_.resolved()) {
358 lexrc.printError("Warning: defaultfont should "
359 "be fully instantiated!");
360 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
366 secnumdepth_ = lexrc.getInteger();
371 tocdepth_ = lexrc.getInteger();
374 // First step to support options
375 case TC_CLASSOPTIONS:
376 readClassOptions(lexrc);
380 preamble_ = lexrc.getLongString("EndPreamble");
383 case TC_PROVIDESAMSMATH:
384 if (lexrc.next() && lexrc.getInteger())
385 provides_ |= amsmath;
388 case TC_PROVIDESNATBIB:
389 if (lexrc.next() && lexrc.getInteger())
393 case TC_PROVIDESMAKEIDX:
394 if (lexrc.next() && lexrc.getInteger())
395 provides_ |= makeidx;
399 if (lexrc.next() && lexrc.getInteger())
403 case TC_LEFTMARGIN: // left margin type
405 leftmargin_ = lexrc.getString();
408 case TC_RIGHTMARGIN: // right margin type
410 rightmargin_ = lexrc.getString();
414 string const name = subst(lexrc.getString(), '_', ' ');
415 readCharStyle(lexrc, name);
424 case TC_TITLELATEXTYPE:
425 readTitleType(lexrc);
427 case TC_TITLELATEXNAME:
429 titlename_ = lexrc.getString();
433 string const nofloat = lexrc.getString();
434 floatlist_->erase(nofloat);
438 if (format != FORMAT)
442 if (format != FORMAT) {
443 lyxerr[Debug::TCLASS] << "Converting layout file from format "
444 << format << " to " << FORMAT << endl;
445 string const tempfile = lyx::support::tempName();
446 error = !layout2layout(filename, tempfile);
448 error = read(tempfile, merge);
449 lyx::support::unlink(tempfile);
453 if (!merge) { // we are at top level here.
454 lyxerr[Debug::TCLASS] << "Finished reading textclass "
455 << lyx::to_utf8(makeDisplayPath(filename))
457 if (defaultlayout_.empty()) {
458 lyxerr << "Error: Textclass '" << name_
459 << "' is missing a defaultstyle." << endl;
463 min_toclevel_ = LyXLayout::NOT_IN_TOC;
464 max_toclevel_ = LyXLayout::NOT_IN_TOC;
465 const_iterator cit = begin();
466 const_iterator the_end = end();
467 for ( ; cit != the_end ; ++cit) {
468 int const toclevel = (*cit)->toclevel;
469 if (toclevel != LyXLayout::NOT_IN_TOC) {
470 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
471 min_toclevel_ = toclevel;
473 min_toclevel_ = std::min(min_toclevel_,
475 max_toclevel_ = std::max(max_toclevel_,
479 lyxerr[Debug::TCLASS]
480 << "Minimum TocLevel is " << min_toclevel_
481 << ", maximum is " << max_toclevel_ <<endl;
484 lyxerr[Debug::TCLASS] << "Finished reading input file "
485 << lyx::to_utf8(makeDisplayPath(filename))
492 void LyXTextClass::readTitleType(LyXLex & lexrc)
494 keyword_item titleTypeTags[] = {
495 { "commandafter", TITLE_COMMAND_AFTER },
496 { "environment", TITLE_ENVIRONMENT }
499 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
501 int le = lexrc.lex();
503 case LyXLex::LEX_UNDEF:
504 lexrc.printError("Unknown output type `$$Token'");
506 case TITLE_COMMAND_AFTER:
507 case TITLE_ENVIRONMENT:
508 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
511 lyxerr << "Unhandled value " << le
512 << " in LyXTextClass::readTitleType." << endl;
519 void LyXTextClass::readOutputType(LyXLex & lexrc)
521 keyword_item outputTypeTags[] = {
522 { "docbook", DOCBOOK },
524 { "literate", LITERATE }
527 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
529 int le = lexrc.lex();
531 case LyXLex::LEX_UNDEF:
532 lexrc.printError("Unknown output type `$$Token'");
537 outputType_ = static_cast<OutputType>(le);
540 lyxerr << "Unhandled value " << le
541 << " in LyXTextClass::readOutputType." << endl;
548 enum ClassOptionsTags {
557 void LyXTextClass::readClassOptions(LyXLex & lexrc)
559 keyword_item classOptionsTags[] = {
561 {"fontsize", CO_FONTSIZE },
562 {"header", CO_HEADER },
563 {"other", CO_OTHER },
564 {"pagestyle", CO_PAGESTYLE }
567 lexrc.pushTable(classOptionsTags, CO_END);
569 while (!getout && lexrc.isOK()) {
570 int le = lexrc.lex();
572 case LyXLex::LEX_UNDEF:
573 lexrc.printError("Unknown ClassOption tag `$$Token'");
577 switch (static_cast<ClassOptionsTags>(le)) {
580 opt_fontsize_ = rtrim(lexrc.getString());
584 opt_pagestyle_ = rtrim(lexrc.getString());
588 options_ = lexrc.getString();
592 class_header_ = subst(lexrc.getString(), """, "\"");
613 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
615 keyword_item elementTags[] = {
618 { "labelfont", CS_LABELFONT },
619 { "latexname", CS_LATEXNAME },
620 { "latexparam", CS_LATEXPARAM },
621 { "latextype", CS_LATEXTYPE },
622 { "preamble", CS_PREAMBLE}
625 lexrc.pushTable(elementTags, CS_END);
630 LyXFont font(LyXFont::ALL_INHERIT);
631 LyXFont labelfont(LyXFont::ALL_INHERIT);
635 while (!getout && lexrc.isOK()) {
636 int le = lexrc.lex();
638 case LyXLex::LEX_UNDEF:
639 lexrc.printError("Unknown ClassOption tag `$$Token'");
643 switch (static_cast<CharStyleTags>(le)) {
646 latextype = lexrc.getString();
650 latexname = lexrc.getString();
654 latexparam = subst(lexrc.getString(), """, "\"");
657 labelfont.lyxRead(lexrc);
664 preamble = lexrc.getLongString("EndPreamble");
673 // Here add element to list if getout == true
677 cs.latextype = latextype;
678 cs.latexname = latexname;
679 cs.latexparam = latexparam;
681 cs.labelfont = labelfont;
682 cs.preamble = preamble;
683 charstyles().push_back(cs);
703 void LyXTextClass::readFloat(LyXLex & lexrc)
705 keyword_item floatTags[] = {
707 { "extension", FT_EXT },
708 { "guiname", FT_NAME },
709 { "latexbuiltin", FT_BUILTIN },
710 { "listname", FT_LISTNAME },
711 { "numberwithin", FT_WITHIN },
712 { "placement", FT_PLACEMENT },
713 { "style", FT_STYLE },
717 lexrc.pushTable(floatTags, FT_END);
726 bool builtin = false;
729 while (!getout && lexrc.isOK()) {
730 int le = lexrc.lex();
732 case LyXLex::LEX_UNDEF:
733 lexrc.printError("Unknown ClassOption tag `$$Token'");
737 switch (static_cast<FloatTags>(le)) {
740 type = lexrc.getString();
741 // Here we could check if this type is already defined
742 // and modify it with the rest of the vars instead.
746 name = lexrc.getString();
750 placement = lexrc.getString();
754 ext = lexrc.getString();
758 within = lexrc.getString();
759 if (within == "none")
764 style = lexrc.getString();
768 listname = lexrc.getString();
772 builtin = lexrc.getBool();
780 // Here if have a full float if getout == true
782 Floating newfloat(type, placement, ext, within,
783 style, name, listname, builtin);
784 floatlist_->newFloat(newfloat);
797 void LyXTextClass::readCounter(LyXLex & lexrc)
799 keyword_item counterTags[] = {
802 { "within", CT_WITHIN }
805 lexrc.pushTable(counterTags, CT_END);
811 while (!getout && lexrc.isOK()) {
812 int le = lexrc.lex();
814 case LyXLex::LEX_UNDEF:
815 lexrc.printError("Unknown ClassOption tag `$$Token'");
819 switch (static_cast<CounterTags>(le)) {
822 name = lexrc.getString();
826 within = lexrc.getString();
827 if (within == "none")
836 // Here if have a full counter if getout == true
838 if (within.empty()) {
839 ctrs_->newCounter(name);
841 ctrs_->newCounter(name, within);
849 LyXFont const & LyXTextClass::defaultfont() const
855 string const & LyXTextClass::leftmargin() const
861 string const & LyXTextClass::rightmargin() const
867 bool LyXTextClass::hasLayout(string const & n) const
869 string const name = (n.empty() ? defaultLayoutName() : n);
871 return find_if(layoutlist_.begin(), layoutlist_.end(),
872 LayoutNamesEqual(name))
873 != layoutlist_.end();
878 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
880 BOOST_ASSERT(!name.empty());
882 LayoutList::const_iterator cit =
883 find_if(layoutlist_.begin(),
885 LayoutNamesEqual(name));
887 if (cit == layoutlist_.end()) {
888 lyxerr << "We failed to find the layout '" << name
889 << "' in the layout list. You MUST investigate!"
891 for (LayoutList::const_iterator it = layoutlist_.begin();
892 it != layoutlist_.end(); ++it)
893 lyxerr << " " << it->get()->name() << endl;
895 // we require the name to exist
904 bool LyXTextClass::delete_layout(string const & name)
906 if (name == defaultLayoutName())
909 LayoutList::iterator it =
910 remove_if(layoutlist_.begin(), layoutlist_.end(),
911 LayoutNamesEqual(name));
913 LayoutList::iterator end = layoutlist_.end();
914 bool const ret = (it != end);
915 layoutlist_.erase(it, end);
920 // Load textclass info if not loaded yet
921 bool LyXTextClass::load(string const & path) const
926 // Read style-file, provided path is searched before the system ones
929 layout_file = addName(path, name_ + ".layout");
930 if (layout_file.empty() || !fs::exists(layout_file))
931 layout_file = libFileSearch("layouts", name_, "layout");
932 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
935 lyxerr << "Error reading `"
936 << lyx::to_utf8(makeDisplayPath(layout_file))
937 << "'\n(Check `" << name_
938 << "')\nCheck your installation and "
939 "try Options/Reconfigure..." << endl;
946 FloatList & LyXTextClass::floats()
948 return *floatlist_.get();
952 FloatList const & LyXTextClass::floats() const
954 return *floatlist_.get();
958 Counters & LyXTextClass::counters() const
964 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
966 CharStyles::iterator cs = charstyles().begin();
967 CharStyles::iterator csend = charstyles().end();
968 for (; cs != csend; ++cs) {
976 string const & LyXTextClass::defaultLayoutName() const
978 // This really should come from the actual layout... (Lgb)
979 return defaultlayout_;
983 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
985 return operator[](defaultLayoutName());
989 string const & LyXTextClass::name() const
995 string const & LyXTextClass::latexname() const
997 const_cast<LyXTextClass*>(this)->load();
1002 string const & LyXTextClass::description() const
1004 return description_;
1008 string const & LyXTextClass::opt_fontsize() const
1010 return opt_fontsize_;
1014 string const & LyXTextClass::opt_pagestyle() const
1016 return opt_pagestyle_;
1020 string const & LyXTextClass::options() const
1026 string const & LyXTextClass::class_header() const
1028 return class_header_;
1032 string const & LyXTextClass::pagestyle() const
1038 string const & LyXTextClass::preamble() const
1044 LyXTextClass::PageSides LyXTextClass::sides() const
1050 int LyXTextClass::secnumdepth() const
1052 return secnumdepth_;
1056 int LyXTextClass::tocdepth() const
1062 OutputType LyXTextClass::outputType() const
1068 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1070 return provides_ & p;
1074 unsigned int LyXTextClass::columns() const
1080 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1086 string const & LyXTextClass::titlename() const
1092 int LyXTextClass::size() const
1094 return layoutlist_.size();
1098 int LyXTextClass::min_toclevel() const
1100 return min_toclevel_;
1104 int LyXTextClass::max_toclevel() const
1106 return max_toclevel_;
1110 bool LyXTextClass::hasTocLevels() const
1112 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1116 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1119 case LyXTextClass::OneSide:
1122 case LyXTextClass::TwoSides: