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;
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 { "requiredargs", LT_REQARGS },
218 { "requires", LT_REQUIRES },
219 { "rightmargin", LT_RIGHTMARGIN },
220 { "spacing", LT_SPACING },
221 { "spellcheck", LT_SPELLCHECK },
222 { "textfont", LT_TEXTFONT },
223 { "toclevel", LT_TOCLEVEL },
224 { "topsep", LT_TOPSEP }
228 bool finished = false;
229 lex.pushTable(layoutTags);
230 // parse style section
231 while (!finished && lex.isOK() && !error) {
233 // See comment in LyXRC.cpp.
235 case Lexer::LEX_FEOF:
238 case Lexer::LEX_UNDEF: // parse error
239 lex.printError("Unknown layout tag `$$Token'");
246 switch (static_cast<LayoutTags>(le)) {
247 case LT_END: // end of structure
255 case LT_COPYSTYLE: { // initialize with a known style
258 style = subst(style, '_', ' ');
260 if (tclass.hasLayout(style)) {
261 docstring const tmpname = name_;
262 this->operator=(tclass[style]);
265 LYXERR0("Cannot copy unknown style `"
267 << "All layouts so far:");
268 DocumentClass::const_iterator lit = tclass.begin();
269 DocumentClass::const_iterator len = tclass.end();
270 for (; lit != len; ++lit)
271 LYXERR0(lit->name());
276 case LT_OBSOLETEDBY: { // replace with a known style
279 style = subst(style, '_', ' ');
281 if (tclass.hasLayout(style)) {
282 docstring const tmpname = name_;
283 this->operator=(tclass[style]);
285 if (obsoleted_by().empty())
286 obsoleted_by_ = style;
288 LYXERR0("Cannot replace with unknown style `"
291 //lex.printError("Cannot replace with"
300 depends_on_ = subst(depends_on_, '_', ' ');
303 case LT_MARGIN: // margin style definition.
307 case LT_LATEXTYPE: // LaTeX style definition.
331 case LT_NEED_PROTECT:
340 font = lyxRead(lex, font);
345 font = lyxRead(lex, font);
349 labelfont = lyxRead(lex, labelfont);
352 case LT_NEXTNOINDENT: // Indent next paragraph?
356 case LT_COMMANDDEPTH:
366 latexparam_ = subst(latexparam_, """, "\"");
382 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
385 case LT_LANGPREAMBLE:
386 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
389 case LT_BABELPREAMBLE:
390 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
397 case LT_ENDLABELTYPE:
398 readEndLabelType(lex);
401 case LT_LEFTMARGIN: // left margin type
405 case LT_RIGHTMARGIN: // right margin type
409 case LT_LABELINDENT: // label indenting flag
413 case LT_PARINDENT: // paragraph indent. flag
417 case LT_PARSKIP: // paragraph skip size
421 case LT_ITEMSEP: // item separation size
425 case LT_TOPSEP: // top separation size
429 case LT_BOTTOMSEP: // bottom separation size
433 case LT_LABEL_BOTTOMSEP: // label bottom separation size
434 lex >> labelbottomsep;
437 case LT_LABELSEP: // label separator
439 labelsep = subst(labelsep, 'x', ' ');
442 case LT_PARSEP: // par. separation size
446 case LT_FILL_TOP: // fill top flag
450 case LT_FILL_BOTTOM: // fill bottom flag
454 case LT_NEWLINE: // newlines allowed?
455 lex >> newline_allowed;
458 case LT_ALIGN: // paragraph align
461 case LT_ALIGNPOSSIBLE: // paragraph allowed align
462 readAlignPossible(lex);
465 case LT_LABELSTRING: // label string definition
466 // FIXME: this means LT_ENDLABELSTRING may only
467 // occur after LT_LABELSTRING
469 labelstring_ = trim(labelstring_);
470 labelstring_appendix_ = labelstring_;
473 case LT_ENDLABELSTRING: // endlabel string definition
474 lex >> endlabelstring_;
475 endlabelstring_ = trim(endlabelstring_);
478 case LT_LABELSTRING_APPENDIX: // label string appendix definition
479 lex >> labelstring_appendix_;
480 labelstring_appendix_ = trim(labelstring_appendix_);
483 case LT_LABELCOUNTER: // name of counter to use
485 counter = trim(counter);
488 case LT_FREE_SPACING: // Allow for free spacing.
492 case LT_PASS_THRU: // Allow for pass thru.
496 case LT_SPACING: // setspace.sty
502 vector<string> const req =
503 getVectorFromString(lex.getString());
504 requires_.insert(req.begin(), req.end());
524 case LT_HTMLITEMATTR:
525 lex >> htmlitemattr_;
529 lex >> htmllabeltag_;
532 case LT_HTMLLABELATTR:
533 lex >> htmllabelattr_;
536 case LT_HTMLLABELFIRST:
537 lex >> htmllabelfirst_;
541 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
544 case LT_HTMLFORCECSS:
545 lex >> htmlforcecss_;
547 case LT_HTMLPREAMBLE:
548 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
575 LexerKeyword alignTags[] = {
576 { "block", AT_BLOCK },
577 { "center", AT_CENTER },
578 { "layout", AT_LAYOUT },
580 { "right", AT_RIGHT }
584 void Layout::readAlign(Lexer & lex)
586 PushPopHelper pph(lex, alignTags);
589 case Lexer::LEX_UNDEF:
590 lex.printError("Unknown alignment `$$Token'");
596 align = LYX_ALIGN_BLOCK;
599 align = LYX_ALIGN_LEFT;
602 align = LYX_ALIGN_RIGHT;
605 align = LYX_ALIGN_CENTER;
608 align = LYX_ALIGN_LAYOUT;
614 void Layout::readAlignPossible(Lexer & lex)
616 lex.pushTable(alignTags);
617 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
618 int lineno = lex.lineNumber();
622 case Lexer::LEX_UNDEF:
623 lex.printError("Unknown alignment `$$Token'");
629 alignpossible |= LYX_ALIGN_BLOCK;
632 alignpossible |= LYX_ALIGN_LEFT;
635 alignpossible |= LYX_ALIGN_RIGHT;
638 alignpossible |= LYX_ALIGN_CENTER;
641 alignpossible |= LYX_ALIGN_LAYOUT;
644 } while (lineno == lex.lineNumber());
649 void Layout::readLabelType(Lexer & lex)
655 LA_CENTERED_TOP_ENVIRONMENT,
665 LexerKeyword labelTypeTags[] = {
666 { "bibliography", LA_BIBLIO },
667 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
668 { "counter", LA_COUNTER },
669 { "enumerate", LA_ENUMERATE },
670 { "itemize", LA_ITEMIZE },
671 { "manual", LA_MANUAL },
672 { "no_label", LA_NO_LABEL },
673 { "sensitive", LA_SENSITIVE },
674 { "static", LA_STATIC },
675 { "top_environment", LA_TOP_ENVIRONMENT }
678 PushPopHelper pph(lex, labelTypeTags);
681 case Lexer::LEX_UNDEF:
682 lex.printError("Unknown labeltype tag `$$Token'");
688 labeltype = LABEL_NO_LABEL;
691 labeltype = LABEL_MANUAL;
693 case LA_TOP_ENVIRONMENT:
694 labeltype = LABEL_TOP_ENVIRONMENT;
696 case LA_CENTERED_TOP_ENVIRONMENT:
697 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
700 labeltype = LABEL_STATIC;
703 labeltype = LABEL_SENSITIVE;
706 labeltype = LABEL_COUNTER;
709 labeltype = LABEL_ENUMERATE;
712 labeltype = LABEL_ITEMIZE;
715 labeltype = LABEL_BIBLIO;
721 void Layout::readEndLabelType(Lexer & lex)
723 static LexerKeyword endlabelTypeTags[] = {
724 { "box", END_LABEL_BOX },
725 { "filled_box", END_LABEL_FILLED_BOX },
726 { "no_label", END_LABEL_NO_LABEL },
727 { "static", END_LABEL_STATIC }
730 PushPopHelper pph(lex, endlabelTypeTags);
733 case Lexer::LEX_UNDEF:
734 lex.printError("Unknown labeltype tag `$$Token'");
736 case END_LABEL_STATIC:
738 case END_LABEL_FILLED_BOX:
739 case END_LABEL_NO_LABEL:
740 endlabeltype = static_cast<EndLabelType>(le);
743 LYXERR0("Unhandled value " << le);
749 void Layout::readMargin(Lexer & lex)
751 LexerKeyword marginTags[] = {
752 { "dynamic", MARGIN_DYNAMIC },
753 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
754 { "manual", MARGIN_MANUAL },
755 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
756 { "static", MARGIN_STATIC }
759 PushPopHelper pph(lex, marginTags);
763 case Lexer::LEX_UNDEF:
764 lex.printError("Unknown margin type tag `$$Token'");
769 case MARGIN_FIRST_DYNAMIC:
770 case MARGIN_RIGHT_ADDRESS_BOX:
771 margintype = static_cast<MarginType>(le);
774 LYXERR0("Unhandled value " << le);
780 void Layout::readLatexType(Lexer & lex)
782 LexerKeyword latexTypeTags[] = {
783 { "bib_environment", LATEX_BIB_ENVIRONMENT },
784 { "command", LATEX_COMMAND },
785 { "environment", LATEX_ENVIRONMENT },
786 { "item_environment", LATEX_ITEM_ENVIRONMENT },
787 { "list_environment", LATEX_LIST_ENVIRONMENT },
788 { "paragraph", LATEX_PARAGRAPH }
791 PushPopHelper pph(lex, latexTypeTags);
794 case Lexer::LEX_UNDEF:
795 lex.printError("Unknown latextype tag `$$Token'");
797 case LATEX_PARAGRAPH:
799 case LATEX_ENVIRONMENT:
800 case LATEX_ITEM_ENVIRONMENT:
801 case LATEX_BIB_ENVIRONMENT:
802 case LATEX_LIST_ENVIRONMENT:
803 latextype = static_cast<LatexType>(le);
806 LYXERR0("Unhandled value " << le);
812 void Layout::readSpacing(Lexer & lex)
815 ST_SPACING_SINGLE = 1,
821 LexerKeyword spacingTags[] = {
822 {"double", ST_SPACING_DOUBLE },
823 {"onehalf", ST_SPACING_ONEHALF },
824 {"other", ST_OTHER },
825 {"single", ST_SPACING_SINGLE }
828 PushPopHelper pph(lex, spacingTags);
831 case Lexer::LEX_UNDEF:
832 lex.printError("Unknown spacing token `$$Token'");
837 case ST_SPACING_SINGLE:
838 spacing.set(Spacing::Single);
840 case ST_SPACING_ONEHALF:
841 spacing.set(Spacing::Onehalf);
843 case ST_SPACING_DOUBLE:
844 spacing.set(Spacing::Double);
848 spacing.set(Spacing::Other, lex.getString());
856 docstring const i18npreamble(Language const * lang, docstring const & templ)
861 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
864 // tex2lyx does not have getMessages()
865 LASSERT(false, /**/);
868 // boost::regex is not unicode-safe.
869 // Should use QRegExp or (boost::u32regex, but that requires ICU)
870 static boost::regex const reg("_\\(([^\\)]+)\\)");
872 while (boost::regex_search(preamble, sub, reg)) {
873 string const key = sub.str(1);
876 translated = to_utf8(getMessages(lang->code()).get(key));
878 lyxerr << "Warning: not translating `" << key
879 << "' because it is not pure ASCII." << endl;
882 preamble = subst(preamble, sub.str(), translated);
885 return from_utf8(preamble);
891 docstring const Layout::langpreamble(Language const * lang) const
893 return i18npreamble(lang, langpreamble_);
897 docstring const Layout::babelpreamble(Language const * lang) const
899 return i18npreamble(lang, babelpreamble_);
903 string const & Layout::htmltag() const
905 if (htmltag_.empty())
911 string const & Layout::htmlattr() const
913 if (htmlattr_.empty())
914 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
919 string const & Layout::htmlitemtag() const
921 if (htmlitemtag_.empty())
922 htmlitemtag_ = "div";
927 string const & Layout::htmlitemattr() const
929 if (htmlitemattr_.empty())
930 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
931 return htmlitemattr_;
935 string const & Layout::htmllabeltag() const
937 if (htmllabeltag_.empty())
938 htmllabeltag_ = "span";
939 return htmllabeltag_;
943 string const & Layout::htmllabelattr() const
945 if (htmllabelattr_.empty())
946 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
947 return htmllabelattr_;
951 docstring Layout::htmlstyle() const {
952 if (!htmlstyle_.empty() && !htmlforcecss_)
954 if (htmldefaultstyle_.empty())
956 docstring retval = htmldefaultstyle_;
957 if (!htmlstyle_.empty())
958 retval += '\n' + htmlstyle_;
963 string Layout::defaultCSSClass() const
965 if (!defaultcssclass_.empty())
966 return defaultcssclass_;
968 docstring::const_iterator it = name().begin();
969 docstring::const_iterator en = name().end();
970 for (; it != en; ++it) {
978 // are there other characters we need to remove?
979 defaultcssclass_ = to_utf8(d);
980 return defaultcssclass_;
984 // NOTE There is a whole ton of stuff that could go into this.
985 // Things like bottomsep, topsep, and parsep could become various
986 // sorts of margins or padding, for example. But for now we are
987 // going to keep it simple.
988 void Layout::makeDefaultCSS() const {
989 // this never needs to be redone, since reloading layouts will
990 // wipe out what we did before.
991 if (!htmldefaultstyle_.empty())
993 docstring const mainfontCSS = font.asCSS();
994 if (!mainfontCSS.empty())
996 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
997 mainfontCSS + from_ascii("\n}\n");
998 if (labelfont == font || htmllabeltag() == "NONE")
1000 docstring const labelfontCSS = labelfont.asCSS();
1001 if (!labelfontCSS.empty())
1002 htmldefaultstyle_ +=
1003 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1004 labelfontCSS + from_ascii("\n}\n");
1008 bool Layout::operator==(Layout const & rhs) const
1010 // This is enough for the applications we actually make,
1011 // at least at the moment. But we could check more.
1012 return name() == rhs.name()
1013 && latexname() == rhs.latexname()
1014 && latextype == rhs.latextype;