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,
108 LT_INTITLE // keep this last!
111 /////////////////////
116 margintype = MARGIN_STATIC;
117 latextype = LATEX_PARAGRAPH;
124 labelfont = inherit_font;
126 reslabelfont = sane_font;
127 nextnoindent = false;
132 labelbottomsep = 0.0;
134 align = LYX_ALIGN_BLOCK;
135 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
136 labeltype = LABEL_NO_LABEL;
137 endlabeltype = END_LABEL_NO_LABEL;
138 // Should or should not. That is the question.
139 // spacing.set(Spacing::OneHalf);
142 newline_allowed = true;
143 free_spacing = false;
145 toclevel = NOT_IN_TOC;
147 htmllabelfirst_ = false;
148 htmlforcecss_ = false;
154 bool Layout::read(Lexer & lex, TextClass const & tclass)
156 // This table is sorted alphabetically [asierra 30March96]
157 LexerKeyword layoutTags[] = {
158 { "align", LT_ALIGN },
159 { "alignpossible", LT_ALIGNPOSSIBLE },
160 { "babelpreamble", LT_BABELPREAMBLE },
161 { "bottomsep", LT_BOTTOMSEP },
162 { "category", LT_CATEGORY },
163 { "commanddepth", LT_COMMANDDEPTH },
164 { "copystyle", LT_COPYSTYLE },
165 { "dependson", LT_DEPENDSON },
167 { "endlabelstring", LT_ENDLABELSTRING },
168 { "endlabeltype", LT_ENDLABELTYPE },
169 { "fill_bottom", LT_FILL_BOTTOM },
170 { "fill_top", LT_FILL_TOP },
172 { "freespacing", LT_FREE_SPACING },
173 { "htmlattr", LT_HTMLATTR },
174 { "htmlforcecss", LT_HTMLFORCECSS },
175 { "htmlitem", LT_HTMLITEM },
176 { "htmlitemattr", LT_HTMLITEMATTR },
177 { "htmllabel", LT_HTMLLABEL },
178 { "htmllabelattr", LT_HTMLLABELATTR },
179 { "htmllabelfirst", LT_HTMLLABELFIRST },
180 { "htmlpremable", LT_HTMLPREAMBLE },
181 { "htmlstyle", LT_HTMLSTYLE },
182 { "htmltag", LT_HTMLTAG },
183 { "htmltitle", LT_HTMLTITLE },
184 { "innertag", LT_INNERTAG },
185 { "inpreamble", LT_INPREAMBLE },
186 { "intitle", LT_INTITLE },
187 { "itemsep", LT_ITEMSEP },
188 { "itemtag", LT_ITEMTAG },
189 { "keepempty", LT_KEEPEMPTY },
190 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
191 { "labelcounter", LT_LABELCOUNTER },
192 { "labelfont", LT_LABELFONT },
193 { "labelindent", LT_LABELINDENT },
194 { "labelsep", LT_LABELSEP },
195 { "labelstring", LT_LABELSTRING },
196 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
197 { "labeltag", LT_LABELTAG },
198 { "labeltype", LT_LABELTYPE },
199 { "langpreamble", LT_LANGPREAMBLE },
200 { "latexname", LT_LATEXNAME },
201 { "latexparam", LT_LATEXPARAM },
202 { "latextype", LT_LATEXTYPE },
203 { "leftmargin", LT_LEFTMARGIN },
204 { "margin", LT_MARGIN },
205 { "needprotect", LT_NEED_PROTECT },
206 { "newline", LT_NEWLINE },
207 { "nextnoindent", LT_NEXTNOINDENT },
208 { "obsoletedby", LT_OBSOLETEDBY },
209 { "optionalargs", LT_OPTARGS },
210 { "parindent", LT_PARINDENT },
211 { "parsep", LT_PARSEP },
212 { "parskip", LT_PARSKIP },
213 { "passthru", LT_PASS_THRU },
214 { "preamble", LT_PREAMBLE },
215 { "requires", LT_REQUIRES },
216 { "rightmargin", LT_RIGHTMARGIN },
217 { "spacing", LT_SPACING },
218 { "spellcheck", LT_SPELLCHECK },
219 { "textfont", LT_TEXTFONT },
220 { "toclevel", LT_TOCLEVEL },
221 { "topsep", LT_TOPSEP }
225 bool finished = false;
226 lex.pushTable(layoutTags);
227 // parse style section
228 while (!finished && lex.isOK() && !error) {
230 // See comment in LyXRC.cpp.
232 case Lexer::LEX_FEOF:
235 case Lexer::LEX_UNDEF: // parse error
236 lex.printError("Unknown layout tag `$$Token'");
243 switch (static_cast<LayoutTags>(le)) {
244 case LT_END: // end of structure
252 case LT_COPYSTYLE: { // initialize with a known style
255 style = subst(style, '_', ' ');
257 if (tclass.hasLayout(style)) {
258 docstring const tmpname = name_;
259 this->operator=(tclass[style]);
262 LYXERR0("Cannot copy unknown style `"
264 << "All layouts so far:");
265 DocumentClass::const_iterator lit = tclass.begin();
266 DocumentClass::const_iterator len = tclass.end();
267 for (; lit != len; ++lit)
268 LYXERR0(lit->name());
273 case LT_OBSOLETEDBY: { // replace with a known style
276 style = subst(style, '_', ' ');
278 if (tclass.hasLayout(style)) {
279 docstring const tmpname = name_;
280 this->operator=(tclass[style]);
282 if (obsoleted_by().empty())
283 obsoleted_by_ = style;
285 LYXERR0("Cannot replace with unknown style `"
288 //lex.printError("Cannot replace with"
297 depends_on_ = subst(depends_on_, '_', ' ');
300 case LT_MARGIN: // margin style definition.
304 case LT_LATEXTYPE: // LaTeX style definition.
321 lex >> optionalargs ;
324 case LT_NEED_PROTECT:
333 font = lyxRead(lex, font);
338 font = lyxRead(lex, font);
342 labelfont = lyxRead(lex, labelfont);
345 case LT_NEXTNOINDENT: // Indent next paragraph?
349 case LT_COMMANDDEPTH:
359 latexparam_ = subst(latexparam_, """, "\"");
375 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
378 case LT_LANGPREAMBLE:
379 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
382 case LT_BABELPREAMBLE:
383 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
390 case LT_ENDLABELTYPE:
391 readEndLabelType(lex);
394 case LT_LEFTMARGIN: // left margin type
398 case LT_RIGHTMARGIN: // right margin type
402 case LT_LABELINDENT: // label indenting flag
406 case LT_PARINDENT: // paragraph indent. flag
410 case LT_PARSKIP: // paragraph skip size
414 case LT_ITEMSEP: // item separation size
418 case LT_TOPSEP: // top separation size
422 case LT_BOTTOMSEP: // bottom separation size
426 case LT_LABEL_BOTTOMSEP: // label bottom separation size
427 lex >> labelbottomsep;
430 case LT_LABELSEP: // label separator
432 labelsep = subst(labelsep, 'x', ' ');
435 case LT_PARSEP: // par. separation size
439 case LT_FILL_TOP: // fill top flag
443 case LT_FILL_BOTTOM: // fill bottom flag
447 case LT_NEWLINE: // newlines allowed?
448 lex >> newline_allowed;
451 case LT_ALIGN: // paragraph align
454 case LT_ALIGNPOSSIBLE: // paragraph allowed align
455 readAlignPossible(lex);
458 case LT_LABELSTRING: // label string definition
459 // FIXME: this means LT_ENDLABELSTRING may only
460 // occur after LT_LABELSTRING
462 labelstring_ = trim(labelstring_);
463 labelstring_appendix_ = labelstring_;
466 case LT_ENDLABELSTRING: // endlabel string definition
467 lex >> endlabelstring_;
468 endlabelstring_ = trim(endlabelstring_);
471 case LT_LABELSTRING_APPENDIX: // label string appendix definition
472 lex >> labelstring_appendix_;
473 labelstring_appendix_ = trim(labelstring_appendix_);
476 case LT_LABELCOUNTER: // name of counter to use
478 counter = trim(counter);
481 case LT_FREE_SPACING: // Allow for free spacing.
485 case LT_PASS_THRU: // Allow for pass thru.
489 case LT_SPACING: // setspace.sty
495 vector<string> const req =
496 getVectorFromString(lex.getString());
497 requires_.insert(req.begin(), req.end());
513 case LT_HTMLITEMATTR:
514 lex >> htmlitemattr_;
518 lex >> htmllabeltag_;
521 case LT_HTMLLABELATTR:
522 lex >> htmllabelattr_;
525 case LT_HTMLLABELFIRST:
526 lex >> htmllabelfirst_;
530 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
533 case LT_HTMLFORCECSS:
534 lex >> htmlforcecss_;
536 case LT_HTMLPREAMBLE:
537 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
564 LexerKeyword alignTags[] = {
565 { "block", AT_BLOCK },
566 { "center", AT_CENTER },
567 { "layout", AT_LAYOUT },
569 { "right", AT_RIGHT }
573 void Layout::readAlign(Lexer & lex)
575 PushPopHelper pph(lex, alignTags);
578 case Lexer::LEX_UNDEF:
579 lex.printError("Unknown alignment `$$Token'");
585 align = LYX_ALIGN_BLOCK;
588 align = LYX_ALIGN_LEFT;
591 align = LYX_ALIGN_RIGHT;
594 align = LYX_ALIGN_CENTER;
597 align = LYX_ALIGN_LAYOUT;
603 void Layout::readAlignPossible(Lexer & lex)
605 lex.pushTable(alignTags);
606 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
607 int lineno = lex.lineNumber();
611 case Lexer::LEX_UNDEF:
612 lex.printError("Unknown alignment `$$Token'");
618 alignpossible |= LYX_ALIGN_BLOCK;
621 alignpossible |= LYX_ALIGN_LEFT;
624 alignpossible |= LYX_ALIGN_RIGHT;
627 alignpossible |= LYX_ALIGN_CENTER;
630 alignpossible |= LYX_ALIGN_LAYOUT;
633 } while (lineno == lex.lineNumber());
638 void Layout::readLabelType(Lexer & lex)
644 LA_CENTERED_TOP_ENVIRONMENT,
654 LexerKeyword labelTypeTags[] = {
655 { "bibliography", LA_BIBLIO },
656 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
657 { "counter", LA_COUNTER },
658 { "enumerate", LA_ENUMERATE },
659 { "itemize", LA_ITEMIZE },
660 { "manual", LA_MANUAL },
661 { "no_label", LA_NO_LABEL },
662 { "sensitive", LA_SENSITIVE },
663 { "static", LA_STATIC },
664 { "top_environment", LA_TOP_ENVIRONMENT }
667 PushPopHelper pph(lex, labelTypeTags);
670 case Lexer::LEX_UNDEF:
671 lex.printError("Unknown labeltype tag `$$Token'");
677 labeltype = LABEL_NO_LABEL;
680 labeltype = LABEL_MANUAL;
682 case LA_TOP_ENVIRONMENT:
683 labeltype = LABEL_TOP_ENVIRONMENT;
685 case LA_CENTERED_TOP_ENVIRONMENT:
686 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
689 labeltype = LABEL_STATIC;
692 labeltype = LABEL_SENSITIVE;
695 labeltype = LABEL_COUNTER;
698 labeltype = LABEL_ENUMERATE;
701 labeltype = LABEL_ITEMIZE;
704 labeltype = LABEL_BIBLIO;
710 void Layout::readEndLabelType(Lexer & lex)
712 static LexerKeyword endlabelTypeTags[] = {
713 { "box", END_LABEL_BOX },
714 { "filled_box", END_LABEL_FILLED_BOX },
715 { "no_label", END_LABEL_NO_LABEL },
716 { "static", END_LABEL_STATIC }
719 PushPopHelper pph(lex, endlabelTypeTags);
722 case Lexer::LEX_UNDEF:
723 lex.printError("Unknown labeltype tag `$$Token'");
725 case END_LABEL_STATIC:
727 case END_LABEL_FILLED_BOX:
728 case END_LABEL_NO_LABEL:
729 endlabeltype = static_cast<EndLabelType>(le);
732 LYXERR0("Unhandled value " << le);
738 void Layout::readMargin(Lexer & lex)
740 LexerKeyword marginTags[] = {
741 { "dynamic", MARGIN_DYNAMIC },
742 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
743 { "manual", MARGIN_MANUAL },
744 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
745 { "static", MARGIN_STATIC }
748 PushPopHelper pph(lex, marginTags);
752 case Lexer::LEX_UNDEF:
753 lex.printError("Unknown margin type tag `$$Token'");
758 case MARGIN_FIRST_DYNAMIC:
759 case MARGIN_RIGHT_ADDRESS_BOX:
760 margintype = static_cast<MarginType>(le);
763 LYXERR0("Unhandled value " << le);
769 void Layout::readLatexType(Lexer & lex)
771 LexerKeyword latexTypeTags[] = {
772 { "bib_environment", LATEX_BIB_ENVIRONMENT },
773 { "command", LATEX_COMMAND },
774 { "environment", LATEX_ENVIRONMENT },
775 { "item_environment", LATEX_ITEM_ENVIRONMENT },
776 { "list_environment", LATEX_LIST_ENVIRONMENT },
777 { "paragraph", LATEX_PARAGRAPH }
780 PushPopHelper pph(lex, latexTypeTags);
783 case Lexer::LEX_UNDEF:
784 lex.printError("Unknown latextype tag `$$Token'");
786 case LATEX_PARAGRAPH:
788 case LATEX_ENVIRONMENT:
789 case LATEX_ITEM_ENVIRONMENT:
790 case LATEX_BIB_ENVIRONMENT:
791 case LATEX_LIST_ENVIRONMENT:
792 latextype = static_cast<LatexType>(le);
795 LYXERR0("Unhandled value " << le);
801 void Layout::readSpacing(Lexer & lex)
804 ST_SPACING_SINGLE = 1,
810 LexerKeyword spacingTags[] = {
811 {"double", ST_SPACING_DOUBLE },
812 {"onehalf", ST_SPACING_ONEHALF },
813 {"other", ST_OTHER },
814 {"single", ST_SPACING_SINGLE }
817 PushPopHelper pph(lex, spacingTags);
820 case Lexer::LEX_UNDEF:
821 lex.printError("Unknown spacing token `$$Token'");
826 case ST_SPACING_SINGLE:
827 spacing.set(Spacing::Single);
829 case ST_SPACING_ONEHALF:
830 spacing.set(Spacing::Onehalf);
832 case ST_SPACING_DOUBLE:
833 spacing.set(Spacing::Double);
837 spacing.set(Spacing::Other, lex.getString());
845 docstring const i18npreamble(Language const * lang, docstring const & templ)
850 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
853 // tex2lyx does not have getMessages()
854 LASSERT(false, /**/);
857 // boost::regex is not unicode-safe.
858 // Should use QRegExp or (boost::u32regex, but that requires ICU)
859 static boost::regex const reg("_\\(([^\\)]+)\\)");
861 while (boost::regex_search(preamble, sub, reg)) {
862 string const key = sub.str(1);
865 translated = to_utf8(getMessages(lang->code()).get(key));
867 lyxerr << "Warning: not translating `" << key
868 << "' because it is not pure ASCII." << endl;
871 preamble = subst(preamble, sub.str(), translated);
874 return from_utf8(preamble);
880 docstring const Layout::langpreamble(Language const * lang) const
882 return i18npreamble(lang, langpreamble_);
886 docstring const Layout::babelpreamble(Language const * lang) const
888 return i18npreamble(lang, babelpreamble_);
892 string const & Layout::htmltag() const
894 if (htmltag_.empty())
900 string const & Layout::htmlattr() const
902 if (htmlattr_.empty())
903 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
908 string const & Layout::htmlitemtag() const
910 if (htmlitemtag_.empty())
911 htmlitemtag_ = "div";
916 string const & Layout::htmlitemattr() const
918 if (htmlitemattr_.empty())
919 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
920 return htmlitemattr_;
924 string const & Layout::htmllabeltag() const
926 if (htmllabeltag_.empty())
927 htmllabeltag_ = "span";
928 return htmllabeltag_;
932 string const & Layout::htmllabelattr() const
934 if (htmllabelattr_.empty())
935 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
936 return htmllabelattr_;
940 docstring Layout::htmlstyle() const {
941 if (!htmlstyle_.empty() && !htmlforcecss_)
943 if (htmldefaultstyle_.empty())
945 docstring retval = htmldefaultstyle_;
946 if (!htmlstyle_.empty())
947 retval += '\n' + htmlstyle_;
952 string Layout::defaultCSSClass() const
954 if (!defaultcssclass_.empty())
955 return defaultcssclass_;
957 docstring::const_iterator it = name().begin();
958 docstring::const_iterator en = name().end();
959 for (; it != en; ++it) {
967 // are there other characters we need to remove?
968 defaultcssclass_ = to_utf8(d);
969 return defaultcssclass_;
973 // NOTE There is a whole ton of stuff that could go into this.
974 // Things like bottomsep, topsep, and parsep could become various
975 // sorts of margins or padding, for example. But for now we are
976 // going to keep it simple.
977 void Layout::makeDefaultCSS() const {
978 // this never needs to be redone, since reloading layouts will
979 // wipe out what we did before.
980 if (!htmldefaultstyle_.empty())
982 docstring const mainfontCSS = font.asCSS();
983 if (!mainfontCSS.empty())
985 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
986 mainfontCSS + from_ascii("\n}\n");
987 if (labelfont == font || htmllabeltag() == "NONE")
989 docstring const labelfontCSS = labelfont.asCSS();
990 if (!labelfontCSS.empty())
992 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
993 labelfontCSS + from_ascii("\n}\n");
997 bool Layout::operator==(Layout const & rhs) const
999 // This is enough for the applications we actually make,
1000 // at least at the moment. But we could check more.
1001 return name() == rhs.name()
1002 && latexname() == rhs.latexname()
1003 && latextype == rhs.latextype;