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,
109 LT_INTITLE // keep this last!
112 /////////////////////
117 margintype = MARGIN_STATIC;
118 latextype = LATEX_PARAGRAPH;
125 labelfont = inherit_font;
127 reslabelfont = sane_font;
128 nextnoindent = false;
133 labelbottomsep = 0.0;
135 align = LYX_ALIGN_BLOCK;
136 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
137 labeltype = LABEL_NO_LABEL;
138 endlabeltype = END_LABEL_NO_LABEL;
139 // Should or should not. That is the question.
140 // spacing.set(Spacing::OneHalf);
143 newline_allowed = true;
144 free_spacing = false;
146 toclevel = NOT_IN_TOC;
148 htmllabelfirst_ = false;
149 htmlforcecss_ = false;
155 bool Layout::read(Lexer & lex, TextClass const & tclass)
157 // This table is sorted alphabetically [asierra 30March96]
158 LexerKeyword layoutTags[] = {
159 { "align", LT_ALIGN },
160 { "alignpossible", LT_ALIGNPOSSIBLE },
161 { "babelpreamble", LT_BABELPREAMBLE },
162 { "bottomsep", LT_BOTTOMSEP },
163 { "category", LT_CATEGORY },
164 { "commanddepth", LT_COMMANDDEPTH },
165 { "copystyle", LT_COPYSTYLE },
166 { "dependson", LT_DEPENDSON },
168 { "endlabelstring", LT_ENDLABELSTRING },
169 { "endlabeltype", LT_ENDLABELTYPE },
170 { "fill_bottom", LT_FILL_BOTTOM },
171 { "fill_top", LT_FILL_TOP },
173 { "freespacing", LT_FREE_SPACING },
174 { "htmlattr", LT_HTMLATTR },
175 { "htmlforcecss", LT_HTMLFORCECSS },
176 { "htmlitem", LT_HTMLITEM },
177 { "htmlitemattr", LT_HTMLITEMATTR },
178 { "htmllabel", LT_HTMLLABEL },
179 { "htmllabelattr", LT_HTMLLABELATTR },
180 { "htmllabelfirst", LT_HTMLLABELFIRST },
181 { "htmlpremable", LT_HTMLPREAMBLE },
182 { "htmlstyle", LT_HTMLSTYLE },
183 { "htmltag", LT_HTMLTAG },
184 { "htmltitle", LT_HTMLTITLE },
185 { "innertag", LT_INNERTAG },
186 { "inpreamble", LT_INPREAMBLE },
187 { "intitle", LT_INTITLE },
188 { "itemsep", LT_ITEMSEP },
189 { "itemtag", LT_ITEMTAG },
190 { "keepempty", LT_KEEPEMPTY },
191 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
192 { "labelcounter", LT_LABELCOUNTER },
193 { "labelfont", LT_LABELFONT },
194 { "labelindent", LT_LABELINDENT },
195 { "labelsep", LT_LABELSEP },
196 { "labelstring", LT_LABELSTRING },
197 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
198 { "labeltag", LT_LABELTAG },
199 { "labeltype", LT_LABELTYPE },
200 { "langpreamble", LT_LANGPREAMBLE },
201 { "latexname", LT_LATEXNAME },
202 { "latexparam", LT_LATEXPARAM },
203 { "latextype", LT_LATEXTYPE },
204 { "leftmargin", LT_LEFTMARGIN },
205 { "margin", LT_MARGIN },
206 { "needprotect", LT_NEED_PROTECT },
207 { "newline", LT_NEWLINE },
208 { "nextnoindent", LT_NEXTNOINDENT },
209 { "obsoletedby", LT_OBSOLETEDBY },
210 { "optionalargs", LT_OPTARGS },
211 { "parindent", LT_PARINDENT },
212 { "parsep", LT_PARSEP },
213 { "parskip", LT_PARSKIP },
214 { "passthru", LT_PASS_THRU },
215 { "preamble", LT_PREAMBLE },
216 { "refprefix", LT_REFPREFIX },
217 { "requires", LT_REQUIRES },
218 { "rightmargin", LT_RIGHTMARGIN },
219 { "spacing", LT_SPACING },
220 { "spellcheck", LT_SPELLCHECK },
221 { "textfont", LT_TEXTFONT },
222 { "toclevel", LT_TOCLEVEL },
223 { "topsep", LT_TOPSEP }
227 bool finished = false;
228 lex.pushTable(layoutTags);
229 // parse style section
230 while (!finished && lex.isOK() && !error) {
232 // See comment in LyXRC.cpp.
234 case Lexer::LEX_FEOF:
237 case Lexer::LEX_UNDEF: // parse error
238 lex.printError("Unknown layout tag `$$Token'");
245 switch (static_cast<LayoutTags>(le)) {
246 case LT_END: // end of structure
254 case LT_COPYSTYLE: { // initialize with a known style
257 style = subst(style, '_', ' ');
259 if (tclass.hasLayout(style)) {
260 docstring const tmpname = name_;
261 this->operator=(tclass[style]);
264 LYXERR0("Cannot copy unknown style `"
266 << "All layouts so far:");
267 DocumentClass::const_iterator lit = tclass.begin();
268 DocumentClass::const_iterator len = tclass.end();
269 for (; lit != len; ++lit)
270 LYXERR0(lit->name());
275 case LT_OBSOLETEDBY: { // replace with a known style
278 style = subst(style, '_', ' ');
280 if (tclass.hasLayout(style)) {
281 docstring const tmpname = name_;
282 this->operator=(tclass[style]);
284 if (obsoleted_by().empty())
285 obsoleted_by_ = style;
287 LYXERR0("Cannot replace with unknown style `"
290 //lex.printError("Cannot replace with"
299 depends_on_ = subst(depends_on_, '_', ' ');
302 case LT_MARGIN: // margin style definition.
306 case LT_LATEXTYPE: // LaTeX style definition.
323 lex >> optionalargs ;
326 case LT_NEED_PROTECT:
335 font = lyxRead(lex, font);
340 font = lyxRead(lex, font);
344 labelfont = lyxRead(lex, labelfont);
347 case LT_NEXTNOINDENT: // Indent next paragraph?
351 case LT_COMMANDDEPTH:
361 latexparam_ = subst(latexparam_, """, "\"");
377 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
380 case LT_LANGPREAMBLE:
381 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
384 case LT_BABELPREAMBLE:
385 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
392 case LT_ENDLABELTYPE:
393 readEndLabelType(lex);
396 case LT_LEFTMARGIN: // left margin type
400 case LT_RIGHTMARGIN: // right margin type
404 case LT_LABELINDENT: // label indenting flag
408 case LT_PARINDENT: // paragraph indent. flag
412 case LT_PARSKIP: // paragraph skip size
416 case LT_ITEMSEP: // item separation size
420 case LT_TOPSEP: // top separation size
424 case LT_BOTTOMSEP: // bottom separation size
428 case LT_LABEL_BOTTOMSEP: // label bottom separation size
429 lex >> labelbottomsep;
432 case LT_LABELSEP: // label separator
434 labelsep = subst(labelsep, 'x', ' ');
437 case LT_PARSEP: // par. separation size
441 case LT_FILL_TOP: // fill top flag
445 case LT_FILL_BOTTOM: // fill bottom flag
449 case LT_NEWLINE: // newlines allowed?
450 lex >> newline_allowed;
453 case LT_ALIGN: // paragraph align
456 case LT_ALIGNPOSSIBLE: // paragraph allowed align
457 readAlignPossible(lex);
460 case LT_LABELSTRING: // label string definition
461 // FIXME: this means LT_ENDLABELSTRING may only
462 // occur after LT_LABELSTRING
464 labelstring_ = trim(labelstring_);
465 labelstring_appendix_ = labelstring_;
468 case LT_ENDLABELSTRING: // endlabel string definition
469 lex >> endlabelstring_;
470 endlabelstring_ = trim(endlabelstring_);
473 case LT_LABELSTRING_APPENDIX: // label string appendix definition
474 lex >> labelstring_appendix_;
475 labelstring_appendix_ = trim(labelstring_appendix_);
478 case LT_LABELCOUNTER: // name of counter to use
480 counter = trim(counter);
483 case LT_FREE_SPACING: // Allow for free spacing.
487 case LT_PASS_THRU: // Allow for pass thru.
491 case LT_SPACING: // setspace.sty
497 vector<string> const req =
498 getVectorFromString(lex.getString());
499 requires_.insert(req.begin(), req.end());
519 case LT_HTMLITEMATTR:
520 lex >> htmlitemattr_;
524 lex >> htmllabeltag_;
527 case LT_HTMLLABELATTR:
528 lex >> htmllabelattr_;
531 case LT_HTMLLABELFIRST:
532 lex >> htmllabelfirst_;
536 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
539 case LT_HTMLFORCECSS:
540 lex >> htmlforcecss_;
542 case LT_HTMLPREAMBLE:
543 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
570 LexerKeyword alignTags[] = {
571 { "block", AT_BLOCK },
572 { "center", AT_CENTER },
573 { "layout", AT_LAYOUT },
575 { "right", AT_RIGHT }
579 void Layout::readAlign(Lexer & lex)
581 PushPopHelper pph(lex, alignTags);
584 case Lexer::LEX_UNDEF:
585 lex.printError("Unknown alignment `$$Token'");
591 align = LYX_ALIGN_BLOCK;
594 align = LYX_ALIGN_LEFT;
597 align = LYX_ALIGN_RIGHT;
600 align = LYX_ALIGN_CENTER;
603 align = LYX_ALIGN_LAYOUT;
609 void Layout::readAlignPossible(Lexer & lex)
611 lex.pushTable(alignTags);
612 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
613 int lineno = lex.lineNumber();
617 case Lexer::LEX_UNDEF:
618 lex.printError("Unknown alignment `$$Token'");
624 alignpossible |= LYX_ALIGN_BLOCK;
627 alignpossible |= LYX_ALIGN_LEFT;
630 alignpossible |= LYX_ALIGN_RIGHT;
633 alignpossible |= LYX_ALIGN_CENTER;
636 alignpossible |= LYX_ALIGN_LAYOUT;
639 } while (lineno == lex.lineNumber());
644 void Layout::readLabelType(Lexer & lex)
650 LA_CENTERED_TOP_ENVIRONMENT,
660 LexerKeyword labelTypeTags[] = {
661 { "bibliography", LA_BIBLIO },
662 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
663 { "counter", LA_COUNTER },
664 { "enumerate", LA_ENUMERATE },
665 { "itemize", LA_ITEMIZE },
666 { "manual", LA_MANUAL },
667 { "no_label", LA_NO_LABEL },
668 { "sensitive", LA_SENSITIVE },
669 { "static", LA_STATIC },
670 { "top_environment", LA_TOP_ENVIRONMENT }
673 PushPopHelper pph(lex, labelTypeTags);
676 case Lexer::LEX_UNDEF:
677 lex.printError("Unknown labeltype tag `$$Token'");
683 labeltype = LABEL_NO_LABEL;
686 labeltype = LABEL_MANUAL;
688 case LA_TOP_ENVIRONMENT:
689 labeltype = LABEL_TOP_ENVIRONMENT;
691 case LA_CENTERED_TOP_ENVIRONMENT:
692 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
695 labeltype = LABEL_STATIC;
698 labeltype = LABEL_SENSITIVE;
701 labeltype = LABEL_COUNTER;
704 labeltype = LABEL_ENUMERATE;
707 labeltype = LABEL_ITEMIZE;
710 labeltype = LABEL_BIBLIO;
716 void Layout::readEndLabelType(Lexer & lex)
718 static LexerKeyword endlabelTypeTags[] = {
719 { "box", END_LABEL_BOX },
720 { "filled_box", END_LABEL_FILLED_BOX },
721 { "no_label", END_LABEL_NO_LABEL },
722 { "static", END_LABEL_STATIC }
725 PushPopHelper pph(lex, endlabelTypeTags);
728 case Lexer::LEX_UNDEF:
729 lex.printError("Unknown labeltype tag `$$Token'");
731 case END_LABEL_STATIC:
733 case END_LABEL_FILLED_BOX:
734 case END_LABEL_NO_LABEL:
735 endlabeltype = static_cast<EndLabelType>(le);
738 LYXERR0("Unhandled value " << le);
744 void Layout::readMargin(Lexer & lex)
746 LexerKeyword marginTags[] = {
747 { "dynamic", MARGIN_DYNAMIC },
748 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
749 { "manual", MARGIN_MANUAL },
750 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
751 { "static", MARGIN_STATIC }
754 PushPopHelper pph(lex, marginTags);
758 case Lexer::LEX_UNDEF:
759 lex.printError("Unknown margin type tag `$$Token'");
764 case MARGIN_FIRST_DYNAMIC:
765 case MARGIN_RIGHT_ADDRESS_BOX:
766 margintype = static_cast<MarginType>(le);
769 LYXERR0("Unhandled value " << le);
775 void Layout::readLatexType(Lexer & lex)
777 LexerKeyword latexTypeTags[] = {
778 { "bib_environment", LATEX_BIB_ENVIRONMENT },
779 { "command", LATEX_COMMAND },
780 { "environment", LATEX_ENVIRONMENT },
781 { "item_environment", LATEX_ITEM_ENVIRONMENT },
782 { "list_environment", LATEX_LIST_ENVIRONMENT },
783 { "paragraph", LATEX_PARAGRAPH }
786 PushPopHelper pph(lex, latexTypeTags);
789 case Lexer::LEX_UNDEF:
790 lex.printError("Unknown latextype tag `$$Token'");
792 case LATEX_PARAGRAPH:
794 case LATEX_ENVIRONMENT:
795 case LATEX_ITEM_ENVIRONMENT:
796 case LATEX_BIB_ENVIRONMENT:
797 case LATEX_LIST_ENVIRONMENT:
798 latextype = static_cast<LatexType>(le);
801 LYXERR0("Unhandled value " << le);
807 void Layout::readSpacing(Lexer & lex)
810 ST_SPACING_SINGLE = 1,
816 LexerKeyword spacingTags[] = {
817 {"double", ST_SPACING_DOUBLE },
818 {"onehalf", ST_SPACING_ONEHALF },
819 {"other", ST_OTHER },
820 {"single", ST_SPACING_SINGLE }
823 PushPopHelper pph(lex, spacingTags);
826 case Lexer::LEX_UNDEF:
827 lex.printError("Unknown spacing token `$$Token'");
832 case ST_SPACING_SINGLE:
833 spacing.set(Spacing::Single);
835 case ST_SPACING_ONEHALF:
836 spacing.set(Spacing::Onehalf);
838 case ST_SPACING_DOUBLE:
839 spacing.set(Spacing::Double);
843 spacing.set(Spacing::Other, lex.getString());
851 docstring const i18npreamble(Language const * lang, docstring const & templ)
856 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
859 // tex2lyx does not have getMessages()
860 LASSERT(false, /**/);
863 // boost::regex is not unicode-safe.
864 // Should use QRegExp or (boost::u32regex, but that requires ICU)
865 static boost::regex const reg("_\\(([^\\)]+)\\)");
867 while (boost::regex_search(preamble, sub, reg)) {
868 string const key = sub.str(1);
871 translated = to_utf8(getMessages(lang->code()).get(key));
873 lyxerr << "Warning: not translating `" << key
874 << "' because it is not pure ASCII." << endl;
877 preamble = subst(preamble, sub.str(), translated);
880 return from_utf8(preamble);
886 docstring const Layout::langpreamble(Language const * lang) const
888 return i18npreamble(lang, langpreamble_);
892 docstring const Layout::babelpreamble(Language const * lang) const
894 return i18npreamble(lang, babelpreamble_);
898 string const & Layout::htmltag() const
900 if (htmltag_.empty())
906 string const & Layout::htmlattr() const
908 if (htmlattr_.empty())
909 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
914 string const & Layout::htmlitemtag() const
916 if (htmlitemtag_.empty())
917 htmlitemtag_ = "div";
922 string const & Layout::htmlitemattr() const
924 if (htmlitemattr_.empty())
925 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
926 return htmlitemattr_;
930 string const & Layout::htmllabeltag() const
932 if (htmllabeltag_.empty())
933 htmllabeltag_ = "span";
934 return htmllabeltag_;
938 string const & Layout::htmllabelattr() const
940 if (htmllabelattr_.empty())
941 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
942 return htmllabelattr_;
946 docstring Layout::htmlstyle() const {
947 if (!htmlstyle_.empty() && !htmlforcecss_)
949 if (htmldefaultstyle_.empty())
951 docstring retval = htmldefaultstyle_;
952 if (!htmlstyle_.empty())
953 retval += '\n' + htmlstyle_;
958 string Layout::defaultCSSClass() const
960 if (!defaultcssclass_.empty())
961 return defaultcssclass_;
963 docstring::const_iterator it = name().begin();
964 docstring::const_iterator en = name().end();
965 for (; it != en; ++it) {
973 // are there other characters we need to remove?
974 defaultcssclass_ = to_utf8(d);
975 return defaultcssclass_;
979 // NOTE There is a whole ton of stuff that could go into this.
980 // Things like bottomsep, topsep, and parsep could become various
981 // sorts of margins or padding, for example. But for now we are
982 // going to keep it simple.
983 void Layout::makeDefaultCSS() const {
984 // this never needs to be redone, since reloading layouts will
985 // wipe out what we did before.
986 if (!htmldefaultstyle_.empty())
988 docstring const mainfontCSS = font.asCSS();
989 if (!mainfontCSS.empty())
991 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
992 mainfontCSS + from_ascii("\n}\n");
993 if (labelfont == font || htmllabeltag() == "NONE")
995 docstring const labelfontCSS = labelfont.asCSS();
996 if (!labelfontCSS.empty())
998 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
999 labelfontCSS + from_ascii("\n}\n");
1003 bool Layout::operator==(Layout const & rhs) const
1005 // This is enough for the applications we actually make,
1006 // at least at the moment. But we could check more.
1007 return name() == rhs.name()
1008 && latexname() == rhs.latexname()
1009 && latextype == rhs.latextype;