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,
106 LT_INTITLE // keep this last!
109 /////////////////////
114 margintype = MARGIN_STATIC;
115 latextype = LATEX_PARAGRAPH;
122 labelfont = inherit_font;
124 reslabelfont = sane_font;
125 nextnoindent = false;
130 labelbottomsep = 0.0;
132 align = LYX_ALIGN_BLOCK;
133 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
134 labeltype = LABEL_NO_LABEL;
135 endlabeltype = END_LABEL_NO_LABEL;
136 // Should or should not. That is the question.
137 // spacing.set(Spacing::OneHalf);
140 newline_allowed = true;
141 free_spacing = false;
143 toclevel = NOT_IN_TOC;
145 htmllabelfirst_ = false;
146 htmlforcecss_ = false;
150 bool Layout::read(Lexer & lex, TextClass const & tclass)
152 // This table is sorted alphabetically [asierra 30March96]
153 LexerKeyword layoutTags[] = {
154 { "align", LT_ALIGN },
155 { "alignpossible", LT_ALIGNPOSSIBLE },
156 { "babelpreamble", LT_BABELPREAMBLE },
157 { "bottomsep", LT_BOTTOMSEP },
158 { "category", LT_CATEGORY },
159 { "commanddepth", LT_COMMANDDEPTH },
160 { "copystyle", LT_COPYSTYLE },
161 { "dependson", LT_DEPENDSON },
163 { "endlabelstring", LT_ENDLABELSTRING },
164 { "endlabeltype", LT_ENDLABELTYPE },
165 { "fill_bottom", LT_FILL_BOTTOM },
166 { "fill_top", LT_FILL_TOP },
168 { "freespacing", LT_FREE_SPACING },
169 { "htmlattr", LT_HTMLATTR },
170 { "htmlforcecss", LT_HTMLFORCECSS },
171 { "htmlitem", LT_HTMLITEM },
172 { "htmlitemattr", LT_HTMLITEMATTR },
173 { "htmllabel", LT_HTMLLABEL },
174 { "htmllabelattr", LT_HTMLLABELATTR },
175 { "htmllabelfirst", LT_HTMLLABELFIRST },
176 { "htmlpremable", LT_HTMLPREAMBLE },
177 { "htmlstyle", LT_HTMLSTYLE },
178 { "htmltag", LT_HTMLTAG },
179 { "innertag", LT_INNERTAG },
180 { "inpreamble", LT_INPREAMBLE },
181 { "intitle", LT_INTITLE },
182 { "itemsep", LT_ITEMSEP },
183 { "itemtag", LT_ITEMTAG },
184 { "keepempty", LT_KEEPEMPTY },
185 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
186 { "labelcounter", LT_LABELCOUNTER },
187 { "labelfont", LT_LABELFONT },
188 { "labelindent", LT_LABELINDENT },
189 { "labelsep", LT_LABELSEP },
190 { "labelstring", LT_LABELSTRING },
191 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
192 { "labeltag", LT_LABELTAG },
193 { "labeltype", LT_LABELTYPE },
194 { "langpreamble", LT_LANGPREAMBLE },
195 { "latexname", LT_LATEXNAME },
196 { "latexparam", LT_LATEXPARAM },
197 { "latextype", LT_LATEXTYPE },
198 { "leftmargin", LT_LEFTMARGIN },
199 { "margin", LT_MARGIN },
200 { "needprotect", LT_NEED_PROTECT },
201 { "newline", LT_NEWLINE },
202 { "nextnoindent", LT_NEXTNOINDENT },
203 { "obsoletedby", LT_OBSOLETEDBY },
204 { "optionalargs", LT_OPTARGS },
205 { "parindent", LT_PARINDENT },
206 { "parsep", LT_PARSEP },
207 { "parskip", LT_PARSKIP },
208 { "passthru", LT_PASS_THRU },
209 { "preamble", LT_PREAMBLE },
210 { "requires", LT_REQUIRES },
211 { "rightmargin", LT_RIGHTMARGIN },
212 { "spacing", LT_SPACING },
213 { "textfont", LT_TEXTFONT },
214 { "toclevel", LT_TOCLEVEL },
215 { "topsep", LT_TOPSEP }
219 bool finished = false;
220 lex.pushTable(layoutTags);
221 // parse style section
222 while (!finished && lex.isOK() && !error) {
224 // See comment in LyXRC.cpp.
226 case Lexer::LEX_FEOF:
229 case Lexer::LEX_UNDEF: // parse error
230 lex.printError("Unknown layout tag `$$Token'");
237 switch (static_cast<LayoutTags>(le)) {
238 case LT_END: // end of structure
246 case LT_COPYSTYLE: { // initialize with a known style
249 style = subst(style, '_', ' ');
251 if (tclass.hasLayout(style)) {
252 docstring const tmpname = name_;
253 this->operator=(tclass[style]);
256 LYXERR0("Cannot copy unknown style `"
258 << "All layouts so far:");
259 DocumentClass::const_iterator lit = tclass.begin();
260 DocumentClass::const_iterator len = tclass.end();
261 for (; lit != len; ++lit)
262 LYXERR0(lit->name());
267 case LT_OBSOLETEDBY: { // replace with a known style
270 style = subst(style, '_', ' ');
272 if (tclass.hasLayout(style)) {
273 docstring const tmpname = name_;
274 this->operator=(tclass[style]);
276 if (obsoleted_by().empty())
277 obsoleted_by_ = style;
279 LYXERR0("Cannot replace with unknown style `"
282 //lex.printError("Cannot replace with"
291 depends_on_ = subst(depends_on_, '_', ' ');
294 case LT_MARGIN: // margin style definition.
298 case LT_LATEXTYPE: // LaTeX style definition.
315 lex >> optionalargs ;
318 case LT_NEED_PROTECT:
327 font = lyxRead(lex, font);
332 font = lyxRead(lex, font);
336 labelfont = lyxRead(lex, labelfont);
339 case LT_NEXTNOINDENT: // Indent next paragraph?
343 case LT_COMMANDDEPTH:
353 latexparam_ = subst(latexparam_, """, "\"");
369 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
372 case LT_LANGPREAMBLE:
373 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
376 case LT_BABELPREAMBLE:
377 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
384 case LT_ENDLABELTYPE:
385 readEndLabelType(lex);
388 case LT_LEFTMARGIN: // left margin type
392 case LT_RIGHTMARGIN: // right margin type
396 case LT_LABELINDENT: // label indenting flag
400 case LT_PARINDENT: // paragraph indent. flag
404 case LT_PARSKIP: // paragraph skip size
408 case LT_ITEMSEP: // item separation size
412 case LT_TOPSEP: // top separation size
416 case LT_BOTTOMSEP: // bottom separation size
420 case LT_LABEL_BOTTOMSEP: // label bottom separation size
421 lex >> labelbottomsep;
424 case LT_LABELSEP: // label separator
426 labelsep = subst(labelsep, 'x', ' ');
429 case LT_PARSEP: // par. separation size
433 case LT_FILL_TOP: // fill top flag
437 case LT_FILL_BOTTOM: // fill bottom flag
441 case LT_NEWLINE: // newlines allowed?
442 lex >> newline_allowed;
445 case LT_ALIGN: // paragraph align
448 case LT_ALIGNPOSSIBLE: // paragraph allowed align
449 readAlignPossible(lex);
452 case LT_LABELSTRING: // label string definition
453 // FIXME: this means LT_ENDLABELSTRING may only
454 // occur after LT_LABELSTRING
456 labelstring_ = trim(labelstring_);
457 labelstring_appendix_ = labelstring_;
460 case LT_ENDLABELSTRING: // endlabel string definition
461 lex >> endlabelstring_;
462 endlabelstring_ = trim(endlabelstring_);
465 case LT_LABELSTRING_APPENDIX: // label string appendix definition
466 lex >> labelstring_appendix_;
467 labelstring_appendix_ = trim(labelstring_appendix_);
470 case LT_LABELCOUNTER: // name of counter to use
472 counter = trim(counter);
475 case LT_FREE_SPACING: // Allow for free spacing.
479 case LT_PASS_THRU: // Allow for pass thru.
483 case LT_SPACING: // setspace.sty
489 vector<string> const req =
490 getVectorFromString(lex.getString());
491 requires_.insert(req.begin(), req.end());
507 case LT_HTMLITEMATTR:
508 lex >> htmlitemattr_;
512 lex >> htmllabeltag_;
515 case LT_HTMLLABELATTR:
516 lex >> htmllabelattr_;
519 case LT_HTMLLABELFIRST:
520 lex >> htmllabelfirst_;
524 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
527 case LT_HTMLFORCECSS:
528 lex >> htmlforcecss_;
530 case LT_HTMLPREAMBLE:
531 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
551 LexerKeyword alignTags[] = {
552 { "block", AT_BLOCK },
553 { "center", AT_CENTER },
554 { "layout", AT_LAYOUT },
556 { "right", AT_RIGHT }
560 void Layout::readAlign(Lexer & lex)
562 PushPopHelper pph(lex, alignTags);
565 case Lexer::LEX_UNDEF:
566 lex.printError("Unknown alignment `$$Token'");
572 align = LYX_ALIGN_BLOCK;
575 align = LYX_ALIGN_LEFT;
578 align = LYX_ALIGN_RIGHT;
581 align = LYX_ALIGN_CENTER;
584 align = LYX_ALIGN_LAYOUT;
590 void Layout::readAlignPossible(Lexer & lex)
592 lex.pushTable(alignTags);
593 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
594 int lineno = lex.lineNumber();
598 case Lexer::LEX_UNDEF:
599 lex.printError("Unknown alignment `$$Token'");
605 alignpossible |= LYX_ALIGN_BLOCK;
608 alignpossible |= LYX_ALIGN_LEFT;
611 alignpossible |= LYX_ALIGN_RIGHT;
614 alignpossible |= LYX_ALIGN_CENTER;
617 alignpossible |= LYX_ALIGN_LAYOUT;
620 } while (lineno == lex.lineNumber());
625 void Layout::readLabelType(Lexer & lex)
631 LA_CENTERED_TOP_ENVIRONMENT,
641 LexerKeyword labelTypeTags[] = {
642 { "bibliography", LA_BIBLIO },
643 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
644 { "counter", LA_COUNTER },
645 { "enumerate", LA_ENUMERATE },
646 { "itemize", LA_ITEMIZE },
647 { "manual", LA_MANUAL },
648 { "no_label", LA_NO_LABEL },
649 { "sensitive", LA_SENSITIVE },
650 { "static", LA_STATIC },
651 { "top_environment", LA_TOP_ENVIRONMENT }
654 PushPopHelper pph(lex, labelTypeTags);
657 case Lexer::LEX_UNDEF:
658 lex.printError("Unknown labeltype tag `$$Token'");
664 labeltype = LABEL_NO_LABEL;
667 labeltype = LABEL_MANUAL;
669 case LA_TOP_ENVIRONMENT:
670 labeltype = LABEL_TOP_ENVIRONMENT;
672 case LA_CENTERED_TOP_ENVIRONMENT:
673 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
676 labeltype = LABEL_STATIC;
679 labeltype = LABEL_SENSITIVE;
682 labeltype = LABEL_COUNTER;
685 labeltype = LABEL_ENUMERATE;
688 labeltype = LABEL_ITEMIZE;
691 labeltype = LABEL_BIBLIO;
697 void Layout::readEndLabelType(Lexer & lex)
699 static LexerKeyword endlabelTypeTags[] = {
700 { "box", END_LABEL_BOX },
701 { "filled_box", END_LABEL_FILLED_BOX },
702 { "no_label", END_LABEL_NO_LABEL },
703 { "static", END_LABEL_STATIC }
706 PushPopHelper pph(lex, endlabelTypeTags);
709 case Lexer::LEX_UNDEF:
710 lex.printError("Unknown labeltype tag `$$Token'");
712 case END_LABEL_STATIC:
714 case END_LABEL_FILLED_BOX:
715 case END_LABEL_NO_LABEL:
716 endlabeltype = static_cast<EndLabelType>(le);
719 LYXERR0("Unhandled value " << le);
725 void Layout::readMargin(Lexer & lex)
727 LexerKeyword marginTags[] = {
728 { "dynamic", MARGIN_DYNAMIC },
729 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
730 { "manual", MARGIN_MANUAL },
731 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
732 { "static", MARGIN_STATIC }
735 PushPopHelper pph(lex, marginTags);
739 case Lexer::LEX_UNDEF:
740 lex.printError("Unknown margin type tag `$$Token'");
745 case MARGIN_FIRST_DYNAMIC:
746 case MARGIN_RIGHT_ADDRESS_BOX:
747 margintype = static_cast<MarginType>(le);
750 LYXERR0("Unhandled value " << le);
756 void Layout::readLatexType(Lexer & lex)
758 LexerKeyword latexTypeTags[] = {
759 { "bib_environment", LATEX_BIB_ENVIRONMENT },
760 { "command", LATEX_COMMAND },
761 { "environment", LATEX_ENVIRONMENT },
762 { "item_environment", LATEX_ITEM_ENVIRONMENT },
763 { "list_environment", LATEX_LIST_ENVIRONMENT },
764 { "paragraph", LATEX_PARAGRAPH }
767 PushPopHelper pph(lex, latexTypeTags);
770 case Lexer::LEX_UNDEF:
771 lex.printError("Unknown latextype tag `$$Token'");
773 case LATEX_PARAGRAPH:
775 case LATEX_ENVIRONMENT:
776 case LATEX_ITEM_ENVIRONMENT:
777 case LATEX_BIB_ENVIRONMENT:
778 case LATEX_LIST_ENVIRONMENT:
779 latextype = static_cast<LatexType>(le);
782 LYXERR0("Unhandled value " << le);
788 void Layout::readSpacing(Lexer & lex)
791 ST_SPACING_SINGLE = 1,
797 LexerKeyword spacingTags[] = {
798 {"double", ST_SPACING_DOUBLE },
799 {"onehalf", ST_SPACING_ONEHALF },
800 {"other", ST_OTHER },
801 {"single", ST_SPACING_SINGLE }
804 PushPopHelper pph(lex, spacingTags);
807 case Lexer::LEX_UNDEF:
808 lex.printError("Unknown spacing token `$$Token'");
813 case ST_SPACING_SINGLE:
814 spacing.set(Spacing::Single);
816 case ST_SPACING_ONEHALF:
817 spacing.set(Spacing::Onehalf);
819 case ST_SPACING_DOUBLE:
820 spacing.set(Spacing::Double);
824 spacing.set(Spacing::Other, lex.getString());
832 docstring const i18npreamble(Language const * lang, docstring const & templ)
837 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
840 // tex2lyx does not have getMessages()
841 LASSERT(false, /**/);
844 // boost::regex is not unicode-safe.
845 // Should use QRegExp or (boost::u32regex, but that requires ICU)
846 static boost::regex const reg("_\\(([^\\)]+)\\)");
848 while (boost::regex_search(preamble, sub, reg)) {
849 string const key = sub.str(1);
852 translated = to_utf8(getMessages(lang->code()).get(key));
854 lyxerr << "Warning: not translating `" << key
855 << "' because it is not pure ASCII." << endl;
858 preamble = subst(preamble, sub.str(), translated);
861 return from_utf8(preamble);
867 docstring const Layout::langpreamble(Language const * lang) const
869 return i18npreamble(lang, langpreamble_);
873 docstring const Layout::babelpreamble(Language const * lang) const
875 return i18npreamble(lang, babelpreamble_);
879 string const & Layout::htmltag() const
881 if (htmltag_.empty())
887 string const & Layout::htmlattr() const
889 if (htmlattr_.empty())
890 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
895 string const & Layout::htmlitemtag() const
897 if (htmlitemtag_.empty())
898 htmlitemtag_ = "div";
903 string const & Layout::htmlitemattr() const
905 if (htmlitemattr_.empty())
906 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
907 return htmlitemattr_;
911 string const & Layout::htmllabeltag() const
913 if (htmllabeltag_.empty())
914 htmllabeltag_ = "span";
915 return htmllabeltag_;
919 string const & Layout::htmllabelattr() const
921 if (htmllabelattr_.empty())
922 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
923 return htmllabelattr_;
927 docstring Layout::htmlstyle() const {
928 if (!htmlstyle_.empty() && !htmlforcecss_)
930 if (htmldefaultstyle_.empty())
932 docstring retval = htmldefaultstyle_;
933 if (!htmlstyle_.empty())
934 retval += '\n' + htmlstyle_;
939 string Layout::defaultCSSClass() const
941 if (!defaultcssclass_.empty())
942 return defaultcssclass_;
944 docstring::const_iterator it = name().begin();
945 docstring::const_iterator en = name().end();
946 for (; it != en; ++it) {
954 // are there other characters we need to remove?
955 defaultcssclass_ = to_utf8(d);
956 return defaultcssclass_;
960 // NOTE There is a whole ton of stuff that could go into this.
961 // Things like bottomsep, topsep, and parsep could become various
962 // sorts of margins or padding, for example. But for now we are
963 // going to keep it simple.
964 void Layout::makeDefaultCSS() const {
965 // this never needs to be redone, since reloading layouts will
966 // wipe out what we did before.
967 if (!htmldefaultstyle_.empty())
969 docstring const mainfontCSS = font.asCSS();
970 if (!mainfontCSS.empty())
972 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
973 mainfontCSS + from_ascii("\n}\n");
974 if (labelfont == font || htmllabeltag() == "NONE")
976 docstring const labelfontCSS = labelfont.asCSS();
977 if (!labelfontCSS.empty())
979 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
980 labelfontCSS + from_ascii("\n}\n");
984 bool Layout::operator==(Layout const & rhs) const
986 // This is enough for the applications we actually make,
987 // at least at the moment. But we could check more.
988 return name() == rhs.name()
989 && latexname() == rhs.latexname()
990 && latextype == rhs.latextype;