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 "support/regex.h"
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());
532 case LT_HTMLITEMATTR:
533 lex >> htmlitemattr_;
537 lex >> htmllabeltag_;
540 case LT_HTMLLABELATTR:
541 lex >> htmllabelattr_;
544 case LT_HTMLLABELFIRST:
545 lex >> htmllabelfirst_;
549 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
552 case LT_HTMLFORCECSS:
553 lex >> htmlforcecss_;
555 case LT_HTMLPREAMBLE:
556 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
569 // make sure we only have inpreamble = true for commands
570 if (inpreamble && latextype != LATEX_COMMAND) {
571 LYXERR0("InPreamble not permitted except with Command-type layouts.");
572 LYXERR0("Layout name: " << name());
589 LexerKeyword alignTags[] = {
590 { "block", AT_BLOCK },
591 { "center", AT_CENTER },
592 { "layout", AT_LAYOUT },
594 { "right", AT_RIGHT }
598 void Layout::readAlign(Lexer & lex)
600 PushPopHelper pph(lex, alignTags);
603 case Lexer::LEX_UNDEF:
604 lex.printError("Unknown alignment `$$Token'");
610 align = LYX_ALIGN_BLOCK;
613 align = LYX_ALIGN_LEFT;
616 align = LYX_ALIGN_RIGHT;
619 align = LYX_ALIGN_CENTER;
622 align = LYX_ALIGN_LAYOUT;
628 void Layout::readAlignPossible(Lexer & lex)
630 lex.pushTable(alignTags);
631 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
632 int lineno = lex.lineNumber();
636 case Lexer::LEX_UNDEF:
637 lex.printError("Unknown alignment `$$Token'");
643 alignpossible |= LYX_ALIGN_BLOCK;
646 alignpossible |= LYX_ALIGN_LEFT;
649 alignpossible |= LYX_ALIGN_RIGHT;
652 alignpossible |= LYX_ALIGN_CENTER;
655 alignpossible |= LYX_ALIGN_LAYOUT;
658 } while (lineno == lex.lineNumber());
663 void Layout::readLabelType(Lexer & lex)
669 LA_CENTERED_TOP_ENVIRONMENT,
679 LexerKeyword labelTypeTags[] = {
680 { "bibliography", LA_BIBLIO },
681 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
682 { "counter", LA_COUNTER },
683 { "enumerate", LA_ENUMERATE },
684 { "itemize", LA_ITEMIZE },
685 { "manual", LA_MANUAL },
686 { "no_label", LA_NO_LABEL },
687 { "sensitive", LA_SENSITIVE },
688 { "static", LA_STATIC },
689 { "top_environment", LA_TOP_ENVIRONMENT }
692 PushPopHelper pph(lex, labelTypeTags);
695 case Lexer::LEX_UNDEF:
696 lex.printError("Unknown labeltype tag `$$Token'");
702 labeltype = LABEL_NO_LABEL;
705 labeltype = LABEL_MANUAL;
707 case LA_TOP_ENVIRONMENT:
708 labeltype = LABEL_TOP_ENVIRONMENT;
710 case LA_CENTERED_TOP_ENVIRONMENT:
711 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
714 labeltype = LABEL_STATIC;
717 labeltype = LABEL_SENSITIVE;
720 labeltype = LABEL_COUNTER;
723 labeltype = LABEL_ENUMERATE;
726 labeltype = LABEL_ITEMIZE;
729 labeltype = LABEL_BIBLIO;
735 void Layout::readEndLabelType(Lexer & lex)
737 static LexerKeyword endlabelTypeTags[] = {
738 { "box", END_LABEL_BOX },
739 { "filled_box", END_LABEL_FILLED_BOX },
740 { "no_label", END_LABEL_NO_LABEL },
741 { "static", END_LABEL_STATIC }
744 PushPopHelper pph(lex, endlabelTypeTags);
747 case Lexer::LEX_UNDEF:
748 lex.printError("Unknown labeltype tag `$$Token'");
750 case END_LABEL_STATIC:
752 case END_LABEL_FILLED_BOX:
753 case END_LABEL_NO_LABEL:
754 endlabeltype = static_cast<EndLabelType>(le);
757 LYXERR0("Unhandled value " << le);
763 void Layout::readMargin(Lexer & lex)
765 LexerKeyword marginTags[] = {
766 { "dynamic", MARGIN_DYNAMIC },
767 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
768 { "manual", MARGIN_MANUAL },
769 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
770 { "static", MARGIN_STATIC }
773 PushPopHelper pph(lex, marginTags);
777 case Lexer::LEX_UNDEF:
778 lex.printError("Unknown margin type tag `$$Token'");
783 case MARGIN_FIRST_DYNAMIC:
784 case MARGIN_RIGHT_ADDRESS_BOX:
785 margintype = static_cast<MarginType>(le);
788 LYXERR0("Unhandled value " << le);
794 void Layout::readLatexType(Lexer & lex)
796 LexerKeyword latexTypeTags[] = {
797 { "bib_environment", LATEX_BIB_ENVIRONMENT },
798 { "command", LATEX_COMMAND },
799 { "environment", LATEX_ENVIRONMENT },
800 { "item_environment", LATEX_ITEM_ENVIRONMENT },
801 { "list_environment", LATEX_LIST_ENVIRONMENT },
802 { "paragraph", LATEX_PARAGRAPH }
805 PushPopHelper pph(lex, latexTypeTags);
808 case Lexer::LEX_UNDEF:
809 lex.printError("Unknown latextype tag `$$Token'");
811 case LATEX_PARAGRAPH:
813 case LATEX_ENVIRONMENT:
814 case LATEX_ITEM_ENVIRONMENT:
815 case LATEX_BIB_ENVIRONMENT:
816 case LATEX_LIST_ENVIRONMENT:
817 latextype = static_cast<LatexType>(le);
820 LYXERR0("Unhandled value " << le);
826 void Layout::readSpacing(Lexer & lex)
829 ST_SPACING_SINGLE = 1,
835 LexerKeyword spacingTags[] = {
836 {"double", ST_SPACING_DOUBLE },
837 {"onehalf", ST_SPACING_ONEHALF },
838 {"other", ST_OTHER },
839 {"single", ST_SPACING_SINGLE }
842 PushPopHelper pph(lex, spacingTags);
845 case Lexer::LEX_UNDEF:
846 lex.printError("Unknown spacing token `$$Token'");
851 case ST_SPACING_SINGLE:
852 spacing.set(Spacing::Single);
854 case ST_SPACING_ONEHALF:
855 spacing.set(Spacing::Onehalf);
857 case ST_SPACING_DOUBLE:
858 spacing.set(Spacing::Double);
862 spacing.set(Spacing::Other, lex.getString());
870 docstring const i18npreamble(Language const * lang, docstring const & templ)
875 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
878 // tex2lyx does not have getMessages()
879 LASSERT(false, /**/);
882 // lyx::regex is not unicode-safe.
883 // Should use QRegExp or (boost::u32regex, but that requires ICU)
884 static regex const reg("_\\(([^\\)]+)\\)");
886 while (regex_search(preamble, sub, reg)) {
887 string const key = sub.str(1);
890 translated = to_utf8(getMessages(lang->code()).get(key));
892 lyxerr << "Warning: not translating `" << key
893 << "' because it is not pure ASCII." << endl;
896 preamble = subst(preamble, sub.str(), translated);
899 return from_utf8(preamble);
905 docstring const Layout::langpreamble(Language const * lang) const
907 return i18npreamble(lang, langpreamble_);
911 docstring const Layout::babelpreamble(Language const * lang) const
913 return i18npreamble(lang, babelpreamble_);
917 string const & Layout::htmltag() const
919 if (htmltag_.empty())
925 string const & Layout::htmlattr() const
927 if (htmlattr_.empty())
928 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
933 string const & Layout::htmlitemtag() const
935 if (htmlitemtag_.empty())
936 htmlitemtag_ = "div";
941 string const & Layout::htmlitemattr() const
943 if (htmlitemattr_.empty())
944 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
945 return htmlitemattr_;
949 string const & Layout::htmllabeltag() const
951 if (htmllabeltag_.empty())
952 htmllabeltag_ = "span";
953 return htmllabeltag_;
957 string const & Layout::htmllabelattr() const
959 if (htmllabelattr_.empty())
960 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
961 return htmllabelattr_;
965 docstring Layout::htmlstyle() const {
966 if (!htmlstyle_.empty() && !htmlforcecss_)
968 if (htmldefaultstyle_.empty())
970 docstring retval = htmldefaultstyle_;
971 if (!htmlstyle_.empty())
972 retval += '\n' + htmlstyle_;
977 string Layout::defaultCSSClass() const
979 if (!defaultcssclass_.empty())
980 return defaultcssclass_;
982 docstring::const_iterator it = name().begin();
983 docstring::const_iterator en = name().end();
984 for (; it != en; ++it) {
992 // are there other characters we need to remove?
993 defaultcssclass_ = to_utf8(d);
994 return defaultcssclass_;
998 // NOTE There is a whole ton of stuff that could go into this.
999 // Things like bottomsep, topsep, and parsep could become various
1000 // sorts of margins or padding, for example. But for now we are
1001 // going to keep it simple.
1002 void Layout::makeDefaultCSS() const {
1003 // this never needs to be redone, since reloading layouts will
1004 // wipe out what we did before.
1005 if (!htmldefaultstyle_.empty())
1007 docstring const mainfontCSS = font.asCSS();
1008 if (!mainfontCSS.empty())
1010 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1011 mainfontCSS + from_ascii("\n}\n");
1012 if (labelfont == font || htmllabeltag() == "NONE")
1014 docstring const labelfontCSS = labelfont.asCSS();
1015 if (!labelfontCSS.empty())
1016 htmldefaultstyle_ +=
1017 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1018 labelfontCSS + from_ascii("\n}\n");
1022 bool Layout::operator==(Layout const & rhs) const
1024 // This is enough for the applications we actually make,
1025 // at least at the moment. But we could check more.
1026 return name() == rhs.name()
1027 && latexname() == rhs.latexname()
1028 && latextype == rhs.latextype;