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,
110 LT_INTITLE // keep this last!
113 /////////////////////
118 margintype = MARGIN_STATIC;
119 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;
157 bool Layout::read(Lexer & lex, TextClass const & tclass)
159 // This table is sorted alphabetically [asierra 30March96]
160 LexerKeyword layoutTags[] = {
161 { "align", LT_ALIGN },
162 { "alignpossible", LT_ALIGNPOSSIBLE },
163 { "babelpreamble", LT_BABELPREAMBLE },
164 { "bottomsep", LT_BOTTOMSEP },
165 { "category", LT_CATEGORY },
166 { "commanddepth", LT_COMMANDDEPTH },
167 { "copystyle", LT_COPYSTYLE },
168 { "dependson", LT_DEPENDSON },
170 { "endlabelstring", LT_ENDLABELSTRING },
171 { "endlabeltype", LT_ENDLABELTYPE },
172 { "fill_bottom", LT_FILL_BOTTOM },
173 { "fill_top", LT_FILL_TOP },
175 { "freespacing", LT_FREE_SPACING },
176 { "htmlattr", LT_HTMLATTR },
177 { "htmlforcecss", LT_HTMLFORCECSS },
178 { "htmlitem", LT_HTMLITEM },
179 { "htmlitemattr", LT_HTMLITEMATTR },
180 { "htmllabel", LT_HTMLLABEL },
181 { "htmllabelattr", LT_HTMLLABELATTR },
182 { "htmllabelfirst", LT_HTMLLABELFIRST },
183 { "htmlpremable", LT_HTMLPREAMBLE },
184 { "htmlstyle", LT_HTMLSTYLE },
185 { "htmltag", LT_HTMLTAG },
186 { "htmltitle", LT_HTMLTITLE },
187 { "innertag", LT_INNERTAG },
188 { "inpreamble", LT_INPREAMBLE },
189 { "intitle", LT_INTITLE },
190 { "itemsep", LT_ITEMSEP },
191 { "itemtag", LT_ITEMTAG },
192 { "keepempty", LT_KEEPEMPTY },
193 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
194 { "labelcounter", LT_LABELCOUNTER },
195 { "labelfont", LT_LABELFONT },
196 { "labelindent", LT_LABELINDENT },
197 { "labelsep", LT_LABELSEP },
198 { "labelstring", LT_LABELSTRING },
199 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
200 { "labeltag", LT_LABELTAG },
201 { "labeltype", LT_LABELTYPE },
202 { "langpreamble", LT_LANGPREAMBLE },
203 { "latexname", LT_LATEXNAME },
204 { "latexparam", LT_LATEXPARAM },
205 { "latextype", LT_LATEXTYPE },
206 { "leftmargin", LT_LEFTMARGIN },
207 { "margin", LT_MARGIN },
208 { "needprotect", LT_NEED_PROTECT },
209 { "newline", LT_NEWLINE },
210 { "nextnoindent", LT_NEXTNOINDENT },
211 { "obsoletedby", LT_OBSOLETEDBY },
212 { "optionalargs", LT_OPTARGS },
213 { "parindent", LT_PARINDENT },
214 { "parsep", LT_PARSEP },
215 { "parskip", LT_PARSKIP },
216 { "passthru", LT_PASS_THRU },
217 { "preamble", LT_PREAMBLE },
218 { "refprefix", LT_REFPREFIX },
219 { "requiredargs", LT_REQARGS },
220 { "requires", LT_REQUIRES },
221 { "rightmargin", LT_RIGHTMARGIN },
222 { "spacing", LT_SPACING },
223 { "spellcheck", LT_SPELLCHECK },
224 { "textfont", LT_TEXTFONT },
225 { "toclevel", LT_TOCLEVEL },
226 { "topsep", LT_TOPSEP }
230 bool finished = false;
231 lex.pushTable(layoutTags);
232 // parse style section
233 while (!finished && lex.isOK() && !error) {
235 // See comment in LyXRC.cpp.
237 case Lexer::LEX_FEOF:
240 case Lexer::LEX_UNDEF: // parse error
241 lex.printError("Unknown layout tag `$$Token'");
248 switch (static_cast<LayoutTags>(le)) {
249 case LT_END: // end of structure
257 case LT_COPYSTYLE: { // initialize with a known style
260 style = subst(style, '_', ' ');
262 if (tclass.hasLayout(style)) {
263 docstring const tmpname = name_;
264 this->operator=(tclass[style]);
267 LYXERR0("Cannot copy unknown style `"
269 << "All layouts so far:");
270 DocumentClass::const_iterator lit = tclass.begin();
271 DocumentClass::const_iterator len = tclass.end();
272 for (; lit != len; ++lit)
273 LYXERR0(lit->name());
278 case LT_OBSOLETEDBY: { // replace with a known style
281 style = subst(style, '_', ' ');
283 if (tclass.hasLayout(style)) {
284 docstring const tmpname = name_;
285 this->operator=(tclass[style]);
287 if (obsoleted_by().empty())
288 obsoleted_by_ = style;
290 LYXERR0("Cannot replace with unknown style `"
293 //lex.printError("Cannot replace with"
302 depends_on_ = subst(depends_on_, '_', ' ');
305 case LT_MARGIN: // margin style definition.
309 case LT_LATEXTYPE: // LaTeX style definition.
333 case LT_NEED_PROTECT:
342 font = lyxRead(lex, font);
347 font = lyxRead(lex, font);
351 labelfont = lyxRead(lex, labelfont);
354 case LT_NEXTNOINDENT: // Indent next paragraph?
358 case LT_COMMANDDEPTH:
368 latexparam_ = subst(latexparam_, """, "\"");
384 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
387 case LT_LANGPREAMBLE:
388 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
391 case LT_BABELPREAMBLE:
392 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
399 case LT_ENDLABELTYPE:
400 readEndLabelType(lex);
403 case LT_LEFTMARGIN: // left margin type
407 case LT_RIGHTMARGIN: // right margin type
411 case LT_LABELINDENT: // label indenting flag
415 case LT_PARINDENT: // paragraph indent. flag
419 case LT_PARSKIP: // paragraph skip size
423 case LT_ITEMSEP: // item separation size
427 case LT_TOPSEP: // top separation size
431 case LT_BOTTOMSEP: // bottom separation size
435 case LT_LABEL_BOTTOMSEP: // label bottom separation size
436 lex >> labelbottomsep;
439 case LT_LABELSEP: // label separator
441 labelsep = subst(labelsep, 'x', ' ');
444 case LT_PARSEP: // par. separation size
448 case LT_FILL_TOP: // fill top flag
452 case LT_FILL_BOTTOM: // fill bottom flag
456 case LT_NEWLINE: // newlines allowed?
457 lex >> newline_allowed;
460 case LT_ALIGN: // paragraph align
463 case LT_ALIGNPOSSIBLE: // paragraph allowed align
464 readAlignPossible(lex);
467 case LT_LABELSTRING: // label string definition
468 // FIXME: this means LT_ENDLABELSTRING may only
469 // occur after LT_LABELSTRING
471 labelstring_ = trim(labelstring_);
472 labelstring_appendix_ = labelstring_;
475 case LT_ENDLABELSTRING: // endlabel string definition
476 lex >> endlabelstring_;
477 endlabelstring_ = trim(endlabelstring_);
480 case LT_LABELSTRING_APPENDIX: // label string appendix definition
481 lex >> labelstring_appendix_;
482 labelstring_appendix_ = trim(labelstring_appendix_);
485 case LT_LABELCOUNTER: // name of counter to use
487 counter = trim(counter);
490 case LT_FREE_SPACING: // Allow for free spacing.
494 case LT_PASS_THRU: // Allow for pass thru.
498 case LT_SPACING: // setspace.sty
504 vector<string> const req =
505 getVectorFromString(lex.getString());
506 requires_.insert(req.begin(), req.end());
526 case LT_HTMLITEMATTR:
527 lex >> htmlitemattr_;
531 lex >> htmllabeltag_;
534 case LT_HTMLLABELATTR:
535 lex >> htmllabelattr_;
538 case LT_HTMLLABELFIRST:
539 lex >> htmllabelfirst_;
543 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
546 case LT_HTMLFORCECSS:
547 lex >> htmlforcecss_;
549 case LT_HTMLPREAMBLE:
550 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
577 LexerKeyword alignTags[] = {
578 { "block", AT_BLOCK },
579 { "center", AT_CENTER },
580 { "layout", AT_LAYOUT },
582 { "right", AT_RIGHT }
586 void Layout::readAlign(Lexer & lex)
588 PushPopHelper pph(lex, alignTags);
591 case Lexer::LEX_UNDEF:
592 lex.printError("Unknown alignment `$$Token'");
598 align = LYX_ALIGN_BLOCK;
601 align = LYX_ALIGN_LEFT;
604 align = LYX_ALIGN_RIGHT;
607 align = LYX_ALIGN_CENTER;
610 align = LYX_ALIGN_LAYOUT;
616 void Layout::readAlignPossible(Lexer & lex)
618 lex.pushTable(alignTags);
619 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
620 int lineno = lex.lineNumber();
624 case Lexer::LEX_UNDEF:
625 lex.printError("Unknown alignment `$$Token'");
631 alignpossible |= LYX_ALIGN_BLOCK;
634 alignpossible |= LYX_ALIGN_LEFT;
637 alignpossible |= LYX_ALIGN_RIGHT;
640 alignpossible |= LYX_ALIGN_CENTER;
643 alignpossible |= LYX_ALIGN_LAYOUT;
646 } while (lineno == lex.lineNumber());
651 void Layout::readLabelType(Lexer & lex)
657 LA_CENTERED_TOP_ENVIRONMENT,
667 LexerKeyword labelTypeTags[] = {
668 { "bibliography", LA_BIBLIO },
669 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
670 { "counter", LA_COUNTER },
671 { "enumerate", LA_ENUMERATE },
672 { "itemize", LA_ITEMIZE },
673 { "manual", LA_MANUAL },
674 { "no_label", LA_NO_LABEL },
675 { "sensitive", LA_SENSITIVE },
676 { "static", LA_STATIC },
677 { "top_environment", LA_TOP_ENVIRONMENT }
680 PushPopHelper pph(lex, labelTypeTags);
683 case Lexer::LEX_UNDEF:
684 lex.printError("Unknown labeltype tag `$$Token'");
690 labeltype = LABEL_NO_LABEL;
693 labeltype = LABEL_MANUAL;
695 case LA_TOP_ENVIRONMENT:
696 labeltype = LABEL_TOP_ENVIRONMENT;
698 case LA_CENTERED_TOP_ENVIRONMENT:
699 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
702 labeltype = LABEL_STATIC;
705 labeltype = LABEL_SENSITIVE;
708 labeltype = LABEL_COUNTER;
711 labeltype = LABEL_ENUMERATE;
714 labeltype = LABEL_ITEMIZE;
717 labeltype = LABEL_BIBLIO;
723 void Layout::readEndLabelType(Lexer & lex)
725 static LexerKeyword endlabelTypeTags[] = {
726 { "box", END_LABEL_BOX },
727 { "filled_box", END_LABEL_FILLED_BOX },
728 { "no_label", END_LABEL_NO_LABEL },
729 { "static", END_LABEL_STATIC }
732 PushPopHelper pph(lex, endlabelTypeTags);
735 case Lexer::LEX_UNDEF:
736 lex.printError("Unknown labeltype tag `$$Token'");
738 case END_LABEL_STATIC:
740 case END_LABEL_FILLED_BOX:
741 case END_LABEL_NO_LABEL:
742 endlabeltype = static_cast<EndLabelType>(le);
745 LYXERR0("Unhandled value " << le);
751 void Layout::readMargin(Lexer & lex)
753 LexerKeyword marginTags[] = {
754 { "dynamic", MARGIN_DYNAMIC },
755 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
756 { "manual", MARGIN_MANUAL },
757 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
758 { "static", MARGIN_STATIC }
761 PushPopHelper pph(lex, marginTags);
765 case Lexer::LEX_UNDEF:
766 lex.printError("Unknown margin type tag `$$Token'");
771 case MARGIN_FIRST_DYNAMIC:
772 case MARGIN_RIGHT_ADDRESS_BOX:
773 margintype = static_cast<MarginType>(le);
776 LYXERR0("Unhandled value " << le);
782 void Layout::readLatexType(Lexer & lex)
784 LexerKeyword latexTypeTags[] = {
785 { "bib_environment", LATEX_BIB_ENVIRONMENT },
786 { "command", LATEX_COMMAND },
787 { "environment", LATEX_ENVIRONMENT },
788 { "item_environment", LATEX_ITEM_ENVIRONMENT },
789 { "list_environment", LATEX_LIST_ENVIRONMENT },
790 { "paragraph", LATEX_PARAGRAPH }
793 PushPopHelper pph(lex, latexTypeTags);
796 case Lexer::LEX_UNDEF:
797 lex.printError("Unknown latextype tag `$$Token'");
799 case LATEX_PARAGRAPH:
801 case LATEX_ENVIRONMENT:
802 case LATEX_ITEM_ENVIRONMENT:
803 case LATEX_BIB_ENVIRONMENT:
804 case LATEX_LIST_ENVIRONMENT:
805 latextype = static_cast<LatexType>(le);
808 LYXERR0("Unhandled value " << le);
814 void Layout::readSpacing(Lexer & lex)
817 ST_SPACING_SINGLE = 1,
823 LexerKeyword spacingTags[] = {
824 {"double", ST_SPACING_DOUBLE },
825 {"onehalf", ST_SPACING_ONEHALF },
826 {"other", ST_OTHER },
827 {"single", ST_SPACING_SINGLE }
830 PushPopHelper pph(lex, spacingTags);
833 case Lexer::LEX_UNDEF:
834 lex.printError("Unknown spacing token `$$Token'");
839 case ST_SPACING_SINGLE:
840 spacing.set(Spacing::Single);
842 case ST_SPACING_ONEHALF:
843 spacing.set(Spacing::Onehalf);
845 case ST_SPACING_DOUBLE:
846 spacing.set(Spacing::Double);
850 spacing.set(Spacing::Other, lex.getString());
858 docstring const i18npreamble(Language const * lang, docstring const & templ)
863 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
866 // tex2lyx does not have getMessages()
867 LASSERT(false, /**/);
870 // boost::regex is not unicode-safe.
871 // Should use QRegExp or (boost::u32regex, but that requires ICU)
872 static boost::regex const reg("_\\(([^\\)]+)\\)");
874 while (boost::regex_search(preamble, sub, reg)) {
875 string const key = sub.str(1);
878 translated = to_utf8(getMessages(lang->code()).get(key));
880 lyxerr << "Warning: not translating `" << key
881 << "' because it is not pure ASCII." << endl;
884 preamble = subst(preamble, sub.str(), translated);
887 return from_utf8(preamble);
893 docstring const Layout::langpreamble(Language const * lang) const
895 return i18npreamble(lang, langpreamble_);
899 docstring const Layout::babelpreamble(Language const * lang) const
901 return i18npreamble(lang, babelpreamble_);
905 string const & Layout::htmltag() const
907 if (htmltag_.empty())
913 string const & Layout::htmlattr() const
915 if (htmlattr_.empty())
916 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
921 string const & Layout::htmlitemtag() const
923 if (htmlitemtag_.empty())
924 htmlitemtag_ = "div";
929 string const & Layout::htmlitemattr() const
931 if (htmlitemattr_.empty())
932 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
933 return htmlitemattr_;
937 string const & Layout::htmllabeltag() const
939 if (htmllabeltag_.empty())
940 htmllabeltag_ = "span";
941 return htmllabeltag_;
945 string const & Layout::htmllabelattr() const
947 if (htmllabelattr_.empty())
948 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
949 return htmllabelattr_;
953 docstring Layout::htmlstyle() const {
954 if (!htmlstyle_.empty() && !htmlforcecss_)
956 if (htmldefaultstyle_.empty())
958 docstring retval = htmldefaultstyle_;
959 if (!htmlstyle_.empty())
960 retval += '\n' + htmlstyle_;
965 string Layout::defaultCSSClass() const
967 if (!defaultcssclass_.empty())
968 return defaultcssclass_;
970 docstring::const_iterator it = name().begin();
971 docstring::const_iterator en = name().end();
972 for (; it != en; ++it) {
980 // are there other characters we need to remove?
981 defaultcssclass_ = to_utf8(d);
982 return defaultcssclass_;
986 // NOTE There is a whole ton of stuff that could go into this.
987 // Things like bottomsep, topsep, and parsep could become various
988 // sorts of margins or padding, for example. But for now we are
989 // going to keep it simple.
990 void Layout::makeDefaultCSS() const {
991 // this never needs to be redone, since reloading layouts will
992 // wipe out what we did before.
993 if (!htmldefaultstyle_.empty())
995 docstring const mainfontCSS = font.asCSS();
996 if (!mainfontCSS.empty())
998 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
999 mainfontCSS + from_ascii("\n}\n");
1000 if (labelfont == font || htmllabeltag() == "NONE")
1002 docstring const labelfontCSS = labelfont.asCSS();
1003 if (!labelfontCSS.empty())
1004 htmldefaultstyle_ +=
1005 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1006 labelfontCSS + from_ascii("\n}\n");
1010 bool Layout::operator==(Layout const & rhs) const
1012 // This is enough for the applications we actually make,
1013 // at least at the moment. But we could check more.
1014 return name() == rhs.name()
1015 && latexname() == rhs.latexname()
1016 && latextype == rhs.latextype;