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
10 * Full author contact details are available in file CREDITS.
17 #include "TextClass.h"
21 #include "support/Messages.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
26 #include <boost/regex.hpp>
29 using namespace lyx::support;
33 /// Special value of toclevel for layouts that to not belong in a TOC
34 const int Layout::NOT_IN_TOC = -1000;
36 // The order of the LayoutTags enum is no more important. [asierra300396]
50 //LT_ENVIRONMENT_DEFAULT,
67 LT_LABELSTRING_APPENDIX,
107 LT_INTITLE // keep this last!
110 /////////////////////
115 margintype = MARGIN_STATIC;
116 latextype = LATEX_PARAGRAPH;
123 labelfont = inherit_font;
125 reslabelfont = sane_font;
126 nextnoindent = false;
131 labelbottomsep = 0.0;
133 align = LYX_ALIGN_BLOCK;
134 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
135 labeltype = LABEL_NO_LABEL;
136 endlabeltype = END_LABEL_NO_LABEL;
137 // Should or should not. That is the question.
138 // spacing.set(Spacing::OneHalf);
141 newline_allowed = true;
142 free_spacing = false;
144 toclevel = NOT_IN_TOC;
146 htmllabelfirst_ = false;
147 htmlforcecss_ = false;
152 bool Layout::read(Lexer & lex, TextClass const & tclass)
154 // This table is sorted alphabetically [asierra 30March96]
155 LexerKeyword layoutTags[] = {
156 { "align", LT_ALIGN },
157 { "alignpossible", LT_ALIGNPOSSIBLE },
158 { "babelpreamble", LT_BABELPREAMBLE },
159 { "bottomsep", LT_BOTTOMSEP },
160 { "category", LT_CATEGORY },
161 { "commanddepth", LT_COMMANDDEPTH },
162 { "copystyle", LT_COPYSTYLE },
163 { "dependson", LT_DEPENDSON },
165 { "endlabelstring", LT_ENDLABELSTRING },
166 { "endlabeltype", LT_ENDLABELTYPE },
167 { "fill_bottom", LT_FILL_BOTTOM },
168 { "fill_top", LT_FILL_TOP },
170 { "freespacing", LT_FREE_SPACING },
171 { "htmlattr", LT_HTMLATTR },
172 { "htmlforcecss", LT_HTMLFORCECSS },
173 { "htmlitem", LT_HTMLITEM },
174 { "htmlitemattr", LT_HTMLITEMATTR },
175 { "htmllabel", LT_HTMLLABEL },
176 { "htmllabelattr", LT_HTMLLABELATTR },
177 { "htmllabelfirst", LT_HTMLLABELFIRST },
178 { "htmlpremable", LT_HTMLPREAMBLE },
179 { "htmlstyle", LT_HTMLSTYLE },
180 { "htmltag", LT_HTMLTAG },
181 { "htmltitle", LT_HTMLTITLE },
182 { "innertag", LT_INNERTAG },
183 { "inpreamble", LT_INPREAMBLE },
184 { "intitle", LT_INTITLE },
185 { "itemsep", LT_ITEMSEP },
186 { "itemtag", LT_ITEMTAG },
187 { "keepempty", LT_KEEPEMPTY },
188 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
189 { "labelcounter", LT_LABELCOUNTER },
190 { "labelfont", LT_LABELFONT },
191 { "labelindent", LT_LABELINDENT },
192 { "labelsep", LT_LABELSEP },
193 { "labelstring", LT_LABELSTRING },
194 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
195 { "labeltag", LT_LABELTAG },
196 { "labeltype", LT_LABELTYPE },
197 { "langpreamble", LT_LANGPREAMBLE },
198 { "latexname", LT_LATEXNAME },
199 { "latexparam", LT_LATEXPARAM },
200 { "latextype", LT_LATEXTYPE },
201 { "leftmargin", LT_LEFTMARGIN },
202 { "margin", LT_MARGIN },
203 { "needprotect", LT_NEED_PROTECT },
204 { "newline", LT_NEWLINE },
205 { "nextnoindent", LT_NEXTNOINDENT },
206 { "obsoletedby", LT_OBSOLETEDBY },
207 { "optionalargs", LT_OPTARGS },
208 { "parindent", LT_PARINDENT },
209 { "parsep", LT_PARSEP },
210 { "parskip", LT_PARSKIP },
211 { "passthru", LT_PASS_THRU },
212 { "preamble", LT_PREAMBLE },
213 { "requires", LT_REQUIRES },
214 { "rightmargin", LT_RIGHTMARGIN },
215 { "spacing", LT_SPACING },
216 { "textfont", LT_TEXTFONT },
217 { "toclevel", LT_TOCLEVEL },
218 { "topsep", LT_TOPSEP }
222 bool finished = false;
223 lex.pushTable(layoutTags);
224 // parse style section
225 while (!finished && lex.isOK() && !error) {
227 // See comment in LyXRC.cpp.
229 case Lexer::LEX_FEOF:
232 case Lexer::LEX_UNDEF: // parse error
233 lex.printError("Unknown layout tag `$$Token'");
240 switch (static_cast<LayoutTags>(le)) {
241 case LT_END: // end of structure
249 case LT_COPYSTYLE: { // initialize with a known style
252 style = subst(style, '_', ' ');
254 if (tclass.hasLayout(style)) {
255 docstring const tmpname = name_;
256 this->operator=(tclass[style]);
259 LYXERR0("Cannot copy unknown style `"
261 << "All layouts so far:");
262 DocumentClass::const_iterator lit = tclass.begin();
263 DocumentClass::const_iterator len = tclass.end();
264 for (; lit != len; ++lit)
265 LYXERR0(lit->name());
270 case LT_OBSOLETEDBY: { // replace with a known style
273 style = subst(style, '_', ' ');
275 if (tclass.hasLayout(style)) {
276 docstring const tmpname = name_;
277 this->operator=(tclass[style]);
279 if (obsoleted_by().empty())
280 obsoleted_by_ = style;
282 LYXERR0("Cannot replace with unknown style `"
285 //lex.printError("Cannot replace with"
294 depends_on_ = subst(depends_on_, '_', ' ');
297 case LT_MARGIN: // margin style definition.
301 case LT_LATEXTYPE: // LaTeX style definition.
318 lex >> optionalargs ;
321 case LT_NEED_PROTECT:
330 font = lyxRead(lex, font);
335 font = lyxRead(lex, font);
339 labelfont = lyxRead(lex, labelfont);
342 case LT_NEXTNOINDENT: // Indent next paragraph?
346 case LT_COMMANDDEPTH:
356 latexparam_ = subst(latexparam_, """, "\"");
372 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
375 case LT_LANGPREAMBLE:
376 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
379 case LT_BABELPREAMBLE:
380 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
387 case LT_ENDLABELTYPE:
388 readEndLabelType(lex);
391 case LT_LEFTMARGIN: // left margin type
395 case LT_RIGHTMARGIN: // right margin type
399 case LT_LABELINDENT: // label indenting flag
403 case LT_PARINDENT: // paragraph indent. flag
407 case LT_PARSKIP: // paragraph skip size
411 case LT_ITEMSEP: // item separation size
415 case LT_TOPSEP: // top separation size
419 case LT_BOTTOMSEP: // bottom separation size
423 case LT_LABEL_BOTTOMSEP: // label bottom separation size
424 lex >> labelbottomsep;
427 case LT_LABELSEP: // label separator
429 labelsep = subst(labelsep, 'x', ' ');
432 case LT_PARSEP: // par. separation size
436 case LT_FILL_TOP: // fill top flag
440 case LT_FILL_BOTTOM: // fill bottom flag
444 case LT_NEWLINE: // newlines allowed?
445 lex >> newline_allowed;
448 case LT_ALIGN: // paragraph align
451 case LT_ALIGNPOSSIBLE: // paragraph allowed align
452 readAlignPossible(lex);
455 case LT_LABELSTRING: // label string definition
456 // FIXME: this means LT_ENDLABELSTRING may only
457 // occur after LT_LABELSTRING
459 labelstring_ = trim(labelstring_);
460 labelstring_appendix_ = labelstring_;
463 case LT_ENDLABELSTRING: // endlabel string definition
464 lex >> endlabelstring_;
465 endlabelstring_ = trim(endlabelstring_);
468 case LT_LABELSTRING_APPENDIX: // label string appendix definition
469 lex >> labelstring_appendix_;
470 labelstring_appendix_ = trim(labelstring_appendix_);
473 case LT_LABELCOUNTER: // name of counter to use
475 counter = trim(counter);
478 case LT_FREE_SPACING: // Allow for free spacing.
482 case LT_PASS_THRU: // Allow for pass thru.
486 case LT_SPACING: // setspace.sty
492 vector<string> const req =
493 getVectorFromString(lex.getString());
494 requires_.insert(req.begin(), req.end());
510 case LT_HTMLITEMATTR:
511 lex >> htmlitemattr_;
515 lex >> htmllabeltag_;
518 case LT_HTMLLABELATTR:
519 lex >> htmllabelattr_;
522 case LT_HTMLLABELFIRST:
523 lex >> htmllabelfirst_;
527 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
530 case LT_HTMLFORCECSS:
531 lex >> htmlforcecss_;
533 case LT_HTMLPREAMBLE:
534 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
557 LexerKeyword alignTags[] = {
558 { "block", AT_BLOCK },
559 { "center", AT_CENTER },
560 { "layout", AT_LAYOUT },
562 { "right", AT_RIGHT }
566 void Layout::readAlign(Lexer & lex)
568 PushPopHelper pph(lex, alignTags);
571 case Lexer::LEX_UNDEF:
572 lex.printError("Unknown alignment `$$Token'");
578 align = LYX_ALIGN_BLOCK;
581 align = LYX_ALIGN_LEFT;
584 align = LYX_ALIGN_RIGHT;
587 align = LYX_ALIGN_CENTER;
590 align = LYX_ALIGN_LAYOUT;
596 void Layout::readAlignPossible(Lexer & lex)
598 lex.pushTable(alignTags);
599 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
600 int lineno = lex.lineNumber();
604 case Lexer::LEX_UNDEF:
605 lex.printError("Unknown alignment `$$Token'");
611 alignpossible |= LYX_ALIGN_BLOCK;
614 alignpossible |= LYX_ALIGN_LEFT;
617 alignpossible |= LYX_ALIGN_RIGHT;
620 alignpossible |= LYX_ALIGN_CENTER;
623 alignpossible |= LYX_ALIGN_LAYOUT;
626 } while (lineno == lex.lineNumber());
631 void Layout::readLabelType(Lexer & lex)
637 LA_CENTERED_TOP_ENVIRONMENT,
647 LexerKeyword labelTypeTags[] = {
648 { "bibliography", LA_BIBLIO },
649 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
650 { "counter", LA_COUNTER },
651 { "enumerate", LA_ENUMERATE },
652 { "itemize", LA_ITEMIZE },
653 { "manual", LA_MANUAL },
654 { "no_label", LA_NO_LABEL },
655 { "sensitive", LA_SENSITIVE },
656 { "static", LA_STATIC },
657 { "top_environment", LA_TOP_ENVIRONMENT }
660 PushPopHelper pph(lex, labelTypeTags);
663 case Lexer::LEX_UNDEF:
664 lex.printError("Unknown labeltype tag `$$Token'");
670 labeltype = LABEL_NO_LABEL;
673 labeltype = LABEL_MANUAL;
675 case LA_TOP_ENVIRONMENT:
676 labeltype = LABEL_TOP_ENVIRONMENT;
678 case LA_CENTERED_TOP_ENVIRONMENT:
679 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
682 labeltype = LABEL_STATIC;
685 labeltype = LABEL_SENSITIVE;
688 labeltype = LABEL_COUNTER;
691 labeltype = LABEL_ENUMERATE;
694 labeltype = LABEL_ITEMIZE;
697 labeltype = LABEL_BIBLIO;
703 void Layout::readEndLabelType(Lexer & lex)
705 static LexerKeyword endlabelTypeTags[] = {
706 { "box", END_LABEL_BOX },
707 { "filled_box", END_LABEL_FILLED_BOX },
708 { "no_label", END_LABEL_NO_LABEL },
709 { "static", END_LABEL_STATIC }
712 PushPopHelper pph(lex, endlabelTypeTags);
715 case Lexer::LEX_UNDEF:
716 lex.printError("Unknown labeltype tag `$$Token'");
718 case END_LABEL_STATIC:
720 case END_LABEL_FILLED_BOX:
721 case END_LABEL_NO_LABEL:
722 endlabeltype = static_cast<EndLabelType>(le);
725 LYXERR0("Unhandled value " << le);
731 void Layout::readMargin(Lexer & lex)
733 LexerKeyword marginTags[] = {
734 { "dynamic", MARGIN_DYNAMIC },
735 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
736 { "manual", MARGIN_MANUAL },
737 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
738 { "static", MARGIN_STATIC }
741 PushPopHelper pph(lex, marginTags);
745 case Lexer::LEX_UNDEF:
746 lex.printError("Unknown margin type tag `$$Token'");
751 case MARGIN_FIRST_DYNAMIC:
752 case MARGIN_RIGHT_ADDRESS_BOX:
753 margintype = static_cast<MarginType>(le);
756 LYXERR0("Unhandled value " << le);
762 void Layout::readLatexType(Lexer & lex)
764 LexerKeyword latexTypeTags[] = {
765 { "bib_environment", LATEX_BIB_ENVIRONMENT },
766 { "command", LATEX_COMMAND },
767 { "environment", LATEX_ENVIRONMENT },
768 { "item_environment", LATEX_ITEM_ENVIRONMENT },
769 { "list_environment", LATEX_LIST_ENVIRONMENT },
770 { "paragraph", LATEX_PARAGRAPH }
773 PushPopHelper pph(lex, latexTypeTags);
776 case Lexer::LEX_UNDEF:
777 lex.printError("Unknown latextype tag `$$Token'");
779 case LATEX_PARAGRAPH:
781 case LATEX_ENVIRONMENT:
782 case LATEX_ITEM_ENVIRONMENT:
783 case LATEX_BIB_ENVIRONMENT:
784 case LATEX_LIST_ENVIRONMENT:
785 latextype = static_cast<LatexType>(le);
788 LYXERR0("Unhandled value " << le);
794 void Layout::readSpacing(Lexer & lex)
797 ST_SPACING_SINGLE = 1,
803 LexerKeyword spacingTags[] = {
804 {"double", ST_SPACING_DOUBLE },
805 {"onehalf", ST_SPACING_ONEHALF },
806 {"other", ST_OTHER },
807 {"single", ST_SPACING_SINGLE }
810 PushPopHelper pph(lex, spacingTags);
813 case Lexer::LEX_UNDEF:
814 lex.printError("Unknown spacing token `$$Token'");
819 case ST_SPACING_SINGLE:
820 spacing.set(Spacing::Single);
822 case ST_SPACING_ONEHALF:
823 spacing.set(Spacing::Onehalf);
825 case ST_SPACING_DOUBLE:
826 spacing.set(Spacing::Double);
830 spacing.set(Spacing::Other, lex.getString());
838 docstring const i18npreamble(Language const * lang, docstring const & templ)
843 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
846 // tex2lyx does not have getMessages()
847 LASSERT(false, /**/);
850 // boost::regex is not unicode-safe.
851 // Should use QRegExp or (boost::u32regex, but that requires ICU)
852 static boost::regex const reg("_\\(([^\\)]+)\\)");
854 while (boost::regex_search(preamble, sub, reg)) {
855 string const key = sub.str(1);
858 translated = to_utf8(getMessages(lang->code()).get(key));
860 lyxerr << "Warning: not translating `" << key
861 << "' because it is not pure ASCII." << endl;
864 preamble = subst(preamble, sub.str(), translated);
867 return from_utf8(preamble);
873 docstring const Layout::langpreamble(Language const * lang) const
875 return i18npreamble(lang, langpreamble_);
879 docstring const Layout::babelpreamble(Language const * lang) const
881 return i18npreamble(lang, babelpreamble_);
885 string const & Layout::htmltag() const
887 if (htmltag_.empty())
893 string const & Layout::htmlattr() const
895 if (htmlattr_.empty())
896 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
901 string const & Layout::htmlitemtag() const
903 if (htmlitemtag_.empty())
904 htmlitemtag_ = "div";
909 string const & Layout::htmlitemattr() const
911 if (htmlitemattr_.empty())
912 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
913 return htmlitemattr_;
917 string const & Layout::htmllabeltag() const
919 if (htmllabeltag_.empty())
920 htmllabeltag_ = "span";
921 return htmllabeltag_;
925 string const & Layout::htmllabelattr() const
927 if (htmllabelattr_.empty())
928 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
929 return htmllabelattr_;
933 docstring Layout::htmlstyle() const {
934 if (!htmlstyle_.empty() && !htmlforcecss_)
936 if (htmldefaultstyle_.empty())
938 docstring retval = htmldefaultstyle_;
939 if (!htmlstyle_.empty())
940 retval += '\n' + htmlstyle_;
945 string Layout::defaultCSSClass() const
947 if (!defaultcssclass_.empty())
948 return defaultcssclass_;
950 docstring::const_iterator it = name().begin();
951 docstring::const_iterator en = name().end();
952 for (; it != en; ++it) {
960 // are there other characters we need to remove?
961 defaultcssclass_ = to_utf8(d);
962 return defaultcssclass_;
966 // NOTE There is a whole ton of stuff that could go into this.
967 // Things like bottomsep, topsep, and parsep could become various
968 // sorts of margins or padding, for example. But for now we are
969 // going to keep it simple.
970 void Layout::makeDefaultCSS() const {
971 // this never needs to be redone, since reloading layouts will
972 // wipe out what we did before.
973 if (!htmldefaultstyle_.empty())
975 docstring const mainfontCSS = font.asCSS();
976 if (!mainfontCSS.empty())
978 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
979 mainfontCSS + from_ascii("\n}\n");
980 if (labelfont == font || htmllabeltag() == "NONE")
982 docstring const labelfontCSS = labelfont.asCSS();
983 if (!labelfontCSS.empty())
985 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
986 labelfontCSS + from_ascii("\n}\n");
990 bool Layout::operator==(Layout const & rhs) const
992 // This is enough for the applications we actually make,
993 // at least at the moment. But we could check more.
994 return name() == rhs.name()
995 && latexname() == rhs.latexname()
996 && latextype == rhs.latextype;