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;
37 using support::FileName;
38 using support::libFileSearch;
39 using support::makeDisplayPath;
40 using support::quoteName;
43 using support::addName;
54 class LayoutNamesEqual : public std::unary_function<LyXLayout_ptr, bool> {
56 LayoutNamesEqual(string const & name)
59 bool operator()(LyXLayout_ptr const & c) const
61 return c->name() == name_;
71 bool layout2layout(FileName const & filename, FileName const & tempfile)
73 FileName const script = libFileSearch("scripts", "layout2layout.py");
75 lyxerr << "Could not find layout conversion "
76 "script layout2layout.py." << endl;
80 std::ostringstream command;
81 command << support::os::python() << ' ' << quoteName(script.toFilesystemEncoding())
82 << ' ' << quoteName(filename.toFilesystemEncoding())
83 << ' ' << quoteName(tempfile.toFilesystemEncoding());
84 string const command_str = command.str();
86 lyxerr[Debug::TCLASS] << "Running `" << command_str << '\'' << endl;
88 support::cmd_ret const ret =
89 support::runCommand(command_str);
91 lyxerr << "Could not run layout conversion "
92 "script layout2layout.py." << endl;
101 LyXTextClass::LyXTextClass(string const & fn, string const & cln,
102 string const & desc, bool texClassAvail )
103 : name_(fn), latexname_(cln), description_(desc),
104 floatlist_(new FloatList), ctrs_(new Counters),
105 texClassAvail_(texClassAvail)
112 pagestyle_ = "default";
113 defaultfont_ = LyXFont(LyXFont::ALL_SANE);
114 opt_fontsize_ = "10|11|12";
115 opt_pagestyle_ = "empty|plain|headings|fancy";
117 titletype_ = TITLE_COMMAND_AFTER;
118 titlename_ = "maketitle";
123 bool LyXTextClass::isTeXClassAvailable() const
125 return texClassAvail_;
129 bool LyXTextClass::do_readStyle(LyXLex & lexrc, LyXLayout & lay)
131 lyxerr[Debug::TCLASS] << "Reading style " << lay.name() << endl;
132 if (!lay.read(lexrc, *this)) {
134 lay.resfont = lay.font;
135 lay.resfont.realize(defaultfont());
136 lay.reslabelfont = lay.labelfont;
137 lay.reslabelfont.realize(defaultfont());
138 return false; // no errors
140 lyxerr << "Error parsing style `" << lay.name() << '\'' << endl;
176 // Reads a textclass structure from file.
177 bool LyXTextClass::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 { "leftmargin", TC_LEFTMARGIN },
197 { "nofloat", TC_NOFLOAT },
198 { "nostyle", TC_NOSTYLE },
199 { "outputtype", TC_OUTPUTTYPE },
200 { "pagestyle", TC_PAGESTYLE },
201 { "preamble", TC_PREAMBLE },
202 { "providesamsmath", TC_PROVIDESAMSMATH },
203 { "providesmakeidx", TC_PROVIDESMAKEIDX },
204 { "providesnatbib", TC_PROVIDESNATBIB },
205 { "providesurl", TC_PROVIDESURL },
206 { "rightmargin", TC_RIGHTMARGIN },
207 { "secnumdepth", TC_SECNUMDEPTH },
208 { "sides", TC_SIDES },
209 { "style", TC_STYLE },
210 { "titlelatexname", TC_TITLELATEXNAME },
211 { "titlelatextype", TC_TITLELATEXTYPE },
212 { "tocdepth", TC_TOCDEPTH }
216 lyxerr[Debug::TCLASS] << "Reading textclass "
217 << to_utf8(makeDisplayPath(filename.absFilename()))
220 lyxerr[Debug::TCLASS] << "Reading input file "
221 << to_utf8(makeDisplayPath(filename.absFilename()))
224 LyXLex lexrc(textClassTags,
225 sizeof(textClassTags) / sizeof(textClassTags[0]));
227 lexrc.setFile(filename);
228 bool error = !lexrc.isOK();
230 // Format of files before the 'Format' tag was introduced
234 while (lexrc.isOK() && !error) {
235 int le = lexrc.lex();
238 case LyXLex::LEX_FEOF:
241 case LyXLex::LEX_UNDEF:
242 lexrc.printError("Unknown TextClass tag `$$Token'");
250 switch (static_cast<TextClassTags>(le)) {
254 format = lexrc.getInteger();
257 case TC_OUTPUTTYPE: // output type definition
258 readOutputType(lexrc);
261 case TC_INPUT: // Include file
263 string const inc = lexrc.getString();
264 FileName tmp = libFileSearch("layouts", inc,
268 lexrc.printError("Could not find input"
271 } else if (read(tmp, true)) {
272 lexrc.printError("Error reading input"
273 "file: " + tmp.absFilename());
279 case TC_DEFAULTSTYLE:
281 string const name = subst(lexrc.getString(),
283 defaultlayout_ = name;
290 string const name = subst(lexrc.getString(),
292 if (hasLayout(name)) {
293 LyXLayout * lay = operator[](name).get();
294 error = do_readStyle(lexrc, *lay);
298 if (le == TC_ENVIRONMENT)
299 lay.is_environment = true;
300 error = do_readStyle(lexrc, lay);
302 layoutlist_.push_back(
303 boost::shared_ptr<LyXLayout>(new LyXLayout(lay))
306 if (defaultlayout_.empty()) {
307 // We do not have a default
308 // layout yet, so we choose
309 // the first layout we
311 defaultlayout_ = name;
316 lexrc.printError("No name given for style: `$$Token'.");
323 string const style = subst(lexrc.getString(),
325 if (!delete_layout(style))
326 lyxerr << "Cannot delete style `"
327 << style << '\'' << endl;
328 // lexrc.printError("Cannot delete style"
335 columns_ = lexrc.getInteger();
340 switch (lexrc.getInteger()) {
341 case 1: sides_ = OneSide; break;
342 case 2: sides_ = TwoSides; break;
344 lyxerr << "Impossible number of page"
345 " sides, setting to one."
355 pagestyle_ = rtrim(lexrc.getString());
359 defaultfont_.lyxRead(lexrc);
360 if (!defaultfont_.resolved()) {
361 lexrc.printError("Warning: defaultfont should "
362 "be fully instantiated!");
363 defaultfont_.realize(LyXFont(LyXFont::ALL_SANE));
369 secnumdepth_ = lexrc.getInteger();
374 tocdepth_ = lexrc.getInteger();
377 // First step to support options
378 case TC_CLASSOPTIONS:
379 readClassOptions(lexrc);
383 preamble_ = from_utf8(lexrc.getLongString("EndPreamble"));
386 case TC_PROVIDESAMSMATH:
387 if (lexrc.next() && lexrc.getInteger())
388 provides_ |= amsmath;
391 case TC_PROVIDESNATBIB:
392 if (lexrc.next() && lexrc.getInteger())
396 case TC_PROVIDESMAKEIDX:
397 if (lexrc.next() && lexrc.getInteger())
398 provides_ |= makeidx;
402 if (lexrc.next() && lexrc.getInteger())
406 case TC_LEFTMARGIN: // left margin type
408 leftmargin_ = lexrc.getString();
411 case TC_RIGHTMARGIN: // right margin type
413 rightmargin_ = lexrc.getString();
417 string const name = subst(lexrc.getString(), '_', ' ');
418 readCharStyle(lexrc, name);
427 case TC_TITLELATEXTYPE:
428 readTitleType(lexrc);
430 case TC_TITLELATEXNAME:
432 titlename_ = lexrc.getString();
436 string const nofloat = lexrc.getString();
437 floatlist_->erase(nofloat);
441 if (format != FORMAT)
445 if (format != FORMAT) {
446 lyxerr[Debug::TCLASS] << "Converting layout file from format "
447 << format << " to " << FORMAT << endl;
448 FileName const tempfile(support::tempName());
449 error = !layout2layout(filename, tempfile);
451 error = read(tempfile, merge);
452 support::unlink(tempfile);
456 if (!merge) { // we are at top level here.
457 lyxerr[Debug::TCLASS] << "Finished reading textclass "
458 << to_utf8(makeDisplayPath(filename.absFilename()))
460 if (defaultlayout_.empty()) {
461 lyxerr << "Error: Textclass '" << name_
462 << "' is missing a defaultstyle." << endl;
466 min_toclevel_ = LyXLayout::NOT_IN_TOC;
467 max_toclevel_ = LyXLayout::NOT_IN_TOC;
468 const_iterator cit = begin();
469 const_iterator the_end = end();
470 for ( ; cit != the_end ; ++cit) {
471 int const toclevel = (*cit)->toclevel;
472 if (toclevel != LyXLayout::NOT_IN_TOC) {
473 if (min_toclevel_ == LyXLayout::NOT_IN_TOC)
474 min_toclevel_ = toclevel;
476 min_toclevel_ = std::min(min_toclevel_,
478 max_toclevel_ = std::max(max_toclevel_,
482 lyxerr[Debug::TCLASS]
483 << "Minimum TocLevel is " << min_toclevel_
484 << ", maximum is " << max_toclevel_ <<endl;
487 lyxerr[Debug::TCLASS] << "Finished reading input file "
488 << to_utf8(makeDisplayPath(filename.absFilename()))
495 void LyXTextClass::readTitleType(LyXLex & lexrc)
497 keyword_item titleTypeTags[] = {
498 { "commandafter", TITLE_COMMAND_AFTER },
499 { "environment", TITLE_ENVIRONMENT }
502 pushpophelper pph(lexrc, titleTypeTags, TITLE_ENVIRONMENT);
504 int le = lexrc.lex();
506 case LyXLex::LEX_UNDEF:
507 lexrc.printError("Unknown output type `$$Token'");
509 case TITLE_COMMAND_AFTER:
510 case TITLE_ENVIRONMENT:
511 titletype_ = static_cast<LYX_TITLE_LATEX_TYPES>(le);
514 lyxerr << "Unhandled value " << le
515 << " in LyXTextClass::readTitleType." << endl;
522 void LyXTextClass::readOutputType(LyXLex & lexrc)
524 keyword_item outputTypeTags[] = {
525 { "docbook", DOCBOOK },
527 { "literate", LITERATE }
530 pushpophelper pph(lexrc, outputTypeTags, LITERATE);
532 int le = lexrc.lex();
534 case LyXLex::LEX_UNDEF:
535 lexrc.printError("Unknown output type `$$Token'");
540 outputType_ = static_cast<OutputType>(le);
543 lyxerr << "Unhandled value " << le
544 << " in LyXTextClass::readOutputType." << endl;
551 enum ClassOptionsTags {
560 void LyXTextClass::readClassOptions(LyXLex & lexrc)
562 keyword_item classOptionsTags[] = {
564 {"fontsize", CO_FONTSIZE },
565 {"header", CO_HEADER },
566 {"other", CO_OTHER },
567 {"pagestyle", CO_PAGESTYLE }
570 lexrc.pushTable(classOptionsTags, CO_END);
572 while (!getout && lexrc.isOK()) {
573 int le = lexrc.lex();
575 case LyXLex::LEX_UNDEF:
576 lexrc.printError("Unknown ClassOption tag `$$Token'");
580 switch (static_cast<ClassOptionsTags>(le)) {
583 opt_fontsize_ = rtrim(lexrc.getString());
587 opt_pagestyle_ = rtrim(lexrc.getString());
591 options_ = lexrc.getString();
595 class_header_ = subst(lexrc.getString(), """, "\"");
616 void LyXTextClass::readCharStyle(LyXLex & lexrc, string const & name)
618 keyword_item elementTags[] = {
621 { "labelfont", CS_LABELFONT },
622 { "latexname", CS_LATEXNAME },
623 { "latexparam", CS_LATEXPARAM },
624 { "latextype", CS_LATEXTYPE },
625 { "preamble", CS_PREAMBLE}
628 lexrc.pushTable(elementTags, CS_END);
633 LyXFont font(LyXFont::ALL_INHERIT);
634 LyXFont labelfont(LyXFont::ALL_INHERIT);
638 while (!getout && lexrc.isOK()) {
639 int le = lexrc.lex();
641 case LyXLex::LEX_UNDEF:
642 lexrc.printError("Unknown ClassOption tag `$$Token'");
646 switch (static_cast<CharStyleTags>(le)) {
649 latextype = lexrc.getString();
653 latexname = lexrc.getString();
657 latexparam = subst(lexrc.getString(), """, "\"");
660 labelfont.lyxRead(lexrc);
667 preamble = lexrc.getLongString("EndPreamble");
676 // Here add element to list if getout == true
680 cs.latextype = latextype;
681 cs.latexname = latexname;
682 cs.latexparam = latexparam;
684 cs.labelfont = labelfont;
685 cs.preamble = from_utf8(preamble);
686 charstyles().push_back(cs);
706 void LyXTextClass::readFloat(LyXLex & lexrc)
708 keyword_item floatTags[] = {
710 { "extension", FT_EXT },
711 { "guiname", FT_NAME },
712 { "latexbuiltin", FT_BUILTIN },
713 { "listname", FT_LISTNAME },
714 { "numberwithin", FT_WITHIN },
715 { "placement", FT_PLACEMENT },
716 { "style", FT_STYLE },
720 lexrc.pushTable(floatTags, FT_END);
729 bool builtin = false;
732 while (!getout && lexrc.isOK()) {
733 int le = lexrc.lex();
735 case LyXLex::LEX_UNDEF:
736 lexrc.printError("Unknown ClassOption tag `$$Token'");
740 switch (static_cast<FloatTags>(le)) {
743 type = lexrc.getString();
744 // Here we could check if this type is already defined
745 // and modify it with the rest of the vars instead.
749 name = lexrc.getString();
753 placement = lexrc.getString();
757 ext = lexrc.getString();
761 within = lexrc.getString();
762 if (within == "none")
767 style = lexrc.getString();
771 listname = lexrc.getString();
775 builtin = lexrc.getBool();
783 // Here if have a full float if getout == true
785 Floating newfloat(type, placement, ext, within,
786 style, name, listname, builtin);
787 floatlist_->newFloat(newfloat);
800 void LyXTextClass::readCounter(LyXLex & lexrc)
802 keyword_item counterTags[] = {
805 { "within", CT_WITHIN }
808 lexrc.pushTable(counterTags, CT_END);
814 while (!getout && lexrc.isOK()) {
815 int le = lexrc.lex();
817 case LyXLex::LEX_UNDEF:
818 lexrc.printError("Unknown ClassOption tag `$$Token'");
822 switch (static_cast<CounterTags>(le)) {
825 name = lyx::from_ascii(lexrc.getString());
829 within = lyx::from_ascii(lexrc.getString());
830 if (within == lyx::from_ascii("none"))
839 // Here if have a full counter if getout == true
842 ctrs_->newCounter(name);
844 ctrs_->newCounter(name, within);
851 LyXFont const & LyXTextClass::defaultfont() const
857 string const & LyXTextClass::leftmargin() const
863 string const & LyXTextClass::rightmargin() const
869 bool LyXTextClass::hasLayout(string const & n) const
871 string const name = (n.empty() ? defaultLayoutName() : n);
873 return find_if(layoutlist_.begin(), layoutlist_.end(),
874 LayoutNamesEqual(name))
875 != layoutlist_.end();
880 LyXLayout_ptr const & LyXTextClass::operator[](string const & name) const
882 BOOST_ASSERT(!name.empty());
884 LayoutList::const_iterator cit =
885 find_if(layoutlist_.begin(),
887 LayoutNamesEqual(name));
889 if (cit == layoutlist_.end()) {
890 lyxerr << "We failed to find the layout '" << name
891 << "' in the layout list. You MUST investigate!"
893 for (LayoutList::const_iterator it = layoutlist_.begin();
894 it != layoutlist_.end(); ++it)
895 lyxerr << " " << it->get()->name() << endl;
897 // we require the name to exist
906 bool LyXTextClass::delete_layout(string const & name)
908 if (name == defaultLayoutName())
911 LayoutList::iterator it =
912 remove_if(layoutlist_.begin(), layoutlist_.end(),
913 LayoutNamesEqual(name));
915 LayoutList::iterator end = layoutlist_.end();
916 bool const ret = (it != end);
917 layoutlist_.erase(it, end);
922 // Load textclass info if not loaded yet
923 bool LyXTextClass::load(string const & path) const
928 // Read style-file, provided path is searched before the system ones
929 FileName layout_file;
931 layout_file = FileName(addName(path, name_ + ".layout"));
932 if (layout_file.empty() || !fs::exists(layout_file.toFilesystemEncoding()))
933 layout_file = libFileSearch("layouts", name_, "layout");
934 loaded_ = const_cast<LyXTextClass*>(this)->read(layout_file) == 0;
937 lyxerr << "Error reading `"
938 << to_utf8(makeDisplayPath(layout_file.absFilename()))
939 << "'\n(Check `" << name_
940 << "')\nCheck your installation and "
941 "try Options/Reconfigure..." << endl;
948 FloatList & LyXTextClass::floats()
950 return *floatlist_.get();
954 FloatList const & LyXTextClass::floats() const
956 return *floatlist_.get();
960 Counters & LyXTextClass::counters() const
966 CharStyles::iterator LyXTextClass::charstyle(string const & s) const
968 CharStyles::iterator cs = charstyles().begin();
969 CharStyles::iterator csend = charstyles().end();
970 for (; cs != csend; ++cs) {
978 string const & LyXTextClass::defaultLayoutName() const
980 // This really should come from the actual layout... (Lgb)
981 return defaultlayout_;
985 LyXLayout_ptr const & LyXTextClass::defaultLayout() const
987 return operator[](defaultLayoutName());
991 string const & LyXTextClass::name() const
997 string const & LyXTextClass::latexname() const
999 const_cast<LyXTextClass*>(this)->load();
1004 string const & LyXTextClass::description() const
1006 return description_;
1010 string const & LyXTextClass::opt_fontsize() const
1012 return opt_fontsize_;
1016 string const & LyXTextClass::opt_pagestyle() const
1018 return opt_pagestyle_;
1022 string const & LyXTextClass::options() const
1028 string const & LyXTextClass::class_header() const
1030 return class_header_;
1034 string const & LyXTextClass::pagestyle() const
1040 docstring const & LyXTextClass::preamble() const
1046 LyXTextClass::PageSides LyXTextClass::sides() const
1052 int LyXTextClass::secnumdepth() const
1054 return secnumdepth_;
1058 int LyXTextClass::tocdepth() const
1064 OutputType LyXTextClass::outputType() const
1070 bool LyXTextClass::provides(LyXTextClass::Provides p) const
1072 return provides_ & p;
1076 unsigned int LyXTextClass::columns() const
1082 LYX_TITLE_LATEX_TYPES LyXTextClass::titletype() const
1088 string const & LyXTextClass::titlename() const
1094 int LyXTextClass::size() const
1096 return layoutlist_.size();
1100 int LyXTextClass::min_toclevel() const
1102 return min_toclevel_;
1106 int LyXTextClass::max_toclevel() const
1108 return max_toclevel_;
1112 bool LyXTextClass::hasTocLevels() const
1114 return min_toclevel_ != LyXLayout::NOT_IN_TOC;
1118 ostream & operator<<(ostream & os, LyXTextClass::PageSides p)
1121 case LyXTextClass::OneSide:
1124 case LyXTextClass::TwoSides: